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