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

View File

@@ -46,6 +46,7 @@ import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
@@ -155,8 +156,17 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
Observable.from(notes)
.flatMap(new Func1<Note, rx.Observable<Note>>() {
@Override
public rx.Observable<Note> call(Note note) {
return NoteService.deleteNote(note);
public rx.Observable<Note> call(final Note 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())

View File

@@ -37,6 +37,7 @@ import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
@@ -162,8 +163,17 @@ public class SearchActivity extends BaseActivity implements NoteAdapter.NoteAdap
Observable.from(notes)
.flatMap(new Func1<Note, Observable<Note>>() {
@Override
public rx.Observable<Note> call(Note note) {
return NoteService.deleteNote(note);
public rx.Observable<Note> call(final Note 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())