code refactoring

This commit is contained in:
houxg
2017-02-21 16:13:35 +08:00
parent b00ccddbec
commit 399cf2a654
3 changed files with 40 additions and 29 deletions

View File

@@ -388,33 +388,24 @@ public class NoteService {
return localIds; return localIds;
} }
//TODO:change to synchonous public static void deleteNote(Note note) {
public static Observable<Note> deleteNote(final Note note) { if (note.isLocalNote()) {
return Observable.create( AppDataBase.deleteNoteByLocalId(note.getId());
new Observable.OnSubscribe<Note>() { } else {
@Override Call<UpdateRe> call = ApiProvider.getInstance().getNoteApi().delete(note.getNoteId(), note.getUsn());
public void call(Subscriber<? super Note> subscriber) { UpdateRe response = RetrofitUtils.excuteWithException(call);
if (!subscriber.isUnsubscribed()) { if (response.isOk()) {
if (TextUtils.isEmpty(note.getNoteId())) { AppDataBase.deleteNoteByLocalId(note.getId());
AppDataBase.deleteNoteByLocalId(note.getId()); updateNoteUsnIfNeed(response.getUsn());
} else { } else {
UpdateRe response = RetrofitUtils.excuteWithException( throw new IllegalStateException(response.getMsg());
ApiProvider.getInstance().getNoteApi().delete(note.getNoteId(), note.getUsn())); }
if (response.isOk()) { }
AppDataBase.deleteNoteByLocalId(note.getId());
updateNoteUsnIfNeed(response.getUsn());
} else {
throw new IllegalStateException(response.getMsg());
}
}
subscriber.onNext(note);
subscriber.onCompleted();
}
}
});
} }
//TODO:delete this method /**
* 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 = AccountService.getCurrent();
if (newUsn - account.getNoteUsn() == 1) { if (newUsn - account.getNoteUsn() == 1) {

View File

@@ -46,6 +46,7 @@ import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import rx.Observable; import rx.Observable;
import rx.Observer; import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1; import rx.functions.Func1;
import rx.schedulers.Schedulers; import rx.schedulers.Schedulers;
@@ -155,8 +156,17 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
Observable.from(notes) Observable.from(notes)
.flatMap(new Func1<Note, rx.Observable<Note>>() { .flatMap(new Func1<Note, rx.Observable<Note>>() {
@Override @Override
public rx.Observable<Note> call(Note note) { public rx.Observable<Note> call(final Note note) {
return NoteService.deleteNote(note); return Observable.create(new Observable.OnSubscribe<Note>() {
@Override
public void call(Subscriber<? super Note> subscriber) {
if (!subscriber.isUnsubscribed()) {
NoteService.deleteNote(note);
subscriber.onNext(note);
subscriber.onCompleted();
}
}
});
} }
}) })
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())

View File

@@ -37,6 +37,7 @@ import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import rx.Observable; import rx.Observable;
import rx.Observer; import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1; import rx.functions.Func1;
import rx.schedulers.Schedulers; import rx.schedulers.Schedulers;
@@ -162,8 +163,17 @@ public class SearchActivity extends BaseActivity implements NoteAdapter.NoteAdap
Observable.from(notes) Observable.from(notes)
.flatMap(new Func1<Note, Observable<Note>>() { .flatMap(new Func1<Note, Observable<Note>>() {
@Override @Override
public rx.Observable<Note> call(Note note) { public rx.Observable<Note> call(final Note note) {
return NoteService.deleteNote(note); return Observable.create(new Observable.OnSubscribe<Note>() {
@Override
public void call(Subscriber<? super Note> subscriber) {
if (!subscriber.isUnsubscribed()) {
NoteService.deleteNote(note);
subscriber.onNext(note);
subscriber.onCompleted();
}
}
});
} }
}) })
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())