From 75582c2167afc405cc6cd4fa7be120ae4a67fc89 Mon Sep 17 00:00:00 2001 From: houxg Date: Tue, 21 Feb 2017 14:53:40 +0800 Subject: [PATCH 01/13] fix wrong condition of local note --- app/src/main/java/org/houxg/leamonax/model/Note.java | 10 ++++++++++ .../java/org/houxg/leamonax/service/NoteService.java | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/houxg/leamonax/model/Note.java b/app/src/main/java/org/houxg/leamonax/model/Note.java index bc2ff0f..0b08f31 100644 --- a/app/src/main/java/org/houxg/leamonax/model/Note.java +++ b/app/src/main/java/org/houxg/leamonax/model/Note.java @@ -1,5 +1,7 @@ package org.houxg.leamonax.model; +import android.text.TextUtils; + import com.elvishew.xlog.XLog; import com.google.gson.annotations.SerializedName; import com.raizlabs.android.dbflow.annotation.Column; @@ -193,6 +195,14 @@ public class Note extends BaseModel implements Serializable { return tagData; } + public boolean isTotalEmpty() { + return TextUtils.isEmpty(title) && TextUtils.isEmpty(content); + } + + public boolean isLocalNote() { + return TextUtils.isEmpty(noteId); + } + public void updateTags() { if (CollectionUtils.isEmpty(tagData)) { tags = ""; diff --git a/app/src/main/java/org/houxg/leamonax/service/NoteService.java b/app/src/main/java/org/houxg/leamonax/service/NoteService.java index ce67d9f..0e81b11 100644 --- a/app/src/main/java/org/houxg/leamonax/service/NoteService.java +++ b/app/src/main/java/org/houxg/leamonax/service/NoteService.java @@ -204,7 +204,7 @@ public class NoteService { Map requestBodyMap = generateCommonBodyMap(modifiedNote); List fileBodies = handleFileBodies(modifiedNote, requestBodyMap); Call call; - if (modifiedNote.getUsn() == 0) { + if (modifiedNote.isLocalNote()) { call = ApiProvider.getInstance().getNoteApi().add(requestBodyMap, fileBodies); } else { Note remoteNote = RetrofitUtils.excuteWithException(getNoteByServerId(modifiedNote.getNoteId())); From de94602c97c9a22c16eb3bb96aa8859ad548e8fe Mon Sep 17 00:00:00 2001 From: houxg Date: Tue, 21 Feb 2017 15:40:09 +0800 Subject: [PATCH 02/13] fix update note but actually add note --- app/src/main/java/org/houxg/leamonax/service/NoteService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/org/houxg/leamonax/service/NoteService.java b/app/src/main/java/org/houxg/leamonax/service/NoteService.java index 0e81b11..0ec38b7 100644 --- a/app/src/main/java/org/houxg/leamonax/service/NoteService.java +++ b/app/src/main/java/org/houxg/leamonax/service/NoteService.java @@ -220,7 +220,7 @@ public class NoteService { } else { requestBodyMap.put("NoteId", createPartFromString(modifiedNote.getNoteId())); requestBodyMap.put("Usn", createPartFromString(String.valueOf(modifiedNote.getUsn()))); - call = ApiProvider.getInstance().getNoteApi().add(requestBodyMap, fileBodies); + call = ApiProvider.getInstance().getNoteApi().update(requestBodyMap, fileBodies); } } Note note = RetrofitUtils.excuteWithException(call); From b00ccddbec90fa277a78282ac42e629af1276b3b Mon Sep 17 00:00:00 2001 From: houxg Date: Tue, 21 Feb 2017 15:55:03 +0800 Subject: [PATCH 03/13] remove redundant method --- .../houxg/leamonax/service/NoteService.java | 25 +++---------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/houxg/leamonax/service/NoteService.java b/app/src/main/java/org/houxg/leamonax/service/NoteService.java index 0ec38b7..400d085 100644 --- a/app/src/main/java/org/houxg/leamonax/service/NoteService.java +++ b/app/src/main/java/org/houxg/leamonax/service/NoteService.java @@ -207,7 +207,7 @@ public class NoteService { if (modifiedNote.isLocalNote()) { call = ApiProvider.getInstance().getNoteApi().add(requestBodyMap, fileBodies); } else { - Note remoteNote = RetrofitUtils.excuteWithException(getNoteByServerId(modifiedNote.getNoteId())); + Note remoteNote = RetrofitUtils.excuteWithException(ApiProvider.getInstance().getNoteApi().getNoteAndContent(modifiedNote.getNoteId())); if (remoteNote.getUsn() != modifiedNote.getUsn()) { remoteNote.setId(modifiedNote.getId()); remoteNote.update(); @@ -274,20 +274,8 @@ public class NoteService { }); } - private static Call> getSyncNotes(int afterUsn, int maxEntry) { - return ApiProvider.getInstance().getNoteApi().getSyncNotes(afterUsn, maxEntry); - } - - private static Call> getSyncNotebooks(int afterUsn, int maxEntry) { - return ApiProvider.getInstance().getNotebookApi().getSyncNotebooks(afterUsn, maxEntry); - } - - public static Call getNoteByServerId(String serverId) { - return ApiProvider.getInstance().getNoteApi().getNoteAndContent(serverId); - } - public static boolean revertNote(String serverId) { - Note serverNote = RetrofitUtils.excute(NoteService.getNoteByServerId(serverId)); + Note serverNote = RetrofitUtils.excute(ApiProvider.getInstance().getNoteApi().getNoteAndContent(serverId)); if (serverNote == null) { return false; } @@ -310,8 +298,6 @@ public class NoteService { return true; } - - @NonNull private static Map generateCommonBodyMap(Note note) { Map requestBodyMap = new HashMap<>(); @@ -402,6 +388,7 @@ public class NoteService { return localIds; } + //TODO:change to synchonous public static Observable deleteNote(final Note note) { return Observable.create( new Observable.OnSubscribe() { @@ -427,6 +414,7 @@ public class NoteService { }); } + //TODO:delete this method private static void updateNoteUsnIfNeed(int newUsn) { Account account = AccountService.getCurrent(); if (newUsn - account.getNoteUsn() == 1) { @@ -435,10 +423,6 @@ public class NoteService { } } - public static Call deleteNote(String noteId, int usn) { - return ApiProvider.getInstance().getNoteApi().delete(noteId, usn); - } - public static void updateTagsToLocal(long noteLocalId, List tags) { String currentUid = AccountService.getCurrent().getUserId(); if (tags == null) { @@ -481,7 +465,6 @@ public class NoteService { } } - private static RequestBody createPartFromString(String content) { return RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), content); } From 399cf2a65487358d6497bf09b1d409b37cac5636 Mon Sep 17 00:00:00 2001 From: houxg Date: Tue, 21 Feb 2017 16:13:35 +0800 Subject: [PATCH 04/13] code refactoring --- .../houxg/leamonax/service/NoteService.java | 41 ++++++++----------- .../org/houxg/leamonax/ui/NoteFragment.java | 14 ++++++- .../org/houxg/leamonax/ui/SearchActivity.java | 14 ++++++- 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/org/houxg/leamonax/service/NoteService.java b/app/src/main/java/org/houxg/leamonax/service/NoteService.java index 400d085..0a58596 100644 --- a/app/src/main/java/org/houxg/leamonax/service/NoteService.java +++ b/app/src/main/java/org/houxg/leamonax/service/NoteService.java @@ -388,33 +388,24 @@ public class NoteService { return localIds; } - //TODO:change to synchonous - public static Observable deleteNote(final Note note) { - return Observable.create( - new Observable.OnSubscribe() { - @Override - public void call(Subscriber subscriber) { - if (!subscriber.isUnsubscribed()) { - if (TextUtils.isEmpty(note.getNoteId())) { - AppDataBase.deleteNoteByLocalId(note.getId()); - } else { - UpdateRe response = RetrofitUtils.excuteWithException( - ApiProvider.getInstance().getNoteApi().delete(note.getNoteId(), note.getUsn())); - if (response.isOk()) { - AppDataBase.deleteNoteByLocalId(note.getId()); - updateNoteUsnIfNeed(response.getUsn()); - } else { - throw new IllegalStateException(response.getMsg()); - } - } - subscriber.onNext(note); - subscriber.onCompleted(); - } - } - }); + public static void deleteNote(Note note) { + if (note.isLocalNote()) { + AppDataBase.deleteNoteByLocalId(note.getId()); + } else { + Call call = ApiProvider.getInstance().getNoteApi().delete(note.getNoteId(), note.getUsn()); + UpdateRe response = RetrofitUtils.excuteWithException(call); + if (response.isOk()) { + AppDataBase.deleteNoteByLocalId(note.getId()); + updateNoteUsnIfNeed(response.getUsn()); + } else { + throw new IllegalStateException(response.getMsg()); + } + } } - //TODO:delete this method + /** + * if new usn equals to (current usn + 1), then just simply update usn without syncing. + */ private static void updateNoteUsnIfNeed(int newUsn) { Account account = AccountService.getCurrent(); if (newUsn - account.getNoteUsn() == 1) { diff --git a/app/src/main/java/org/houxg/leamonax/ui/NoteFragment.java b/app/src/main/java/org/houxg/leamonax/ui/NoteFragment.java index 53d5ccc..520420c 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/NoteFragment.java +++ b/app/src/main/java/org/houxg/leamonax/ui/NoteFragment.java @@ -46,6 +46,7 @@ import butterknife.BindView; import butterknife.ButterKnife; import rx.Observable; import rx.Observer; +import rx.Subscriber; import rx.android.schedulers.AndroidSchedulers; import rx.functions.Func1; import rx.schedulers.Schedulers; @@ -155,8 +156,17 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis Observable.from(notes) .flatMap(new Func1>() { @Override - public rx.Observable call(Note note) { - return NoteService.deleteNote(note); + public rx.Observable call(final Note note) { + return Observable.create(new Observable.OnSubscribe() { + @Override + public void call(Subscriber subscriber) { + if (!subscriber.isUnsubscribed()) { + NoteService.deleteNote(note); + subscriber.onNext(note); + subscriber.onCompleted(); + } + } + }); } }) .subscribeOn(Schedulers.io()) diff --git a/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java b/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java index 91d9ae1..2bb60ae 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java @@ -37,6 +37,7 @@ import butterknife.BindView; import butterknife.ButterKnife; import rx.Observable; import rx.Observer; +import rx.Subscriber; import rx.android.schedulers.AndroidSchedulers; import rx.functions.Func1; import rx.schedulers.Schedulers; @@ -162,8 +163,17 @@ public class SearchActivity extends BaseActivity implements NoteAdapter.NoteAdap Observable.from(notes) .flatMap(new Func1>() { @Override - public rx.Observable call(Note note) { - return NoteService.deleteNote(note); + public rx.Observable call(final Note note) { + return Observable.create(new Observable.OnSubscribe() { + @Override + public void call(Subscriber subscriber) { + if (!subscriber.isUnsubscribed()) { + NoteService.deleteNote(note); + subscriber.onNext(note); + subscriber.onCompleted(); + } + } + }); } }) .subscribeOn(Schedulers.io()) From 4eea3edf4de62f2c239d6d67c7e05989706bbe43 Mon Sep 17 00:00:00 2001 From: houxg Date: Tue, 21 Feb 2017 18:13:13 +0800 Subject: [PATCH 05/13] extract color --- app/src/main/res/layout/activity_main.xml | 2 +- app/src/main/res/layout/activity_preview.xml | 2 +- app/src/main/res/layout/item_note.xml | 2 +- app/src/main/res/layout/item_note_detail_placeholder.xml | 2 +- app/src/main/res/layout/item_note_simple.xml | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 05ae811..649bd83 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -3,7 +3,7 @@ xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" - android:background="#fff" + android:background="@color/white" android:orientation="vertical"> diff --git a/app/src/main/res/layout/activity_preview.xml b/app/src/main/res/layout/activity_preview.xml index 3fa42c6..b325f60 100644 --- a/app/src/main/res/layout/activity_preview.xml +++ b/app/src/main/res/layout/activity_preview.xml @@ -18,7 +18,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" - android:background="#fff" + android:background="@color/white" android:gravity="center_vertical|end" android:paddingEnd="16dp" android:paddingStart="16dp" diff --git a/app/src/main/res/layout/item_note.xml b/app/src/main/res/layout/item_note.xml index 0a4bc43..f9015cc 100644 --- a/app/src/main/res/layout/item_note.xml +++ b/app/src/main/res/layout/item_note.xml @@ -87,7 +87,7 @@ android:paddingStart="4dp" android:paddingTop="2dp" android:text="@string/changed" - android:textColor="#fff" + android:textColor="@color/white" android:textSize="12sp" /> Date: Wed, 22 Feb 2017 11:52:38 +0800 Subject: [PATCH 06/13] when in notebook mode, use current notebook as deafault for create a note, otherwise use recent notebook --- .../org/houxg/leamonax/ui/MainActivity.java | 17 ++++++++++++++--- .../java/org/houxg/leamonax/ui/Navigation.java | 4 ++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java b/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java index a980eff..79fa71e 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java @@ -35,6 +35,7 @@ import org.houxg.leamonax.utils.ToastUtils; import java.util.Collections; import java.util.List; +import java.util.Locale; import butterknife.BindView; import butterknife.ButterKnife; @@ -156,9 +157,19 @@ public class MainActivity extends BaseActivity implements Navigation.Callback { Account account = AccountService.getCurrent(); Note newNote = new Note(); newNote.setUserId(account.getUserId()); - Notebook recentNotebook = AppDataBase.getRecentNoteBook(AccountService.getCurrent().getUserId()); - if (recentNotebook != null) { - newNote.setNoteBookId(recentNotebook.getNotebookId()); + Notebook notebook; + Navigation.Mode currentMode = mNavigation.getCurrentMode(); + if (currentMode == Navigation.Mode.NOTEBOOK) { + notebook = AppDataBase.getNotebookByLocalId(currentMode.notebookId); + } else { + notebook = AppDataBase.getRecentNoteBook(AccountService.getCurrent().getUserId()); + } + if (notebook != null) { + newNote.setNoteBookId(notebook.getNotebookId()); + } else { + Exception exception = new IllegalStateException( + String.format(Locale.US, "notebook is null, mode:%s, notebookLocalId:%d", currentMode, currentMode.notebookId)); + CrashReport.postCatchedException(exception); } newNote.setIsMarkDown(account.getDefaultEditor() == Account.EDITOR_MARKDOWN); newNote.save(); diff --git a/app/src/main/java/org/houxg/leamonax/ui/Navigation.java b/app/src/main/java/org/houxg/leamonax/ui/Navigation.java index e26beef..993f574 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/Navigation.java +++ b/app/src/main/java/org/houxg/leamonax/ui/Navigation.java @@ -366,6 +366,10 @@ public class Navigation { } } + public Mode getCurrentMode() { + return mCurrentMode; + } + public void refresh() { refreshUserInfo(AccountService.getCurrent()); mAccountAdapter.load(AccountService.getAccountList()); From ed7c476754162856f85b1d409c81f067ed2522b7 Mon Sep 17 00:00:00 2001 From: houxg Date: Tue, 28 Feb 2017 18:53:39 +0800 Subject: [PATCH 07/13] code refactoring: clean all static methon in db layer --- .../houxg/leamonax/adapter/NoteAdapter.java | 3 +- .../leamonax/adapter/NotebookAdapter.java | 16 +- .../houxg/leamonax/adapter/TagAdapter.java | 2 +- .../houxg/leamonax/database/AppDataBase.java | 256 +----------------- .../org/houxg/leamonax/model/Account.java | 35 +++ .../java/org/houxg/leamonax/model/Note.java | 87 ++++++ .../org/houxg/leamonax/model/NoteFile.java | 34 +++ .../org/houxg/leamonax/model/Notebook.java | 71 +++++ .../leamonax/model/RelationshipOfNoteTag.java | 8 + .../java/org/houxg/leamonax/model/Tag.java | 65 +++++ .../leamonax/service/AccountService.java | 19 +- .../leamonax/service/NoteFileService.java | 9 +- .../houxg/leamonax/service/NoteService.java | 50 ++-- .../leamonax/service/NotebookService.java | 2 +- .../org/houxg/leamonax/ui/MainActivity.java | 10 +- .../org/houxg/leamonax/ui/Navigation.java | 2 +- .../leamonax/ui/NotePreviewActivity.java | 8 +- .../org/houxg/leamonax/ui/SearchActivity.java | 2 +- .../houxg/leamonax/ui/SettingsActivity.java | 28 +- .../leamonax/ui/edit/NoteEditActivity.java | 15 +- .../leamonax/ui/edit/SettingFragment.java | 4 +- 21 files changed, 367 insertions(+), 359 deletions(-) diff --git a/app/src/main/java/org/houxg/leamonax/adapter/NoteAdapter.java b/app/src/main/java/org/houxg/leamonax/adapter/NoteAdapter.java index f9f4e44..529f87d 100644 --- a/app/src/main/java/org/houxg/leamonax/adapter/NoteAdapter.java +++ b/app/src/main/java/org/houxg/leamonax/adapter/NoteAdapter.java @@ -18,7 +18,6 @@ import android.widget.TextView; import com.bumptech.glide.Glide; import org.houxg.leamonax.R; -import org.houxg.leamonax.database.AppDataBase; import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.NoteFile; import org.houxg.leamonax.model.Notebook; @@ -107,7 +106,7 @@ public class NoteAdapter extends RecyclerView.Adapter { } private void updateNotebookMap() { - List notebooks = AppDataBase.getAllNotebook(AccountService.getCurrent().getUserId()); + List notebooks = Notebook.getAllNotebooks(AccountService.getCurrent().getUserId()); mNotebookId2TitleMaps = new HashMap<>(); for (Notebook notebook : notebooks) { mNotebookId2TitleMaps.put(notebook.getNotebookId(), notebook.getTitle()); diff --git a/app/src/main/java/org/houxg/leamonax/adapter/NotebookAdapter.java b/app/src/main/java/org/houxg/leamonax/adapter/NotebookAdapter.java index f1d1dc9..fc66470 100644 --- a/app/src/main/java/org/houxg/leamonax/adapter/NotebookAdapter.java +++ b/app/src/main/java/org/houxg/leamonax/adapter/NotebookAdapter.java @@ -56,14 +56,14 @@ public class NotebookAdapter extends RecyclerView.Adapter stack) { if (stack.isEmpty()) { - mData = AppDataBase.getRootNotebooks(AccountService.getCurrent().getUserId()); + mData = Notebook.getRootNotebooks(AccountService.getCurrent().getUserId()); } else { - Notebook parent = AppDataBase.getNotebookByServerId(stack.peek()); + Notebook parent = Notebook.getByServerId(stack.peek()); if (parent.isDeleted()) { stack.pop(); getSafeNotebook(stack); } else { - mData = AppDataBase.getChildNotebook(mStack.peek(), AccountService.getCurrent().getUserId()); + mData = Notebook.getChildNotebook(mStack.peek(), AccountService.getCurrent().getUserId()); mData.add(0, parent); } } @@ -148,7 +148,7 @@ public class NotebookAdapter extends RecyclerView.Adapter children = AppDataBase.getChildNotebook(notebook.getNotebookId(), AccountService.getCurrent().getUserId()); + List children = Notebook.getChildNotebook(notebook.getNotebookId(), AccountService.getCurrent().getUserId()); int childrenSize = children.size(); mData.addAll(children); notifyItemRangeInserted(1, childrenSize); diff --git a/app/src/main/java/org/houxg/leamonax/adapter/TagAdapter.java b/app/src/main/java/org/houxg/leamonax/adapter/TagAdapter.java index adc573d..4d9cfac 100644 --- a/app/src/main/java/org/houxg/leamonax/adapter/TagAdapter.java +++ b/app/src/main/java/org/houxg/leamonax/adapter/TagAdapter.java @@ -28,7 +28,7 @@ public class TagAdapter extends RecyclerView.Adapter { } public void refresh() { - mData = AppDataBase.getAllTags(AccountService.getCurrent().getUserId()); + mData = Tag.getAllTags(AccountService.getCurrent().getUserId()); notifyDataSetChanged(); } diff --git a/app/src/main/java/org/houxg/leamonax/database/AppDataBase.java b/app/src/main/java/org/houxg/leamonax/database/AppDataBase.java index 81f1a5f..97bbf45 100644 --- a/app/src/main/java/org/houxg/leamonax/database/AppDataBase.java +++ b/app/src/main/java/org/houxg/leamonax/database/AppDataBase.java @@ -6,10 +6,7 @@ import android.text.TextUtils; import com.raizlabs.android.dbflow.annotation.Database; import com.raizlabs.android.dbflow.annotation.Migration; import com.raizlabs.android.dbflow.sql.SQLiteType; -import com.raizlabs.android.dbflow.sql.language.Join; -import com.raizlabs.android.dbflow.sql.language.NameAlias; import com.raizlabs.android.dbflow.sql.language.SQLite; -import com.raizlabs.android.dbflow.sql.language.property.IProperty; import com.raizlabs.android.dbflow.sql.migration.AlterTableMigration; import com.raizlabs.android.dbflow.sql.migration.BaseMigration; import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; @@ -17,26 +14,14 @@ import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Account_Table; import org.houxg.leamonax.model.Note; -import org.houxg.leamonax.model.NoteFile; -import org.houxg.leamonax.model.NoteFile_Table; import org.houxg.leamonax.model.Note_Table; -import org.houxg.leamonax.model.Notebook; -import org.houxg.leamonax.model.Notebook_Table; import org.houxg.leamonax.model.RelationshipOfNoteTag; -import org.houxg.leamonax.model.RelationshipOfNoteTag_Table; import org.houxg.leamonax.model.Tag; import org.houxg.leamonax.model.Tag_Table; -import org.houxg.leamonax.utils.CollectionUtils; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; @Database(name = "leanote_db", version = 4) public class AppDataBase { - private static final String TAG = "AppDataBase:"; - @Migration(version = 2, database = AppDataBase.class) public static class UpdateTag extends BaseMigration { @@ -97,7 +82,7 @@ public class AppDataBase { } } - @Migration(version = 3, priority = 0, database = AppDataBase.class) + @Migration(version = 3, priority = 0, database = AppDataBase.class) public static class UpdateUsn extends BaseMigration { @Override @@ -142,7 +127,7 @@ public class AppDataBase { } } - @Migration(version = 4, priority = 0, database = AppDataBase.class) + @Migration(version = 4, priority = 0, database = AppDataBase.class) public static class UpdateLastUseTime extends BaseMigration { @Override @@ -172,241 +157,4 @@ public class AppDataBase { cursor.close(); } } - - public static void deleteNoteByLocalId(long localId) { - SQLite.delete().from(Note.class) - .where(Note_Table.id.eq(localId)) - .async() - .execute(); - } - - public static Note getNoteByServerId(String serverId) { - return SQLite.select() - .from(Note.class) - .where(Note_Table.noteId.eq(serverId)) - .querySingle(); - } - - public static Note getNoteByLocalId(long localId) { - return SQLite.select() - .from(Note.class) - .where(Note_Table.id.eq(localId)) - .querySingle(); - } - - public static List getNotesFromNotebook(String userId, long localNotebookId) { - Notebook notebook = getNotebookByLocalId(localNotebookId); - if (notebook == null) { - return new ArrayList<>(); - } - return SQLite.select() - .from(Note.class) - .where(Note_Table.notebookId.eq(notebook.getNotebookId())) - .and(Note_Table.userId.eq(userId)) - .and(Note_Table.isTrash.eq(false)) - .and(Note_Table.isDeleted.eq(false)) - .and(Note_Table.isTrash.eq(false)) - .queryList(); - } - - public static List getNotesByTagText(String tagText, String userId) { - Tag tag = getTagByText(tagText, userId); - if (tag == null) { - return new ArrayList<>(); - } - return getNotesByTagId(tag.getId()); - } - - public static List getAllNotes(String userId) { - return SQLite.select() - .from(Note.class) - .where(Note_Table.userId.eq(userId)) - .and(Note_Table.isTrash.eq(false)) - .and(Note_Table.isDeleted.eq(false)) - .and(Note_Table.isTrash.eq(false)) - .queryList(); - } - - public static Notebook getNotebookByServerId(String serverId) { - return SQLite.select() - .from(Notebook.class) - .where(Notebook_Table.notebookId.eq(serverId)) - .querySingle(); - } - - public static Notebook getNotebookByLocalId(long localId) { - return SQLite.select() - .from(Notebook.class) - .where(Notebook_Table.id.eq(localId)) - .querySingle(); - } - - public static Notebook getRecentNoteBook(String userId) { - Note recentNotes = SQLite.select() - .from(Note.class) - .where(Note_Table.userId.eq(userId)) - .and(Note_Table.notebookId.notEq("")) - .orderBy(Note_Table.updatedTime, false) - .querySingle(); - if (recentNotes != null) { - Notebook notebook = getNotebookByServerId(recentNotes.getNoteBookId()); - if (notebook != null && !notebook.isDeleted()) { - return notebook; - } - } - - return SQLite.select() - .from(Notebook.class) - .where(Notebook_Table.userId.eq(userId)) - .and(Notebook_Table.isDeletedOnServer.eq(false)) - .querySingle(); - } - - public static List getAllNotebook(String userId) { - return SQLite.select() - .from(Notebook.class) - .where(Notebook_Table.userId.eq(userId)) - .and(Notebook_Table.isDeletedOnServer.eq(false)) - .queryList(); - } - - public static List getRootNotebooks(String userId) { - return SQLite.select() - .from(Notebook.class) - .where(Notebook_Table.userId.eq(userId)) - .and(Notebook_Table.parentNotebookId.eq("")) - .and(Notebook_Table.isDeletedOnServer.eq(false)) - .queryList(); - } - - public static List getChildNotebook(String notebookId, String userId) { - return SQLite.select() - .from(Notebook.class) - .where(Notebook_Table.userId.eq(userId)) - .and(Notebook_Table.parentNotebookId.eq(notebookId)) - .and(Notebook_Table.isDeletedOnServer.eq(false)) - .queryList(); - } - - public static List getAllRelatedFile(long noteLocalId) { - return SQLite.select() - .from(NoteFile.class) - .where(NoteFile_Table.noteLocalId.eq(noteLocalId)) - .queryList(); - } - - public static NoteFile getNoteFileByLocalId(String localId) { - return SQLite.select() - .from(NoteFile.class) - .where(NoteFile_Table.localId.eq(localId)) - .querySingle(); - } - - public static NoteFile getNoteFileByServerId(String serverId) { - return SQLite.select() - .from(NoteFile.class) - .where(NoteFile_Table.serverId.eq(serverId)) - .querySingle(); - } - - public static void deleteFileExcept(long noteLocalId, Collection excepts) { - SQLite.delete() - .from(NoteFile.class) - .where(NoteFile_Table.noteLocalId.eq(noteLocalId)) - .and(NoteFile_Table.localId.notIn(excepts)) - .async() - .execute(); - } - - public static Account getAccount(String email, String host) { - return SQLite.select() - .from(Account.class) - .where(Account_Table.email.eq(email)) - .and(Account_Table.host.eq(host)) - .querySingle(); - } - - public static Account getAccountWithToken() { - return SQLite.select() - .from(Account.class) - .where(Account_Table.token.notEq("")) - .orderBy(Account_Table.lastUseTime, false) - .querySingle(); - } - - public static List getAccountListWithToken() { - return SQLite.select() - .from(Account.class) - .where(Account_Table.token.notEq("")) - .orderBy(Account_Table.lastUseTime, false) - .queryList(); - } - - public static List getTagByNoteLocalId(long noteLocalId) { - return SQLite.select() - .from(Tag.class).as("T") - .join(RelationshipOfNoteTag.class, Join.JoinType.INNER).as("R") - .on(Tag_Table.id.withTable(NameAlias.builder("T").build()) - .eq(RelationshipOfNoteTag_Table.tagLocalId.withTable(NameAlias.builder("R").build()))) - .where(RelationshipOfNoteTag_Table.noteLocalId.withTable(NameAlias.builder("R").build()).eq(noteLocalId)) - .queryList(); - } - - public static List getNotesByTagId(long tagId) { - IProperty[] properties = Note_Table.ALL_COLUMN_PROPERTIES; - NameAlias nameAlias = NameAlias.builder("N").build(); - for (int i = 0; i < properties.length; i++) { - properties[i] = properties[i].withTable(nameAlias); - } - return SQLite.select(properties) - .from(Note.class).as("N") - .join(RelationshipOfNoteTag.class, Join.JoinType.INNER).as("R") - .on(Tag_Table.id.withTable(NameAlias.builder("N").build()) - .eq(RelationshipOfNoteTag_Table.noteLocalId.withTable(NameAlias.builder("R").build()))) - .where(RelationshipOfNoteTag_Table.tagLocalId.withTable(NameAlias.builder("R").build()).eq(tagId)) - .queryList(); - } - - public static RelationshipOfNoteTag getRelationShip(long noteLocalId, long tagId, String userId) { - return SQLite.select() - .from(RelationshipOfNoteTag.class) - .where(RelationshipOfNoteTag_Table.userId.eq(userId)) - .and(RelationshipOfNoteTag_Table.tagLocalId.eq(tagId)) - .and(RelationshipOfNoteTag_Table.noteLocalId.eq(noteLocalId)) - .querySingle(); - } - - public static Tag getTagByText(String text, String userId) { - return SQLite.select() - .from(Tag.class) - .where(Tag_Table.userId.eq(userId)) - .and(Tag_Table.text.eq(text)) - .querySingle(); - } - - public static void deleteAllRelatedTags(long noteLocalId, String userId) { - SQLite.delete() - .from(RelationshipOfNoteTag.class) - .where(RelationshipOfNoteTag_Table.userId.eq(userId)) - .and(RelationshipOfNoteTag_Table.noteLocalId.eq(noteLocalId)) - .async() - .execute(); - } - - public static void deleteRelatedTags(long noteLocalId, String userId, long firstReservedId, long... reservedIds) { - SQLite.delete() - .from(RelationshipOfNoteTag.class) - .where(RelationshipOfNoteTag_Table.userId.eq(userId)) - .and(RelationshipOfNoteTag_Table.noteLocalId.eq(noteLocalId)) - .and(RelationshipOfNoteTag_Table.id.notIn(firstReservedId, reservedIds)) - .async() - .execute(); - } - - public static List getAllTags(String userId) { - return SQLite.select() - .from(Tag.class) - .where(Tag_Table.userId.eq(userId)) - .queryList(); - } } diff --git a/app/src/main/java/org/houxg/leamonax/model/Account.java b/app/src/main/java/org/houxg/leamonax/model/Account.java index d7afc03..a833c57 100644 --- a/app/src/main/java/org/houxg/leamonax/model/Account.java +++ b/app/src/main/java/org/houxg/leamonax/model/Account.java @@ -4,10 +4,14 @@ import com.google.gson.annotations.SerializedName; import com.raizlabs.android.dbflow.annotation.Column; import com.raizlabs.android.dbflow.annotation.PrimaryKey; import com.raizlabs.android.dbflow.annotation.Table; +import com.raizlabs.android.dbflow.sql.language.SQLite; +import com.raizlabs.android.dbflow.sql.language.Select; import com.raizlabs.android.dbflow.structure.BaseModel; import org.houxg.leamonax.database.AppDataBase; +import java.util.List; + @Table(name = "Account", database = AppDataBase.class) public class Account extends BaseModel { @@ -170,6 +174,37 @@ public class Account extends BaseModel { return lastUseTime; } + public static Account getAccount(String email, String host) { + return SQLite.select() + .from(Account.class) + .where(Account_Table.email.eq(email)) + .and(Account_Table.host.eq(host)) + .querySingle(); + } + + public static Account getAccountWithToken() { + return SQLite.select() + .from(Account.class) + .where(Account_Table.token.notEq("")) + .orderBy(Account_Table.lastUseTime, false) + .querySingle(); + } + + public static List getAccountListWithToken() { + return SQLite.select() + .from(Account.class) + .where(Account_Table.token.notEq("")) + .orderBy(Account_Table.lastUseTime, false) + .queryList(); + } + + public static Account getAccountById(long id) { + return new Select() + .from(Account.class) + .where(Account_Table.id.eq(id)) + .querySingle(); + } + @Override public String toString() { return "Account{" + diff --git a/app/src/main/java/org/houxg/leamonax/model/Note.java b/app/src/main/java/org/houxg/leamonax/model/Note.java index 0b08f31..dac22b8 100644 --- a/app/src/main/java/org/houxg/leamonax/model/Note.java +++ b/app/src/main/java/org/houxg/leamonax/model/Note.java @@ -7,15 +7,22 @@ import com.google.gson.annotations.SerializedName; import com.raizlabs.android.dbflow.annotation.Column; import com.raizlabs.android.dbflow.annotation.PrimaryKey; import com.raizlabs.android.dbflow.annotation.Table; +import com.raizlabs.android.dbflow.sql.language.Join; +import com.raizlabs.android.dbflow.sql.language.NameAlias; +import com.raizlabs.android.dbflow.sql.language.SQLite; +import com.raizlabs.android.dbflow.sql.language.property.IProperty; import com.raizlabs.android.dbflow.structure.BaseModel; import org.houxg.leamonax.database.AppDataBase; +import org.houxg.leamonax.service.AccountService; import org.houxg.leamonax.utils.CollectionUtils; import org.houxg.leamonax.utils.TimeUtils; import java.io.Serializable; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; +import java.util.Locale; /** * Created by binnchx on 10/18/15. @@ -257,6 +264,86 @@ public class Note extends BaseModel implements Serializable { public void setPublicTime(String publicTime) { } + public static List searchByTitle(String keyword) { + keyword = String.format(Locale.US, "%%%s%%", keyword); + return SQLite.select() + .from(Note.class) + .where(Note_Table.userId.eq(AccountService.getCurrent().getUserId())) + .and(Note_Table.title.like(keyword)) + .and(Note_Table.isTrash.eq(false)) + .and(Note_Table.isDeleted.eq(false)) + .queryList(); + } + + public static Note getByServerId(String serverId) { + return SQLite.select() + .from(Note.class) + .where(Note_Table.noteId.eq(serverId)) + .querySingle(); + } + + public static Note getByLocalId(long localId) { + return SQLite.select() + .from(Note.class) + .where(Note_Table.id.eq(localId)) + .querySingle(); + } + + public static List getAllNotes(String userId) { + return SQLite.select() + .from(Note.class) + .where(Note_Table.userId.eq(userId)) + .and(Note_Table.isTrash.eq(false)) + .and(Note_Table.isDeleted.eq(false)) + .and(Note_Table.isTrash.eq(false)) + .queryList(); + } + + public static List getNotesFromNotebook(String userId, long localNotebookId) { + Notebook notebook = Notebook.getByLocalId(localNotebookId); + if (notebook == null) { + return new ArrayList<>(); + } + return SQLite.select() + .from(Note.class) + .where(Note_Table.notebookId.eq(notebook.getNotebookId())) + .and(Note_Table.userId.eq(userId)) + .and(Note_Table.isTrash.eq(false)) + .and(Note_Table.isDeleted.eq(false)) + .and(Note_Table.isTrash.eq(false)) + .queryList(); + } + + public static List getByTagText(String tagText, String userId) { + Tag tag = Tag.getByText(tagText, userId); + if (tag == null) { + return new ArrayList<>(); + } + return getNotesByTagId(tag.getId()); + } + + private static List getNotesByTagId(long tagId) { + IProperty[] properties = Note_Table.ALL_COLUMN_PROPERTIES; + NameAlias nameAlias = NameAlias.builder("N").build(); + for (int i = 0; i < properties.length; i++) { + properties[i] = properties[i].withTable(nameAlias); + } + return SQLite.select(properties) + .from(Note.class).as("N") + .join(RelationshipOfNoteTag.class, Join.JoinType.INNER).as("R") + .on(Tag_Table.id.withTable(NameAlias.builder("N").build()) + .eq(RelationshipOfNoteTag_Table.noteLocalId.withTable(NameAlias.builder("R").build()))) + .where(RelationshipOfNoteTag_Table.tagLocalId.withTable(NameAlias.builder("R").build()).eq(tagId)) + .queryList(); + } + + public static void deleteAll(String userId) { + SQLite.delete() + .from(Note.class) + .where(Note_Table.userId.eq(userId)) + .execute(); + } + @Override public String toString() { return "Note{" + diff --git a/app/src/main/java/org/houxg/leamonax/model/NoteFile.java b/app/src/main/java/org/houxg/leamonax/model/NoteFile.java index c42f6d2..3688028 100644 --- a/app/src/main/java/org/houxg/leamonax/model/NoteFile.java +++ b/app/src/main/java/org/houxg/leamonax/model/NoteFile.java @@ -4,10 +4,14 @@ import com.google.gson.annotations.SerializedName; import com.raizlabs.android.dbflow.annotation.Column; import com.raizlabs.android.dbflow.annotation.PrimaryKey; import com.raizlabs.android.dbflow.annotation.Table; +import com.raizlabs.android.dbflow.sql.language.SQLite; import com.raizlabs.android.dbflow.structure.BaseModel; import org.houxg.leamonax.database.AppDataBase; +import java.util.Collection; +import java.util.List; + @Table(name = "NoteFile", database = AppDataBase.class) public class NoteFile extends BaseModel { @@ -103,4 +107,34 @@ public class NoteFile extends BaseModel { public void setIsAttach(boolean mIsAttach) { this.mIsAttach = mIsAttach; } + + public static List getAllRelated(long noteLocalId) { + return SQLite.select() + .from(NoteFile.class) + .where(NoteFile_Table.noteLocalId.eq(noteLocalId)) + .queryList(); + } + + public static NoteFile getByLocalId(String localId) { + return SQLite.select() + .from(NoteFile.class) + .where(NoteFile_Table.localId.eq(localId)) + .querySingle(); + } + + public static NoteFile getByServerId(String serverId) { + return SQLite.select() + .from(NoteFile.class) + .where(NoteFile_Table.serverId.eq(serverId)) + .querySingle(); + } + + public static void deleteExcept(long noteLocalId, Collection excepts) { + SQLite.delete() + .from(NoteFile.class) + .where(NoteFile_Table.noteLocalId.eq(noteLocalId)) + .and(NoteFile_Table.localId.notIn(excepts)) + .async() + .execute(); + } } diff --git a/app/src/main/java/org/houxg/leamonax/model/Notebook.java b/app/src/main/java/org/houxg/leamonax/model/Notebook.java index b7d820c..b2fabb4 100644 --- a/app/src/main/java/org/houxg/leamonax/model/Notebook.java +++ b/app/src/main/java/org/houxg/leamonax/model/Notebook.java @@ -4,10 +4,13 @@ import com.google.gson.annotations.SerializedName; import com.raizlabs.android.dbflow.annotation.Column; import com.raizlabs.android.dbflow.annotation.PrimaryKey; import com.raizlabs.android.dbflow.annotation.Table; +import com.raizlabs.android.dbflow.sql.language.SQLite; import com.raizlabs.android.dbflow.structure.BaseModel; import org.houxg.leamonax.database.AppDataBase; +import java.util.List; + /** * Created by binnchx on 11/1/15. */ @@ -177,4 +180,72 @@ public class Notebook extends BaseModel { public String getMsg() { return msg; } + + public static List getAllNotebooks(String userId) { + return SQLite.select() + .from(Notebook.class) + .where(Notebook_Table.userId.eq(userId)) + .and(Notebook_Table.isDeletedOnServer.eq(false)) + .queryList(); + } + + public static Notebook getByLocalId(long localId) { + return SQLite.select() + .from(Notebook.class) + .where(Notebook_Table.id.eq(localId)) + .querySingle(); + } + + public static Notebook getByServerId(String serverId) { + return SQLite.select() + .from(Notebook.class) + .where(Notebook_Table.notebookId.eq(serverId)) + .querySingle(); + } + + public static Notebook getRecentNoteBook(String userId) { + Note recentNotes = SQLite.select() + .from(Note.class) + .where(Note_Table.userId.eq(userId)) + .and(Note_Table.notebookId.notEq("")) + .orderBy(Note_Table.updatedTime, false) + .querySingle(); + if (recentNotes != null) { + Notebook notebook = getByServerId(recentNotes.getNoteBookId()); + if (notebook != null && !notebook.isDeleted()) { + return notebook; + } + } + + return SQLite.select() + .from(Notebook.class) + .where(Notebook_Table.userId.eq(userId)) + .and(Notebook_Table.isDeletedOnServer.eq(false)) + .querySingle(); + } + + public static List getRootNotebooks(String userId) { + return SQLite.select() + .from(Notebook.class) + .where(Notebook_Table.userId.eq(userId)) + .and(Notebook_Table.parentNotebookId.eq("")) + .and(Notebook_Table.isDeletedOnServer.eq(false)) + .queryList(); + } + + public static List getChildNotebook(String notebookId, String userId) { + return SQLite.select() + .from(Notebook.class) + .where(Notebook_Table.userId.eq(userId)) + .and(Notebook_Table.parentNotebookId.eq(notebookId)) + .and(Notebook_Table.isDeletedOnServer.eq(false)) + .queryList(); + } + + public static void deleteAll(String userId) { + SQLite.delete() + .from(Notebook.class) + .where(Notebook_Table.userId.eq(userId)) + .execute(); + } } diff --git a/app/src/main/java/org/houxg/leamonax/model/RelationshipOfNoteTag.java b/app/src/main/java/org/houxg/leamonax/model/RelationshipOfNoteTag.java index a58ee71..ea632f4 100644 --- a/app/src/main/java/org/houxg/leamonax/model/RelationshipOfNoteTag.java +++ b/app/src/main/java/org/houxg/leamonax/model/RelationshipOfNoteTag.java @@ -4,6 +4,7 @@ package org.houxg.leamonax.model; import com.raizlabs.android.dbflow.annotation.Column; import com.raizlabs.android.dbflow.annotation.PrimaryKey; import com.raizlabs.android.dbflow.annotation.Table; +import com.raizlabs.android.dbflow.sql.language.SQLite; import com.raizlabs.android.dbflow.structure.BaseModel; import org.houxg.leamonax.database.AppDataBase; @@ -43,4 +44,11 @@ public class RelationshipOfNoteTag extends BaseModel { public long getTagLocalId() { return tagLocalId; } + + public static void deleteAll(String userId) { + SQLite.delete() + .from(RelationshipOfNoteTag.class) + .where(RelationshipOfNoteTag_Table.userId.eq(userId)) + .execute(); + } } diff --git a/app/src/main/java/org/houxg/leamonax/model/Tag.java b/app/src/main/java/org/houxg/leamonax/model/Tag.java index 3434f1b..3d880fe 100644 --- a/app/src/main/java/org/houxg/leamonax/model/Tag.java +++ b/app/src/main/java/org/houxg/leamonax/model/Tag.java @@ -5,11 +5,16 @@ import com.google.gson.annotations.SerializedName; import com.raizlabs.android.dbflow.annotation.Column; import com.raizlabs.android.dbflow.annotation.PrimaryKey; import com.raizlabs.android.dbflow.annotation.Table; +import com.raizlabs.android.dbflow.sql.language.Join; +import com.raizlabs.android.dbflow.sql.language.NameAlias; +import com.raizlabs.android.dbflow.sql.language.SQLite; import com.raizlabs.android.dbflow.structure.BaseModel; import org.houxg.leamonax.database.AppDataBase; import org.houxg.leamonax.utils.TimeUtils; +import java.util.List; + @Table(name = "Tag", database = AppDataBase.class) public class Tag extends BaseModel{ @@ -116,4 +121,64 @@ public class Tag extends BaseModel{ createdTime = TimeUtils.toTimestamp(createTimeData); updatedTime = TimeUtils.toTimestamp(updatedTimeData); } + + public static Tag getByText(String text, String userId) { + return SQLite.select() + .from(Tag.class) + .where(Tag_Table.userId.eq(userId)) + .and(Tag_Table.text.eq(text)) + .querySingle(); + } + + public static List getAllTags(String userId) { + return SQLite.select() + .from(Tag.class) + .where(Tag_Table.userId.eq(userId)) + .queryList(); + } + + public static List getByNoteLocalId(long noteLocalId) { + return SQLite.select() + .from(Tag.class).as("T") + .join(RelationshipOfNoteTag.class, Join.JoinType.INNER).as("R") + .on(Tag_Table.id.withTable(NameAlias.builder("T").build()) + .eq(RelationshipOfNoteTag_Table.tagLocalId.withTable(NameAlias.builder("R").build()))) + .where(RelationshipOfNoteTag_Table.noteLocalId.withTable(NameAlias.builder("R").build()).eq(noteLocalId)) + .queryList(); + } + + public static RelationshipOfNoteTag getRelationShip(long noteLocalId, long tagId, String userId) { + return SQLite.select() + .from(RelationshipOfNoteTag.class) + .where(RelationshipOfNoteTag_Table.userId.eq(userId)) + .and(RelationshipOfNoteTag_Table.tagLocalId.eq(tagId)) + .and(RelationshipOfNoteTag_Table.noteLocalId.eq(noteLocalId)) + .querySingle(); + } + + public static void deleteAllRelatedTags(long noteLocalId, String userId) { + SQLite.delete() + .from(RelationshipOfNoteTag.class) + .where(RelationshipOfNoteTag_Table.userId.eq(userId)) + .and(RelationshipOfNoteTag_Table.noteLocalId.eq(noteLocalId)) + .async() + .execute(); + } + + public static void deleteRelatedTags(long noteLocalId, String userId, long firstReservedId, long... reservedIds) { + SQLite.delete() + .from(RelationshipOfNoteTag.class) + .where(RelationshipOfNoteTag_Table.userId.eq(userId)) + .and(RelationshipOfNoteTag_Table.noteLocalId.eq(noteLocalId)) + .and(RelationshipOfNoteTag_Table.id.notIn(firstReservedId, reservedIds)) + .async() + .execute(); + } + + public static void deleteAll(String userId) { + SQLite.delete() + .from(Tag.class) + .where(Tag_Table.userId.eq(userId)) + .execute(); + } } diff --git a/app/src/main/java/org/houxg/leamonax/service/AccountService.java b/app/src/main/java/org/houxg/leamonax/service/AccountService.java index 79a697e..6f8e858 100644 --- a/app/src/main/java/org/houxg/leamonax/service/AccountService.java +++ b/app/src/main/java/org/houxg/leamonax/service/AccountService.java @@ -1,10 +1,6 @@ package org.houxg.leamonax.service; -import com.raizlabs.android.dbflow.sql.language.Select; - -import org.houxg.leamonax.database.AppDataBase; import org.houxg.leamonax.model.Account; -import org.houxg.leamonax.model.Account_Table; import org.houxg.leamonax.model.Authentication; import org.houxg.leamonax.model.BaseResponse; import org.houxg.leamonax.model.User; @@ -30,7 +26,7 @@ public class AccountService { } public static long saveToAccount(Authentication authentication, String host) { - Account localAccount = AppDataBase.getAccount(authentication.getEmail(), host); + Account localAccount = Account.getAccount(authentication.getEmail(), host); if (localAccount == null) { localAccount = new Account(); } @@ -44,7 +40,7 @@ public class AccountService { } public static void saveToAccount(User user, String host) { - Account localAccount = AppDataBase.getAccount(user.getEmail(), host); + Account localAccount = Account.getAccount(user.getEmail(), host); if (localAccount == null) { localAccount = new Account(); } @@ -72,18 +68,11 @@ public class AccountService { } public static Account getCurrent() { - return AppDataBase.getAccountWithToken(); + return Account.getAccountWithToken(); } public static List getAccountList() { - return AppDataBase.getAccountListWithToken(); - } - - public static Account getAccountById(long id) { - return new Select() - .from(Account.class) - .where(Account_Table.id.eq(id)) - .querySingle(); + return Account.getAccountListWithToken(); } public static boolean isSignedIn() { diff --git a/app/src/main/java/org/houxg/leamonax/service/NoteFileService.java b/app/src/main/java/org/houxg/leamonax/service/NoteFileService.java index 656f1eb..208a2dd 100644 --- a/app/src/main/java/org/houxg/leamonax/service/NoteFileService.java +++ b/app/src/main/java/org/houxg/leamonax/service/NoteFileService.java @@ -7,7 +7,6 @@ import com.elvishew.xlog.XLog; import org.bson.types.ObjectId; import org.houxg.leamonax.Leamonax; -import org.houxg.leamonax.database.AppDataBase; import org.houxg.leamonax.model.NoteFile; import java.io.File; @@ -32,7 +31,7 @@ public class NoteFileService { private static final String IMAGE_PATH_WITH_SLASH = "/getImage"; public static String convertFromLocalIdToServerId(String localId) { - NoteFile noteFile = AppDataBase.getNoteFileByLocalId(localId); + NoteFile noteFile = NoteFile.getByLocalId(localId); return noteFile == null ? null : noteFile.getServerId(); } @@ -61,7 +60,7 @@ public class NoteFileService { public static String getImagePath(Uri uri) { String localId = uri.getQueryParameter("id"); - NoteFile noteFile = AppDataBase.getNoteFileByLocalId(localId); + NoteFile noteFile = NoteFile.getByLocalId(localId); if (noteFile == null) { return null; } @@ -74,11 +73,11 @@ public class NoteFileService { } public static List getRelatedNoteFiles(long noteLocalId) { - return AppDataBase.getAllRelatedFile(noteLocalId); + return NoteFile.getAllRelated(noteLocalId); } public static InputStream getImage(String localId) { - NoteFile noteFile = AppDataBase.getNoteFileByLocalId(localId); + NoteFile noteFile = NoteFile.getByLocalId(localId); if (noteFile == null) { return null; } diff --git a/app/src/main/java/org/houxg/leamonax/service/NoteService.java b/app/src/main/java/org/houxg/leamonax/service/NoteService.java index 0a58596..adc011d 100644 --- a/app/src/main/java/org/houxg/leamonax/service/NoteService.java +++ b/app/src/main/java/org/houxg/leamonax/service/NoteService.java @@ -12,7 +12,6 @@ import com.raizlabs.android.dbflow.sql.language.SQLite; import org.bson.types.ObjectId; import org.houxg.leamonax.R; import org.houxg.leamonax.ReadableException; -import org.houxg.leamonax.database.AppDataBase; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.NoteFile; @@ -38,8 +37,6 @@ import okhttp3.MediaType; import okhttp3.MultipartBody; import okhttp3.RequestBody; import retrofit2.Call; -import rx.Observable; -import rx.Subscriber; public class NoteService { @@ -57,7 +54,7 @@ public class NoteService { do { notebooks = RetrofitUtils.excuteWithException(ApiProvider.getInstance().getNotebookApi().getSyncNotebooks(notebookUsn, MAX_ENTRY)); for (Notebook remoteNotebook : notebooks) { - Notebook localNotebook = AppDataBase.getNotebookByServerId(remoteNotebook.getNotebookId()); + Notebook localNotebook = Notebook.getByServerId(remoteNotebook.getNotebookId()); if (localNotebook == null) { XLog.i(TAG + "notebook insert, usn=" + remoteNotebook.getUsn() + ", id=" + remoteNotebook.getNotebookId()); remoteNotebook.insert(); @@ -82,7 +79,7 @@ public class NoteService { notes = RetrofitUtils.excuteWithException(ApiProvider.getInstance().getNoteApi().getSyncNotes(noteUsn, MAX_ENTRY)); for (Note noteMeta : notes) { Note remoteNote = RetrofitUtils.excuteWithException(ApiProvider.getInstance().getNoteApi().getNoteAndContent(noteMeta.getNoteId())); - Note localNote = AppDataBase.getNoteByServerId(noteMeta.getNoteId()); + Note localNote = Note.getByServerId(noteMeta.getNoteId()); noteUsn = remoteNote.getUsn(); long localId; if (localNote == null) { @@ -129,9 +126,9 @@ public class NoteService { for (NoteFile remote : remoteFiles) { NoteFile local; if (TextUtils.isEmpty(remote.getLocalId())) { - local = AppDataBase.getNoteFileByServerId(remote.getServerId()); + local = NoteFile.getByServerId(remote.getServerId()); } else { - local = AppDataBase.getNoteFileByLocalId(remote.getLocalId()); + local = NoteFile.getByLocalId(remote.getLocalId()); } if (local != null) { XLog.i(TAG + "has local file, id=" + remote.getServerId()); @@ -146,7 +143,7 @@ public class NoteService { local.save(); excepts.add(local.getLocalId()); } - AppDataBase.deleteFileExcept(noteLocalId, excepts); + NoteFile.deleteExcept(noteLocalId, excepts); } private static String convertToLocalImageLinkForRichText(long noteLocalId, String noteContent) { @@ -159,7 +156,7 @@ public class NoteService { XLog.i(TAG + "in=" + original); Uri linkUri = Uri.parse(original.substring(6, original.length() - 1)); String serverId = linkUri.getQueryParameter("fileId"); - NoteFile noteFile = AppDataBase.getNoteFileByServerId(serverId); + NoteFile noteFile = NoteFile.getByServerId(serverId); if (noteFile == null) { noteFile = new NoteFile(); noteFile.setNoteId((Long) extraData[0]); @@ -184,7 +181,7 @@ public class NoteService { public String replaceWith(String original, Object... extraData) { Uri linkUri = Uri.parse(original.substring(1, original.length() - 1)); String serverId = linkUri.getQueryParameter("fileId"); - NoteFile noteFile = AppDataBase.getNoteFileByServerId(serverId); + NoteFile noteFile = NoteFile.getByServerId(serverId); if (noteFile == null) { noteFile = new NoteFile(); noteFile.setNoteId((Long) extraData[0]); @@ -199,7 +196,7 @@ public class NoteService { } public static void saveNote(final long noteLocalId) { - Note modifiedNote = AppDataBase.getNoteByLocalId(noteLocalId); + Note modifiedNote = Note.getByLocalId(noteLocalId); Map requestBodyMap = generateCommonBodyMap(modifiedNote); List fileBodies = handleFileBodies(modifiedNote, requestBodyMap); @@ -279,7 +276,7 @@ public class NoteService { if (serverNote == null) { return false; } - Note localNote = AppDataBase.getNoteByServerId(serverId); + Note localNote = Note.getByServerId(serverId); long localId; if (localNote == null) { localId = serverNote.insert(); @@ -315,7 +312,7 @@ public class NoteService { requestBodyMap.put("CreatedTime", createPartFromString(TimeUtils.toServerTime(note.getCreatedTimeVal()))); requestBodyMap.put("UpdatedTime", createPartFromString(TimeUtils.toServerTime(note.getUpdatedTimeVal()))); - List tags = AppDataBase.getTagByNoteLocalId(note.getId()); + List tags = Tag.getByNoteLocalId(note.getId()); if (CollectionUtils.isNotEmpty(tags)) { int size = tags.size(); @@ -337,8 +334,8 @@ public class NoteService { } else { imageLocalIds = getImagesFromContentForRichText(note.getContent()); } - AppDataBase.deleteFileExcept(note.getId(), imageLocalIds); - List files = AppDataBase.getAllRelatedFile(note.getId()); + NoteFile.deleteExcept(note.getId(), imageLocalIds); + List files = NoteFile.getAllRelated(note.getId()); if (CollectionUtils.isNotEmpty(files)) { int size = files.size(); for (int index = 0; index < size; index++) { @@ -390,12 +387,12 @@ public class NoteService { public static void deleteNote(Note note) { if (note.isLocalNote()) { - AppDataBase.deleteNoteByLocalId(note.getId()); + note.delete(); } else { Call call = ApiProvider.getInstance().getNoteApi().delete(note.getNoteId(), note.getUsn()); UpdateRe response = RetrofitUtils.excuteWithException(call); if (response.isOk()) { - AppDataBase.deleteNoteByLocalId(note.getId()); + note.delete(); updateNoteUsnIfNeed(response.getUsn()); } else { throw new IllegalStateException(response.getMsg()); @@ -425,7 +422,7 @@ public class NoteService { if (TextUtils.isEmpty(tagText)) { continue; } - Tag tag = AppDataBase.getTagByText(tagText, currentUid); + Tag tag = Tag.getByText(tagText, currentUid); long tagId; long relationShipId; RelationshipOfNoteTag relationShip; @@ -436,7 +433,7 @@ public class NoteService { tagId = tag.getId(); } - relationShip = AppDataBase.getRelationShip(noteLocalId, tagId, currentUid); + relationShip = Tag.getRelationShip(noteLocalId, tagId, currentUid); if (relationShip == null) { relationShip = new RelationshipOfNoteTag(noteLocalId, tagId, currentUid); relationShipId = relationShip.insert(); @@ -446,9 +443,9 @@ public class NoteService { reservedIds.add(relationShipId); } if (CollectionUtils.isEmpty(reservedIds)) { - AppDataBase.deleteAllRelatedTags(noteLocalId, currentUid); + Tag.deleteAllRelatedTags(noteLocalId, currentUid); } else { - AppDataBase.deleteRelatedTags(noteLocalId, + Tag.deleteRelatedTags(noteLocalId, currentUid, reservedIds.get(0), CollectionUtils.toPrimitive(reservedIds.subList(1, reservedIds.size())) @@ -480,15 +477,4 @@ public class NoteService { RequestBody fileBody = RequestBody.create(MediaType.parse(mimeType), tempFile); return MultipartBody.Part.createFormData(String.format("FileDatas[%s]", noteFile.getLocalId()), tempFile.getName(), fileBody); } - - public static List searchNoteWithTitle(String keyword) { - keyword = String.format(Locale.US, "%%%s%%", keyword); - return SQLite.select() - .from(Note.class) - .where(Note_Table.userId.eq(AccountService.getCurrent().getUserId())) - .and(Note_Table.title.like(keyword)) - .and(Note_Table.isTrash.eq(false)) - .and(Note_Table.isDeleted.eq(false)) - .queryList(); - } } diff --git a/app/src/main/java/org/houxg/leamonax/service/NotebookService.java b/app/src/main/java/org/houxg/leamonax/service/NotebookService.java index d9f9536..3b22ca4 100644 --- a/app/src/main/java/org/houxg/leamonax/service/NotebookService.java +++ b/app/src/main/java/org/houxg/leamonax/service/NotebookService.java @@ -33,7 +33,7 @@ public class NotebookService { } public static String getTitle(long notebookLocalId) { - Notebook notebook = AppDataBase.getNotebookByLocalId(notebookLocalId); + Notebook notebook = Notebook.getByLocalId(notebookLocalId); return notebook != null ? notebook.getTitle() : ""; } } diff --git a/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java b/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java index 79fa71e..bd89a49 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java @@ -160,9 +160,9 @@ public class MainActivity extends BaseActivity implements Navigation.Callback { Notebook notebook; Navigation.Mode currentMode = mNavigation.getCurrentMode(); if (currentMode == Navigation.Mode.NOTEBOOK) { - notebook = AppDataBase.getNotebookByLocalId(currentMode.notebookId); + notebook = Notebook.getByLocalId(currentMode.notebookId); } else { - notebook = AppDataBase.getRecentNoteBook(AccountService.getCurrent().getUserId()); + notebook = Notebook.getRecentNoteBook(AccountService.getCurrent().getUserId()); } if (notebook != null) { newNote.setNoteBookId(notebook.getNotebookId()); @@ -192,13 +192,13 @@ public class MainActivity extends BaseActivity implements Navigation.Callback { List notes; switch (mode) { case RECENT_NOTES: - notes = AppDataBase.getAllNotes(AccountService.getCurrent().getUserId()); + notes = Note.getAllNotes(AccountService.getCurrent().getUserId()); break; case NOTEBOOK: - notes = AppDataBase.getNotesFromNotebook(AccountService.getCurrent().getUserId(), mode.notebookId); + notes = Note.getNotesFromNotebook(AccountService.getCurrent().getUserId(), mode.notebookId); break; case TAG: - notes = AppDataBase.getNotesByTagText(mode.tagText, AccountService.getCurrent().getUserId()); + notes = Note.getByTagText(mode.tagText, AccountService.getCurrent().getUserId()); break; default: return false; diff --git a/app/src/main/java/org/houxg/leamonax/ui/Navigation.java b/app/src/main/java/org/houxg/leamonax/ui/Navigation.java index 993f574..109ce91 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/Navigation.java +++ b/app/src/main/java/org/houxg/leamonax/ui/Navigation.java @@ -416,7 +416,7 @@ public class Navigation { public boolean onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQ_ADD_ACCOUNT) { if (resultCode == RESULT_OK) { - Account account = AccountService.getAccountById(SignInActivity.getAccountIdFromData(data)); + Account account = Account.getAccountById(SignInActivity.getAccountIdFromData(data)); if (account != null) { changeAccount(account); } diff --git a/app/src/main/java/org/houxg/leamonax/ui/NotePreviewActivity.java b/app/src/main/java/org/houxg/leamonax/ui/NotePreviewActivity.java index f12c80f..733bc40 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/NotePreviewActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/NotePreviewActivity.java @@ -56,7 +56,7 @@ public class NotePreviewActivity extends BaseActivity implements EditorFragment. ButterKnife.bind(this); initToolBar((Toolbar) findViewById(R.id.toolbar), true); long noteLocalId = getIntent().getLongExtra(EXT_NOTE_LOCAL_ID, -1); - mNote = AppDataBase.getNoteByLocalId(noteLocalId); + mNote = Note.getByLocalId(noteLocalId); if (mNote == null) { ToastUtils.show(this, R.string.note_not_found); CrashReport.postCatchedException(new IllegalStateException("Note not found while preview, localId=" + noteLocalId)); @@ -113,7 +113,7 @@ public class NotePreviewActivity extends BaseActivity implements EditorFragment. if (requestCode == REQ_EDIT) { switch (resultCode) { case RESULT_OK: - mNote = AppDataBase.getNoteByLocalId(mNote.getId()); + mNote = Note.getByLocalId(mNote.getId()); if (mNote == null) { finish(); } else { @@ -163,7 +163,7 @@ public class NotePreviewActivity extends BaseActivity implements EditorFragment. @Override public void onNext(Long aLong) { - mNote = AppDataBase.getNoteByLocalId(mNote.getId()); + mNote = Note.getByLocalId(mNote.getId()); mNote.setIsDirty(false); mNote.save(); refresh(); @@ -205,7 +205,7 @@ public class NotePreviewActivity extends BaseActivity implements EditorFragment. @Override public void call(Boolean isSucceed) { if (isSucceed) { - mNote = AppDataBase.getNoteByServerId(mNote.getNoteId()); + mNote = Note.getByServerId(mNote.getNoteId()); refresh(); } } diff --git a/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java b/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java index 2bb60ae..0e43155 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java @@ -136,7 +136,7 @@ public class SearchActivity extends BaseActivity implements NoteAdapter.NoteAdap if (TextUtils.isEmpty(keyword)) { mNotes = new ArrayList<>(); } else { - mNotes = NoteService.searchNoteWithTitle(keyword); + mNotes = Note.searchByTitle(keyword); Collections.sort(mNotes, new Note.UpdateTimeComparetor()); } mAdapter.setHighlight(keyword); diff --git a/app/src/main/java/org/houxg/leamonax/ui/SettingsActivity.java b/app/src/main/java/org/houxg/leamonax/ui/SettingsActivity.java index 3bbbab6..ecdd87a 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/SettingsActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/SettingsActivity.java @@ -192,26 +192,14 @@ public class SettingsActivity extends BaseActivity { public void call(Subscriber subscriber) { if (!subscriber.isUnsubscribed()) { Account currentUser = AccountService.getCurrent(); - SQLite.delete() - .from(Note.class) - .where(Note_Table.userId.eq(currentUser.getUserId())) - .execute(); - SQLite.delete() - .from(Notebook.class) - .where(Notebook_Table.userId.eq(currentUser.getUserId())) - .execute(); - SQLite.delete() - .from(Tag.class) - .where(Tag_Table.userId.eq(currentUser.getUserId())) - .execute(); - SQLite.delete() - .from(RelationshipOfNoteTag.class) - .where(RelationshipOfNoteTag_Table.userId.eq(currentUser.getUserId())) - .execute(); - Account account = AccountService.getCurrent(); - account.setNoteUsn(0); - account.setNotebookUsn(0); - account.update(); + String userId = currentUser.getUserId(); + Note.deleteAll(userId); + Notebook.deleteAll(userId); + Tag.deleteAll(userId); + RelationshipOfNoteTag.deleteAll(userId); + currentUser.setNoteUsn(0); + currentUser.setNotebookUsn(0); + currentUser.update(); subscriber.onNext(null); subscriber.onCompleted(); } diff --git a/app/src/main/java/org/houxg/leamonax/ui/edit/NoteEditActivity.java b/app/src/main/java/org/houxg/leamonax/ui/edit/NoteEditActivity.java index 2bacb7a..2ca002d 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/edit/NoteEditActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/edit/NoteEditActivity.java @@ -17,7 +17,6 @@ import com.elvishew.xlog.XLog; import org.houxg.leamonax.Leamonax; import org.houxg.leamonax.R; import org.houxg.leamonax.ReadableException; -import org.houxg.leamonax.database.AppDataBase; import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.Tag; import org.houxg.leamonax.service.NoteFileService; @@ -82,8 +81,8 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi return; } mIsNewNote = getIntent().getBooleanExtra(EXT_IS_NEW_NOTE, false); - mOriginal = new Wrapper(AppDataBase.getNoteByLocalId(noteLocalId)); - mModified = new Wrapper(AppDataBase.getNoteByLocalId(noteLocalId)); + mOriginal = new Wrapper(Note.getByLocalId(noteLocalId)); + mModified = new Wrapper(Note.getByLocalId(noteLocalId)); setResult(RESULT_CANCELED); } @@ -156,7 +155,7 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi @Override public void onNext(Long noteLocalId) { - Note localNote = AppDataBase.getNoteByLocalId(noteLocalId); + Note localNote = Note.getByLocalId(noteLocalId); localNote.setIsDirty(false); localNote.save(); } @@ -185,7 +184,7 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi if (mIsNewNote && isTitleContentEmpty(wrapper.note)) { XLog.i(TAG + "remove empty note, id=" + wrapper.note.getId()); - AppDataBase.deleteNoteByLocalId(wrapper.note.getId()); + wrapper.note.delete(); } else { saveAsDraft(wrapper); } @@ -232,7 +231,7 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi if (mIsNewNote && isTitleContentEmpty(wrapper.note)) { XLog.i(TAG + "remove empty note, id=" + wrapper.note.getId()); - AppDataBase.deleteNoteByLocalId(wrapper.note.getId()); + wrapper.note.delete(); } else { saveAsDraft(wrapper); } @@ -287,7 +286,7 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi private void saveAsDraft(Wrapper wrapper) { Note modifiedNote = wrapper.note; XLog.i(TAG + "saveAsDraft(), local id=" + modifiedNote.getId()); - Note noteFromDb = AppDataBase.getNoteByLocalId(modifiedNote.getId()); + Note noteFromDb = Note.getByLocalId(modifiedNote.getId()); noteFromDb.setContent(modifiedNote.getContent()); noteFromDb.setTitle(modifiedNote.getTitle()); noteFromDb.setNoteBookId(modifiedNote.getNoteBookId()); @@ -376,7 +375,7 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi public Wrapper(Note note) { this.note = note; - List tags = AppDataBase.getTagByNoteLocalId(note.getId()); + List tags = Tag.getByNoteLocalId(note.getId()); this.tags = new ArrayList<>(); for (Tag tag : tags) { this.tags.add(tag.getText()); diff --git a/app/src/main/java/org/houxg/leamonax/ui/edit/SettingFragment.java b/app/src/main/java/org/houxg/leamonax/ui/edit/SettingFragment.java index e7ae1a0..5e0578f 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/edit/SettingFragment.java +++ b/app/src/main/java/org/houxg/leamonax/ui/edit/SettingFragment.java @@ -63,7 +63,7 @@ public class SettingFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_settings, container, false); ButterKnife.bind(this, view); - List tags = AppDataBase.getAllTags(AccountService.getCurrent().getUserId()); + List tags = Tag.getAllTags(AccountService.getCurrent().getUserId()); String[] tagTexts = new String[tags.size()]; int i = 0; for (Tag tag : tags) { @@ -136,7 +136,7 @@ public class SettingFragment extends Fragment { public void setNotebookId(String notebookId) { mNoteBookId = notebookId; if (!TextUtils.isEmpty(mNoteBookId)) { - Notebook notebook = AppDataBase.getNotebookByServerId(mNoteBookId); + Notebook notebook = Notebook.getByServerId(mNoteBookId); if (notebook != null) { mNotebookTv.setText(notebook.getTitle()); } From 259167bf9a4c2b8729c99e1efa75ffd03a90e292 Mon Sep 17 00:00:00 2001 From: houxg Date: Wed, 1 Mar 2017 17:31:43 +0800 Subject: [PATCH 08/13] code refactoring: move getCurrent() method to Account class --- .../houxg/leamonax/adapter/NoteAdapter.java | 4 ++-- .../leamonax/adapter/NotebookAdapter.java | 15 ++++++------ .../houxg/leamonax/adapter/TagAdapter.java | 6 ++--- .../org/houxg/leamonax/model/Account.java | 2 +- .../java/org/houxg/leamonax/model/Note.java | 3 +-- .../java/org/houxg/leamonax/model/User.java | 4 +--- .../houxg/leamonax/network/ApiProvider.java | 5 ++-- .../leamonax/service/AccountService.java | 8 ++----- .../leamonax/service/NoteFileService.java | 5 ++-- .../houxg/leamonax/service/NoteService.java | 20 ++++++++-------- .../leamonax/service/NotebookService.java | 3 +-- .../org/houxg/leamonax/ui/AboutActivity.java | 4 ++-- .../org/houxg/leamonax/ui/LaunchActivity.java | 2 +- .../org/houxg/leamonax/ui/MainActivity.java | 18 +++++---------- .../org/houxg/leamonax/ui/Navigation.java | 14 ++++------- .../org/houxg/leamonax/ui/NoteFragment.java | 13 ----------- .../leamonax/ui/NotePreviewActivity.java | 1 - .../org/houxg/leamonax/ui/SearchActivity.java | 7 ------ .../houxg/leamonax/ui/SettingsActivity.java | 23 ++++++++----------- .../leamonax/ui/edit/EditorFragment.java | 1 - .../leamonax/ui/edit/SettingFragment.java | 5 ++-- 21 files changed, 55 insertions(+), 108 deletions(-) diff --git a/app/src/main/java/org/houxg/leamonax/adapter/NoteAdapter.java b/app/src/main/java/org/houxg/leamonax/adapter/NoteAdapter.java index 529f87d..daebb13 100644 --- a/app/src/main/java/org/houxg/leamonax/adapter/NoteAdapter.java +++ b/app/src/main/java/org/houxg/leamonax/adapter/NoteAdapter.java @@ -18,10 +18,10 @@ import android.widget.TextView; import com.bumptech.glide.Glide; import org.houxg.leamonax.R; +import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.NoteFile; import org.houxg.leamonax.model.Notebook; -import org.houxg.leamonax.service.AccountService; import org.houxg.leamonax.service.NoteFileService; import org.houxg.leamonax.utils.FileUtils; import org.houxg.leamonax.utils.TimeUtils; @@ -106,7 +106,7 @@ public class NoteAdapter extends RecyclerView.Adapter { } private void updateNotebookMap() { - List notebooks = Notebook.getAllNotebooks(AccountService.getCurrent().getUserId()); + List notebooks = Notebook.getAllNotebooks(Account.getCurrent().getUserId()); mNotebookId2TitleMaps = new HashMap<>(); for (Notebook notebook : notebooks) { mNotebookId2TitleMaps.put(notebook.getNotebookId(), notebook.getTitle()); diff --git a/app/src/main/java/org/houxg/leamonax/adapter/NotebookAdapter.java b/app/src/main/java/org/houxg/leamonax/adapter/NotebookAdapter.java index fc66470..48fcf06 100644 --- a/app/src/main/java/org/houxg/leamonax/adapter/NotebookAdapter.java +++ b/app/src/main/java/org/houxg/leamonax/adapter/NotebookAdapter.java @@ -10,9 +10,8 @@ import android.widget.ImageView; import android.widget.TextView; import org.houxg.leamonax.R; -import org.houxg.leamonax.database.AppDataBase; +import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Notebook; -import org.houxg.leamonax.service.AccountService; import org.houxg.leamonax.utils.CollectionUtils; import java.util.ArrayList; @@ -56,14 +55,14 @@ public class NotebookAdapter extends RecyclerView.Adapter stack) { if (stack.isEmpty()) { - mData = Notebook.getRootNotebooks(AccountService.getCurrent().getUserId()); + mData = Notebook.getRootNotebooks(Account.getCurrent().getUserId()); } else { Notebook parent = Notebook.getByServerId(stack.peek()); if (parent.isDeleted()) { stack.pop(); getSafeNotebook(stack); } else { - mData = Notebook.getChildNotebook(mStack.peek(), AccountService.getCurrent().getUserId()); + mData = Notebook.getChildNotebook(mStack.peek(), Account.getCurrent().getUserId()); mData.add(0, parent); } } @@ -148,7 +147,7 @@ public class NotebookAdapter extends RecyclerView.Adapter children = Notebook.getChildNotebook(notebook.getNotebookId(), AccountService.getCurrent().getUserId()); + List children = Notebook.getChildNotebook(notebook.getNotebookId(), Account.getCurrent().getUserId()); int childrenSize = children.size(); mData.addAll(children); notifyItemRangeInserted(1, childrenSize); diff --git a/app/src/main/java/org/houxg/leamonax/adapter/TagAdapter.java b/app/src/main/java/org/houxg/leamonax/adapter/TagAdapter.java index 4d9cfac..c787e31 100644 --- a/app/src/main/java/org/houxg/leamonax/adapter/TagAdapter.java +++ b/app/src/main/java/org/houxg/leamonax/adapter/TagAdapter.java @@ -8,11 +8,9 @@ import android.view.ViewGroup; import android.widget.TextView; import org.houxg.leamonax.R; -import org.houxg.leamonax.database.AppDataBase; +import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Tag; -import org.houxg.leamonax.service.AccountService; -import java.util.ArrayList; import java.util.List; import butterknife.BindView; @@ -28,7 +26,7 @@ public class TagAdapter extends RecyclerView.Adapter { } public void refresh() { - mData = Tag.getAllTags(AccountService.getCurrent().getUserId()); + mData = Tag.getAllTags(Account.getCurrent().getUserId()); notifyDataSetChanged(); } diff --git a/app/src/main/java/org/houxg/leamonax/model/Account.java b/app/src/main/java/org/houxg/leamonax/model/Account.java index a833c57..20037d1 100644 --- a/app/src/main/java/org/houxg/leamonax/model/Account.java +++ b/app/src/main/java/org/houxg/leamonax/model/Account.java @@ -182,7 +182,7 @@ public class Account extends BaseModel { .querySingle(); } - public static Account getAccountWithToken() { + public static Account getCurrent() { return SQLite.select() .from(Account.class) .where(Account_Table.token.notEq("")) diff --git a/app/src/main/java/org/houxg/leamonax/model/Note.java b/app/src/main/java/org/houxg/leamonax/model/Note.java index dac22b8..10d6a31 100644 --- a/app/src/main/java/org/houxg/leamonax/model/Note.java +++ b/app/src/main/java/org/houxg/leamonax/model/Note.java @@ -14,7 +14,6 @@ import com.raizlabs.android.dbflow.sql.language.property.IProperty; import com.raizlabs.android.dbflow.structure.BaseModel; import org.houxg.leamonax.database.AppDataBase; -import org.houxg.leamonax.service.AccountService; import org.houxg.leamonax.utils.CollectionUtils; import org.houxg.leamonax.utils.TimeUtils; @@ -268,7 +267,7 @@ public class Note extends BaseModel implements Serializable { keyword = String.format(Locale.US, "%%%s%%", keyword); return SQLite.select() .from(Note.class) - .where(Note_Table.userId.eq(AccountService.getCurrent().getUserId())) + .where(Note_Table.userId.eq(Account.getCurrent().getUserId())) .and(Note_Table.title.like(keyword)) .and(Note_Table.isTrash.eq(false)) .and(Note_Table.isDeleted.eq(false)) diff --git a/app/src/main/java/org/houxg/leamonax/model/User.java b/app/src/main/java/org/houxg/leamonax/model/User.java index f6fff61..510e5b6 100644 --- a/app/src/main/java/org/houxg/leamonax/model/User.java +++ b/app/src/main/java/org/houxg/leamonax/model/User.java @@ -4,8 +4,6 @@ package org.houxg.leamonax.model; import com.google.gson.annotations.SerializedName; import com.raizlabs.android.dbflow.annotation.Column; -import org.houxg.leamonax.service.AccountService; - public class User extends BaseResponse { @SerializedName("UserId") @@ -37,7 +35,7 @@ public class User extends BaseResponse { } public String getAvatar() { - Account current = AccountService.getCurrent(); + Account current = Account.getCurrent(); String host = current.getHost(); if(host.equals("https://leanote.com")){ return avatar;} diff --git a/app/src/main/java/org/houxg/leamonax/network/ApiProvider.java b/app/src/main/java/org/houxg/leamonax/network/ApiProvider.java index d660675..45fcec2 100644 --- a/app/src/main/java/org/houxg/leamonax/network/ApiProvider.java +++ b/app/src/main/java/org/houxg/leamonax/network/ApiProvider.java @@ -8,7 +8,6 @@ import org.houxg.leamonax.network.api.AuthApi; import org.houxg.leamonax.network.api.NoteApi; import org.houxg.leamonax.network.api.NotebookApi; import org.houxg.leamonax.network.api.UserApi; -import org.houxg.leamonax.service.AccountService; import java.io.IOException; @@ -29,7 +28,7 @@ public class ApiProvider { } public static ApiProvider getInstance() { - Account account = AccountService.getCurrent(); + Account account = Account.getCurrent(); if (account != null && SingletonHolder.INSTANCE.mApiRetrofit == null) { SingletonHolder.INSTANCE.init(account.getHost()); } @@ -50,7 +49,7 @@ public class ApiProvider { HttpUrl newUrl = url; if (shouldAddTokenToQuery(path)) { newUrl = url.newBuilder() - .addQueryParameter("token", AccountService.getCurrent().getAccessToken()) + .addQueryParameter("token", Account.getCurrent().getAccessToken()) .build(); } Request newRequest = request.newBuilder() diff --git a/app/src/main/java/org/houxg/leamonax/service/AccountService.java b/app/src/main/java/org/houxg/leamonax/service/AccountService.java index 6f8e858..5ad40b0 100644 --- a/app/src/main/java/org/houxg/leamonax/service/AccountService.java +++ b/app/src/main/java/org/houxg/leamonax/service/AccountService.java @@ -54,7 +54,7 @@ public class AccountService { } public static void logout() { - Account account = getCurrent(); + Account account = Account.getCurrent(); account.setAccessToken(""); account.update(); } @@ -67,15 +67,11 @@ public class AccountService { return RetrofitUtils.create(ApiProvider.getInstance().getUserApi().updateUsername(userName)); } - public static Account getCurrent() { - return Account.getAccountWithToken(); - } - public static List getAccountList() { return Account.getAccountListWithToken(); } public static boolean isSignedIn() { - return getCurrent() != null; + return Account.getCurrent() != null; } } diff --git a/app/src/main/java/org/houxg/leamonax/service/NoteFileService.java b/app/src/main/java/org/houxg/leamonax/service/NoteFileService.java index 208a2dd..590ad38 100644 --- a/app/src/main/java/org/houxg/leamonax/service/NoteFileService.java +++ b/app/src/main/java/org/houxg/leamonax/service/NoteFileService.java @@ -7,6 +7,7 @@ import com.elvishew.xlog.XLog; import org.bson.types.ObjectId; import org.houxg.leamonax.Leamonax; +import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.NoteFile; import java.io.File; @@ -50,7 +51,7 @@ public class NoteFileService { } public static Uri getServerImageUri(String serverId) { - Uri uri = Uri.parse(AccountService.getCurrent().getHost()); + Uri uri = Uri.parse(Account.getCurrent().getHost()); return uri.buildUpon().appendEncodedPath("api/file/getImage").appendQueryParameter("fileId", serverId).build(); } @@ -86,7 +87,7 @@ public class NoteFileService { filePath = noteFile.getLocalPath(); XLog.i(TAG + "use local image, path=" + filePath); } else { - String url = NoteFileService.getUrl(AccountService.getCurrent().getHost(), noteFile.getServerId(), AccountService.getCurrent().getAccessToken()); + String url = NoteFileService.getUrl(Account.getCurrent().getHost(), noteFile.getServerId(), Account.getCurrent().getAccessToken()); XLog.i(TAG + "use server image, url=" + url); try { filePath = NoteFileService.getImageFromServer(Uri.parse(url), Leamonax.getContext().getCacheDir()); diff --git a/app/src/main/java/org/houxg/leamonax/service/NoteService.java b/app/src/main/java/org/houxg/leamonax/service/NoteService.java index adc011d..97bba5a 100644 --- a/app/src/main/java/org/houxg/leamonax/service/NoteService.java +++ b/app/src/main/java/org/houxg/leamonax/service/NoteService.java @@ -7,7 +7,6 @@ import android.text.TextUtils; import android.webkit.MimeTypeMap; import com.elvishew.xlog.XLog; -import com.raizlabs.android.dbflow.sql.language.SQLite; import org.bson.types.ObjectId; import org.houxg.leamonax.R; @@ -15,7 +14,6 @@ import org.houxg.leamonax.ReadableException; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.NoteFile; -import org.houxg.leamonax.model.Note_Table; import org.houxg.leamonax.model.Notebook; import org.houxg.leamonax.model.RelationshipOfNoteTag; import org.houxg.leamonax.model.Tag; @@ -49,7 +47,7 @@ public class NoteService { public static void fetchFromServer() { //sync notebook - int notebookUsn = AccountService.getCurrent().getNotebookUsn(); + int notebookUsn = Account.getCurrent().getNotebookUsn(); List notebooks; do { notebooks = RetrofitUtils.excuteWithException(ApiProvider.getInstance().getNotebookApi().getSyncNotebooks(notebookUsn, MAX_ENTRY)); @@ -65,7 +63,7 @@ public class NoteService { remoteNotebook.update(); } notebookUsn = remoteNotebook.getUsn(); - Account account = AccountService.getCurrent(); + Account account = Account.getCurrent(); account.setNotebookUsn(notebookUsn); account.save(); } @@ -73,7 +71,7 @@ public class NoteService { //sync note - int noteUsn = AccountService.getCurrent().getNoteUsn(); + int noteUsn = Account.getCurrent().getNoteUsn(); List notes; do { notes = RetrofitUtils.excuteWithException(ApiProvider.getInstance().getNoteApi().getSyncNotes(noteUsn, MAX_ENTRY)); @@ -110,7 +108,7 @@ public class NoteService { remoteNote.update(); handleFile(localId, remoteNote.getNoteFiles()); updateTagsToLocal(localId, remoteNote.getTagData()); - Account account = AccountService.getCurrent(); + Account account = Account.getCurrent(); account.setNoteUsn(noteUsn); account.save(); } @@ -149,7 +147,7 @@ public class NoteService { private static String convertToLocalImageLinkForRichText(long noteLocalId, String noteContent) { return StringUtils.replace(noteContent, "]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>", - String.format(Locale.US, "\\ssrc\\s*=\\s*\"%s/api/file/getImage\\?fileId=.*?\"", AccountService.getCurrent().getHost()), + String.format(Locale.US, "\\ssrc\\s*=\\s*\"%s/api/file/getImage\\?fileId=.*?\"", Account.getCurrent().getHost()), new StringUtils.Replacer() { @Override public String replaceWith(String original, Object... extraData) { @@ -174,8 +172,8 @@ public class NoteService { private static String convertToLocalImageLinkForMD(long noteLocalId, String noteContent) { return StringUtils.replace(noteContent, - String.format(Locale.US, "!\\[.*?\\]\\(%s/api/file/getImage\\?fileId=.*?\\)", AccountService.getCurrent().getHost()), - String.format(Locale.US, "\\(%s/api/file/getImage\\?fileId=.*?\\)", AccountService.getCurrent().getHost()), + String.format(Locale.US, "!\\[.*?\\]\\(%s/api/file/getImage\\?fileId=.*?\\)", Account.getCurrent().getHost()), + String.format(Locale.US, "\\(%s/api/file/getImage\\?fileId=.*?\\)", Account.getCurrent().getHost()), new StringUtils.Replacer() { @Override public String replaceWith(String original, Object... extraData) { @@ -404,7 +402,7 @@ public class NoteService { * if new usn equals to (current usn + 1), then just simply update usn without syncing. */ private static void updateNoteUsnIfNeed(int newUsn) { - Account account = AccountService.getCurrent(); + Account account = Account.getCurrent(); if (newUsn - account.getNoteUsn() == 1) { account.setNoteUsn(newUsn); account.update(); @@ -412,7 +410,7 @@ public class NoteService { } public static void updateTagsToLocal(long noteLocalId, List tags) { - String currentUid = AccountService.getCurrent().getUserId(); + String currentUid = Account.getCurrent().getUserId(); if (tags == null) { tags = new ArrayList<>(); } diff --git a/app/src/main/java/org/houxg/leamonax/service/NotebookService.java b/app/src/main/java/org/houxg/leamonax/service/NotebookService.java index 3b22ca4..3c3accb 100644 --- a/app/src/main/java/org/houxg/leamonax/service/NotebookService.java +++ b/app/src/main/java/org/houxg/leamonax/service/NotebookService.java @@ -3,7 +3,6 @@ package org.houxg.leamonax.service; import com.elvishew.xlog.XLog; -import org.houxg.leamonax.database.AppDataBase; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Notebook; import org.houxg.leamonax.network.ApiProvider; @@ -19,7 +18,7 @@ public class NotebookService { throw new IllegalStateException("Network error"); } if (notebook.isOk()) { - Account account = AccountService.getCurrent(); + Account account = Account.getCurrent(); if (notebook.getUsn() - account.getNotebookUsn() == 1) { XLog.d(TAG + "update usn=" + notebook.getUsn()); account.setNotebookUsn(notebook.getUsn()); diff --git a/app/src/main/java/org/houxg/leamonax/ui/AboutActivity.java b/app/src/main/java/org/houxg/leamonax/ui/AboutActivity.java index bb00a90..1890fe9 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/AboutActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/AboutActivity.java @@ -13,8 +13,8 @@ import org.bson.types.ObjectId; import org.houxg.leamonax.BuildConfig; import org.houxg.leamonax.R; import org.houxg.leamonax.database.AppDataBase; +import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Note; -import org.houxg.leamonax.service.AccountService; import org.houxg.leamonax.utils.OpenUtils; import org.houxg.leamonax.utils.TestUtils; @@ -54,7 +54,7 @@ public class AboutActivity extends BaseActivity { new Observable.OnSubscribe() { @Override public void call(Subscriber subscriber) { - String userId = AccountService.getCurrent().getUserId(); + String userId = Account.getCurrent().getUserId(); SecureRandom random = new SecureRandom(); String notebookId = new ObjectId().toString(); List notes = new ArrayList<>(8000); diff --git a/app/src/main/java/org/houxg/leamonax/ui/LaunchActivity.java b/app/src/main/java/org/houxg/leamonax/ui/LaunchActivity.java index 1c0ba59..0934ab4 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/LaunchActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/LaunchActivity.java @@ -15,7 +15,7 @@ public class LaunchActivity extends Activity { super.onCreate(savedInstanceState); Intent intent; if (AccountService.isSignedIn()) { - Account account = AccountService.getCurrent(); + Account account = Account.getCurrent(); ApiProvider.getInstance().init(account.getHost()); intent = MainActivity.getOpenIntent(this, false); } else { diff --git a/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java b/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java index bd89a49..4bee64c 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java @@ -8,7 +8,6 @@ import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.Toolbar; -import android.text.TextUtils; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -20,20 +19,15 @@ import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import org.houxg.leamonax.R; -import org.houxg.leamonax.adapter.NotebookAdapter; import org.houxg.leamonax.background.NoteSyncService; -import org.houxg.leamonax.database.AppDataBase; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.Notebook; import org.houxg.leamonax.model.SyncEvent; -import org.houxg.leamonax.model.Tag; -import org.houxg.leamonax.service.AccountService; import org.houxg.leamonax.ui.edit.NoteEditActivity; import org.houxg.leamonax.utils.NetworkUtils; import org.houxg.leamonax.utils.ToastUtils; -import java.util.Collections; import java.util.List; import java.util.Locale; @@ -69,7 +63,7 @@ public class MainActivity extends BaseActivity implements Navigation.Callback { initToolBar((Toolbar) findViewById(R.id.toolbar)); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_white); - CrashReport.setUserId(AccountService.getCurrent().getUserId()); + CrashReport.setUserId(Account.getCurrent().getUserId()); mNavigation = new Navigation(this); mNavigation.init(this, mNavigationView); @@ -154,7 +148,7 @@ public class MainActivity extends BaseActivity implements Navigation.Callback { @OnClick(R.id.fab) void clickedFab() { - Account account = AccountService.getCurrent(); + Account account = Account.getCurrent(); Note newNote = new Note(); newNote.setUserId(account.getUserId()); Notebook notebook; @@ -162,7 +156,7 @@ public class MainActivity extends BaseActivity implements Navigation.Callback { if (currentMode == Navigation.Mode.NOTEBOOK) { notebook = Notebook.getByLocalId(currentMode.notebookId); } else { - notebook = Notebook.getRecentNoteBook(AccountService.getCurrent().getUserId()); + notebook = Notebook.getRecentNoteBook(Account.getCurrent().getUserId()); } if (notebook != null) { newNote.setNoteBookId(notebook.getNotebookId()); @@ -192,13 +186,13 @@ public class MainActivity extends BaseActivity implements Navigation.Callback { List notes; switch (mode) { case RECENT_NOTES: - notes = Note.getAllNotes(AccountService.getCurrent().getUserId()); + notes = Note.getAllNotes(Account.getCurrent().getUserId()); break; case NOTEBOOK: - notes = Note.getNotesFromNotebook(AccountService.getCurrent().getUserId(), mode.notebookId); + notes = Note.getNotesFromNotebook(Account.getCurrent().getUserId(), mode.notebookId); break; case TAG: - notes = Note.getByTagText(mode.tagText, AccountService.getCurrent().getUserId()); + notes = Note.getByTagText(mode.tagText, Account.getCurrent().getUserId()); break; default: return false; diff --git a/app/src/main/java/org/houxg/leamonax/ui/Navigation.java b/app/src/main/java/org/houxg/leamonax/ui/Navigation.java index 109ce91..4832794 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/Navigation.java +++ b/app/src/main/java/org/houxg/leamonax/ui/Navigation.java @@ -6,16 +6,12 @@ import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; -import android.content.res.ColorStateList; import android.graphics.drawable.Drawable; -import android.graphics.drawable.RippleDrawable; import android.support.v4.view.GravityCompat; -import android.support.v4.view.ViewGroupCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.TextUtils; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -25,8 +21,6 @@ import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; -import com.elvishew.xlog.XLog; -import com.tencent.bugly.Bugly; import com.tencent.bugly.crashreport.CrashReport; import org.houxg.leamonax.R; @@ -108,7 +102,7 @@ public class Navigation { } private void fetchInfo() { - AccountService.getInfo(AccountService.getCurrent().getUserId()) + AccountService.getInfo(Account.getCurrent().getUserId()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer() { @@ -124,8 +118,8 @@ public class Navigation { @Override public void onNext(User user) { - AccountService.saveToAccount(user, AccountService.getCurrent().getHost()); - refreshUserInfo(AccountService.getCurrent()); + AccountService.saveToAccount(user, Account.getCurrent().getHost()); + refreshUserInfo(Account.getCurrent()); mAccountAdapter.notifyDataSetChanged(); } }); @@ -371,7 +365,7 @@ public class Navigation { } public void refresh() { - refreshUserInfo(AccountService.getCurrent()); + refreshUserInfo(Account.getCurrent()); mAccountAdapter.load(AccountService.getAccountList()); mTagAdapter.refresh(); mNotebookAdapter.refresh(); diff --git a/app/src/main/java/org/houxg/leamonax/ui/NoteFragment.java b/app/src/main/java/org/houxg/leamonax/ui/NoteFragment.java index 520420c..04dbefe 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/NoteFragment.java +++ b/app/src/main/java/org/houxg/leamonax/ui/NoteFragment.java @@ -6,10 +6,7 @@ import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; -import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.RecyclerView; -import android.text.TextUtils; -import android.view.ActionMode; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -17,22 +14,13 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import com.elvishew.xlog.XLog; - import org.greenrobot.eventbus.EventBus; -import org.greenrobot.eventbus.Subscribe; -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.ActionModeHandler; import org.houxg.leamonax.utils.CollectionUtils; -import org.houxg.leamonax.utils.NetworkUtils; import org.houxg.leamonax.utils.SharedPreferenceUtils; import org.houxg.leamonax.utils.ToastUtils; import org.houxg.leamonax.widget.NoteList; @@ -40,7 +28,6 @@ import org.houxg.leamonax.widget.NoteList; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Locale; import butterknife.BindView; import butterknife.ButterKnife; diff --git a/app/src/main/java/org/houxg/leamonax/ui/NotePreviewActivity.java b/app/src/main/java/org/houxg/leamonax/ui/NotePreviewActivity.java index 733bc40..86be51f 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/NotePreviewActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/NotePreviewActivity.java @@ -15,7 +15,6 @@ import com.tencent.bugly.crashreport.CrashReport; import org.houxg.leamonax.BuildConfig; import org.houxg.leamonax.R; -import org.houxg.leamonax.database.AppDataBase; import org.houxg.leamonax.model.Note; import org.houxg.leamonax.service.NoteService; import org.houxg.leamonax.ui.edit.EditorFragment; diff --git a/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java b/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java index 0e43155..220bd6d 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java @@ -3,7 +3,6 @@ package org.houxg.leamonax.ui; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; -import android.support.v4.view.GravityCompat; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; @@ -11,10 +10,7 @@ import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; import android.text.TextUtils; import android.transition.Slide; -import android.transition.Transition; -import android.transition.TransitionInflater; import android.view.Gravity; -import android.view.View; import android.view.Window; import android.widget.ImageView; @@ -24,14 +20,11 @@ import org.houxg.leamonax.model.Note; import org.houxg.leamonax.service.NoteService; import org.houxg.leamonax.utils.ActionModeHandler; import org.houxg.leamonax.utils.CollectionUtils; -import org.houxg.leamonax.utils.DisplayUtils; import org.houxg.leamonax.utils.ToastUtils; -import org.houxg.leamonax.widget.DividerDecoration; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Locale; import butterknife.BindView; import butterknife.ButterKnife; diff --git a/app/src/main/java/org/houxg/leamonax/ui/SettingsActivity.java b/app/src/main/java/org/houxg/leamonax/ui/SettingsActivity.java index ecdd87a..c724525 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/SettingsActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/SettingsActivity.java @@ -14,20 +14,15 @@ import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; -import com.raizlabs.android.dbflow.sql.language.SQLite; import org.houxg.leamonax.BuildConfig; import org.houxg.leamonax.R; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.BaseResponse; import org.houxg.leamonax.model.Note; -import org.houxg.leamonax.model.RelationshipOfNoteTag; -import org.houxg.leamonax.model.Note_Table; import org.houxg.leamonax.model.Notebook; -import org.houxg.leamonax.model.Notebook_Table; -import org.houxg.leamonax.model.RelationshipOfNoteTag_Table; +import org.houxg.leamonax.model.RelationshipOfNoteTag; import org.houxg.leamonax.model.Tag; -import org.houxg.leamonax.model.Tag_Table; import org.houxg.leamonax.service.AccountService; import org.houxg.leamonax.utils.ToastUtils; @@ -76,11 +71,11 @@ public class SettingsActivity extends BaseActivity { void selectEditor() { new AlertDialog.Builder(this) .setTitle(R.string.choose_editor) - .setSingleChoiceItems(mEditors, AccountService.getCurrent().getDefaultEditor(), new DialogInterface.OnClickListener() { + .setSingleChoiceItems(mEditors, Account.getCurrent().getDefaultEditor(), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); - Account account = AccountService.getCurrent(); + Account account = Account.getCurrent(); account.setDefaultEditor(which); account.update(); mEditorTv.setText(mEditors[which]); @@ -128,7 +123,7 @@ public class SettingsActivity extends BaseActivity { void clickedUserName() { View view = LayoutInflater.from(this).inflate(R.layout.dialog_sigle_edittext, null); final EditText mUserNameEt = (EditText) view.findViewById(R.id.edit); - mUserNameEt.setText(AccountService.getCurrent().getUserName()); + mUserNameEt.setText(Account.getCurrent().getUserName()); new AlertDialog.Builder(this) .setTitle(R.string.change_user_name) .setView(view) @@ -191,7 +186,7 @@ public class SettingsActivity extends BaseActivity { @Override public void call(Subscriber subscriber) { if (!subscriber.isUnsubscribed()) { - Account currentUser = AccountService.getCurrent(); + Account currentUser = Account.getCurrent(); String userId = currentUser.getUserId(); Note.deleteAll(userId); Notebook.deleteAll(userId); @@ -228,18 +223,18 @@ public class SettingsActivity extends BaseActivity { @Override public void onError(Throwable e) { ToastUtils.showNetworkError(SettingsActivity.this); - mUserNameTv.setText(AccountService.getCurrent().getUserName()); + mUserNameTv.setText(Account.getCurrent().getUserName()); } @Override public void onNext(BaseResponse baseResponse) { if (baseResponse.isOk()) { - Account account = AccountService.getCurrent(); + Account account = Account.getCurrent(); account.setUserName(username); account.update(); ToastUtils.show(SettingsActivity.this, R.string.change_user_name_successful); } else { - mUserNameTv.setText(AccountService.getCurrent().getUserName()); + mUserNameTv.setText(Account.getCurrent().getUserName()); ToastUtils.show(SettingsActivity.this, R.string.change_user_name_failed); } } @@ -273,7 +268,7 @@ public class SettingsActivity extends BaseActivity { } private void refresh() { - Account current = AccountService.getCurrent(); + Account current = Account.getCurrent(); mEditorTv.setText(mEditors[current.getDefaultEditor()]); mUserNameTv.setText(current.getUserName()); mEmailTv.setText(current.getEmail()); diff --git a/app/src/main/java/org/houxg/leamonax/ui/edit/EditorFragment.java b/app/src/main/java/org/houxg/leamonax/ui/edit/EditorFragment.java index 9c1f631..8761d03 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/edit/EditorFragment.java +++ b/app/src/main/java/org/houxg/leamonax/ui/edit/EditorFragment.java @@ -41,7 +41,6 @@ import org.houxg.leamonax.utils.DialogUtils; import org.houxg.leamonax.utils.OpenUtils; import org.houxg.leamonax.widget.ToggleImageButton; -import java.io.File; import java.util.List; import java.util.Map; diff --git a/app/src/main/java/org/houxg/leamonax/ui/edit/SettingFragment.java b/app/src/main/java/org/houxg/leamonax/ui/edit/SettingFragment.java index 5e0578f..15ff290 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/edit/SettingFragment.java +++ b/app/src/main/java/org/houxg/leamonax/ui/edit/SettingFragment.java @@ -18,10 +18,9 @@ import android.widget.Switch; import android.widget.TextView; import org.houxg.leamonax.R; -import org.houxg.leamonax.database.AppDataBase; +import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Notebook; import org.houxg.leamonax.model.Tag; -import org.houxg.leamonax.service.AccountService; import org.houxg.leamonax.utils.CollectionUtils; import org.houxg.leamonax.utils.DialogUtils; import org.houxg.leamonax.utils.DisplayUtils; @@ -63,7 +62,7 @@ public class SettingFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_settings, container, false); ButterKnife.bind(this, view); - List tags = Tag.getAllTags(AccountService.getCurrent().getUserId()); + List tags = Tag.getAllTags(Account.getCurrent().getUserId()); String[] tagTexts = new String[tags.size()]; int i = 0; for (Tag tag : tags) { From 9affd8498146d6efd331b8a9629f95ee3d4e1de6 Mon Sep 17 00:00:00 2001 From: houxg Date: Wed, 1 Mar 2017 17:34:41 +0800 Subject: [PATCH 09/13] code refactoring: extract pull-to-refresh --- .../leamonax/component/PullToRefresh.java | 33 +++++++++++++++++++ .../org/houxg/leamonax/ui/MainActivity.java | 33 ++++++++----------- 2 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/org/houxg/leamonax/component/PullToRefresh.java diff --git a/app/src/main/java/org/houxg/leamonax/component/PullToRefresh.java b/app/src/main/java/org/houxg/leamonax/component/PullToRefresh.java new file mode 100644 index 0000000..8a3776e --- /dev/null +++ b/app/src/main/java/org/houxg/leamonax/component/PullToRefresh.java @@ -0,0 +1,33 @@ +package org.houxg.leamonax.component; + + +import android.support.v4.widget.SwipeRefreshLayout; + +import com.elvishew.xlog.XLog; + +public class PullToRefresh { + + private SwipeRefreshLayout refreshLayout; + private SwipeRefreshLayout.OnRefreshListener listener; + + public PullToRefresh(SwipeRefreshLayout refreshLayout, SwipeRefreshLayout.OnRefreshListener listener) { + this.listener = listener; + this.refreshLayout = refreshLayout; + refreshLayout.setOnRefreshListener(listener); + } + + public void forceRefresh() { + refreshLayout.postDelayed(new Runnable() { + @Override + public void run() { + XLog.i("fetching notes"); + refreshLayout.setRefreshing(true); + listener.onRefresh(); + } + }, 200); + } + + public void stopRefreshing() { + refreshLayout.setRefreshing(false); + } +} diff --git a/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java b/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java index 4bee64c..861b051 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java @@ -20,6 +20,7 @@ import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import org.houxg.leamonax.R; import org.houxg.leamonax.background.NoteSyncService; +import org.houxg.leamonax.component.PullToRefresh; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.Notebook; @@ -44,9 +45,8 @@ public class MainActivity extends BaseActivity implements Navigation.Callback { @BindView(R.id.drawer) View mNavigationView; - @BindView(R.id.swiperefresh) - SwipeRefreshLayout mSwipeRefresh; + PullToRefresh mPullToRefresh; Navigation mNavigation; public static Intent getOpenIntent(Context context, boolean shouldReload) { @@ -77,31 +77,26 @@ public class MainActivity extends BaseActivity implements Navigation.Callback { mNoteFragment = (NoteFragment) getSupportFragmentManager().findFragmentByTag(TAG_NOTE_FRAGMENT); } - mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { - @Override - public void onRefresh() { - syncNotes(); - } - }); + mPullToRefresh = new PullToRefresh( + (SwipeRefreshLayout) findViewById(R.id.swiperefresh), + new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + syncNotes(); + } + }); EventBus.getDefault().register(this); if (shouldReload) { - mSwipeRefresh.postDelayed(new Runnable() { - @Override - public void run() { - XLog.i("fetching notes"); - mSwipeRefresh.setRefreshing(true); - syncNotes(); - } - }, 200); + mPullToRefresh.forceRefresh(); } } private void syncNotes() { if (!NetworkUtils.isNetworkAvailable()) { ToastUtils.showNetworkUnavailable(MainActivity.this); - mSwipeRefresh.setRefreshing(false); + mPullToRefresh.stopRefreshing(); return; } NoteSyncService.startServiceForNote(MainActivity.this); @@ -176,7 +171,7 @@ public class MainActivity extends BaseActivity implements Navigation.Callback { account.updateLastUseTime(); account.update(); mNavigation.refresh(); - mSwipeRefresh.setRefreshing(true); + mPullToRefresh.stopRefreshing(); syncNotes(); return true; } @@ -216,7 +211,7 @@ public class MainActivity extends BaseActivity implements Navigation.Callback { @Subscribe(threadMode = ThreadMode.MAIN) public void onEvent(SyncEvent event) { XLog.i("RequestNotes rcv: isSucceed=" + event.isSucceed()); - mSwipeRefresh.setRefreshing(false); + mPullToRefresh.stopRefreshing(); if (event.isSucceed()) { mNavigation.refresh(); } else { From 66be19733deec0ca26def2d5cbe997d9b13479f5 Mon Sep 17 00:00:00 2001 From: houxg Date: Tue, 7 Mar 2017 10:51:53 +0800 Subject: [PATCH 10/13] fix wrong example --- app/src/main/res/values-zh/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index eadbe88..176986a 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -105,5 +105,5 @@ 生成随机笔记 蚂蚁笔记 糟糕,笔记冲突 - 登录接口地址将会是:\n%s/api/login + 登录接口地址将会是:\n%s/api/auth/login \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 335216e..d9b3a21 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -107,6 +107,6 @@ Github Generate random note Oops! Conflicts occurs - For example, login api will be:\n%s/api/login + For example, login api will be:\n%s/api/auth/login From 8457376f58af9570f85cd31f246f5f1fb9954c64 Mon Sep 17 00:00:00 2001 From: houxg Date: Tue, 7 Mar 2017 11:10:04 +0800 Subject: [PATCH 11/13] fix can't login/register issue for some of self-hosts --- .../java/org/houxg/leamonax/network/ApiProvider.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/houxg/leamonax/network/ApiProvider.java b/app/src/main/java/org/houxg/leamonax/network/ApiProvider.java index 45fcec2..9325a3f 100644 --- a/app/src/main/java/org/houxg/leamonax/network/ApiProvider.java +++ b/app/src/main/java/org/houxg/leamonax/network/ApiProvider.java @@ -1,13 +1,19 @@ package org.houxg.leamonax.network; +import android.text.TextUtils; + import com.elvishew.xlog.XLog; +import com.tencent.bugly.crashreport.CrashReport; import org.houxg.leamonax.BuildConfig; +import org.houxg.leamonax.Leamonax; +import org.houxg.leamonax.R; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.network.api.AuthApi; import org.houxg.leamonax.network.api.NoteApi; import org.houxg.leamonax.network.api.NotebookApi; import org.houxg.leamonax.network.api.UserApi; +import org.houxg.leamonax.utils.ToastUtils; import java.io.IOException; @@ -77,8 +83,8 @@ public class ApiProvider { } private static boolean shouldAddTokenToQuery(String path) { - return !path.startsWith("/api/auth/login") - && !path.startsWith("/api/auth/register"); + return !path.endsWith("/api/auth/login") + && !path.endsWith("/api/auth/register"); } public AuthApi getAuthApi() { From 0343a345dcd670213a27c283b014845244444e05 Mon Sep 17 00:00:00 2001 From: houxg Date: Thu, 9 Mar 2017 11:10:37 +0800 Subject: [PATCH 12/13] code refactoring: replace database query code to xxDataStore --- .../houxg/leamonax/adapter/NoteAdapter.java | 3 +- .../leamonax/adapter/NotebookAdapter.java | 17 +-- .../leamonax/database/AccountDataStore.java | 43 +++++++ .../leamonax/database/NoteDataStore.java | 113 ++++++++++++++++++ .../leamonax/database/NoteFileDataStore.java | 43 +++++++ .../leamonax/database/NoteTagDataStore.java | 16 +++ .../leamonax/database/NotebookDataStore.java | 81 +++++++++++++ .../org/houxg/leamonax/model/Account.java | 30 +---- .../java/org/houxg/leamonax/model/Note.java | 110 ----------------- .../org/houxg/leamonax/model/NoteFile.java | 30 ----- .../org/houxg/leamonax/model/Notebook.java | 68 ----------- .../leamonax/model/RelationshipOfNoteTag.java | 7 -- .../leamonax/service/AccountService.java | 7 +- .../leamonax/service/NoteFileService.java | 9 +- .../houxg/leamonax/service/NoteService.java | 25 ++-- .../leamonax/service/NotebookService.java | 3 +- .../org/houxg/leamonax/ui/MainActivity.java | 12 +- .../org/houxg/leamonax/ui/Navigation.java | 3 +- .../leamonax/ui/NotePreviewActivity.java | 9 +- .../org/houxg/leamonax/ui/SearchActivity.java | 3 +- .../houxg/leamonax/ui/SettingsActivity.java | 9 +- .../leamonax/ui/edit/NoteEditActivity.java | 9 +- .../leamonax/ui/edit/SettingFragment.java | 3 +- 23 files changed, 363 insertions(+), 290 deletions(-) create mode 100644 app/src/main/java/org/houxg/leamonax/database/AccountDataStore.java create mode 100644 app/src/main/java/org/houxg/leamonax/database/NoteDataStore.java create mode 100644 app/src/main/java/org/houxg/leamonax/database/NoteFileDataStore.java create mode 100644 app/src/main/java/org/houxg/leamonax/database/NoteTagDataStore.java create mode 100644 app/src/main/java/org/houxg/leamonax/database/NotebookDataStore.java diff --git a/app/src/main/java/org/houxg/leamonax/adapter/NoteAdapter.java b/app/src/main/java/org/houxg/leamonax/adapter/NoteAdapter.java index daebb13..b6f87bf 100644 --- a/app/src/main/java/org/houxg/leamonax/adapter/NoteAdapter.java +++ b/app/src/main/java/org/houxg/leamonax/adapter/NoteAdapter.java @@ -18,6 +18,7 @@ import android.widget.TextView; import com.bumptech.glide.Glide; import org.houxg.leamonax.R; +import org.houxg.leamonax.database.NotebookDataStore; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.NoteFile; @@ -106,7 +107,7 @@ public class NoteAdapter extends RecyclerView.Adapter { } private void updateNotebookMap() { - List notebooks = Notebook.getAllNotebooks(Account.getCurrent().getUserId()); + List notebooks = NotebookDataStore.getAllNotebooks(Account.getCurrent().getUserId()); mNotebookId2TitleMaps = new HashMap<>(); for (Notebook notebook : notebooks) { mNotebookId2TitleMaps.put(notebook.getNotebookId(), notebook.getTitle()); diff --git a/app/src/main/java/org/houxg/leamonax/adapter/NotebookAdapter.java b/app/src/main/java/org/houxg/leamonax/adapter/NotebookAdapter.java index 48fcf06..d4357d0 100644 --- a/app/src/main/java/org/houxg/leamonax/adapter/NotebookAdapter.java +++ b/app/src/main/java/org/houxg/leamonax/adapter/NotebookAdapter.java @@ -10,6 +10,7 @@ import android.widget.ImageView; import android.widget.TextView; import org.houxg.leamonax.R; +import org.houxg.leamonax.database.NotebookDataStore; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Notebook; import org.houxg.leamonax.utils.CollectionUtils; @@ -55,14 +56,14 @@ public class NotebookAdapter extends RecyclerView.Adapter stack) { if (stack.isEmpty()) { - mData = Notebook.getRootNotebooks(Account.getCurrent().getUserId()); + mData = NotebookDataStore.getRootNotebooks(Account.getCurrent().getUserId()); } else { - Notebook parent = Notebook.getByServerId(stack.peek()); + Notebook parent = NotebookDataStore.getByServerId(stack.peek()); if (parent.isDeleted()) { stack.pop(); getSafeNotebook(stack); } else { - mData = Notebook.getChildNotebook(mStack.peek(), Account.getCurrent().getUserId()); + mData = NotebookDataStore.getChildNotebook(mStack.peek(), Account.getCurrent().getUserId()); mData.add(0, parent); } } @@ -147,7 +148,7 @@ public class NotebookAdapter extends RecyclerView.Adapter children = Notebook.getChildNotebook(notebook.getNotebookId(), Account.getCurrent().getUserId()); + List children = NotebookDataStore.getChildNotebook(notebook.getNotebookId(), Account.getCurrent().getUserId()); int childrenSize = children.size(); mData.addAll(children); notifyItemRangeInserted(1, childrenSize); diff --git a/app/src/main/java/org/houxg/leamonax/database/AccountDataStore.java b/app/src/main/java/org/houxg/leamonax/database/AccountDataStore.java new file mode 100644 index 0000000..2f6600c --- /dev/null +++ b/app/src/main/java/org/houxg/leamonax/database/AccountDataStore.java @@ -0,0 +1,43 @@ +package org.houxg.leamonax.database; + + +import com.raizlabs.android.dbflow.sql.language.SQLite; +import com.raizlabs.android.dbflow.sql.language.Select; + +import org.houxg.leamonax.model.Account; +import org.houxg.leamonax.model.Account_Table; + +import java.util.List; + +public class AccountDataStore { + public static Account getAccount(String email, String host) { + return SQLite.select() + .from(Account.class) + .where(Account_Table.email.eq(email)) + .and(Account_Table.host.eq(host)) + .querySingle(); + } + + public static Account getCurrent() { + return SQLite.select() + .from(Account.class) + .where(Account_Table.token.notEq("")) + .orderBy(Account_Table.lastUseTime, false) + .querySingle(); + } + + public static List getAccountListWithToken() { + return SQLite.select() + .from(Account.class) + .where(Account_Table.token.notEq("")) + .orderBy(Account_Table.lastUseTime, false) + .queryList(); + } + + public static Account getAccountById(long id) { + return new Select() + .from(Account.class) + .where(Account_Table.id.eq(id)) + .querySingle(); + } +} diff --git a/app/src/main/java/org/houxg/leamonax/database/NoteDataStore.java b/app/src/main/java/org/houxg/leamonax/database/NoteDataStore.java new file mode 100644 index 0000000..ffac351 --- /dev/null +++ b/app/src/main/java/org/houxg/leamonax/database/NoteDataStore.java @@ -0,0 +1,113 @@ +package org.houxg.leamonax.database; + + +import com.raizlabs.android.dbflow.sql.language.Join; +import com.raizlabs.android.dbflow.sql.language.NameAlias; +import com.raizlabs.android.dbflow.sql.language.SQLite; +import com.raizlabs.android.dbflow.sql.language.property.IProperty; + +import org.houxg.leamonax.model.Account; +import org.houxg.leamonax.model.Note; +import org.houxg.leamonax.model.Note_Table; +import org.houxg.leamonax.model.Notebook; +import org.houxg.leamonax.model.RelationshipOfNoteTag; +import org.houxg.leamonax.model.RelationshipOfNoteTag_Table; +import org.houxg.leamonax.model.Tag; +import org.houxg.leamonax.model.Tag_Table; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +public class NoteDataStore { + public static List searchByTitle(String keyword) { + keyword = String.format(Locale.US, "%%%s%%", keyword); + return SQLite.select() + .from(Note.class) + .where(Note_Table.userId.eq(Account.getCurrent().getUserId())) + .and(Note_Table.title.like(keyword)) + .and(Note_Table.isTrash.eq(false)) + .and(Note_Table.isDeleted.eq(false)) + .queryList(); + } + + public static Note getByServerId(String serverId) { + return SQLite.select() + .from(Note.class) + .where(Note_Table.noteId.eq(serverId)) + .querySingle(); + } + + public static Note getByLocalId(long localId) { + return SQLite.select() + .from(Note.class) + .where(Note_Table.id.eq(localId)) + .querySingle(); + } + + public static List getAllNotes(String userId) { + return SQLite.select() + .from(Note.class) + .where(Note_Table.userId.eq(userId)) + .and(Note_Table.isTrash.eq(false)) + .and(Note_Table.isDeleted.eq(false)) + .and(Note_Table.isTrash.eq(false)) + .queryList(); + } + + public static List getAllDirtyNotes(String userId) { + return SQLite.select() + .from(Note.class) + .where(Note_Table.userId.eq(userId)) + .and(Note_Table.isTrash.eq(false)) + .and(Note_Table.isDeleted.eq(false)) + .and(Note_Table.isTrash.eq(false)) + .and(Note_Table.isDirty.eq(true)) + .queryList(); + } + + public static List getNotesFromNotebook(String userId, long localNotebookId) { + Notebook notebook = NotebookDataStore.getByLocalId(localNotebookId); + if (notebook == null) { + return new ArrayList<>(); + } + return SQLite.select() + .from(Note.class) + .where(Note_Table.notebookId.eq(notebook.getNotebookId())) + .and(Note_Table.userId.eq(userId)) + .and(Note_Table.isTrash.eq(false)) + .and(Note_Table.isDeleted.eq(false)) + .and(Note_Table.isTrash.eq(false)) + .queryList(); + } + + public static List getByTagText(String tagText, String userId) { + Tag tag = Tag.getByText(tagText, userId); + if (tag == null) { + return new ArrayList<>(); + } + return getNotesByTagId(tag.getId()); + } + + private static List getNotesByTagId(long tagId) { + IProperty[] properties = Note_Table.ALL_COLUMN_PROPERTIES; + NameAlias nameAlias = NameAlias.builder("N").build(); + for (int i = 0; i < properties.length; i++) { + properties[i] = properties[i].withTable(nameAlias); + } + return SQLite.select(properties) + .from(Note.class).as("N") + .join(RelationshipOfNoteTag.class, Join.JoinType.INNER).as("R") + .on(Tag_Table.id.withTable(NameAlias.builder("N").build()) + .eq(RelationshipOfNoteTag_Table.noteLocalId.withTable(NameAlias.builder("R").build()))) + .where(RelationshipOfNoteTag_Table.tagLocalId.withTable(NameAlias.builder("R").build()).eq(tagId)) + .queryList(); + } + + public static void deleteAll(String userId) { + SQLite.delete() + .from(Note.class) + .where(Note_Table.userId.eq(userId)) + .execute(); + } +} diff --git a/app/src/main/java/org/houxg/leamonax/database/NoteFileDataStore.java b/app/src/main/java/org/houxg/leamonax/database/NoteFileDataStore.java new file mode 100644 index 0000000..0e35fdc --- /dev/null +++ b/app/src/main/java/org/houxg/leamonax/database/NoteFileDataStore.java @@ -0,0 +1,43 @@ +package org.houxg.leamonax.database; + + +import com.raizlabs.android.dbflow.sql.language.SQLite; + +import org.houxg.leamonax.model.NoteFile; +import org.houxg.leamonax.model.NoteFile_Table; + +import java.util.Collection; +import java.util.List; + +public class NoteFileDataStore { + + public static List getAllRelated(long noteLocalId) { + return SQLite.select() + .from(NoteFile.class) + .where(NoteFile_Table.noteLocalId.eq(noteLocalId)) + .queryList(); + } + + public static NoteFile getByLocalId(String localId) { + return SQLite.select() + .from(NoteFile.class) + .where(NoteFile_Table.localId.eq(localId)) + .querySingle(); + } + + public static NoteFile getByServerId(String serverId) { + return SQLite.select() + .from(NoteFile.class) + .where(NoteFile_Table.serverId.eq(serverId)) + .querySingle(); + } + + public static void deleteExcept(long noteLocalId, Collection excepts) { + SQLite.delete() + .from(NoteFile.class) + .where(NoteFile_Table.noteLocalId.eq(noteLocalId)) + .and(NoteFile_Table.localId.notIn(excepts)) + .async() + .execute(); + } +} diff --git a/app/src/main/java/org/houxg/leamonax/database/NoteTagDataStore.java b/app/src/main/java/org/houxg/leamonax/database/NoteTagDataStore.java new file mode 100644 index 0000000..02994f2 --- /dev/null +++ b/app/src/main/java/org/houxg/leamonax/database/NoteTagDataStore.java @@ -0,0 +1,16 @@ +package org.houxg.leamonax.database; + + +import com.raizlabs.android.dbflow.sql.language.SQLite; + +import org.houxg.leamonax.model.RelationshipOfNoteTag; +import org.houxg.leamonax.model.RelationshipOfNoteTag_Table; + +public class NoteTagDataStore { + public static void deleteAll(String userId) { + SQLite.delete() + .from(RelationshipOfNoteTag.class) + .where(RelationshipOfNoteTag_Table.userId.eq(userId)) + .execute(); + } +} diff --git a/app/src/main/java/org/houxg/leamonax/database/NotebookDataStore.java b/app/src/main/java/org/houxg/leamonax/database/NotebookDataStore.java new file mode 100644 index 0000000..7d0a029 --- /dev/null +++ b/app/src/main/java/org/houxg/leamonax/database/NotebookDataStore.java @@ -0,0 +1,81 @@ +package org.houxg.leamonax.database; + + +import com.raizlabs.android.dbflow.sql.language.SQLite; + +import org.houxg.leamonax.model.Note; +import org.houxg.leamonax.model.Note_Table; +import org.houxg.leamonax.model.Notebook; +import org.houxg.leamonax.model.Notebook_Table; + +import java.util.List; + +public class NotebookDataStore { + public static List getAllNotebooks(String userId) { + return SQLite.select() + .from(Notebook.class) + .where(Notebook_Table.userId.eq(userId)) + .and(Notebook_Table.isDeletedOnServer.eq(false)) + .queryList(); + } + + public static Notebook getByLocalId(long localId) { + return SQLite.select() + .from(Notebook.class) + .where(Notebook_Table.id.eq(localId)) + .querySingle(); + } + + public static Notebook getByServerId(String serverId) { + return SQLite.select() + .from(Notebook.class) + .where(Notebook_Table.notebookId.eq(serverId)) + .querySingle(); + } + + public static Notebook getRecentNoteBook(String userId) { + Note recentNotes = SQLite.select() + .from(Note.class) + .where(Note_Table.userId.eq(userId)) + .and(Note_Table.notebookId.notEq("")) + .orderBy(Note_Table.updatedTime, false) + .querySingle(); + if (recentNotes != null) { + Notebook notebook = getByServerId(recentNotes.getNoteBookId()); + if (notebook != null && !notebook.isDeleted()) { + return notebook; + } + } + + return SQLite.select() + .from(Notebook.class) + .where(Notebook_Table.userId.eq(userId)) + .and(Notebook_Table.isDeletedOnServer.eq(false)) + .querySingle(); + } + + public static List getRootNotebooks(String userId) { + return SQLite.select() + .from(Notebook.class) + .where(Notebook_Table.userId.eq(userId)) + .and(Notebook_Table.parentNotebookId.eq("")) + .and(Notebook_Table.isDeletedOnServer.eq(false)) + .queryList(); + } + + public static List getChildNotebook(String notebookId, String userId) { + return SQLite.select() + .from(Notebook.class) + .where(Notebook_Table.userId.eq(userId)) + .and(Notebook_Table.parentNotebookId.eq(notebookId)) + .and(Notebook_Table.isDeletedOnServer.eq(false)) + .queryList(); + } + + public static void deleteAll(String userId) { + SQLite.delete() + .from(Notebook.class) + .where(Notebook_Table.userId.eq(userId)) + .execute(); + } +} diff --git a/app/src/main/java/org/houxg/leamonax/model/Account.java b/app/src/main/java/org/houxg/leamonax/model/Account.java index 20037d1..302f98c 100644 --- a/app/src/main/java/org/houxg/leamonax/model/Account.java +++ b/app/src/main/java/org/houxg/leamonax/model/Account.java @@ -8,6 +8,7 @@ import com.raizlabs.android.dbflow.sql.language.SQLite; import com.raizlabs.android.dbflow.sql.language.Select; import com.raizlabs.android.dbflow.structure.BaseModel; +import org.houxg.leamonax.database.AccountDataStore; import org.houxg.leamonax.database.AppDataBase; import java.util.List; @@ -174,35 +175,8 @@ public class Account extends BaseModel { return lastUseTime; } - public static Account getAccount(String email, String host) { - return SQLite.select() - .from(Account.class) - .where(Account_Table.email.eq(email)) - .and(Account_Table.host.eq(host)) - .querySingle(); - } - public static Account getCurrent() { - return SQLite.select() - .from(Account.class) - .where(Account_Table.token.notEq("")) - .orderBy(Account_Table.lastUseTime, false) - .querySingle(); - } - - public static List getAccountListWithToken() { - return SQLite.select() - .from(Account.class) - .where(Account_Table.token.notEq("")) - .orderBy(Account_Table.lastUseTime, false) - .queryList(); - } - - public static Account getAccountById(long id) { - return new Select() - .from(Account.class) - .where(Account_Table.id.eq(id)) - .querySingle(); + return AccountDataStore.getCurrent(); } @Override diff --git a/app/src/main/java/org/houxg/leamonax/model/Note.java b/app/src/main/java/org/houxg/leamonax/model/Note.java index 10d6a31..596658d 100644 --- a/app/src/main/java/org/houxg/leamonax/model/Note.java +++ b/app/src/main/java/org/houxg/leamonax/model/Note.java @@ -23,9 +23,6 @@ import java.util.Comparator; import java.util.List; import java.util.Locale; -/** - * Created by binnchx on 10/18/15. - */ @Table(name = "Note", database = AppDataBase.class) public class Note extends BaseModel implements Serializable { @@ -236,113 +233,6 @@ public class Note extends BaseModel implements Serializable { return noteFiles; } - //TODO:delete this - public String getUpdatedTime() { - return updatedTimeData; - } - - //TODO:delete this - public String getCreatedTime() { - return updatedTimeData; - } - - //TODO:delete this - public String getPublicTime() { - return publicTimeData; - } - - //TODO:delete this - public void setUpdatedTime(String v) { - } - - //TODO:delete this - public void setCreatedTime(String v) { - } - - //TODO:delete this - public void setPublicTime(String publicTime) { - } - - public static List searchByTitle(String keyword) { - keyword = String.format(Locale.US, "%%%s%%", keyword); - return SQLite.select() - .from(Note.class) - .where(Note_Table.userId.eq(Account.getCurrent().getUserId())) - .and(Note_Table.title.like(keyword)) - .and(Note_Table.isTrash.eq(false)) - .and(Note_Table.isDeleted.eq(false)) - .queryList(); - } - - public static Note getByServerId(String serverId) { - return SQLite.select() - .from(Note.class) - .where(Note_Table.noteId.eq(serverId)) - .querySingle(); - } - - public static Note getByLocalId(long localId) { - return SQLite.select() - .from(Note.class) - .where(Note_Table.id.eq(localId)) - .querySingle(); - } - - public static List getAllNotes(String userId) { - return SQLite.select() - .from(Note.class) - .where(Note_Table.userId.eq(userId)) - .and(Note_Table.isTrash.eq(false)) - .and(Note_Table.isDeleted.eq(false)) - .and(Note_Table.isTrash.eq(false)) - .queryList(); - } - - public static List getNotesFromNotebook(String userId, long localNotebookId) { - Notebook notebook = Notebook.getByLocalId(localNotebookId); - if (notebook == null) { - return new ArrayList<>(); - } - return SQLite.select() - .from(Note.class) - .where(Note_Table.notebookId.eq(notebook.getNotebookId())) - .and(Note_Table.userId.eq(userId)) - .and(Note_Table.isTrash.eq(false)) - .and(Note_Table.isDeleted.eq(false)) - .and(Note_Table.isTrash.eq(false)) - .queryList(); - } - - public static List getByTagText(String tagText, String userId) { - Tag tag = Tag.getByText(tagText, userId); - if (tag == null) { - return new ArrayList<>(); - } - return getNotesByTagId(tag.getId()); - } - - private static List getNotesByTagId(long tagId) { - IProperty[] properties = Note_Table.ALL_COLUMN_PROPERTIES; - NameAlias nameAlias = NameAlias.builder("N").build(); - for (int i = 0; i < properties.length; i++) { - properties[i] = properties[i].withTable(nameAlias); - } - return SQLite.select(properties) - .from(Note.class).as("N") - .join(RelationshipOfNoteTag.class, Join.JoinType.INNER).as("R") - .on(Tag_Table.id.withTable(NameAlias.builder("N").build()) - .eq(RelationshipOfNoteTag_Table.noteLocalId.withTable(NameAlias.builder("R").build()))) - .where(RelationshipOfNoteTag_Table.tagLocalId.withTable(NameAlias.builder("R").build()).eq(tagId)) - .queryList(); - } - - public static void deleteAll(String userId) { - SQLite.delete() - .from(Note.class) - .where(Note_Table.userId.eq(userId)) - .execute(); - } - @Override public String toString() { return "Note{" + diff --git a/app/src/main/java/org/houxg/leamonax/model/NoteFile.java b/app/src/main/java/org/houxg/leamonax/model/NoteFile.java index 3688028..c8bca69 100644 --- a/app/src/main/java/org/houxg/leamonax/model/NoteFile.java +++ b/app/src/main/java/org/houxg/leamonax/model/NoteFile.java @@ -107,34 +107,4 @@ public class NoteFile extends BaseModel { public void setIsAttach(boolean mIsAttach) { this.mIsAttach = mIsAttach; } - - public static List getAllRelated(long noteLocalId) { - return SQLite.select() - .from(NoteFile.class) - .where(NoteFile_Table.noteLocalId.eq(noteLocalId)) - .queryList(); - } - - public static NoteFile getByLocalId(String localId) { - return SQLite.select() - .from(NoteFile.class) - .where(NoteFile_Table.localId.eq(localId)) - .querySingle(); - } - - public static NoteFile getByServerId(String serverId) { - return SQLite.select() - .from(NoteFile.class) - .where(NoteFile_Table.serverId.eq(serverId)) - .querySingle(); - } - - public static void deleteExcept(long noteLocalId, Collection excepts) { - SQLite.delete() - .from(NoteFile.class) - .where(NoteFile_Table.noteLocalId.eq(noteLocalId)) - .and(NoteFile_Table.localId.notIn(excepts)) - .async() - .execute(); - } } diff --git a/app/src/main/java/org/houxg/leamonax/model/Notebook.java b/app/src/main/java/org/houxg/leamonax/model/Notebook.java index b2fabb4..db0fde7 100644 --- a/app/src/main/java/org/houxg/leamonax/model/Notebook.java +++ b/app/src/main/java/org/houxg/leamonax/model/Notebook.java @@ -180,72 +180,4 @@ public class Notebook extends BaseModel { public String getMsg() { return msg; } - - public static List getAllNotebooks(String userId) { - return SQLite.select() - .from(Notebook.class) - .where(Notebook_Table.userId.eq(userId)) - .and(Notebook_Table.isDeletedOnServer.eq(false)) - .queryList(); - } - - public static Notebook getByLocalId(long localId) { - return SQLite.select() - .from(Notebook.class) - .where(Notebook_Table.id.eq(localId)) - .querySingle(); - } - - public static Notebook getByServerId(String serverId) { - return SQLite.select() - .from(Notebook.class) - .where(Notebook_Table.notebookId.eq(serverId)) - .querySingle(); - } - - public static Notebook getRecentNoteBook(String userId) { - Note recentNotes = SQLite.select() - .from(Note.class) - .where(Note_Table.userId.eq(userId)) - .and(Note_Table.notebookId.notEq("")) - .orderBy(Note_Table.updatedTime, false) - .querySingle(); - if (recentNotes != null) { - Notebook notebook = getByServerId(recentNotes.getNoteBookId()); - if (notebook != null && !notebook.isDeleted()) { - return notebook; - } - } - - return SQLite.select() - .from(Notebook.class) - .where(Notebook_Table.userId.eq(userId)) - .and(Notebook_Table.isDeletedOnServer.eq(false)) - .querySingle(); - } - - public static List getRootNotebooks(String userId) { - return SQLite.select() - .from(Notebook.class) - .where(Notebook_Table.userId.eq(userId)) - .and(Notebook_Table.parentNotebookId.eq("")) - .and(Notebook_Table.isDeletedOnServer.eq(false)) - .queryList(); - } - - public static List getChildNotebook(String notebookId, String userId) { - return SQLite.select() - .from(Notebook.class) - .where(Notebook_Table.userId.eq(userId)) - .and(Notebook_Table.parentNotebookId.eq(notebookId)) - .and(Notebook_Table.isDeletedOnServer.eq(false)) - .queryList(); - } - - public static void deleteAll(String userId) { - SQLite.delete() - .from(Notebook.class) - .where(Notebook_Table.userId.eq(userId)) - .execute(); - } } diff --git a/app/src/main/java/org/houxg/leamonax/model/RelationshipOfNoteTag.java b/app/src/main/java/org/houxg/leamonax/model/RelationshipOfNoteTag.java index ea632f4..6fb7517 100644 --- a/app/src/main/java/org/houxg/leamonax/model/RelationshipOfNoteTag.java +++ b/app/src/main/java/org/houxg/leamonax/model/RelationshipOfNoteTag.java @@ -44,11 +44,4 @@ public class RelationshipOfNoteTag extends BaseModel { public long getTagLocalId() { return tagLocalId; } - - public static void deleteAll(String userId) { - SQLite.delete() - .from(RelationshipOfNoteTag.class) - .where(RelationshipOfNoteTag_Table.userId.eq(userId)) - .execute(); - } } diff --git a/app/src/main/java/org/houxg/leamonax/service/AccountService.java b/app/src/main/java/org/houxg/leamonax/service/AccountService.java index 5ad40b0..2b8742c 100644 --- a/app/src/main/java/org/houxg/leamonax/service/AccountService.java +++ b/app/src/main/java/org/houxg/leamonax/service/AccountService.java @@ -1,5 +1,6 @@ package org.houxg.leamonax.service; +import org.houxg.leamonax.database.AccountDataStore; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Authentication; import org.houxg.leamonax.model.BaseResponse; @@ -26,7 +27,7 @@ public class AccountService { } public static long saveToAccount(Authentication authentication, String host) { - Account localAccount = Account.getAccount(authentication.getEmail(), host); + Account localAccount = AccountDataStore.getAccount(authentication.getEmail(), host); if (localAccount == null) { localAccount = new Account(); } @@ -40,7 +41,7 @@ public class AccountService { } public static void saveToAccount(User user, String host) { - Account localAccount = Account.getAccount(user.getEmail(), host); + Account localAccount = AccountDataStore.getAccount(user.getEmail(), host); if (localAccount == null) { localAccount = new Account(); } @@ -68,7 +69,7 @@ public class AccountService { } public static List getAccountList() { - return Account.getAccountListWithToken(); + return AccountDataStore.getAccountListWithToken(); } public static boolean isSignedIn() { diff --git a/app/src/main/java/org/houxg/leamonax/service/NoteFileService.java b/app/src/main/java/org/houxg/leamonax/service/NoteFileService.java index 590ad38..3916cad 100644 --- a/app/src/main/java/org/houxg/leamonax/service/NoteFileService.java +++ b/app/src/main/java/org/houxg/leamonax/service/NoteFileService.java @@ -7,6 +7,7 @@ import com.elvishew.xlog.XLog; import org.bson.types.ObjectId; import org.houxg.leamonax.Leamonax; +import org.houxg.leamonax.database.NoteFileDataStore; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.NoteFile; @@ -32,7 +33,7 @@ public class NoteFileService { private static final String IMAGE_PATH_WITH_SLASH = "/getImage"; public static String convertFromLocalIdToServerId(String localId) { - NoteFile noteFile = NoteFile.getByLocalId(localId); + NoteFile noteFile = NoteFileDataStore.getByLocalId(localId); return noteFile == null ? null : noteFile.getServerId(); } @@ -61,7 +62,7 @@ public class NoteFileService { public static String getImagePath(Uri uri) { String localId = uri.getQueryParameter("id"); - NoteFile noteFile = NoteFile.getByLocalId(localId); + NoteFile noteFile = NoteFileDataStore.getByLocalId(localId); if (noteFile == null) { return null; } @@ -74,11 +75,11 @@ public class NoteFileService { } public static List getRelatedNoteFiles(long noteLocalId) { - return NoteFile.getAllRelated(noteLocalId); + return NoteFileDataStore.getAllRelated(noteLocalId); } public static InputStream getImage(String localId) { - NoteFile noteFile = NoteFile.getByLocalId(localId); + NoteFile noteFile = NoteFileDataStore.getByLocalId(localId); if (noteFile == null) { return null; } diff --git a/app/src/main/java/org/houxg/leamonax/service/NoteService.java b/app/src/main/java/org/houxg/leamonax/service/NoteService.java index 97bba5a..ec81e54 100644 --- a/app/src/main/java/org/houxg/leamonax/service/NoteService.java +++ b/app/src/main/java/org/houxg/leamonax/service/NoteService.java @@ -11,6 +11,9 @@ import com.elvishew.xlog.XLog; import org.bson.types.ObjectId; import org.houxg.leamonax.R; import org.houxg.leamonax.ReadableException; +import org.houxg.leamonax.database.NoteDataStore; +import org.houxg.leamonax.database.NoteFileDataStore; +import org.houxg.leamonax.database.NotebookDataStore; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.NoteFile; @@ -52,7 +55,7 @@ public class NoteService { do { notebooks = RetrofitUtils.excuteWithException(ApiProvider.getInstance().getNotebookApi().getSyncNotebooks(notebookUsn, MAX_ENTRY)); for (Notebook remoteNotebook : notebooks) { - Notebook localNotebook = Notebook.getByServerId(remoteNotebook.getNotebookId()); + Notebook localNotebook = NotebookDataStore.getByServerId(remoteNotebook.getNotebookId()); if (localNotebook == null) { XLog.i(TAG + "notebook insert, usn=" + remoteNotebook.getUsn() + ", id=" + remoteNotebook.getNotebookId()); remoteNotebook.insert(); @@ -77,7 +80,7 @@ public class NoteService { notes = RetrofitUtils.excuteWithException(ApiProvider.getInstance().getNoteApi().getSyncNotes(noteUsn, MAX_ENTRY)); for (Note noteMeta : notes) { Note remoteNote = RetrofitUtils.excuteWithException(ApiProvider.getInstance().getNoteApi().getNoteAndContent(noteMeta.getNoteId())); - Note localNote = Note.getByServerId(noteMeta.getNoteId()); + Note localNote = NoteDataStore.getByServerId(noteMeta.getNoteId()); noteUsn = remoteNote.getUsn(); long localId; if (localNote == null) { @@ -124,9 +127,9 @@ public class NoteService { for (NoteFile remote : remoteFiles) { NoteFile local; if (TextUtils.isEmpty(remote.getLocalId())) { - local = NoteFile.getByServerId(remote.getServerId()); + local = NoteFileDataStore.getByServerId(remote.getServerId()); } else { - local = NoteFile.getByLocalId(remote.getLocalId()); + local = NoteFileDataStore.getByLocalId(remote.getLocalId()); } if (local != null) { XLog.i(TAG + "has local file, id=" + remote.getServerId()); @@ -141,7 +144,7 @@ public class NoteService { local.save(); excepts.add(local.getLocalId()); } - NoteFile.deleteExcept(noteLocalId, excepts); + NoteFileDataStore.deleteExcept(noteLocalId, excepts); } private static String convertToLocalImageLinkForRichText(long noteLocalId, String noteContent) { @@ -154,7 +157,7 @@ public class NoteService { XLog.i(TAG + "in=" + original); Uri linkUri = Uri.parse(original.substring(6, original.length() - 1)); String serverId = linkUri.getQueryParameter("fileId"); - NoteFile noteFile = NoteFile.getByServerId(serverId); + NoteFile noteFile = NoteFileDataStore.getByServerId(serverId); if (noteFile == null) { noteFile = new NoteFile(); noteFile.setNoteId((Long) extraData[0]); @@ -179,7 +182,7 @@ public class NoteService { public String replaceWith(String original, Object... extraData) { Uri linkUri = Uri.parse(original.substring(1, original.length() - 1)); String serverId = linkUri.getQueryParameter("fileId"); - NoteFile noteFile = NoteFile.getByServerId(serverId); + NoteFile noteFile = NoteFileDataStore.getByServerId(serverId); if (noteFile == null) { noteFile = new NoteFile(); noteFile.setNoteId((Long) extraData[0]); @@ -194,7 +197,7 @@ public class NoteService { } public static void saveNote(final long noteLocalId) { - Note modifiedNote = Note.getByLocalId(noteLocalId); + Note modifiedNote = NoteDataStore.getByLocalId(noteLocalId); Map requestBodyMap = generateCommonBodyMap(modifiedNote); List fileBodies = handleFileBodies(modifiedNote, requestBodyMap); @@ -274,7 +277,7 @@ public class NoteService { if (serverNote == null) { return false; } - Note localNote = Note.getByServerId(serverId); + Note localNote = NoteDataStore.getByServerId(serverId); long localId; if (localNote == null) { localId = serverNote.insert(); @@ -332,8 +335,8 @@ public class NoteService { } else { imageLocalIds = getImagesFromContentForRichText(note.getContent()); } - NoteFile.deleteExcept(note.getId(), imageLocalIds); - List files = NoteFile.getAllRelated(note.getId()); + NoteFileDataStore.deleteExcept(note.getId(), imageLocalIds); + List files = NoteFileDataStore.getAllRelated(note.getId()); if (CollectionUtils.isNotEmpty(files)) { int size = files.size(); for (int index = 0; index < size; index++) { diff --git a/app/src/main/java/org/houxg/leamonax/service/NotebookService.java b/app/src/main/java/org/houxg/leamonax/service/NotebookService.java index 3c3accb..74fc9b2 100644 --- a/app/src/main/java/org/houxg/leamonax/service/NotebookService.java +++ b/app/src/main/java/org/houxg/leamonax/service/NotebookService.java @@ -3,6 +3,7 @@ package org.houxg.leamonax.service; import com.elvishew.xlog.XLog; +import org.houxg.leamonax.database.NotebookDataStore; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Notebook; import org.houxg.leamonax.network.ApiProvider; @@ -32,7 +33,7 @@ public class NotebookService { } public static String getTitle(long notebookLocalId) { - Notebook notebook = Notebook.getByLocalId(notebookLocalId); + Notebook notebook = NotebookDataStore.getByLocalId(notebookLocalId); return notebook != null ? notebook.getTitle() : ""; } } diff --git a/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java b/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java index 861b051..5b6681c 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/MainActivity.java @@ -21,6 +21,8 @@ import org.greenrobot.eventbus.ThreadMode; import org.houxg.leamonax.R; import org.houxg.leamonax.background.NoteSyncService; import org.houxg.leamonax.component.PullToRefresh; +import org.houxg.leamonax.database.NoteDataStore; +import org.houxg.leamonax.database.NotebookDataStore; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.Notebook; @@ -149,9 +151,9 @@ public class MainActivity extends BaseActivity implements Navigation.Callback { Notebook notebook; Navigation.Mode currentMode = mNavigation.getCurrentMode(); if (currentMode == Navigation.Mode.NOTEBOOK) { - notebook = Notebook.getByLocalId(currentMode.notebookId); + notebook = NotebookDataStore.getByLocalId(currentMode.notebookId); } else { - notebook = Notebook.getRecentNoteBook(Account.getCurrent().getUserId()); + notebook = NotebookDataStore.getRecentNoteBook(Account.getCurrent().getUserId()); } if (notebook != null) { newNote.setNoteBookId(notebook.getNotebookId()); @@ -181,13 +183,13 @@ public class MainActivity extends BaseActivity implements Navigation.Callback { List notes; switch (mode) { case RECENT_NOTES: - notes = Note.getAllNotes(Account.getCurrent().getUserId()); + notes = NoteDataStore.getAllNotes(Account.getCurrent().getUserId()); break; case NOTEBOOK: - notes = Note.getNotesFromNotebook(Account.getCurrent().getUserId(), mode.notebookId); + notes = NoteDataStore.getNotesFromNotebook(Account.getCurrent().getUserId(), mode.notebookId); break; case TAG: - notes = Note.getByTagText(mode.tagText, Account.getCurrent().getUserId()); + notes = NoteDataStore.getByTagText(mode.tagText, Account.getCurrent().getUserId()); break; default: return false; diff --git a/app/src/main/java/org/houxg/leamonax/ui/Navigation.java b/app/src/main/java/org/houxg/leamonax/ui/Navigation.java index 4832794..483b2d8 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/Navigation.java +++ b/app/src/main/java/org/houxg/leamonax/ui/Navigation.java @@ -27,6 +27,7 @@ import org.houxg.leamonax.R; import org.houxg.leamonax.adapter.AccountAdapter; import org.houxg.leamonax.adapter.NotebookAdapter; import org.houxg.leamonax.adapter.TagAdapter; +import org.houxg.leamonax.database.AccountDataStore; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Notebook; import org.houxg.leamonax.model.Tag; @@ -410,7 +411,7 @@ public class Navigation { public boolean onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQ_ADD_ACCOUNT) { if (resultCode == RESULT_OK) { - Account account = Account.getAccountById(SignInActivity.getAccountIdFromData(data)); + Account account = AccountDataStore.getAccountById(SignInActivity.getAccountIdFromData(data)); if (account != null) { changeAccount(account); } diff --git a/app/src/main/java/org/houxg/leamonax/ui/NotePreviewActivity.java b/app/src/main/java/org/houxg/leamonax/ui/NotePreviewActivity.java index 86be51f..09713d6 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/NotePreviewActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/NotePreviewActivity.java @@ -15,6 +15,7 @@ import com.tencent.bugly.crashreport.CrashReport; import org.houxg.leamonax.BuildConfig; import org.houxg.leamonax.R; +import org.houxg.leamonax.database.NoteDataStore; import org.houxg.leamonax.model.Note; import org.houxg.leamonax.service.NoteService; import org.houxg.leamonax.ui.edit.EditorFragment; @@ -55,7 +56,7 @@ public class NotePreviewActivity extends BaseActivity implements EditorFragment. ButterKnife.bind(this); initToolBar((Toolbar) findViewById(R.id.toolbar), true); long noteLocalId = getIntent().getLongExtra(EXT_NOTE_LOCAL_ID, -1); - mNote = Note.getByLocalId(noteLocalId); + mNote = NoteDataStore.getByLocalId(noteLocalId); if (mNote == null) { ToastUtils.show(this, R.string.note_not_found); CrashReport.postCatchedException(new IllegalStateException("Note not found while preview, localId=" + noteLocalId)); @@ -112,7 +113,7 @@ public class NotePreviewActivity extends BaseActivity implements EditorFragment. if (requestCode == REQ_EDIT) { switch (resultCode) { case RESULT_OK: - mNote = Note.getByLocalId(mNote.getId()); + mNote = NoteDataStore.getByLocalId(mNote.getId()); if (mNote == null) { finish(); } else { @@ -162,7 +163,7 @@ public class NotePreviewActivity extends BaseActivity implements EditorFragment. @Override public void onNext(Long aLong) { - mNote = Note.getByLocalId(mNote.getId()); + mNote = NoteDataStore.getByLocalId(mNote.getId()); mNote.setIsDirty(false); mNote.save(); refresh(); @@ -204,7 +205,7 @@ public class NotePreviewActivity extends BaseActivity implements EditorFragment. @Override public void call(Boolean isSucceed) { if (isSucceed) { - mNote = Note.getByServerId(mNote.getNoteId()); + mNote = NoteDataStore.getByServerId(mNote.getNoteId()); refresh(); } } diff --git a/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java b/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java index 220bd6d..b7da0a4 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java @@ -16,6 +16,7 @@ import android.widget.ImageView; import org.houxg.leamonax.R; import org.houxg.leamonax.adapter.NoteAdapter; +import org.houxg.leamonax.database.NoteDataStore; import org.houxg.leamonax.model.Note; import org.houxg.leamonax.service.NoteService; import org.houxg.leamonax.utils.ActionModeHandler; @@ -129,7 +130,7 @@ public class SearchActivity extends BaseActivity implements NoteAdapter.NoteAdap if (TextUtils.isEmpty(keyword)) { mNotes = new ArrayList<>(); } else { - mNotes = Note.searchByTitle(keyword); + mNotes = NoteDataStore.searchByTitle(keyword); Collections.sort(mNotes, new Note.UpdateTimeComparetor()); } mAdapter.setHighlight(keyword); diff --git a/app/src/main/java/org/houxg/leamonax/ui/SettingsActivity.java b/app/src/main/java/org/houxg/leamonax/ui/SettingsActivity.java index c724525..bd6a45b 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/SettingsActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/SettingsActivity.java @@ -17,6 +17,9 @@ import com.bumptech.glide.Glide; import org.houxg.leamonax.BuildConfig; import org.houxg.leamonax.R; +import org.houxg.leamonax.database.NoteDataStore; +import org.houxg.leamonax.database.NoteTagDataStore; +import org.houxg.leamonax.database.NotebookDataStore; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.BaseResponse; import org.houxg.leamonax.model.Note; @@ -188,10 +191,10 @@ public class SettingsActivity extends BaseActivity { if (!subscriber.isUnsubscribed()) { Account currentUser = Account.getCurrent(); String userId = currentUser.getUserId(); - Note.deleteAll(userId); - Notebook.deleteAll(userId); + NoteDataStore.deleteAll(userId); + NotebookDataStore.deleteAll(userId); Tag.deleteAll(userId); - RelationshipOfNoteTag.deleteAll(userId); + NoteTagDataStore.deleteAll(userId); currentUser.setNoteUsn(0); currentUser.setNotebookUsn(0); currentUser.update(); diff --git a/app/src/main/java/org/houxg/leamonax/ui/edit/NoteEditActivity.java b/app/src/main/java/org/houxg/leamonax/ui/edit/NoteEditActivity.java index 2ca002d..6e9a95d 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/edit/NoteEditActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/edit/NoteEditActivity.java @@ -17,6 +17,7 @@ import com.elvishew.xlog.XLog; import org.houxg.leamonax.Leamonax; import org.houxg.leamonax.R; import org.houxg.leamonax.ReadableException; +import org.houxg.leamonax.database.NoteDataStore; import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.Tag; import org.houxg.leamonax.service.NoteFileService; @@ -81,8 +82,8 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi return; } mIsNewNote = getIntent().getBooleanExtra(EXT_IS_NEW_NOTE, false); - mOriginal = new Wrapper(Note.getByLocalId(noteLocalId)); - mModified = new Wrapper(Note.getByLocalId(noteLocalId)); + mOriginal = new Wrapper(NoteDataStore.getByLocalId(noteLocalId)); + mModified = new Wrapper(NoteDataStore.getByLocalId(noteLocalId)); setResult(RESULT_CANCELED); } @@ -155,7 +156,7 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi @Override public void onNext(Long noteLocalId) { - Note localNote = Note.getByLocalId(noteLocalId); + Note localNote = NoteDataStore.getByLocalId(noteLocalId); localNote.setIsDirty(false); localNote.save(); } @@ -286,7 +287,7 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi private void saveAsDraft(Wrapper wrapper) { Note modifiedNote = wrapper.note; XLog.i(TAG + "saveAsDraft(), local id=" + modifiedNote.getId()); - Note noteFromDb = Note.getByLocalId(modifiedNote.getId()); + Note noteFromDb = NoteDataStore.getByLocalId(modifiedNote.getId()); noteFromDb.setContent(modifiedNote.getContent()); noteFromDb.setTitle(modifiedNote.getTitle()); noteFromDb.setNoteBookId(modifiedNote.getNoteBookId()); diff --git a/app/src/main/java/org/houxg/leamonax/ui/edit/SettingFragment.java b/app/src/main/java/org/houxg/leamonax/ui/edit/SettingFragment.java index 15ff290..eb7063c 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/edit/SettingFragment.java +++ b/app/src/main/java/org/houxg/leamonax/ui/edit/SettingFragment.java @@ -18,6 +18,7 @@ import android.widget.Switch; import android.widget.TextView; import org.houxg.leamonax.R; +import org.houxg.leamonax.database.NotebookDataStore; import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Notebook; import org.houxg.leamonax.model.Tag; @@ -135,7 +136,7 @@ public class SettingFragment extends Fragment { public void setNotebookId(String notebookId) { mNoteBookId = notebookId; if (!TextUtils.isEmpty(mNoteBookId)) { - Notebook notebook = Notebook.getByServerId(mNoteBookId); + Notebook notebook = NotebookDataStore.getByServerId(mNoteBookId); if (notebook != null) { mNotebookTv.setText(notebook.getTitle()); } From f3e5de1d5f443862f38dc4c0ae8fd2d6c7704370 Mon Sep 17 00:00:00 2001 From: houxg Date: Thu, 9 Mar 2017 14:48:18 +0800 Subject: [PATCH 13/13] fix can't delete note --- app/src/main/java/org/houxg/leamonax/ui/NoteFragment.java | 2 -- .../main/java/org/houxg/leamonax/ui/SearchActivity.java | 2 -- .../java/org/houxg/leamonax/utils/ActionModeHandler.java | 7 ++----- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/houxg/leamonax/ui/NoteFragment.java b/app/src/main/java/org/houxg/leamonax/ui/NoteFragment.java index 04dbefe..489dbe1 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/NoteFragment.java +++ b/app/src/main/java/org/houxg/leamonax/ui/NoteFragment.java @@ -204,8 +204,6 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); - mActionModeHandler.dismiss(); - mNoteList.invalidateAllSelected(); } }) .show(); diff --git a/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java b/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java index b7da0a4..8477425 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java @@ -216,8 +216,6 @@ public class SearchActivity extends BaseActivity implements NoteAdapter.NoteAdap @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); - mActionModeHandler.dismiss(); - mAdapter.invalidateAllSelected(); } }) .show(); diff --git a/app/src/main/java/org/houxg/leamonax/utils/ActionModeHandler.java b/app/src/main/java/org/houxg/leamonax/utils/ActionModeHandler.java index 341b15a..0f73ce0 100644 --- a/app/src/main/java/org/houxg/leamonax/utils/ActionModeHandler.java +++ b/app/src/main/java/org/houxg/leamonax/utils/ActionModeHandler.java @@ -43,16 +43,13 @@ public class ActionModeHandler { @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { - boolean isProceed = mCallback.onAction(item.getItemId(), mPendingItems); - if (isProceed) { - mPendingItems.clear(); - } - return isProceed; + return mCallback.onAction(item.getItemId(), mPendingItems); } @Override public void onDestroyActionMode(ActionMode mode) { mActionMode = null; + mPendingItems = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mContext.getWindow().setStatusBarColor(mContext.getResources().getColor(R.color.colorPrimary)); }