From 3deb2529ad7576d703b9217cd948cd226c8464ce Mon Sep 17 00:00:00 2001 From: houxg Date: Tue, 29 Nov 2016 14:21:42 +0800 Subject: [PATCH] improve update procedure, fix some crash issue --- .../houxg/leamonax/service/NoteService.java | 24 +++--- .../leamonax/ui/NotePreviewActivity.java | 61 +++++++--------- .../leamonax/ui/edit/NoteEditActivity.java | 73 ++++++++++++++----- .../houxg/leamonax/utils/DialogDisplayer.java | 25 +++++++ .../houxg/leamonax/utils/NetworkUtils.java | 15 ++++ .../houxg/leamonax/utils/RetrofitUtils.java | 7 +- 6 files changed, 136 insertions(+), 69 deletions(-) create mode 100644 app/src/main/java/org/houxg/leamonax/utils/DialogDisplayer.java 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 6f8334e..be15d3c 100644 --- a/app/src/main/java/org/houxg/leamonax/service/NoteService.java +++ b/app/src/main/java/org/houxg/leamonax/service/NoteService.java @@ -205,19 +205,18 @@ public class NoteService { }, noteLocalId); } - public static boolean updateNote(final Note modifiedNote) { + public static void updateNote(final long noteLocalId) { + Note localNote = AppDataBase.getNoteByLocalId(noteLocalId); + NoteService.updateNote(localNote); + } + + private static void updateNote(final Note modifiedNote) throws IllegalStateException { Note note; if (modifiedNote.getUsn() == 0) { - note = RetrofitUtils.excute(addNote(modifiedNote)); + note = RetrofitUtils.excuteWithException(addNote(modifiedNote)); } else { - Note remoteNote = RetrofitUtils.excute(getNoteByServerId(modifiedNote.getNoteId())); - if (remoteNote == null) { - return false; - } - note = RetrofitUtils.excute(updateNote(remoteNote, modifiedNote)); - } - if (note == null) { - return false; + Note remoteNote = RetrofitUtils.excuteWithException(getNoteByServerId(modifiedNote.getNoteId())); + note = RetrofitUtils.excuteWithException(updateNote(remoteNote, modifiedNote)); } if (note.isOk()) { note.setId(modifiedNote.getId()); @@ -228,9 +227,8 @@ public class NoteService { note.save(); updateUsnIfNeed(note.getUsn()); } else { - throw new IllegalArgumentException(note.getMsg()); + throw new IllegalStateException(note.getMsg()); } - return true; } private static String convertToServerImageLinkForMD(String noteContent) { @@ -309,7 +307,7 @@ public class NoteService { List fileBodies = handleFileBodies(note, requestBodyMap); return ApiProvider.getInstance().getNoteApi().add(requestBodyMap, fileBodies); } - + private static Call updateNote(Note original, Note modified) { Map requestBodyMap = generateCommonBodyMap(modified); requestBodyMap.put("NoteId", createPartFromString(original.getNoteId())); 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 50e0391..b62e1b6 100644 --- a/app/src/main/java/org/houxg/leamonax/ui/NotePreviewActivity.java +++ b/app/src/main/java/org/houxg/leamonax/ui/NotePreviewActivity.java @@ -16,6 +16,7 @@ import org.houxg.leamonax.model.Note; import org.houxg.leamonax.service.NoteService; import org.houxg.leamonax.ui.edit.EditorFragment; import org.houxg.leamonax.ui.edit.NoteEditActivity; +import org.houxg.leamonax.utils.DialogDisplayer; import org.houxg.leamonax.utils.NetworkUtils; import org.houxg.leamonax.utils.ToastUtils; @@ -23,6 +24,7 @@ import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import rx.Observable; +import rx.Observer; import rx.Subscriber; import rx.android.schedulers.AndroidSchedulers; import rx.functions.Action0; @@ -95,16 +97,14 @@ public class NotePreviewActivity extends BaseActivity implements EditorFragment. @OnClick(R.id.tv_save) void push() { - if (!NetworkUtils.isNetworkAvailable(this)) { - ToastUtils.showNetworkUnavailable(this); - return; - } Observable.create( - new Observable.OnSubscribe() { + new Observable.OnSubscribe() { @Override - public void call(Subscriber subscriber) { + public void call(Subscriber subscriber) { if (!subscriber.isUnsubscribed()) { - subscriber.onNext(NoteService.updateNote(AppDataBase.getNoteByLocalId(mNote.getId()))); + NetworkUtils.checkNetwork(); + NoteService.updateNote(mNote.getId()); + subscriber.onNext(mNote.getId()); subscriber.onCompleted(); } } @@ -114,26 +114,27 @@ public class NotePreviewActivity extends BaseActivity implements EditorFragment. .doOnSubscribe(new Action0() { @Override public void call() { - showProgress(getString(R.string.saving_note)); + DialogDisplayer.showProgress(NotePreviewActivity.this, R.string.saving_note); } }) - .doOnCompleted(new Action0() { + .subscribe(new Observer() { @Override - public void call() { - dismissProgress(); + public void onCompleted() { + DialogDisplayer.dismissProgress(); } - }) - .subscribe(new Action1() { + @Override - public void call(Boolean isSucceed) { - if (isSucceed) { - mNote = AppDataBase.getNoteByLocalId(mNote.getId()); - mNote.setIsDirty(false); - mNote.save(); - refresh(); - } else { - ToastUtils.show(NotePreviewActivity.this, R.string.save_note_failed); - } + public void onError(Throwable e) { + DialogDisplayer.dismissProgress(); + ToastUtils.show(NotePreviewActivity.this, e.getMessage()); + } + + @Override + public void onNext(Long aLong) { + mNote = AppDataBase.getNoteByLocalId(mNote.getId()); + mNote.setIsDirty(false); + mNote.save(); + refresh(); } }); } @@ -159,13 +160,13 @@ public class NotePreviewActivity extends BaseActivity implements EditorFragment. .doOnSubscribe(new Action0() { @Override public void call() { - showProgress(getString(R.string.reverting)); + DialogDisplayer.showProgress(NotePreviewActivity.this, R.string.reverting); } }) .doOnCompleted(new Action0() { @Override public void call() { - dismissProgress(); + DialogDisplayer.dismissProgress(); } }) .subscribe(new Action1() { @@ -180,18 +181,6 @@ public class NotePreviewActivity extends BaseActivity implements EditorFragment. } - private void showProgress(String message) { - dismissProgress(); - mProgressDialog = ProgressDialog.show(NotePreviewActivity.this, "", message, false); - } - - private void dismissProgress() { - if (mProgressDialog != null && mProgressDialog.isShowing()) { - mProgressDialog.dismiss(); - mProgressDialog = null; - } - } - @Override public Uri createImage(String filePath) { return null; 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 87541c9..60315e2 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 @@ -20,15 +20,18 @@ import org.houxg.leamonax.model.Note; import org.houxg.leamonax.service.NoteFileService; import org.houxg.leamonax.service.NoteService; import org.houxg.leamonax.ui.BaseActivity; +import org.houxg.leamonax.utils.DialogDisplayer; import org.houxg.leamonax.utils.NetworkUtils; import org.houxg.leamonax.utils.ToastUtils; import org.houxg.leamonax.widget.LeaViewPager; import rx.Observable; +import rx.Observer; import rx.Subscriber; import rx.android.schedulers.AndroidSchedulers; import rx.functions.Action0; import rx.functions.Action1; +import rx.functions.Func1; import rx.schedulers.Schedulers; //TODO: hide action bar @@ -102,30 +105,50 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi switch (item.getItemId()) { case R.id.action_save: filterUnchanged() - .doOnCompleted(new Action0() { - @Override - public void call() { - finish(); - } - }) - .subscribe(new Action1() { + .doOnNext(new Action1() { @Override public void call(Note note) { saveAsDraft(note); setResult(RESULT_OK); - if (NetworkUtils.isNetworkAvailable(NoteEditActivity.this)) { - boolean isSucceed = NoteService.updateNote(AppDataBase.getNoteByLocalId(note.getId())); - if (isSucceed) { - Note localNote = AppDataBase.getNoteByLocalId(note.getId()); - localNote.setIsDirty(false); - localNote.save(); - } else { - ToastUtils.show(NoteEditActivity.this, R.string.save_note_failed); - } - } else { - ToastUtils.showNetworkUnavailable(NoteEditActivity.this); + NetworkUtils.checkNetwork(); + } + }) + .flatMap(new Func1>() { + @Override + public Observable call(Note note) { + return uploadToServer(note.getId()); + } + }) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .doOnSubscribe(new Action0() { + @Override + public void call() { + DialogDisplayer.showProgress(NoteEditActivity.this, R.string.saving_note); + } + }) + .subscribe(new Observer() { + @Override + public void onCompleted() { + DialogDisplayer.dismissProgress(); + finish(); + } + + @Override + public void onError(Throwable e) { + DialogDisplayer.dismissProgress(); + ToastUtils.show(NoteEditActivity.this, e.getMessage()); + if (e instanceof NetworkUtils.NetworkUnavailableException) { + finish(); } } + + @Override + public void onNext(Long noteLocalId) { + Note localNote = AppDataBase.getNoteByLocalId(noteLocalId); + localNote.setIsDirty(false); + localNote.save(); + } }); return true; case R.id.action_settings: @@ -135,6 +158,20 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi return super.onOptionsItemSelected(item); } + private Observable uploadToServer(final long noteLocalId) { + return Observable.create( + new Observable.OnSubscribe() { + @Override + public void call(Subscriber subscriber) { + if (!subscriber.isUnsubscribed()) { + NoteService.updateNote(noteLocalId); + subscriber.onNext(noteLocalId); + subscriber.onCompleted(); + } + } + }); + } + @Override public void onBackPressed() { if (mPager.getCurrentItem() > FRAG_EDITOR) { diff --git a/app/src/main/java/org/houxg/leamonax/utils/DialogDisplayer.java b/app/src/main/java/org/houxg/leamonax/utils/DialogDisplayer.java new file mode 100644 index 0000000..afbc360 --- /dev/null +++ b/app/src/main/java/org/houxg/leamonax/utils/DialogDisplayer.java @@ -0,0 +1,25 @@ +package org.houxg.leamonax.utils; + + +import android.app.ProgressDialog; +import android.content.Context; + +public class DialogDisplayer { + private static ProgressDialog mProgressDialog; + + public static void showProgress(Context context, String message) { + dismissProgress(); + mProgressDialog = ProgressDialog.show(context, "", message, false); + } + + public static void showProgress(Context context, int messageResId) { + showProgress(context, context.getString(messageResId)); + } + + public static void dismissProgress() { + if (mProgressDialog != null && mProgressDialog.isShowing()) { + mProgressDialog.dismiss(); + mProgressDialog = null; + } + } +} diff --git a/app/src/main/java/org/houxg/leamonax/utils/NetworkUtils.java b/app/src/main/java/org/houxg/leamonax/utils/NetworkUtils.java index fe877fa..c8cf277 100644 --- a/app/src/main/java/org/houxg/leamonax/utils/NetworkUtils.java +++ b/app/src/main/java/org/houxg/leamonax/utils/NetworkUtils.java @@ -5,6 +5,9 @@ import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; +import org.houxg.leamonax.Leamonax; +import org.houxg.leamonax.R; + public class NetworkUtils { private static NetworkInfo getActiveNetworkInfo(Context context) { @@ -23,4 +26,16 @@ public class NetworkUtils { NetworkInfo info = getActiveNetworkInfo(context); return (info != null && info.isConnected()); } + + public static void checkNetwork() throws NetworkUnavailableException { + if (!isNetworkAvailable(Leamonax.getContext())) { + throw new NetworkUnavailableException(); + } + } + + public static class NetworkUnavailableException extends IllegalStateException { + public NetworkUnavailableException() { + super(Leamonax.getContext().getResources().getString(R.string.network_is_unavailable)); + } + } } diff --git a/app/src/main/java/org/houxg/leamonax/utils/RetrofitUtils.java b/app/src/main/java/org/houxg/leamonax/utils/RetrofitUtils.java index 78e7803..2a015cb 100644 --- a/app/src/main/java/org/houxg/leamonax/utils/RetrofitUtils.java +++ b/app/src/main/java/org/houxg/leamonax/utils/RetrofitUtils.java @@ -1,6 +1,9 @@ package org.houxg.leamonax.utils; +import org.houxg.leamonax.Leamonax; +import org.houxg.leamonax.R; + import java.io.IOException; import retrofit2.Call; @@ -50,10 +53,10 @@ public class RetrofitUtils { if (response.isSuccessful()) { return response.body(); } else { - throw new IllegalStateException("response not successful"); + throw new IllegalStateException(Leamonax.getContext().getString(R.string.network_error)); } } catch (IOException e) { - throw new IllegalStateException(e.getCause()); + throw new IllegalStateException(Leamonax.getContext().getString(R.string.network_error)); } } }