mirror of
https://github.com/leanote/leanote-android.git
synced 2025-10-17 16:07:48 +00:00
code refactoring: extract the data loading from NoteAdapter, do the binding only
This commit is contained in:
@@ -16,8 +16,6 @@ import org.houxg.leamonax.model.Notebook;
|
|||||||
import org.houxg.leamonax.service.AccountService;
|
import org.houxg.leamonax.service.AccountService;
|
||||||
import org.houxg.leamonax.utils.TimeUtils;
|
import org.houxg.leamonax.utils.TimeUtils;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -35,44 +33,12 @@ public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {
|
|||||||
mListener = listener;
|
mListener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadFromLocal() {
|
|
||||||
mData = AppDataBase.getAllNotes(AccountService.getCurrent().getUserId());
|
|
||||||
sortByUpdatedTime();
|
|
||||||
notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void loadFromLocal(long notebookLocalId) {
|
|
||||||
mData = AppDataBase.getNotesFromNotebook(AccountService.getCurrent().getUserId(), notebookLocalId);
|
|
||||||
sortByUpdatedTime();
|
|
||||||
updateNotebookMap();
|
|
||||||
notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void load(List<Note> source) {
|
public void load(List<Note> source) {
|
||||||
mData = source;
|
mData = source;
|
||||||
sortByUpdatedTime();
|
|
||||||
updateNotebookMap();
|
updateNotebookMap();
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sortByUpdatedTime() {
|
|
||||||
Collections.sort(mData, new Comparator<Note>() {
|
|
||||||
@Override
|
|
||||||
public int compare(Note lhs, Note rhs) {
|
|
||||||
long lTime = lhs.getUpdatedTimeVal();
|
|
||||||
long rTime = rhs.getUpdatedTimeVal();
|
|
||||||
if (lTime > rTime) {
|
|
||||||
return -1;
|
|
||||||
} else if (lTime < rTime) {
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
updateNotebookMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NoteAdapter.NoteHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
public NoteAdapter.NoteHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||||
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note, parent, false);
|
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note, parent, false);
|
||||||
|
@@ -13,6 +13,7 @@ import org.houxg.leamonax.utils.CollectionUtils;
|
|||||||
import org.houxg.leamonax.utils.TimeUtils;
|
import org.houxg.leamonax.utils.TimeUtils;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -238,13 +239,16 @@ public class Note extends BaseModel implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//TODO:delete this
|
//TODO:delete this
|
||||||
public void setUpdatedTime(String v) {}
|
public void setUpdatedTime(String v) {
|
||||||
|
}
|
||||||
|
|
||||||
//TODO:delete this
|
//TODO:delete this
|
||||||
public void setCreatedTime(String v) {}
|
public void setCreatedTime(String v) {
|
||||||
|
}
|
||||||
|
|
||||||
//TODO:delete this
|
//TODO:delete this
|
||||||
public void setPublicTime(String publicTime) {}
|
public void setPublicTime(String publicTime) {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
@@ -275,9 +279,9 @@ public class Note extends BaseModel implements Serializable {
|
|||||||
return otherNote == null
|
return otherNote == null
|
||||||
|| isChanged("title", title, otherNote.title)
|
|| isChanged("title", title, otherNote.title)
|
||||||
|| isChanged("content", content, otherNote.content)
|
|| isChanged("content", content, otherNote.content)
|
||||||
|| isChanged("notebookId" , noteBookId, otherNote.noteBookId)
|
|| isChanged("notebookId", noteBookId, otherNote.noteBookId)
|
||||||
|| isChanged("isMarkDown", isMarkDown, otherNote.isMarkDown)
|
|| isChanged("isMarkDown", isMarkDown, otherNote.isMarkDown)
|
||||||
|| isChanged("tags" , tags, otherNote.tags)
|
|| isChanged("tags", tags, otherNote.tags)
|
||||||
|| isChanged("isBlog", isPublicBlog, otherNote.isPublicBlog);
|
|| isChanged("isBlog", isPublicBlog, otherNote.isPublicBlog);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -368,4 +372,19 @@ public class Note extends BaseModel implements Serializable {
|
|||||||
public String getMsg() {
|
public String getMsg() {
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class UpdateTimeComparetor implements Comparator<Note> {
|
||||||
|
@Override
|
||||||
|
public int compare(Note lhs, Note rhs) {
|
||||||
|
long lTime = lhs.getUpdatedTimeVal();
|
||||||
|
long rTime = rhs.getUpdatedTimeVal();
|
||||||
|
if (lTime > rTime) {
|
||||||
|
return -1;
|
||||||
|
} else if (lTime < rTime) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -28,13 +28,17 @@ import org.greenrobot.eventbus.ThreadMode;
|
|||||||
import org.houxg.leamonax.R;
|
import org.houxg.leamonax.R;
|
||||||
import org.houxg.leamonax.adapter.NoteAdapter;
|
import org.houxg.leamonax.adapter.NoteAdapter;
|
||||||
import org.houxg.leamonax.background.NoteSyncService;
|
import org.houxg.leamonax.background.NoteSyncService;
|
||||||
|
import org.houxg.leamonax.database.AppDataBase;
|
||||||
import org.houxg.leamonax.model.Note;
|
import org.houxg.leamonax.model.Note;
|
||||||
import org.houxg.leamonax.model.SyncEvent;
|
import org.houxg.leamonax.model.SyncEvent;
|
||||||
|
import org.houxg.leamonax.service.AccountService;
|
||||||
import org.houxg.leamonax.service.NoteService;
|
import org.houxg.leamonax.service.NoteService;
|
||||||
import org.houxg.leamonax.utils.DisplayUtils;
|
import org.houxg.leamonax.utils.DisplayUtils;
|
||||||
import org.houxg.leamonax.utils.NetworkUtils;
|
import org.houxg.leamonax.utils.NetworkUtils;
|
||||||
import org.houxg.leamonax.utils.ToastUtils;
|
import org.houxg.leamonax.utils.ToastUtils;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import butterknife.BindView;
|
import butterknife.BindView;
|
||||||
@@ -58,6 +62,7 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
|
|||||||
@BindView(R.id.swiperefresh)
|
@BindView(R.id.swiperefresh)
|
||||||
SwipeRefreshLayout mSwipeRefresh;
|
SwipeRefreshLayout mSwipeRefresh;
|
||||||
|
|
||||||
|
List<Note> mNotes;
|
||||||
private NoteAdapter mAdapter;
|
private NoteAdapter mAdapter;
|
||||||
|
|
||||||
private long mCurrentNotebookId = RECENT_NOTES;
|
private long mCurrentNotebookId = RECENT_NOTES;
|
||||||
@@ -109,7 +114,6 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
|
|||||||
mScrollPosition = dy;
|
mScrollPosition = dy;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +130,7 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
|
|||||||
super.onActivityCreated(savedInstanceState);
|
super.onActivityCreated(savedInstanceState);
|
||||||
EventBus.getDefault().register(this);
|
EventBus.getDefault().register(this);
|
||||||
if (savedInstanceState == null) {
|
if (savedInstanceState == null) {
|
||||||
mAdapter.loadFromLocal();
|
loadNoteFromLocal(RECENT_NOTES);
|
||||||
if (getArguments().getBoolean(EXT_SHOULD_FETCH_NOTES, false)) {
|
if (getArguments().getBoolean(EXT_SHOULD_FETCH_NOTES, false)) {
|
||||||
mSwipeRefresh.postDelayed(new Runnable() {
|
mSwipeRefresh.postDelayed(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
@@ -165,11 +169,13 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
|
|||||||
public void loadNoteFromLocal(long notebookLocalId) {
|
public void loadNoteFromLocal(long notebookLocalId) {
|
||||||
if (notebookLocalId < 0) {
|
if (notebookLocalId < 0) {
|
||||||
mCurrentNotebookId = RECENT_NOTES;
|
mCurrentNotebookId = RECENT_NOTES;
|
||||||
mAdapter.loadFromLocal();
|
mNotes = AppDataBase.getAllNotes(AccountService.getCurrent().getUserId());
|
||||||
} else {
|
} else {
|
||||||
mCurrentNotebookId = notebookLocalId;
|
mCurrentNotebookId = notebookLocalId;
|
||||||
mAdapter.loadFromLocal(mCurrentNotebookId);
|
mNotes = AppDataBase.getNotesFromNotebook(AccountService.getCurrent().getUserId(), notebookLocalId);
|
||||||
}
|
}
|
||||||
|
Collections.sort(mNotes, new Note.UpdateTimeComparetor());
|
||||||
|
mAdapter.load(mNotes);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -236,11 +242,7 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
|
|||||||
Log.i(TAG, "RequestNotes rcv: isSucceed=" + event.isSucceed());
|
Log.i(TAG, "RequestNotes rcv: isSucceed=" + event.isSucceed());
|
||||||
if (isAdded()) {
|
if (isAdded()) {
|
||||||
mSwipeRefresh.setRefreshing(false);
|
mSwipeRefresh.setRefreshing(false);
|
||||||
if (mCurrentNotebookId > 0) {
|
loadNoteFromLocal(mCurrentNotebookId);
|
||||||
mAdapter.loadFromLocal(mCurrentNotebookId);
|
|
||||||
} else {
|
|
||||||
mAdapter.loadFromLocal();
|
|
||||||
}
|
|
||||||
if (!event.isSucceed()) {
|
if (!event.isSucceed()) {
|
||||||
ToastUtils.show(getActivity(), R.string.sync_notes_failed);
|
ToastUtils.show(getActivity(), R.string.sync_notes_failed);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user