fix new note doesn’t show on the list

This commit is contained in:
houxg
2016-11-21 15:31:44 +08:00
parent 267574e966
commit d717085935
4 changed files with 39 additions and 24 deletions

View File

@@ -25,9 +25,6 @@ public class ApiProvider {
} }
public static ApiProvider getInstance() { public static ApiProvider getInstance() {
if (SingletonHolder.INSTANCE.mApiRetrofit == null) {
SingletonHolder.INSTANCE.init(AccountService.getCurrent().getHost());
}
return SingletonHolder.INSTANCE; return SingletonHolder.INSTANCE;
} }

View File

@@ -238,9 +238,10 @@ public class MainActivity extends BaseActivity implements NotebookAdapter.Notebo
Note newNote = new Note(); Note newNote = new Note();
Notebook notebook = AppDataBase.getRecentNoteBook(account.getUserId()); Notebook notebook = AppDataBase.getRecentNoteBook(account.getUserId());
newNote.setNoteBookId(notebook.getNotebookId()); newNote.setNoteBookId(notebook.getNotebookId());
newNote.setUserId(account.getUserId());
newNote.setIsMarkDown(account.getDefaultEditor() == Account.EDITOR_MARKDOWN); newNote.setIsMarkDown(account.getDefaultEditor() == Account.EDITOR_MARKDOWN);
newNote.save(); newNote.save();
Intent intent = NoteEditActivity.getOpenIntent(this, newNote.getId()); Intent intent = NoteEditActivity.getOpenIntent(this, newNote.getId(), true);
startActivity(intent); startActivity(intent);
} }

View File

@@ -74,7 +74,7 @@ public class NotePreviewActivity extends BaseActivity implements EditorFragment.
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_edit: 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 true;
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);

View File

