From 2888023eb1789e273bfed3bbcd6f22ad9902e1f3 Mon Sep 17 00:00:00 2001 From: inorichi Date: Wed, 7 Oct 2015 00:26:53 +0200 Subject: [PATCH] Insert or remove method (meh) --- .../data/managers/ChapterManager.java | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java index ab37c4dc4..00f5ca1de 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java @@ -53,28 +53,27 @@ public class ChapterManager extends BaseManager { // Add new chapters or delete if the source deletes them public Observable insertOrRemove(Manga manga, List chapters) { // I don't know a better approach + // TODO Fix this method return Observable.create(subscriber -> { - List dbGet = prepareGet(manga).executeAsBlocking(); + List dbChapters = prepareGet(manga).executeAsBlocking(); - Observable.just(dbGet) - .doOnNext(dbChapters -> { - Observable.from(chapters) - .filter(c -> !dbChapters.contains(c)) - .toList() - .subscribe(newChapters -> { - if (newChapters.size() > 0) - insert(newChapters).subscribe(); - }); - }) - .flatMap(Observable::from) + Observable> newChaptersObs = + Observable.from(chapters) + .filter(c -> !dbChapters.contains(c)) + .toList(); + + Observable> deletedChaptersObs = + Observable.from(dbChapters) .filter(c -> !chapters.contains(c)) - .toList() - .subscribe(removedChapters -> { - if (removedChapters.size() > 0) - delete(removedChapters).subscribe(); - subscriber.onCompleted(); - }); + .toList(); + Observable.zip(newChaptersObs, deletedChaptersObs, + (newChapters, deletedChapters) -> { + insert(newChapters).subscribe(); + delete(deletedChapters).subscribe(); + subscriber.onCompleted(); + return null; + }).subscribe(); }); }