add change note type action

This commit is contained in:
houxg
2017-02-10 17:44:17 +08:00
parent acb72fe618
commit 53fafebf32
7 changed files with 281 additions and 41 deletions

View File

@@ -2,6 +2,7 @@ package org.houxg.leamonax.adapter;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.SpannableStringBuilder;
@@ -19,6 +20,7 @@ import org.houxg.leamonax.model.Note;
import org.houxg.leamonax.model.Notebook;
import org.houxg.leamonax.service.AccountService;
import org.houxg.leamonax.utils.TimeUtils;
import org.houxg.leamonax.widget.NoteList;
import java.util.HashMap;
import java.util.List;
@@ -31,10 +33,13 @@ import butterknife.ButterKnife;
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {
private List<Note> mData;
private Map<String, String> mNotebookId2TitleMaps;
private NoteAdapterListener mListener;
private Pattern mTitleHighlight;
private int mCurrentType = NoteList.TYPE_SIMPLE;
public NoteAdapter(NoteAdapterListener listener) {
mListener = listener;
@@ -54,11 +59,19 @@ public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {
}
}
public void setType(int type) {
mCurrentType = type;
}
@Override
public NoteAdapter.NoteHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note, parent, false);
NoteHolder holder = new NoteAdapter.NoteHolder(view);
return holder;
View view = null;
if (viewType == NoteList.TYPE_SIMPLE) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note_simple, parent, false);
} else if (viewType == NoteList.TYPE_DETAIL) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note, parent, false);
}
return new NoteHolder(view);
}
private void updateNotebookMap() {
@@ -69,9 +82,22 @@ public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {
}
}
@Override
public int getItemViewType(int position) {
return mCurrentType;
}
@Override
public void onBindViewHolder(NoteAdapter.NoteHolder holder, int position) {
final Note note = mData.get(position);
if (getItemViewType(position) == NoteList.TYPE_DETAIL) {
renderDetail(holder, note);
} else {
renderSimple(holder, note);
}
}
private void renderDetail(NoteHolder holder, final Note note) {
if (TextUtils.isEmpty(note.getTitle())) {
holder.titleTv.setText(R.string.untitled);
} else {
@@ -120,6 +146,47 @@ public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {
});
}
private void renderSimple(NoteHolder holder, final Note note) {
if (TextUtils.isEmpty(note.getTitle())) {
holder.titleTv.setText(R.string.untitled);
} else {
holder.titleTv.setText(getHighlightedText(note.getTitle()));
}
holder.notebookTv.setText(mNotebookId2TitleMaps.get(note.getNoteBookId()));
long updateTime = note.getUpdatedTimeVal();
Context context = holder.updateTimeTv.getContext();
String time;
if (updateTime >= TimeUtils.getToday().getTimeInMillis()) {
time = TimeUtils.toTimeFormat(updateTime);
} else if (updateTime >= TimeUtils.getYesterday().getTimeInMillis()) {
time = context.getString(R.string.time_yesterday, TimeUtils.toTimeFormat(updateTime));
} else if (updateTime >= TimeUtils.getThisYear().getTimeInMillis()) {
time = TimeUtils.toDateFormat(updateTime);
} else {
time = TimeUtils.toYearFormat(updateTime);
}
holder.updateTimeTv.setText(time);
holder.dirtyTv.setVisibility(note.isDirty() ? View.VISIBLE : View.GONE);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onClickNote(note);
}
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (mListener != null) {
mListener.onLongClickNote(note);
}
return true;
}
});
}
private CharSequence getHighlightedText(String text) {
if (mTitleHighlight == null) {
return text;
@@ -152,6 +219,7 @@ public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {
@BindView(R.id.tv_title)
TextView titleTv;
@Nullable
@BindView(R.id.tv_content)
TextView contentTv;
@BindView(R.id.tv_notebook)

View File

@@ -12,6 +12,9 @@ import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@@ -32,6 +35,7 @@ import org.houxg.leamonax.utils.DisplayUtils;
import org.houxg.leamonax.utils.NetworkUtils;
import org.houxg.leamonax.utils.ToastUtils;
import org.houxg.leamonax.widget.DividerDecoration;
import org.houxg.leamonax.widget.NoteList;
import java.util.Collections;
import java.util.List;
@@ -56,11 +60,10 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
@BindView(R.id.swiperefresh)
SwipeRefreshLayout mSwipeRefresh;
List<Note> mNotes;
private NoteAdapter mAdapter;
private OnSyncFinishListener mSyncFinishListener;
NoteList mNoteList;
private float mScrollPosition;
List<Note> mNotes;
private OnSyncFinishListener mSyncFinishListener;
public NoteFragment() {
}
@@ -77,37 +80,38 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
mSyncFinishListener = listener;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.note, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_view_type) {
mNoteList.toggleType();
}
return super.onOptionsItemSelected(item);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_note, container, false);
ButterKnife.bind(this, view);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(container.getContext());
mNoteListView.setLayoutManager(layoutManager);
mNoteListView.setItemAnimator(new DefaultItemAnimator());
mNoteListView.addItemDecoration(new DividerDecoration(DisplayUtils.dp2px(8)));
mAdapter = new NoteAdapter(this);
mNoteListView.setAdapter(mAdapter);
mNoteList = new NoteList(container.getContext(), view, this);
mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
syncNotes();
}
});
mNoteListView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
mScrollPosition = dy;
}
});
return view;
}
@@ -124,8 +128,8 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
EventBus.getDefault().register(this);
refreshNotes();
if (savedInstanceState == null) {
refreshNotes();
if (getArguments().getBoolean(EXT_SHOULD_FETCH_NOTES, false)) {
mSwipeRefresh.postDelayed(new Runnable() {
@Override
@@ -136,23 +140,15 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
}
}, 200);
}
} else {
mNoteList.setScrollPosition(savedInstanceState.getInt(EXT_SCROLL_POSITION, 0));
}
if (savedInstanceState != null) {
mScrollPosition = savedInstanceState.getFloat(EXT_SCROLL_POSITION, 0);
}
}
@Override
public void onResume() {
super.onResume();
mNoteListView.scrollTo(0, (int) mScrollPosition);
refreshNotes();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putFloat(EXT_SCROLL_POSITION, mScrollPosition);
outState.putInt(EXT_SCROLL_POSITION, mNoteList.getScrollPosition());
}
@Override
@@ -195,7 +191,7 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
mNotes = AppDataBase.getNotesByTagText(mCurrentMode.tagText, AccountService.getCurrent().getUserId());
}
Collections.sort(mNotes, new Note.UpdateTimeComparetor());
mAdapter.load(mNotes);
mNoteList.render(mNotes);
}
@Override
@@ -242,7 +238,7 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
@Override
public void onNext(Void aVoid) {
mAdapter.delete(note);
mNoteList.remove(note);
}
});
}