@@ -36,6 +36,7 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi
private static final String TAG = "NoteEditActivity"; private static final String TAG = "NoteEditActivity";
public static final String EXT_NOTE_LOCAL_ID = "ext_note_local_id"; 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_EDITOR = "tag_editor_tag";
public static final String TAG_SETTING = "tag_setting_tag"; public static final String TAG_SETTING = "tag_setting_tag";
public static final int FRAG_EDITOR = 0; public static final int FRAG_EDITOR = 0;
@@ -45,6 +46,7 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi
private SettingFragment mSettingsFragment; private SettingFragment mSettingsFragment;
private Note mOriginal; private Note mOriginal;
private Note mModified; private Note mModified;
private boolean mIsNewNote;
private LeaViewPager mPager; private LeaViewPager mPager;
@@ -68,14 +70,16 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi
finish(); finish();
return; return;
} }
mIsNewNote = getIntent().getBooleanExtra(EXT_IS_NEW_NOTE, false);
mOriginal = AppDataBase.getNoteByLocalId(noteLocalId); mOriginal = AppDataBase.getNoteByLocalId(noteLocalId);
mModified = AppDataBase.getNoteByLocalId(noteLocalId); mModified = AppDataBase.getNoteByLocalId(noteLocalId);
setResult(RESULT_CANCELED); 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 intent = new Intent(context, NoteEditActivity.class);
intent.putExtra(EXT_NOTE_LOCAL_ID, noteLocalId); intent.putExtra(EXT_NOTE_LOCAL_ID, noteLocalId);
intent.putExtra(EXT_IS_NEW_NOTE, isNewNote);
return intent; return intent;
} }
@@ -97,7 +101,7 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_save: case R.id.action_save:
checkChangeOrDirty() filterUnchanged()
.doOnCompleted(new Action0() { .doOnCompleted(new Action0() {
@Override @Override
public void call() { public void call() {
@@ -106,15 +110,15 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi
}) })
.subscribe(new Action1<Note>() { .subscribe(new Action1<Note>() {
@Override @Override
public void call(Note noteInfo) { public void call(Note note) {
saveAsDraft(noteInfo); saveAsDraft(note);
setResult(RESULT_OK); setResult(RESULT_OK);
if (NetworkUtils.isNetworkAvailable(NoteEditActivity.this)) { if (NetworkUtils.isNetworkAvailable(NoteEditActivity.this)) {
boolean isSucceed = NoteService.updateNote(AppDataBase.getNoteByLocalId(mModified.getId())); boolean isSucceed = NoteService.updateNote(AppDataBase.getNoteByLocalId(note.getId()));
if (isSucceed) { if (isSucceed) {
Note note = AppDataBase.getNoteByLocalId(mModified.getId()); Note localNote = AppDataBase.getNoteByLocalId(note.getId());
note.setIsDirty(false); localNote.setIsDirty(false);
note.save(); localNote.save();
} else { } else {
ToastUtils.show(NoteEditActivity.this, R.string.save_note_failed); 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) { if (mPager.getCurrentItem() > FRAG_EDITOR) {
mPager.setCurrentItem(FRAG_EDITOR); mPager.setCurrentItem(FRAG_EDITOR);
} else { } else {
checkChangeOrDirty() filterUnchanged()
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.doOnCompleted(new Action0() { .doOnCompleted(new Action0() {
@Override @Override
@@ -148,26 +152,30 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi
@Override @Override
public void call(Note note) { public void call(Note note) {
setResult(RESULT_OK); setResult(RESULT_OK);
if (!isNewNote(note) || Log.i(TAG, note.toString());
!(TextUtils.isEmpty(note.getTitle()) || TextUtils.isEmpty(note.getContent()))) {
saveAsDraft(note); if (mIsNewNote && isTitleContentEmpty(note)) {
} else {
Log.i(TAG, "remove empty note, id=" + note.getId()); Log.i(TAG, "remove empty note, id=" + note.getId());
AppDataBase.deleteNoteByLocalId(note.getId()); AppDataBase.deleteNoteByLocalId(note.getId());
} else {
saveAsDraft(note);
} }
} }
}); });
} }
} }
private Observable<Note> checkChangeOrDirty() { private Observable<Note> filterUnchanged() {
return Observable.create( return Observable.create(
new Observable.OnSubscribe<Note>() { new Observable.OnSubscribe<Note>() {
@Override @Override
public void call(Subscriber<? super Note> subscriber) { public void call(Subscriber<? super Note> subscriber) {
if (!subscriber.isUnsubscribed()) { if (!subscriber.isUnsubscribed()) {
updateNote(); updateNote();
if (mModified.isDirty() || mModified.hasChanges(mOriginal) || isNewNote(mModified)) { if (mModified.isDirty()
|| mModified.hasChanges(mOriginal)
|| isLocalNote(mModified)
|| isTitleContentEmpty(mModified)) {
subscriber.onNext(mModified); subscriber.onNext(mModified);
} }
subscriber.onCompleted(); subscriber.onCompleted();
@@ -177,6 +185,14 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi
.subscribeOn(Schedulers.io()); .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() { private void updateNote() {
String title = mEditorFragment.getTitle(); String title = mEditorFragment.getTitle();
String content = mEditorFragment.getContent(); String content = mEditorFragment.getContent();
@@ -196,13 +212,14 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi
noteFromDb.setTags(note.getTags()); noteFromDb.setTags(note.getTags());
noteFromDb.setIsPublicBlog(note.isPublicBlog()); noteFromDb.setIsPublicBlog(note.isPublicBlog());
noteFromDb.setIsDirty(true); noteFromDb.setIsDirty(true);
long updateTime = System.currentTimeMillis();
noteFromDb.setUpdatedTimeVal(updateTime);
if (mIsNewNote) {
noteFromDb.setCreatedTimeVal(updateTime);
}
noteFromDb.update(); noteFromDb.update();
} }
private boolean isNewNote(Note note) {
return TextUtils.isEmpty(note.getNoteId());
}
@Override @Override
public Uri createImage(String filePath) { public Uri createImage(String filePath) {
return NoteFileService.createImageFile(mModified.getId(), filePath); return NoteFileService.createImageFile(mModified.getId(), filePath);