From ea8ded549b454d52fea0c8a92248a163a255331e Mon Sep 17 00:00:00 2001 From: inorichi Date: Thu, 8 Oct 2015 22:48:50 +0200 Subject: [PATCH] Insert or remove chapters with one subscriber --- .../sqlite/operations/post/PostResult.java | 39 +++++++++++++++++ .../data/managers/ChapterManagerImpl.java | 43 ++++++++----------- 2 files changed, 58 insertions(+), 24 deletions(-) create mode 100644 app/src/main/java/com/pushtorefresh/storio/sqlite/operations/post/PostResult.java diff --git a/app/src/main/java/com/pushtorefresh/storio/sqlite/operations/post/PostResult.java b/app/src/main/java/com/pushtorefresh/storio/sqlite/operations/post/PostResult.java new file mode 100644 index 000000000..04e2fa122 --- /dev/null +++ b/app/src/main/java/com/pushtorefresh/storio/sqlite/operations/post/PostResult.java @@ -0,0 +1,39 @@ +package com.pushtorefresh.storio.sqlite.operations.post; + +import android.support.annotation.Nullable; + +/** + * Created by len on 08/10/2015. + */ +public class PostResult { + + @Nullable + private final Integer numberOfRowsUpdated; + + @Nullable + private final Integer numberOfRowsInserted; + + @Nullable + private final Integer numberOfRowsDeleted; + + public PostResult(Integer numberOfRowsUpdated, Integer numberOfRowsInserted, Integer numberOfRowsDeleted) { + this.numberOfRowsUpdated = numberOfRowsUpdated; + this.numberOfRowsInserted = numberOfRowsInserted; + this.numberOfRowsDeleted = numberOfRowsDeleted; + } + + @Nullable + public Integer getNumberOfRowsUpdated() { + return numberOfRowsUpdated; + } + + @Nullable + public Integer getNumberOfRowsInserted() { + return numberOfRowsInserted; + } + + @Nullable + public Integer getNumberOfRowsDeleted() { + return numberOfRowsDeleted; + } +} diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java index 6c270a507..b37374d24 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java @@ -4,11 +4,11 @@ import com.pushtorefresh.storio.sqlite.StorIOSQLite; import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult; import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResults; import com.pushtorefresh.storio.sqlite.operations.get.PreparedGetListOfObjects; +import com.pushtorefresh.storio.sqlite.operations.post.PostResult; import com.pushtorefresh.storio.sqlite.operations.put.PutResult; import com.pushtorefresh.storio.sqlite.operations.put.PutResults; import com.pushtorefresh.storio.sqlite.queries.Query; -import java.util.ArrayList; import java.util.List; import eu.kanade.mangafeed.data.models.Chapter; @@ -22,7 +22,7 @@ public class ChapterManagerImpl extends BaseManager implements ChapterManager { super(db); } - private PreparedGetListOfObjects prepareGet(Manga manga) { + private PreparedGetListOfObjects prepareGetChapters(Manga manga) { return db.get() .listOfObjects(Chapter.class) .withQuery(Query.builder() @@ -35,7 +35,7 @@ public class ChapterManagerImpl extends BaseManager implements ChapterManager { @Override public Observable> getChapters(Manga manga) { - return prepareGet(manga).createObservable(); + return prepareGetChapters(manga).createObservable(); } @Override @@ -56,36 +56,31 @@ public class ChapterManagerImpl extends BaseManager implements ChapterManager { // Add new chapters or delete if the source deletes them @Override - public Observable insertOrRemoveChapters(Manga manga, List chapters) { - // I don't know a better approach - // TODO Fix this method - return Observable.create(subscriber -> { - List dbChapters = prepareGet(manga).executeAsBlocking(); + public Observable insertOrRemoveChapters(Manga manga, List chapters) { + Observable> chapterList = Observable.create(subscriber -> { + subscriber.onNext(prepareGetChapters(manga).executeAsBlocking()); + subscriber.onCompleted(); + }); - Observable newChaptersObs = - Observable.from(chapters) + Observable newChaptersObs = + chapterList + .flatMap(dbChapters -> Observable.from(chapters) .filter(c -> !dbChapters.contains(c)) .toList() .flatMap(this::insertChapters) - .map(PutResults::numberOfInserts); + .map(PutResults::numberOfInserts)); - Observable deletedChaptersObs = - Observable.from(dbChapters) + Observable deletedChaptersObs = + chapterList + .flatMap(dbChapters -> Observable.from(dbChapters) .filter(c -> !chapters.contains(c)) .toList() .flatMap(this::deleteChapters) - .map(result -> result.results().size()); + .map( d -> d.results().size() )); - Observable.zip(newChaptersObs, deletedChaptersObs, - (newChapters, deletedChapters) -> { - ArrayList results = new ArrayList<>(); - results.add(newChapters); - results.add(deletedChapters); - subscriber.onNext(results); - subscriber.onCompleted(); - return results; - }).subscribe(); - }); + return Observable.zip(newChaptersObs, deletedChaptersObs, + (insertions, deletions) -> new PostResult(0, insertions, deletions) + ); } @Override