View File

@@ -0,0 +1,85 @@
package org.houxg.leamonax.widget;
import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import org.houxg.leamonax.R;
import org.houxg.leamonax.adapter.NoteAdapter;
import org.houxg.leamonax.model.Note;
import org.houxg.leamonax.utils.DisplayUtils;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class NoteList {
public static final int TYPE_SIMPLE = 0;
public static final int TYPE_DETAIL = 1;
@BindView(R.id.recycler_view)
RecyclerView mNoteListView;
private NoteAdapter mAdapter;
private int mScrollPosition = 0;
private int mCurrentType = TYPE_SIMPLE;
private RecyclerView.ItemDecoration mItemDecoration;
public NoteList(Context context, View rootView, NoteAdapter.NoteAdapterListener adapterListener) {
ButterKnife.bind(this, rootView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
mNoteListView.setLayoutManager(layoutManager);
mNoteListView.setItemAnimator(new DefaultItemAnimator());
mAdapter = new NoteAdapter(adapterListener);
mNoteListView.setAdapter(mAdapter);
mNoteListView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
mScrollPosition = dy;
}
});
mItemDecoration = new DividerDecoration(DisplayUtils.dp2px(8));
setType(mCurrentType);
}
public void setType(int type) {
mAdapter.setType(type);
mNoteListView.removeItemDecoration(mItemDecoration);
if (type == TYPE_SIMPLE) {
} else if (type == TYPE_DETAIL) {
mNoteListView.addItemDecoration(mItemDecoration);
}
mAdapter.notifyDataSetChanged();
}
public void toggleType() {
mCurrentType = mCurrentType == TYPE_SIMPLE ? TYPE_DETAIL : TYPE_SIMPLE;
setType(mCurrentType);
}
public void render(List<Note> notes) {
mAdapter.load(notes);
}
public void remove(Note note) {
mAdapter.delete(note);
}
public int getScrollPosition() {
return mScrollPosition;
}
public void setScrollPosition(int position) {
mScrollPosition = position;
mNoteListView.scrollTo(0, position);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 220 B

View File

@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:paddingTop="16dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:textColor="@color/primary_text_light"
android:textSize="20sp"
tools:text="Leanote API" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="8dp"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:paddingTop="8dp">
<TextView
android:id="@+id/tv_dirty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:background="@drawable/shape_changed"
android:fontFamily="sans-serif"
android:paddingBottom="2dp"
android:paddingEnd="4dp"
android:paddingStart="4dp"
android:paddingTop="2dp"
android:text="@string/changed"
android:textColor="#fff"
android:textSize="12sp" />
<TextView
android:id="@+id/tv_update_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif-light"
android:textColor="@color/hint_text_light"
android:textSize="12sp"
tools:text="9-18 23:23" />
<TextView
android:id="@+id/tv_notebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:textColor="@color/hint_text_light"
android:textSize="12sp"
tools:text="Gradle" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#BDBDBD" />
</LinearLayout>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_view_type"
android:icon="@drawable/ic_view_type"
android:title="@string/view_type"
app:showAsAction="always"/>
</menu>

View File

@@ -97,5 +97,6 @@
<string name="upgrade_now">Upgrade now</string>
<string name="your_are_the_latest_version">You are using latest version</string>
<string name="cant_open_url">Can\'t open this url</string>
<string name="view_type">View type</string>
</resources>