Update insertOrRemoveChapters method

This commit is contained in:
inorichi 2015-10-08 14:28:57 +02:00
parent 67a2e99cc1
commit 74ccd8bd24

View file

@ -62,22 +62,28 @@ public class ChapterManagerImpl extends BaseManager implements ChapterManager {
return Observable.create(subscriber -> { return Observable.create(subscriber -> {
List<Chapter> dbChapters = prepareGet(manga).executeAsBlocking(); List<Chapter> dbChapters = prepareGet(manga).executeAsBlocking();
Observable<List<Chapter>> newChaptersObs = Observable<Integer> newChaptersObs =
Observable.from(chapters) Observable.from(chapters)
.filter(c -> !dbChapters.contains(c)) .filter(c -> !dbChapters.contains(c))
.toList(); .toList()
.flatMap(this::insertChapters)
.map(PutResults::numberOfInserts);
Observable<List<Chapter>> deletedChaptersObs = Observable<Integer> deletedChaptersObs =
Observable.from(dbChapters) Observable.from(dbChapters)
.filter(c -> !chapters.contains(c)) .filter(c -> !chapters.contains(c))
.toList(); .toList()
.flatMap(this::deleteChapters)
.map(result -> result.results().size());
Observable.zip(newChaptersObs, deletedChaptersObs, Observable.zip(newChaptersObs, deletedChaptersObs,
(newChapters, deletedChapters) -> { (newChapters, deletedChapters) -> {
insertChapters(newChapters).subscribe(); ArrayList<Integer> results = new ArrayList<>();
deleteChapters(deletedChapters).subscribe(); results.add(newChapters);
results.add(deletedChapters);
subscriber.onNext(results);
subscriber.onCompleted(); subscriber.onCompleted();
return null; return results;
}).subscribe(); }).subscribe();
}); });
} }