From 0343a345dcd670213a27c283b014845244444e05 Mon Sep 17 00:00:00 2001 From: houxg Date: Thu, 9 Mar 2017 11:10:37 +0800 Subject: [PATCH] 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()); }