code refactoring: move getCurrent() method to Account class

This commit is contained in:
houxg
2017-03-01 17:31:43 +08:00
parent ed7c476754
commit 259167bf9a
21 changed files with 55 additions and 108 deletions

View File

@@ -18,10 +18,10 @@ import android.widget.TextView;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import org.houxg.leamonax.R; import org.houxg.leamonax.R;
import org.houxg.leamonax.model.Account;
import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.Note;
import org.houxg.leamonax.model.NoteFile; import org.houxg.leamonax.model.NoteFile;
import org.houxg.leamonax.model.Notebook; import org.houxg.leamonax.model.Notebook;
import org.houxg.leamonax.service.AccountService;
import org.houxg.leamonax.service.NoteFileService; import org.houxg.leamonax.service.NoteFileService;
import org.houxg.leamonax.utils.FileUtils; import org.houxg.leamonax.utils.FileUtils;
import org.houxg.leamonax.utils.TimeUtils; import org.houxg.leamonax.utils.TimeUtils;
@@ -106,7 +106,7 @@ public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {
} }
private void updateNotebookMap() { private void updateNotebookMap() {
List<Notebook> notebooks = Notebook.getAllNotebooks(AccountService.getCurrent().getUserId()); List<Notebook> notebooks = Notebook.getAllNotebooks(Account.getCurrent().getUserId());
mNotebookId2TitleMaps = new HashMap<>(); mNotebookId2TitleMaps = new HashMap<>();
for (Notebook notebook : notebooks) { for (Notebook notebook : notebooks) {
mNotebookId2TitleMaps.put(notebook.getNotebookId(), notebook.getTitle()); mNotebookId2TitleMaps.put(notebook.getNotebookId(), notebook.getTitle());

View File

@@ -10,9 +10,8 @@ import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import org.houxg.leamonax.R; 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.Notebook;
import org.houxg.leamonax.service.AccountService;
import org.houxg.leamonax.utils.CollectionUtils; import org.houxg.leamonax.utils.CollectionUtils;
import java.util.ArrayList; import java.util.ArrayList;
@@ -56,14 +55,14 @@ public class NotebookAdapter extends RecyclerView.Adapter<NotebookAdapter.Notebo
private void getSafeNotebook(Stack<String> stack) { private void getSafeNotebook(Stack<String> stack) {
if (stack.isEmpty()) { if (stack.isEmpty()) {
mData = Notebook.getRootNotebooks(AccountService.getCurrent().getUserId()); mData = Notebook.getRootNotebooks(Account.getCurrent().getUserId());
} else { } else {
Notebook parent = Notebook.getByServerId(stack.peek()); Notebook parent = Notebook.getByServerId(stack.peek());
if (parent.isDeleted()) { if (parent.isDeleted()) {
stack.pop(); stack.pop();
getSafeNotebook(stack); getSafeNotebook(stack);
} else { } else {
mData = Notebook.getChildNotebook(mStack.peek(), AccountService.getCurrent().getUserId()); mData = Notebook.getChildNotebook(mStack.peek(), Account.getCurrent().getUserId());
mData.add(0, parent); mData.add(0, parent);
} }
} }
@@ -148,7 +147,7 @@ public class NotebookAdapter extends RecyclerView.Adapter<NotebookAdapter.Notebo
} }
private boolean hasChild(String notebookId) { private boolean hasChild(String notebookId) {
return CollectionUtils.isNotEmpty(Notebook.getChildNotebook(notebookId, AccountService.getCurrent().getUserId())); return CollectionUtils.isNotEmpty(Notebook.getChildNotebook(notebookId, Account.getCurrent().getUserId()));
} }
private void listUpper() { private void listUpper() {
@@ -158,11 +157,11 @@ public class NotebookAdapter extends RecyclerView.Adapter<NotebookAdapter.Notebo
mStack.pop(); mStack.pop();
if (mStack.isEmpty()) { if (mStack.isEmpty()) {
mData = Notebook.getRootNotebooks(AccountService.getCurrent().getUserId()); mData = Notebook.getRootNotebooks(Account.getCurrent().getUserId());
} else { } else {
String parentId = mStack.peek(); String parentId = mStack.peek();
mData.add(Notebook.getByServerId(parentId)); mData.add(Notebook.getByServerId(parentId));
mData.addAll(Notebook.getChildNotebook(parentId, AccountService.getCurrent().getUserId())); mData.addAll(Notebook.getChildNotebook(parentId, Account.getCurrent().getUserId()));
} }
notifyItemRangeInserted(0, mData.size()); notifyItemRangeInserted(0, mData.size());
} }
@@ -181,7 +180,7 @@ public class NotebookAdapter extends RecyclerView.Adapter<NotebookAdapter.Notebo
notifyItemChanged(0); notifyItemChanged(0);
mStack.push(notebook.getNotebookId()); mStack.push(notebook.getNotebookId());
List<Notebook> children = Notebook.getChildNotebook(notebook.getNotebookId(), AccountService.getCurrent().getUserId()); List<Notebook> children = Notebook.getChildNotebook(notebook.getNotebookId(), Account.getCurrent().getUserId());
int childrenSize = children.size(); int childrenSize = children.size();
mData.addAll(children); mData.addAll(children);
notifyItemRangeInserted(1, childrenSize); notifyItemRangeInserted(1, childrenSize);

View File

@@ -8,11 +8,9 @@ import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import org.houxg.leamonax.R; 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.model.Tag;
import org.houxg.leamonax.service.AccountService;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import butterknife.BindView; import butterknife.BindView;
@@ -28,7 +26,7 @@ public class TagAdapter extends RecyclerView.Adapter<TagAdapter.TagHolder> {
} }
public void refresh() { public void refresh() {
mData = Tag.getAllTags(AccountService.getCurrent().getUserId()); mData = Tag.getAllTags(Account.getCurrent().getUserId());
notifyDataSetChanged(); notifyDataSetChanged();
} }

View File

@@ -182,7 +182,7 @@ public class Account extends BaseModel {
.querySingle(); .querySingle();
} }
public static Account getAccountWithToken() { public static Account getCurrent() {
return SQLite.select() return SQLite.select()
.from(Account.class) .from(Account.class)
.where(Account_Table.token.notEq("")) .where(Account_Table.token.notEq(""))

View File

@@ -14,7 +14,6 @@ import com.raizlabs.android.dbflow.sql.language.property.IProperty;
import com.raizlabs.android.dbflow.structure.BaseModel; import com.raizlabs.android.dbflow.structure.BaseModel;
import org.houxg.leamonax.database.AppDataBase; import org.houxg.leamonax.database.AppDataBase;
import org.houxg.leamonax.service.AccountService;
import org.houxg.leamonax.utils.CollectionUtils; import org.houxg.leamonax.utils.CollectionUtils;
import org.houxg.leamonax.utils.TimeUtils; import org.houxg.leamonax.utils.TimeUtils;
@@ -268,7 +267,7 @@ public class Note extends BaseModel implements Serializable {
keyword = String.format(Locale.US, "%%%s%%", keyword); keyword = String.format(Locale.US, "%%%s%%", keyword);
return SQLite.select() return SQLite.select()
.from(Note.class) .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.title.like(keyword))
.and(Note_Table.isTrash.eq(false)) .and(Note_Table.isTrash.eq(false))
.and(Note_Table.isDeleted.eq(false)) .and(Note_Table.isDeleted.eq(false))

View File

@@ -4,8 +4,6 @@ package org.houxg.leamonax.model;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import com.raizlabs.android.dbflow.annotation.Column; import com.raizlabs.android.dbflow.annotation.Column;
import org.houxg.leamonax.service.AccountService;
public class User extends BaseResponse { public class User extends BaseResponse {
@SerializedName("UserId") @SerializedName("UserId")
@@ -37,7 +35,7 @@ public class User extends BaseResponse {
} }
public String getAvatar() { public String getAvatar() {
Account current = AccountService.getCurrent(); Account current = Account.getCurrent();
String host = current.getHost(); String host = current.getHost();
if(host.equals("https://leanote.com")){ if(host.equals("https://leanote.com")){
return avatar;} return avatar;}

View File

@@ -8,7 +8,6 @@ import org.houxg.leamonax.network.api.AuthApi;
import org.houxg.leamonax.network.api.NoteApi; import org.houxg.leamonax.network.api.NoteApi;
import org.houxg.leamonax.network.api.NotebookApi; import org.houxg.leamonax.network.api.NotebookApi;
import org.houxg.leamonax.network.api.UserApi; import org.houxg.leamonax.network.api.UserApi;
import org.houxg.leamonax.service.AccountService;
import java.io.IOException; import java.io.IOException;
@@ -29,7 +28,7 @@ public class ApiProvider {
} }
public static ApiProvider getInstance() { public static ApiProvider getInstance() {
Account account = AccountService.getCurrent(); Account account = Account.getCurrent();
if (account != null && SingletonHolder.INSTANCE.mApiRetrofit == null) { if (account != null && SingletonHolder.INSTANCE.mApiRetrofit == null) {
SingletonHolder.INSTANCE.init(account.getHost()); SingletonHolder.INSTANCE.init(account.getHost());
} }
@@ -50,7 +49,7 @@ public class ApiProvider {
HttpUrl newUrl = url; HttpUrl newUrl = url;
if (shouldAddTokenToQuery(path)) { if (shouldAddTokenToQuery(path)) {
newUrl = url.newBuilder() newUrl = url.newBuilder()
.addQueryParameter("token", AccountService.getCurrent().getAccessToken()) .addQueryParameter("token", Account.getCurrent().getAccessToken())
.build(); .build();
} }
Request newRequest = request.newBuilder() Request newRequest = request.newBuilder()

View File

@@ -54,7 +54,7 @@ public class AccountService {
} }
public static void logout() { public static void logout() {
Account account = getCurrent(); Account account = Account.getCurrent();
account.setAccessToken(""); account.setAccessToken("");
account.update(); account.update();
} }
@@ -67,15 +67,11 @@ public class AccountService {
return RetrofitUtils.create(ApiProvider.getInstance().getUserApi().updateUsername(userName)); return RetrofitUtils.create(ApiProvider.getInstance().getUserApi().updateUsername(userName));
} }
public static Account getCurrent() {
return Account.getAccountWithToken();
}
public static List<Account> getAccountList() { public static List<Account> getAccountList() {
return Account.getAccountListWithToken(); return Account.getAccountListWithToken();
} }
public static boolean isSignedIn() { public static boolean isSignedIn() {
return getCurrent() != null; return Account.getCurrent() != null;
} }
} }

View File

@@ -7,6 +7,7 @@ import com.elvishew.xlog.XLog;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.houxg.leamonax.Leamonax; import org.houxg.leamonax.Leamonax;
import org.houxg.leamonax.model.Account;
import org.houxg.leamonax.model.NoteFile; import org.houxg.leamonax.model.NoteFile;
import java.io.File; import java.io.File;
@@ -50,7 +51,7 @@ public class NoteFileService {
} }
public static Uri getServerImageUri(String serverId) { 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(); return uri.buildUpon().appendEncodedPath("api/file/getImage").appendQueryParameter("fileId", serverId).build();
} }
@@ -86,7 +87,7 @@ public class NoteFileService {
filePath = noteFile.getLocalPath(); filePath = noteFile.getLocalPath();
XLog.i(TAG + "use local image, path=" + filePath); XLog.i(TAG + "use local image, path=" + filePath);
} else { } 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); XLog.i(TAG + "use server image, url=" + url);
try { try {
filePath = NoteFileService.getImageFromServer(Uri.parse(url), Leamonax.getContext().getCacheDir()); filePath = NoteFileService.getImageFromServer(Uri.parse(url), Leamonax.getContext().getCacheDir());

View File

@@ -7,7 +7,6 @@ import android.text.TextUtils;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
import com.elvishew.xlog.XLog; import com.elvishew.xlog.XLog;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.houxg.leamonax.R; 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.Account;
import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.Note;
import org.houxg.leamonax.model.NoteFile; import org.houxg.leamonax.model.NoteFile;
import org.houxg.leamonax.model.Note_Table;
import org.houxg.leamonax.model.Notebook; import org.houxg.leamonax.model.Notebook;
import org.houxg.leamonax.model.RelationshipOfNoteTag; import org.houxg.leamonax.model.RelationshipOfNoteTag;
import org.houxg.leamonax.model.Tag; import org.houxg.leamonax.model.Tag;
@@ -49,7 +47,7 @@ public class NoteService {
public static void fetchFromServer() { public static void fetchFromServer() {
//sync notebook //sync notebook
int notebookUsn = AccountService.getCurrent().getNotebookUsn(); int notebookUsn = Account.getCurrent().getNotebookUsn();
List<Notebook> notebooks; List<Notebook> notebooks;
do { do {
notebooks = RetrofitUtils.excuteWithException(ApiProvider.getInstance().getNotebookApi().getSyncNotebooks(notebookUsn, MAX_ENTRY)); notebooks = RetrofitUtils.excuteWithException(ApiProvider.getInstance().getNotebookApi().getSyncNotebooks(notebookUsn, MAX_ENTRY));
@@ -65,7 +63,7 @@ public class NoteService {
remoteNotebook.update(); remoteNotebook.update();
} }
notebookUsn = remoteNotebook.getUsn(); notebookUsn = remoteNotebook.getUsn();
Account account = AccountService.getCurrent(); Account account = Account.getCurrent();
account.setNotebookUsn(notebookUsn); account.setNotebookUsn(notebookUsn);
account.save(); account.save();
} }
@@ -73,7 +71,7 @@ public class NoteService {
//sync note //sync note
int noteUsn = AccountService.getCurrent().getNoteUsn(); int noteUsn = Account.getCurrent().getNoteUsn();
List<Note> notes; List<Note> notes;
do { do {
notes = RetrofitUtils.excuteWithException(ApiProvider.getInstance().getNoteApi().getSyncNotes(noteUsn, MAX_ENTRY)); notes = RetrofitUtils.excuteWithException(ApiProvider.getInstance().getNoteApi().getSyncNotes(noteUsn, MAX_ENTRY));
@@ -110,7 +108,7 @@ public class NoteService {
remoteNote.update(); remoteNote.update();
handleFile(localId, remoteNote.getNoteFiles()); handleFile(localId, remoteNote.getNoteFiles());
updateTagsToLocal(localId, remoteNote.getTagData()); updateTagsToLocal(localId, remoteNote.getTagData());
Account account = AccountService.getCurrent(); Account account = Account.getCurrent();
account.setNoteUsn(noteUsn); account.setNoteUsn(noteUsn);
account.save(); account.save();
} }
@@ -149,7 +147,7 @@ public class NoteService {
private static String convertToLocalImageLinkForRichText(long noteLocalId, String noteContent) { private static String convertToLocalImageLinkForRichText(long noteLocalId, String noteContent) {
return StringUtils.replace(noteContent, return StringUtils.replace(noteContent,
"<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>", "<img[^>]+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() { new StringUtils.Replacer() {
@Override @Override
public String replaceWith(String original, Object... extraData) { public String replaceWith(String original, Object... extraData) {
@@ -174,8 +172,8 @@ public class NoteService {
private static String convertToLocalImageLinkForMD(long noteLocalId, String noteContent) { private static String convertToLocalImageLinkForMD(long noteLocalId, String noteContent) {
return StringUtils.replace(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=.*?\\)", Account.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()),
new StringUtils.Replacer() { new StringUtils.Replacer() {
@Override @Override
public String replaceWith(String original, Object... extraData) { 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. * if new usn equals to (current usn + 1), then just simply update usn without syncing.
*/ */
private static void updateNoteUsnIfNeed(int newUsn) { private static void updateNoteUsnIfNeed(int newUsn) {
Account account = AccountService.getCurrent(); Account account = Account.getCurrent();
if (newUsn - account.getNoteUsn() == 1) { if (newUsn - account.getNoteUsn() == 1) {
account.setNoteUsn(newUsn); account.setNoteUsn(newUsn);
account.update(); account.update();
@@ -412,7 +410,7 @@ public class NoteService {
} }
public static void updateTagsToLocal(long noteLocalId, List<String> tags) { public static void updateTagsToLocal(long noteLocalId, List<String> tags) {
String currentUid = AccountService.getCurrent().getUserId(); String currentUid = Account.getCurrent().getUserId();
if (tags == null) { if (tags == null) {
tags = new ArrayList<>(); tags = new ArrayList<>();
} }

View File

@@ -3,7 +3,6 @@ package org.houxg.leamonax.service;
import com.elvishew.xlog.XLog; import com.elvishew.xlog.XLog;
import org.houxg.leamonax.database.AppDataBase;
import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Account;
import org.houxg.leamonax.model.Notebook; import org.houxg.leamonax.model.Notebook;
import org.houxg.leamonax.network.ApiProvider; import org.houxg.leamonax.network.ApiProvider;
@@ -19,7 +18,7 @@ public class NotebookService {
throw new IllegalStateException("Network error"); throw new IllegalStateException("Network error");
} }
if (notebook.isOk()) { if (notebook.isOk()) {
Account account = AccountService.getCurrent(); Account account = Account.getCurrent();
if (notebook.getUsn() - account.getNotebookUsn() == 1) { if (notebook.getUsn() - account.getNotebookUsn() == 1) {
XLog.d(TAG + "update usn=" + notebook.getUsn()); XLog.d(TAG + "update usn=" + notebook.getUsn());
account.setNotebookUsn(notebook.getUsn()); account.setNotebookUsn(notebook.getUsn());

View File

@@ -13,8 +13,8 @@ import org.bson.types.ObjectId;
import org.houxg.leamonax.BuildConfig; import org.houxg.leamonax.BuildConfig;
import org.houxg.leamonax.R; import org.houxg.leamonax.R;
import org.houxg.leamonax.database.AppDataBase; import org.houxg.leamonax.database.AppDataBase;
import org.houxg.leamonax.model.Account;
import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.Note;
import org.houxg.leamonax.service.AccountService;
import org.houxg.leamonax.utils.OpenUtils; import org.houxg.leamonax.utils.OpenUtils;
import org.houxg.leamonax.utils.TestUtils; import org.houxg.leamonax.utils.TestUtils;
@@ -54,7 +54,7 @@ public class AboutActivity extends BaseActivity {
new Observable.OnSubscribe<Void>() { new Observable.OnSubscribe<Void>() {
@Override @Override
public void call(Subscriber<? super Void> subscriber) { public void call(Subscriber<? super Void> subscriber) {
String userId = AccountService.getCurrent().getUserId(); String userId = Account.getCurrent().getUserId();
SecureRandom random = new SecureRandom(); SecureRandom random = new SecureRandom();
String notebookId = new ObjectId().toString(); String notebookId = new ObjectId().toString();
List<Note> notes = new ArrayList<>(8000); List<Note> notes = new ArrayList<>(8000);

View File

@@ -15,7 +15,7 @@ public class LaunchActivity extends Activity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
Intent intent; Intent intent;
if (AccountService.isSignedIn()) { if (AccountService.isSignedIn()) {
Account account = AccountService.getCurrent(); Account account = Account.getCurrent();
ApiProvider.getInstance().init(account.getHost()); ApiProvider.getInstance().init(account.getHost());
intent = MainActivity.getOpenIntent(this, false); intent = MainActivity.getOpenIntent(this, false);
} else { } else {

View File

@@ -8,7 +8,6 @@ import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@@ -20,20 +19,15 @@ import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode; import org.greenrobot.eventbus.ThreadMode;
import org.houxg.leamonax.R; import org.houxg.leamonax.R;
import org.houxg.leamonax.adapter.NotebookAdapter;
import org.houxg.leamonax.background.NoteSyncService; import org.houxg.leamonax.background.NoteSyncService;
import org.houxg.leamonax.database.AppDataBase;
import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Account;
import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.Note;
import org.houxg.leamonax.model.Notebook; import org.houxg.leamonax.model.Notebook;
import org.houxg.leamonax.model.SyncEvent; 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.ui.edit.NoteEditActivity;
import org.houxg.leamonax.utils.NetworkUtils; import org.houxg.leamonax.utils.NetworkUtils;
import org.houxg.leamonax.utils.ToastUtils; import org.houxg.leamonax.utils.ToastUtils;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@@ -69,7 +63,7 @@ public class MainActivity extends BaseActivity implements Navigation.Callback {
initToolBar((Toolbar) findViewById(R.id.toolbar)); initToolBar((Toolbar) findViewById(R.id.toolbar));
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_white); getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_white);
CrashReport.setUserId(AccountService.getCurrent().getUserId()); CrashReport.setUserId(Account.getCurrent().getUserId());
mNavigation = new Navigation(this); mNavigation = new Navigation(this);
mNavigation.init(this, mNavigationView); mNavigation.init(this, mNavigationView);
@@ -154,7 +148,7 @@ public class MainActivity extends BaseActivity implements Navigation.Callback {
@OnClick(R.id.fab) @OnClick(R.id.fab)
void clickedFab() { void clickedFab() {
Account account = AccountService.getCurrent(); Account account = Account.getCurrent();
Note newNote = new Note(); Note newNote = new Note();
newNote.setUserId(account.getUserId()); newNote.setUserId(account.getUserId());
Notebook notebook; Notebook notebook;
@@ -162,7 +156,7 @@ public class MainActivity extends BaseActivity implements Navigation.Callback {
if (currentMode == Navigation.Mode.NOTEBOOK) { if (currentMode == Navigation.Mode.NOTEBOOK) {
notebook = Notebook.getByLocalId(currentMode.notebookId); notebook = Notebook.getByLocalId(currentMode.notebookId);
} else { } else {
notebook = Notebook.getRecentNoteBook(AccountService.getCurrent().getUserId()); notebook = Notebook.getRecentNoteBook(Account.getCurrent().getUserId());
} }
if (notebook != null) { if (notebook != null) {
newNote.setNoteBookId(notebook.getNotebookId()); newNote.setNoteBookId(notebook.getNotebookId());
@@ -192,13 +186,13 @@ public class MainActivity extends BaseActivity implements Navigation.Callback {
List<Note> notes; List<Note> notes;
switch (mode) { switch (mode) {
case RECENT_NOTES: case RECENT_NOTES:
notes = Note.getAllNotes(AccountService.getCurrent().getUserId()); notes = Note.getAllNotes(Account.getCurrent().getUserId());
break; break;
case NOTEBOOK: case NOTEBOOK:
notes = Note.getNotesFromNotebook(AccountService.getCurrent().getUserId(), mode.notebookId); notes = Note.getNotesFromNotebook(Account.getCurrent().getUserId(), mode.notebookId);
break; break;
case TAG: case TAG:
notes = Note.getByTagText(mode.tagText, AccountService.getCurrent().getUserId()); notes = Note.getByTagText(mode.tagText, Account.getCurrent().getUserId());
break; break;
default: default:
return false; return false;

View File

@@ -6,16 +6,12 @@ import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.support.v4.view.GravityCompat; import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewGroupCompat;
import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@@ -25,8 +21,6 @@ import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.elvishew.xlog.XLog;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.crashreport.CrashReport; import com.tencent.bugly.crashreport.CrashReport;
import org.houxg.leamonax.R; import org.houxg.leamonax.R;
@@ -108,7 +102,7 @@ public class Navigation {
} }
private void fetchInfo() { private void fetchInfo() {
AccountService.getInfo(AccountService.getCurrent().getUserId()) AccountService.getInfo(Account.getCurrent().getUserId())
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<User>() { .subscribe(new Observer<User>() {
@@ -124,8 +118,8 @@ public class Navigation {
@Override @Override
public void onNext(User user) { public void onNext(User user) {
AccountService.saveToAccount(user, AccountService.getCurrent().getHost()); AccountService.saveToAccount(user, Account.getCurrent().getHost());
refreshUserInfo(AccountService.getCurrent()); refreshUserInfo(Account.getCurrent());
mAccountAdapter.notifyDataSetChanged(); mAccountAdapter.notifyDataSetChanged();
} }
}); });
@@ -371,7 +365,7 @@ public class Navigation {
} }
public void refresh() { public void refresh() {
refreshUserInfo(AccountService.getCurrent()); refreshUserInfo(Account.getCurrent());
mAccountAdapter.load(AccountService.getAccountList()); mAccountAdapter.load(AccountService.getAccountList());
mTagAdapter.refresh(); mTagAdapter.refresh();
mNotebookAdapter.refresh(); mNotebookAdapter.refresh();

View File

@@ -6,10 +6,7 @@ import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.ActionMode;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@@ -17,22 +14,13 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import com.elvishew.xlog.XLog;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.houxg.leamonax.R; import org.houxg.leamonax.R;
import org.houxg.leamonax.adapter.NoteAdapter; import org.houxg.leamonax.adapter.NoteAdapter;
import org.houxg.leamonax.background.NoteSyncService;
import org.houxg.leamonax.database.AppDataBase;
import org.houxg.leamonax.model.Note; 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.service.NoteService;
import org.houxg.leamonax.utils.ActionModeHandler; import org.houxg.leamonax.utils.ActionModeHandler;
import org.houxg.leamonax.utils.CollectionUtils; import org.houxg.leamonax.utils.CollectionUtils;
import org.houxg.leamonax.utils.NetworkUtils;
import org.houxg.leamonax.utils.SharedPreferenceUtils; import org.houxg.leamonax.utils.SharedPreferenceUtils;
import org.houxg.leamonax.utils.ToastUtils; import org.houxg.leamonax.utils.ToastUtils;
import org.houxg.leamonax.widget.NoteList; import org.houxg.leamonax.widget.NoteList;
@@ -40,7 +28,6 @@ import org.houxg.leamonax.widget.NoteList;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;

View File

@@ -15,7 +15,6 @@ import com.tencent.bugly.crashreport.CrashReport;
import org.houxg.leamonax.BuildConfig; import org.houxg.leamonax.BuildConfig;
import org.houxg.leamonax.R; import org.houxg.leamonax.R;
import org.houxg.leamonax.database.AppDataBase;
import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.Note;
import org.houxg.leamonax.service.NoteService; import org.houxg.leamonax.service.NoteService;
import org.houxg.leamonax.ui.edit.EditorFragment; import org.houxg.leamonax.ui.edit.EditorFragment;

View File

@@ -3,7 +3,6 @@ package org.houxg.leamonax.ui;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
@@ -11,10 +10,7 @@ import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.text.TextUtils; import android.text.TextUtils;
import android.transition.Slide; import android.transition.Slide;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.view.Gravity; import android.view.Gravity;
import android.view.View;
import android.view.Window; import android.view.Window;
import android.widget.ImageView; import android.widget.ImageView;
@@ -24,14 +20,11 @@ import org.houxg.leamonax.model.Note;
import org.houxg.leamonax.service.NoteService; import org.houxg.leamonax.service.NoteService;
import org.houxg.leamonax.utils.ActionModeHandler; import org.houxg.leamonax.utils.ActionModeHandler;
import org.houxg.leamonax.utils.CollectionUtils; import org.houxg.leamonax.utils.CollectionUtils;
import org.houxg.leamonax.utils.DisplayUtils;
import org.houxg.leamonax.utils.ToastUtils; import org.houxg.leamonax.utils.ToastUtils;
import org.houxg.leamonax.widget.DividerDecoration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;

View File

@@ -14,20 +14,15 @@ import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import org.houxg.leamonax.BuildConfig; import org.houxg.leamonax.BuildConfig;
import org.houxg.leamonax.R; import org.houxg.leamonax.R;
import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Account;
import org.houxg.leamonax.model.BaseResponse; import org.houxg.leamonax.model.BaseResponse;
import org.houxg.leamonax.model.Note; 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;
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;
import org.houxg.leamonax.model.Tag_Table;
import org.houxg.leamonax.service.AccountService; import org.houxg.leamonax.service.AccountService;
import org.houxg.leamonax.utils.ToastUtils; import org.houxg.leamonax.utils.ToastUtils;
@@ -76,11 +71,11 @@ public class SettingsActivity extends BaseActivity {
void selectEditor() { void selectEditor() {
new AlertDialog.Builder(this) new AlertDialog.Builder(this)
.setTitle(R.string.choose_editor) .setTitle(R.string.choose_editor)
.setSingleChoiceItems(mEditors, AccountService.getCurrent().getDefaultEditor(), new DialogInterface.OnClickListener() { .setSingleChoiceItems(mEditors, Account.getCurrent().getDefaultEditor(), new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); dialog.dismiss();
Account account = AccountService.getCurrent(); Account account = Account.getCurrent();
account.setDefaultEditor(which); account.setDefaultEditor(which);
account.update(); account.update();
mEditorTv.setText(mEditors[which]); mEditorTv.setText(mEditors[which]);
@@ -128,7 +123,7 @@ public class SettingsActivity extends BaseActivity {
void clickedUserName() { void clickedUserName() {
View view = LayoutInflater.from(this).inflate(R.layout.dialog_sigle_edittext, null); View view = LayoutInflater.from(this).inflate(R.layout.dialog_sigle_edittext, null);
final EditText mUserNameEt = (EditText) view.findViewById(R.id.edit); final EditText mUserNameEt = (EditText) view.findViewById(R.id.edit);
mUserNameEt.setText(AccountService.getCurrent().getUserName()); mUserNameEt.setText(Account.getCurrent().getUserName());
new AlertDialog.Builder(this) new AlertDialog.Builder(this)
.setTitle(R.string.change_user_name) .setTitle(R.string.change_user_name)
.setView(view) .setView(view)
@@ -191,7 +186,7 @@ public class SettingsActivity extends BaseActivity {
@Override @Override
public void call(Subscriber<? super Void> subscriber) { public void call(Subscriber<? super Void> subscriber) {
if (!subscriber.isUnsubscribed()) { if (!subscriber.isUnsubscribed()) {
Account currentUser = AccountService.getCurrent(); Account currentUser = Account.getCurrent();
String userId = currentUser.getUserId(); String userId = currentUser.getUserId();
Note.deleteAll(userId); Note.deleteAll(userId);
Notebook.deleteAll(userId); Notebook.deleteAll(userId);
@@ -228,18 +223,18 @@ public class SettingsActivity extends BaseActivity {
@Override @Override
public void onError(Throwable e) { public void onError(Throwable e) {
ToastUtils.showNetworkError(SettingsActivity.this); ToastUtils.showNetworkError(SettingsActivity.this);
mUserNameTv.setText(AccountService.getCurrent().getUserName()); mUserNameTv.setText(Account.getCurrent().getUserName());
} }
@Override @Override
public void onNext(BaseResponse baseResponse) { public void onNext(BaseResponse baseResponse) {
if (baseResponse.isOk()) { if (baseResponse.isOk()) {
Account account = AccountService.getCurrent(); Account account = Account.getCurrent();
account.setUserName(username); account.setUserName(username);
account.update(); account.update();
ToastUtils.show(SettingsActivity.this, R.string.change_user_name_successful); ToastUtils.show(SettingsActivity.this, R.string.change_user_name_successful);
} else { } else {
mUserNameTv.setText(AccountService.getCurrent().getUserName()); mUserNameTv.setText(Account.getCurrent().getUserName());
ToastUtils.show(SettingsActivity.this, R.string.change_user_name_failed); ToastUtils.show(SettingsActivity.this, R.string.change_user_name_failed);
} }
} }
@@ -273,7 +268,7 @@ public class SettingsActivity extends BaseActivity {
} }
private void refresh() { private void refresh() {
Account current = AccountService.getCurrent(); Account current = Account.getCurrent();
mEditorTv.setText(mEditors[current.getDefaultEditor()]); mEditorTv.setText(mEditors[current.getDefaultEditor()]);
mUserNameTv.setText(current.getUserName()); mUserNameTv.setText(current.getUserName());
mEmailTv.setText(current.getEmail()); mEmailTv.setText(current.getEmail());

View File

@@ -41,7 +41,6 @@ import org.houxg.leamonax.utils.DialogUtils;
import org.houxg.leamonax.utils.OpenUtils; import org.houxg.leamonax.utils.OpenUtils;
import org.houxg.leamonax.widget.ToggleImageButton; import org.houxg.leamonax.widget.ToggleImageButton;
import java.io.File;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;

View File

@@ -18,10 +18,9 @@ import android.widget.Switch;
import android.widget.TextView; import android.widget.TextView;
import org.houxg.leamonax.R; 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.Notebook;
import org.houxg.leamonax.model.Tag; import org.houxg.leamonax.model.Tag;
import org.houxg.leamonax.service.AccountService;
import org.houxg.leamonax.utils.CollectionUtils; import org.houxg.leamonax.utils.CollectionUtils;
import org.houxg.leamonax.utils.DialogUtils; import org.houxg.leamonax.utils.DialogUtils;
import org.houxg.leamonax.utils.DisplayUtils; import org.houxg.leamonax.utils.DisplayUtils;
@@ -63,7 +62,7 @@ public class SettingFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false); View view = inflater.inflate(R.layout.fragment_settings, container, false);
ButterKnife.bind(this, view); ButterKnife.bind(this, view);
List<Tag> tags = Tag.getAllTags(AccountService.getCurrent().getUserId()); List<Tag> tags = Tag.getAllTags(Account.getCurrent().getUserId());
String[] tagTexts = new String[tags.size()]; String[] tagTexts = new String[tags.size()];
int i = 0; int i = 0;
for (Tag tag : tags) { for (Tag tag : tags) {