From 7c37262a9f44b5ad733b921d466fa7f132627907 Mon Sep 17 00:00:00 2001 From: inorichi Date: Sun, 8 Nov 2015 13:50:49 +0100 Subject: [PATCH] Merge database operations in a single class, deleting duplicated methods (as observable or blocking) and let the consumer decide the option --- .../data/helpers/DatabaseHelper.java | 258 +++++++++++------- .../mangafeed/data/managers/BaseManager.java | 12 - .../data/managers/ChapterManager.java | 33 --- .../data/managers/ChapterManagerImpl.java | 125 --------- .../mangafeed/data/managers/MangaManager.java | 37 --- .../data/managers/MangaManagerImpl.java | 147 ---------- .../data/services/LibraryUpdateService.java | 2 +- .../presenter/CataloguePresenter.java | 7 +- .../mangafeed/presenter/LibraryPresenter.java | 4 +- .../presenter/MangaChaptersPresenter.java | 4 +- .../presenter/MangaDetailPresenter.java | 2 +- .../presenter/MangaInfoPresenter.java | 2 +- .../mangafeed/presenter/ReaderPresenter.java | 2 +- 13 files changed, 177 insertions(+), 458 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/mangafeed/data/managers/BaseManager.java delete mode 100644 app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java delete mode 100644 app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java delete mode 100644 app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java delete mode 100644 app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManagerImpl.java diff --git a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java index 76ecfadea8..fdcb1998a3 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java @@ -5,17 +5,17 @@ import android.content.Context; import com.pushtorefresh.storio.sqlite.SQLiteTypeMapping; import com.pushtorefresh.storio.sqlite.StorIOSQLite; import com.pushtorefresh.storio.sqlite.impl.DefaultStorIOSQLite; -import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult; -import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResults; -import com.pushtorefresh.storio.sqlite.operations.put.PutResult; +import com.pushtorefresh.storio.sqlite.operations.delete.PreparedDeleteCollectionOfObjects; +import com.pushtorefresh.storio.sqlite.operations.delete.PreparedDeleteObject; +import com.pushtorefresh.storio.sqlite.operations.get.PreparedGetListOfObjects; +import com.pushtorefresh.storio.sqlite.operations.put.PreparedPutCollectionOfObjects; +import com.pushtorefresh.storio.sqlite.operations.put.PreparedPutObject; import com.pushtorefresh.storio.sqlite.operations.put.PutResults; +import com.pushtorefresh.storio.sqlite.queries.Query; +import com.pushtorefresh.storio.sqlite.queries.RawQuery; import java.util.List; -import eu.kanade.mangafeed.data.managers.ChapterManager; -import eu.kanade.mangafeed.data.managers.ChapterManagerImpl; -import eu.kanade.mangafeed.data.managers.MangaManager; -import eu.kanade.mangafeed.data.managers.MangaManagerImpl; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.ChapterStorIOSQLiteDeleteResolver; import eu.kanade.mangafeed.data.models.ChapterStorIOSQLiteGetResolver; @@ -24,18 +24,18 @@ import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.data.models.MangaStorIOSQLiteDeleteResolver; import eu.kanade.mangafeed.data.models.MangaStorIOSQLitePutResolver; import eu.kanade.mangafeed.data.resolvers.MangaWithUnreadGetResolver; +import eu.kanade.mangafeed.data.tables.ChaptersTable; +import eu.kanade.mangafeed.data.tables.MangasTable; import eu.kanade.mangafeed.util.PostResult; import rx.Observable; -public class DatabaseHelper implements MangaManager, ChapterManager { +public class DatabaseHelper { - private StorIOSQLite mDb; - private MangaManagerImpl mMangaManager; - private ChapterManagerImpl mChapterManager; + private StorIOSQLite db; public DatabaseHelper(Context context) { - mDb = DefaultStorIOSQLite.builder() + db = DefaultStorIOSQLite.builder() .sqliteOpenHelper(new DbOpenHelper(context)) .addTypeMapping(Manga.class, SQLiteTypeMapping.builder() .putResolver(new MangaStorIOSQLitePutResolver()) @@ -48,103 +48,175 @@ public class DatabaseHelper implements MangaManager, ChapterManager { .deleteResolver(new ChapterStorIOSQLiteDeleteResolver()) .build()) .build(); - - mMangaManager = new MangaManagerImpl(mDb); - mChapterManager = new ChapterManagerImpl(mDb); } - @Override - public Observable> getChapters(Manga manga) { - return mChapterManager.getChapters(manga); + // Mangas related queries + + private final String favoriteMangasWithUnreadQuery = String.format( + "SELECT %1$s.*, COUNT(C.%4$s) AS %5$s FROM %1$s LEFT JOIN " + + "(SELECT %4$s FROM %2$s WHERE %6$s = 0) AS C ON %3$s = C.%4$s " + + "WHERE %7$s = 1 GROUP BY %3$s", + MangasTable.TABLE, + ChaptersTable.TABLE, + MangasTable.TABLE + "." + MangasTable.COLUMN_ID, + ChaptersTable.COLUMN_MANGA_ID, + MangasTable.COLUMN_UNREAD, + ChaptersTable.COLUMN_READ, + MangasTable.COLUMN_FAVORITE + ); + + public PreparedGetListOfObjects getMangas() { + return db.get() + .listOfObjects(Manga.class) + .withQuery(Query.builder() + .table(MangasTable.TABLE) + .build()) + .prepare(); } - @Override - public Observable> getChapters(long manga_id) { - return mChapterManager.getChapters(manga_id); + public PreparedGetListOfObjects getMangasWithUnread() { + return db.get() + .listOfObjects(Manga.class) + .withQuery(RawQuery.builder() + .query(favoriteMangasWithUnreadQuery) + .observesTables(MangasTable.TABLE, ChaptersTable.TABLE) + .build()) + .prepare(); } - @Override - public Observable insertChapter(Chapter chapter) { - return mChapterManager.insertChapter(chapter); + public PreparedGetListOfObjects getFavoriteMangas() { + return db.get() + .listOfObjects(Manga.class) + .withQuery(Query.builder() + .table(MangasTable.TABLE) + .where(MangasTable.COLUMN_FAVORITE + "=?") + .whereArgs(1) + .build()) + .prepare(); } - @Override - public Observable> insertChapters(List chapters) { - return mChapterManager.insertChapters(chapters); + public PreparedGetListOfObjects getManga(String url) { + return db.get() + .listOfObjects(Manga.class) + .withQuery(Query.builder() + .table(MangasTable.TABLE) + .where(MangasTable.COLUMN_URL + "=?") + .whereArgs(url) + .build()) + .prepare(); } - @Override - public PutResult insertChapterBlock(Chapter chapter) { - return mChapterManager.insertChapterBlock(chapter); + public PreparedGetListOfObjects getManga(long id) { + return db.get() + .listOfObjects(Manga.class) + .withQuery(Query.builder() + .table(MangasTable.TABLE) + .where(MangasTable.COLUMN_ID + "=?") + .whereArgs(id) + .build()) + .prepare(); } - @Override + public PreparedPutObject insertManga(Manga manga) { + return db.put() + .object(manga) + .prepare(); + } + + public PreparedPutCollectionOfObjects insertMangas(List mangas) { + return db.put() + .objects(mangas) + .prepare(); + } + + public PreparedDeleteObject deleteManga(Manga manga) { + return db.delete() + .object(manga) + .prepare(); + } + + public PreparedDeleteCollectionOfObjects deleteMangas(List mangas) { + return db.delete() + .objects(mangas) + .prepare(); + } + + + // Chapters related queries + + public PreparedGetListOfObjects getChapters(Manga manga) { + return db.get() + .listOfObjects(Chapter.class) + .withQuery(Query.builder() + .table(ChaptersTable.TABLE) + .where(ChaptersTable.COLUMN_MANGA_ID + "=?") + .whereArgs(manga.id) + .build()) + .prepare(); + } + + public PreparedGetListOfObjects getChapters(long manga_id) { + return db.get() + .listOfObjects(Chapter.class) + .withQuery(Query.builder() + .table(ChaptersTable.TABLE) + .where(ChaptersTable.COLUMN_MANGA_ID + "=?") + .whereArgs(manga_id) + .build()) + .prepare(); + } + + public PreparedPutObject insertChapter(Chapter chapter) { + return db.put() + .object(chapter) + .prepare(); + } + + public PreparedPutCollectionOfObjects insertChapters(List chapters) { + return db.put() + .objects(chapters) + .prepare(); + } + + // Add new chapters or delete if the source deletes them public Observable insertOrRemoveChapters(Manga manga, List chapters) { - return mChapterManager.insertOrRemoveChapters(manga, chapters); + for (Chapter chapter : chapters) { + chapter.manga_id = manga.id; + } + + Observable> chapterList = Observable.create(subscriber -> { + subscriber.onNext(getChapters(manga).executeAsBlocking()); + subscriber.onCompleted(); + }); + + Observable newChaptersObs = chapterList + .flatMap(dbChapters -> Observable.from(chapters) + .filter(c -> !dbChapters.contains(c)) + .toList() + .flatMap(newChapters -> insertChapters(newChapters).createObservable()) + .map(PutResults::numberOfInserts)); + + Observable deletedChaptersObs = chapterList + .flatMap(dbChapters -> Observable.from(dbChapters) + .filter(c -> !chapters.contains(c)) + .toList() + .flatMap(deletedChapters -> deleteChapters(deletedChapters).createObservable()) + .map( d -> d.results().size() )); + + return Observable.zip(newChaptersObs, deletedChaptersObs, + (insertions, deletions) -> new PostResult(0, insertions, deletions) + ); } - @Override - public Observable deleteChapter(Chapter chapter) { - return mChapterManager.deleteChapter(chapter); + public PreparedDeleteObject deleteChapter(Chapter chapter) { + return db.delete() + .object(chapter) + .prepare(); } - @Override - public Observable> deleteChapters(List chapters) { - return mChapterManager.deleteChapters(chapters); - } - - @Override - public Observable> getMangas() { - return mMangaManager.getMangas(); - } - - @Override - public Observable> getMangasWithUnread() { - return mMangaManager.getMangasWithUnread(); - } - - @Override - public Observable> getFavoriteMangas() { - return mMangaManager.getFavoriteMangas(); - } - - @Override - public Observable> getManga(String url) { - return mMangaManager.getManga(url); - } - - @Override - public Observable> getManga(long id) { - return mMangaManager.getManga(id); - } - - @Override - public Manga getMangaBlock(String url) { - return mMangaManager.getMangaBlock(url); - } - - @Override - public Observable insertManga(Manga manga) { - return mMangaManager.insertManga(manga); - } - - @Override - public Observable> insertMangas(List mangas) { - return mMangaManager.insertMangas(mangas); - } - - @Override - public PutResult insertMangaBlock(Manga manga) { - return mMangaManager.insertMangaBlock(manga); - } - - @Override - public Observable deleteManga(Manga manga) { - return mMangaManager.deleteManga(manga); - } - - @Override - public Observable> deleteMangas(List mangas) { - return mMangaManager.deleteMangas(mangas); + public PreparedDeleteCollectionOfObjects deleteChapters(List chapters) { + return db.delete() + .objects(chapters) + .prepare(); } } diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/BaseManager.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/BaseManager.java deleted file mode 100644 index bab0ca8817..0000000000 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/BaseManager.java +++ /dev/null @@ -1,12 +0,0 @@ -package eu.kanade.mangafeed.data.managers; - -import com.pushtorefresh.storio.sqlite.StorIOSQLite; - -public abstract class BaseManager { - - protected StorIOSQLite db; - - public BaseManager(StorIOSQLite db) { - this.db = db; - } -} 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 deleted file mode 100644 index 28a3881954..0000000000 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java +++ /dev/null @@ -1,33 +0,0 @@ -package eu.kanade.mangafeed.data.managers; - -import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult; -import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResults; -import com.pushtorefresh.storio.sqlite.operations.put.PutResult; -import com.pushtorefresh.storio.sqlite.operations.put.PutResults; - -import java.util.List; - -import eu.kanade.mangafeed.data.models.Chapter; -import eu.kanade.mangafeed.data.models.Manga; -import eu.kanade.mangafeed.util.PostResult; -import rx.Observable; - -public interface ChapterManager { - - Observable> getChapters(Manga manga); - - Observable> getChapters(long manga_id); - - Observable insertChapter(Chapter chapter); - - Observable> insertChapters(List chapters); - - PutResult insertChapterBlock(Chapter chapter); - - Observable insertOrRemoveChapters(Manga manga, List chapters); - - Observable deleteChapter(Chapter chapter); - - Observable> deleteChapters(List chapters); - -} 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 deleted file mode 100644 index 3d6a7b4878..0000000000 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java +++ /dev/null @@ -1,125 +0,0 @@ -package eu.kanade.mangafeed.data.managers; - -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.put.PutResult; -import com.pushtorefresh.storio.sqlite.operations.put.PutResults; -import com.pushtorefresh.storio.sqlite.queries.Query; - -import java.util.List; - -import eu.kanade.mangafeed.data.models.Chapter; -import eu.kanade.mangafeed.data.models.Manga; -import eu.kanade.mangafeed.data.tables.ChaptersTable; -import eu.kanade.mangafeed.util.PostResult; -import rx.Observable; - -public class ChapterManagerImpl extends BaseManager implements ChapterManager { - - public ChapterManagerImpl(StorIOSQLite db) { - super(db); - } - - private PreparedGetListOfObjects prepareGetChapters(Manga manga) { - return db.get() - .listOfObjects(Chapter.class) - .withQuery(Query.builder() - .table(ChaptersTable.TABLE) - .where(ChaptersTable.COLUMN_MANGA_ID + "=?") - .whereArgs(manga.id) - .build()) - .prepare(); - } - - @Override - public Observable> getChapters(Manga manga) { - return prepareGetChapters(manga).createObservable(); - } - - @Override - public Observable> getChapters(long manga_id) { - return db.get() - .listOfObjects(Chapter.class) - .withQuery(Query.builder() - .table(ChaptersTable.TABLE) - .where(ChaptersTable.COLUMN_MANGA_ID + "=?") - .whereArgs(manga_id) - .build()) - .prepare() - .createObservable(); - } - - @Override - public Observable insertChapter(Chapter chapter) { - return db.put() - .object(chapter) - .prepare() - .createObservable(); - } - - @Override - public Observable> insertChapters(List chapters) { - return db.put() - .objects(chapters) - .prepare() - .createObservable(); - } - - @Override - public PutResult insertChapterBlock(Chapter chapter) { - return db.put() - .object(chapter) - .prepare() - .executeAsBlocking(); - } - - // Add new chapters or delete if the source deletes them - @Override - public Observable insertOrRemoveChapters(Manga manga, List chapters) { - for (Chapter chapter : chapters) { - chapter.manga_id = manga.id; - } - - Observable> chapterList = Observable.create(subscriber -> { - subscriber.onNext(prepareGetChapters(manga).executeAsBlocking()); - subscriber.onCompleted(); - }); - - Observable newChaptersObs = chapterList - .flatMap(dbChapters -> Observable.from(chapters) - .filter(c -> !dbChapters.contains(c)) - .toList() - .flatMap(this::insertChapters) - .map(PutResults::numberOfInserts)); - - Observable deletedChaptersObs = chapterList - .flatMap(dbChapters -> Observable.from(dbChapters) - .filter(c -> !chapters.contains(c)) - .toList() - .flatMap(this::deleteChapters) - .map( d -> d.results().size() )); - - return Observable.zip(newChaptersObs, deletedChaptersObs, - (insertions, deletions) -> new PostResult(0, insertions, deletions) - ); - } - - @Override - public Observable deleteChapter(Chapter chapter) { - return db.delete() - .object(chapter) - .prepare() - .createObservable(); - } - - @Override - public Observable> deleteChapters(List chapters) { - return db.delete() - .objects(chapters) - .prepare() - .createObservable(); - } - -} diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java deleted file mode 100644 index 6aa717da09..0000000000 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManager.java +++ /dev/null @@ -1,37 +0,0 @@ -package eu.kanade.mangafeed.data.managers; - -import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResult; -import com.pushtorefresh.storio.sqlite.operations.delete.DeleteResults; -import com.pushtorefresh.storio.sqlite.operations.put.PutResult; -import com.pushtorefresh.storio.sqlite.operations.put.PutResults; - -import java.util.List; - -import eu.kanade.mangafeed.data.models.Manga; -import rx.Observable; - -public interface MangaManager { - - Observable> getMangas(); - - Observable> getMangasWithUnread(); - - Observable> getFavoriteMangas(); - - Observable> getManga(String url); - - Observable> getManga(long id); - - Manga getMangaBlock(String url); - - Observable insertManga(Manga manga); - - Observable> insertMangas(List mangas); - - PutResult insertMangaBlock(Manga manga); - - Observable deleteManga(Manga manga); - - Observable> deleteMangas(List mangas); - -} diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManagerImpl.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManagerImpl.java deleted file mode 100644 index a4ae052fd2..0000000000 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/MangaManagerImpl.java +++ /dev/null @@ -1,147 +0,0 @@ -package eu.kanade.mangafeed.data.managers; - -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.put.PutResult; -import com.pushtorefresh.storio.sqlite.operations.put.PutResults; -import com.pushtorefresh.storio.sqlite.queries.Query; -import com.pushtorefresh.storio.sqlite.queries.RawQuery; - -import java.util.List; - -import eu.kanade.mangafeed.data.models.Manga; -import eu.kanade.mangafeed.data.tables.ChaptersTable; -import eu.kanade.mangafeed.data.tables.MangasTable; -import rx.Observable; - -public class MangaManagerImpl extends BaseManager implements MangaManager { - - public MangaManagerImpl(StorIOSQLite db) { - super(db); - } - - private final String favoriteMangasWithUnreadQuery = String.format( - "SELECT %1$s.*, COUNT(C.%4$s) AS %5$s FROM %1$s LEFT JOIN " + - "(SELECT %4$s FROM %2$s WHERE %6$s = 0) AS C ON %3$s = C.%4$s " + - "WHERE %7$s = 1 GROUP BY %3$s", - MangasTable.TABLE, - ChaptersTable.TABLE, - MangasTable.TABLE + "." + MangasTable.COLUMN_ID, - ChaptersTable.COLUMN_MANGA_ID, - MangasTable.COLUMN_UNREAD, - ChaptersTable.COLUMN_READ, - MangasTable.COLUMN_FAVORITE - ); - - public Observable> getMangas() { - return db.get() - .listOfObjects(Manga.class) - .withQuery(Query.builder() - .table(MangasTable.TABLE) - .build()) - .prepare() - .createObservable(); - } - - public Observable> getMangasWithUnread() { - return db.get() - .listOfObjects(Manga.class) - .withQuery(RawQuery.builder() - .query(favoriteMangasWithUnreadQuery) - .observesTables(MangasTable.TABLE, ChaptersTable.TABLE) - .build()) - .prepare() - .createObservable(); - } - - @Override - public Observable> getFavoriteMangas() { - return db.get() - .listOfObjects(Manga.class) - .withQuery(Query.builder() - .table(MangasTable.TABLE) - .where(MangasTable.COLUMN_FAVORITE + "=?") - .whereArgs(1) - .build()) - .prepare() - .createObservable(); - } - - public Observable> getManga(String url) { - return db.get() - .listOfObjects(Manga.class) - .withQuery(Query.builder() - .table(MangasTable.TABLE) - .where(MangasTable.COLUMN_URL + "=?") - .whereArgs(url) - .build()) - .prepare() - .createObservable(); - } - - public Observable> getManga(long id) { - return db.get() - .listOfObjects(Manga.class) - .withQuery(Query.builder() - .table(MangasTable.TABLE) - .where(MangasTable.COLUMN_ID + "=?") - .whereArgs(id) - .build()) - .prepare() - .createObservable(); - } - - @Override - public Manga getMangaBlock(String url) { - List result = db.get() - .listOfObjects(Manga.class) - .withQuery(Query.builder() - .table(MangasTable.TABLE) - .where(MangasTable.COLUMN_URL + "=?") - .whereArgs(url) - .build()) - .prepare() - .executeAsBlocking(); - - if (result.isEmpty()) - return null; - - return result.get(0); - } - - public Observable insertManga(Manga manga) { - return db.put() - .object(manga) - .prepare() - .createObservable(); - } - - public Observable> insertMangas(List mangas) { - return db.put() - .objects(mangas) - .prepare() - .createObservable(); - } - - public PutResult insertMangaBlock(Manga manga) { - return db.put() - .object(manga) - .prepare() - .executeAsBlocking(); - } - - public Observable deleteManga(Manga manga) { - return db.delete() - .object(manga) - .prepare() - .createObservable(); - } - - public Observable> deleteMangas(List mangas) { - return db.delete() - .objects(mangas) - .prepare() - .createObservable(); - } -} diff --git a/app/src/main/java/eu/kanade/mangafeed/data/services/LibraryUpdateService.java b/app/src/main/java/eu/kanade/mangafeed/data/services/LibraryUpdateService.java index a692e32f50..bce498cc2b 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/services/LibraryUpdateService.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/services/LibraryUpdateService.java @@ -71,7 +71,7 @@ public class LibraryUpdateService extends Service { if (favoriteMangasSubscription != null && !favoriteMangasSubscription.isUnsubscribed()) favoriteMangasSubscription.unsubscribe(); - favoriteMangasSubscription = db.getFavoriteMangas() + favoriteMangasSubscription = db.getFavoriteMangas().createObservable() .subscribe(mangas -> { // Don't receive further db updates favoriteMangasSubscription.unsubscribe(); diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/CataloguePresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/CataloguePresenter.java index 7c0b96187f..6b28d581a2 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/CataloguePresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/CataloguePresenter.java @@ -112,9 +112,10 @@ public class CataloguePresenter extends BasePresenter { } private Manga networkToLocalManga(Manga networkManga) { - Manga localManga = db.getMangaBlock(networkManga.url); + List dbResult = db.getManga(networkManga.url).executeAsBlocking(); + Manga localManga = !dbResult.isEmpty() ? dbResult.get(0) : null; if (localManga == null) { - PutResult result = db.insertMangaBlock(networkManga); + PutResult result = db.insertManga(networkManga).executeAsBlocking(); networkManga.id = result.insertedId(); localManga = networkManga; } @@ -154,7 +155,7 @@ public class CataloguePresenter extends BasePresenter { .subscribeOn(Schedulers.io()) .flatMap(networkManga -> { Manga.copyFromNetwork(manga, networkManga); - db.insertMangaBlock(manga); + db.insertManga(manga).executeAsBlocking(); return Observable.just(manga); }); mangaObservables.add(tempObs); diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java index e78ba0c8b4..3ae59472db 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/LibraryPresenter.java @@ -37,7 +37,7 @@ public class LibraryPresenter extends BasePresenter { if (mFavoriteMangasSubscription != null) return; - add(mFavoriteMangasSubscription = db.getMangasWithUnread() + add(mFavoriteMangasSubscription = db.getMangasWithUnread().createObservable() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .compose(deliverLatestCache()) @@ -57,7 +57,7 @@ public class LibraryPresenter extends BasePresenter { return manga; }) .toList() - .flatMap(db::insertMangas) + .flatMap(mangas -> db.insertMangas(mangas).createObservable()) .subscribe()); } diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java index b34e7e8906..ac7d9f4b23 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaChaptersPresenter.java @@ -101,7 +101,7 @@ public class MangaChaptersPresenter extends BasePresenter } private Observable> getDbChaptersObs() { - return db.getChapters(manga.id) + return db.getChapters(manga.id).createObservable() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); } @@ -126,7 +126,7 @@ public class MangaChaptersPresenter extends BasePresenter return chapter; }) .toList() - .flatMap(db::insertChapters) + .flatMap(chapters -> db.insertChapters(chapters).createObservable()) .observeOn(AndroidSchedulers.mainThread()) .doOnCompleted(() -> remove(markReadSubscription)) .subscribe(result -> { diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java index 2f200c8e49..c808979449 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaDetailPresenter.java @@ -42,7 +42,7 @@ public class MangaDetailPresenter extends BasePresenter { } private Observable getDbMangaObservable() { - return db.getManga(mangaId) + return db.getManga(mangaId).createObservable() .subscribeOn(Schedulers.io()) .flatMap(Observable::from) .observeOn(AndroidSchedulers.mainThread()); diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaInfoPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaInfoPresenter.java index be913c7e1d..f09ffb2899 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/MangaInfoPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/MangaInfoPresenter.java @@ -67,7 +67,7 @@ public class MangaInfoPresenter extends BasePresenter { public void toggleFavorite() { manga.favorite = !manga.favorite; - db.insertMangaBlock(manga); + db.insertManga(manga).executeAsBlocking(); } } diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java index ae5fbe0daf..c5ee5923da 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java @@ -135,6 +135,6 @@ public class ReaderPresenter extends BasePresenter { if (currentPage == pageList.size() - 1) { chapter.read = true; } - db.insertChapterBlock(chapter); + db.insertChapter(chapter).executeAsBlocking(); } }