mirror of
https://github.com/leanote/leanote-android.git
synced 2026-01-14 07:00:23 +08:00
improve update procedure, fix some crash issue
This commit is contained in:
@@ -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<MultipartBody.Part> fileBodies = handleFileBodies(note, requestBodyMap);
|
||||
return ApiProvider.getInstance().getNoteApi().add(requestBodyMap, fileBodies);
|
||||
}
|
||||
|
||||
|
||||
private static Call<Note> updateNote(Note original, Note modified) {
|
||||
Map<String, RequestBody> requestBodyMap = generateCommonBodyMap(modified);
|
||||
requestBodyMap.put("NoteId", createPartFromString(original.getNoteId()));
|
||||
|
||||
@@ -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<Boolean>() {
|
||||
new Observable.OnSubscribe<Long>() {
|
||||
@Override
|
||||
public void call(Subscriber<? super Boolean> subscriber) {
|
||||
public void call(Subscriber<? super Long> 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<Long>() {
|
||||
@Override
|
||||
public void call() {
|
||||
dismissProgress();
|
||||
public void onCompleted() {
|
||||
DialogDisplayer.dismissProgress();
|
||||
}
|
||||
})
|
||||
.subscribe(new Action1<Boolean>() {
|
||||
|
||||
@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<Boolean>() {
|
||||
@@ -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;
|
||||
|
||||
@@ -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<Note>() {
|
||||
.doOnNext(new Action1<Note>() {
|
||||
@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<Note, Observable<Long>>() {
|
||||
@Override
|
||||
public Observable<Long> 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<Long>() {
|
||||
@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<Long> uploadToServer(final long noteLocalId) {
|
||||
return Observable.create(
|
||||
new Observable.OnSubscribe<Long>() {
|
||||
@Override
|
||||
public void call(Subscriber<? super Long> subscriber) {
|
||||
if (!subscriber.isUnsubscribed()) {
|
||||
NoteService.updateNote(noteLocalId);
|
||||
subscriber.onNext(noteLocalId);
|
||||
subscriber.onCompleted();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
if (mPager.getCurrentItem() > FRAG_EDITOR) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user