Insert or remove method (meh)

This commit is contained in:
inorichi 2015-10-07 00:26:53 +02:00
parent a3463addc3
commit 2888023eb1

View File

@ -53,28 +53,27 @@ public class ChapterManager extends BaseManager {
// Add new chapters or delete if the source deletes them // Add new chapters or delete if the source deletes them
public Observable insertOrRemove(Manga manga, List<Chapter> chapters) { public Observable insertOrRemove(Manga manga, List<Chapter> chapters) {
// I don't know a better approach // I don't know a better approach
// TODO Fix this method
return Observable.create(subscriber -> { return Observable.create(subscriber -> {
List<Chapter> dbGet = prepareGet(manga).executeAsBlocking(); List<Chapter> dbChapters = prepareGet(manga).executeAsBlocking();
Observable.just(dbGet) Observable<List<Chapter>> newChaptersObs =
.doOnNext(dbChapters -> { Observable.from(chapters)
Observable.from(chapters) .filter(c -> !dbChapters.contains(c))
.filter(c -> !dbChapters.contains(c)) .toList();
.toList()
.subscribe(newChapters -> { Observable<List<Chapter>> deletedChaptersObs =
if (newChapters.size() > 0) Observable.from(dbChapters)
insert(newChapters).subscribe();
});
})
.flatMap(Observable::from)
.filter(c -> !chapters.contains(c)) .filter(c -> !chapters.contains(c))
.toList() .toList();
.subscribe(removedChapters -> {
if (removedChapters.size() > 0)
delete(removedChapters).subscribe();
subscriber.onCompleted();
});
Observable.zip(newChaptersObs, deletedChaptersObs,
(newChapters, deletedChapters) -> {
insert(newChapters).subscribe();
delete(deletedChapters).subscribe();
subscriber.onCompleted();
return null;
}).subscribe();
}); });
} }