From d717085935dfbc2ee84ceb6256511e56a55e11c2 Mon Sep 17 00:00:00 2001 From: houxg Date: Mon, 21 Nov 2016 15:31:44 +0800 Subject: [PATCH] =?UTF-8?q?fix=20new=20note=20doesn=E2=80=99t=20show=20on?= =?UTF-8?q?=20the=20list?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leanotelite/network/ApiProvider.java | 3 - .../houxg/leanotelite/ui/MainActivity.java | 3 +- .../leanotelite/ui/NotePreviewActivity.java | 2 +- .../leanotelite/ui/edit/NoteEditActivity.java | 55 ++++++++++++------- 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/org/houxg/leanotelite/network/ApiProvider.java b/app/src/main/java/org/houxg/leanotelite/network/ApiProvider.java index a0363ac..41d6b8f 100644 --- a/app/src/main/java/org/houxg/leanotelite/network/ApiProvider.java +++ b/app/src/main/java/org/houxg/leanotelite/network/ApiProvider.java @@ -25,9 +25,6 @@ public class ApiProvider { } public static ApiProvider getInstance() { - if (SingletonHolder.INSTANCE.mApiRetrofit == null) { - SingletonHolder.INSTANCE.init(AccountService.getCurrent().getHost()); - } return SingletonHolder.INSTANCE; } diff --git a/app/src/main/java/org/houxg/leanotelite/ui/MainActivity.java b/app/src/main/java/org/houxg/leanotelite/ui/MainActivity.java index d4017e3..0e2cc12 100644 --- a/app/src/main/java/org/houxg/leanotelite/ui/MainActivity.java +++ b/app/src/main/java/org/houxg/leanotelite/ui/MainActivity.java @@ -238,9 +238,10 @@ public class MainActivity extends BaseActivity implements NotebookAdapter.Notebo Note newNote = new Note(); Notebook notebook = AppDataBase.getRecentNoteBook(account.getUserId()); newNote.setNoteBookId(notebook.getNotebookId()); + newNote.setUserId(account.getUserId()); newNote.setIsMarkDown(account.getDefaultEditor() == Account.EDITOR_MARKDOWN); newNote.save(); - Intent intent = NoteEditActivity.getOpenIntent(this, newNote.getId()); + Intent intent = NoteEditActivity.getOpenIntent(this, newNote.getId(), true); startActivity(intent); } diff --git a/app/src/main/java/org/houxg/leanotelite/ui/NotePreviewActivity.java b/app/src/main/java/org/houxg/leanotelite/ui/NotePreviewActivity.java index 9776866..a223782 100644 --- a/app/src/main/java/org/houxg/leanotelite/ui/NotePreviewActivity.java +++ b/app/src/main/java/org/houxg/leanotelite/ui/NotePreviewActivity.java @@ -74,7 +74,7 @@ public class NotePreviewActivity extends BaseActivity implements EditorFragment. public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_edit: - startActivityForResult(NoteEditActivity.getOpenIntent(this, mNote.getId()), REQ_EDIT); + startActivityForResult(NoteEditActivity.getOpenIntent(this, mNote.getId(), false), REQ_EDIT); return true; } return super.onOptionsItemSelected(item); diff --git a/app/src/main/java/org/houxg/leanotelite/ui/edit/NoteEditActivity.java b/app/src/main/java/org/houxg/leanotelite/ui/edit/NoteEditActivity.java index 9a8f19c..8de5d08 100644 --- a/app/src/main/java/org/houxg/leanotelite/ui/edit/NoteEditActivity.java +++ b/app/src/main/java/org/houxg/leanotelite/ui/edit/NoteEditActivity.java @@ -36,6 +36,7 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi private static final String TAG = "NoteEditActivity"; public static final String EXT_NOTE_LOCAL_ID = "ext_note_local_id"; + public static final String EXT_IS_NEW_NOTE = "ext_is_new_note"; public static final String TAG_EDITOR = "tag_editor_tag"; public static final String TAG_SETTING = "tag_setting_tag"; public static final int FRAG_EDITOR = 0; @@ -45,6 +46,7 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi private SettingFragment mSettingsFragment; private Note mOriginal; private Note mModified; + private boolean mIsNewNote; private LeaViewPager mPager; @@ -68,14 +70,16 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi finish(); return; } + mIsNewNote = getIntent().getBooleanExtra(EXT_IS_NEW_NOTE, false); mOriginal = AppDataBase.getNoteByLocalId(noteLocalId); mModified = AppDataBase.getNoteByLocalId(noteLocalId); setResult(RESULT_CANCELED); } - public static Intent getOpenIntent(Context context, long noteLocalId) { + public static Intent getOpenIntent(Context context, long noteLocalId, boolean isNewNote) { Intent intent = new Intent(context, NoteEditActivity.class); intent.putExtra(EXT_NOTE_LOCAL_ID, noteLocalId); + intent.putExtra(EXT_IS_NEW_NOTE, isNewNote); return intent; } @@ -97,7 +101,7 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_save: - checkChangeOrDirty() + filterUnchanged() .doOnCompleted(new Action0() { @Override public void call() { @@ -106,15 +110,15 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi }) .subscribe(new Action1() { @Override - public void call(Note noteInfo) { - saveAsDraft(noteInfo); + public void call(Note note) { + saveAsDraft(note); setResult(RESULT_OK); if (NetworkUtils.isNetworkAvailable(NoteEditActivity.this)) { - boolean isSucceed = NoteService.updateNote(AppDataBase.getNoteByLocalId(mModified.getId())); + boolean isSucceed = NoteService.updateNote(AppDataBase.getNoteByLocalId(note.getId())); if (isSucceed) { - Note note = AppDataBase.getNoteByLocalId(mModified.getId()); - note.setIsDirty(false); - note.save(); + Note localNote = AppDataBase.getNoteByLocalId(note.getId()); + localNote.setIsDirty(false); + localNote.save(); } else { ToastUtils.show(NoteEditActivity.this, R.string.save_note_failed); } @@ -136,7 +140,7 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi if (mPager.getCurrentItem() > FRAG_EDITOR) { mPager.setCurrentItem(FRAG_EDITOR); } else { - checkChangeOrDirty() + filterUnchanged() .observeOn(AndroidSchedulers.mainThread()) .doOnCompleted(new Action0() { @Override @@ -148,26 +152,30 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi @Override public void call(Note note) { setResult(RESULT_OK); - if (!isNewNote(note) || - !(TextUtils.isEmpty(note.getTitle()) || TextUtils.isEmpty(note.getContent()))) { - saveAsDraft(note); - } else { + Log.i(TAG, note.toString()); + + if (mIsNewNote && isTitleContentEmpty(note)) { Log.i(TAG, "remove empty note, id=" + note.getId()); AppDataBase.deleteNoteByLocalId(note.getId()); + } else { + saveAsDraft(note); } } }); } } - private Observable checkChangeOrDirty() { + private Observable filterUnchanged() { return Observable.create( new Observable.OnSubscribe() { @Override public void call(Subscriber subscriber) { if (!subscriber.isUnsubscribed()) { updateNote(); - if (mModified.isDirty() || mModified.hasChanges(mOriginal) || isNewNote(mModified)) { + if (mModified.isDirty() + || mModified.hasChanges(mOriginal) + || isLocalNote(mModified) + || isTitleContentEmpty(mModified)) { subscriber.onNext(mModified); } subscriber.onCompleted(); @@ -177,6 +185,14 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi .subscribeOn(Schedulers.io()); } + private boolean isTitleContentEmpty(Note note) { + return TextUtils.isEmpty(note.getTitle()) && TextUtils.isEmpty(note.getContent()); + } + + private boolean isLocalNote(Note note) { + return TextUtils.isEmpty(note.getNoteId()); + } + private void updateNote() { String title = mEditorFragment.getTitle(); String content = mEditorFragment.getContent(); @@ -196,13 +212,14 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi noteFromDb.setTags(note.getTags()); noteFromDb.setIsPublicBlog(note.isPublicBlog()); noteFromDb.setIsDirty(true); + long updateTime = System.currentTimeMillis(); + noteFromDb.setUpdatedTimeVal(updateTime); + if (mIsNewNote) { + noteFromDb.setCreatedTimeVal(updateTime); + } noteFromDb.update(); } - private boolean isNewNote(Note note) { - return TextUtils.isEmpty(note.getNoteId()); - } - @Override public Uri createImage(String filePath) { return NoteFileService.createImageFile(mModified.getId(), filePath);