From 788cb843fc5ff12dd7a955d42479fbdace01945c Mon Sep 17 00:00:00 2001 From: inorichi Date: Tue, 9 Oct 2018 14:27:00 +0200 Subject: [PATCH] Minor fixes when updating the manga viewer --- .../data/database/queries/MangaQueries.kt | 12 ++++--- .../resolvers/MangaViewerPutResolver.kt | 32 +++++++++++++++++++ .../tachiyomi/ui/reader/ReaderActivity.kt | 6 ++-- .../tachiyomi/ui/reader/ReaderPresenter.kt | 22 +++++++++++-- 4 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/MangaViewerPutResolver.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/MangaQueries.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/MangaQueries.kt index a5b3a4da85..a27a11c342 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/MangaQueries.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/MangaQueries.kt @@ -6,10 +6,7 @@ import com.pushtorefresh.storio.sqlite.queries.RawQuery import eu.kanade.tachiyomi.data.database.DbProvider import eu.kanade.tachiyomi.data.database.models.LibraryManga import eu.kanade.tachiyomi.data.database.models.Manga -import eu.kanade.tachiyomi.data.database.resolvers.LibraryMangaGetResolver -import eu.kanade.tachiyomi.data.database.resolvers.MangaFavoritePutResolver -import eu.kanade.tachiyomi.data.database.resolvers.MangaFlagsPutResolver -import eu.kanade.tachiyomi.data.database.resolvers.MangaLastUpdatedPutResolver +import eu.kanade.tachiyomi.data.database.resolvers.* import eu.kanade.tachiyomi.data.database.tables.CategoryTable import eu.kanade.tachiyomi.data.database.tables.ChapterTable import eu.kanade.tachiyomi.data.database.tables.MangaCategoryTable @@ -80,6 +77,11 @@ interface MangaQueries : DbProvider { .withPutResolver(MangaFavoritePutResolver()) .prepare() + fun updateMangaViewer(manga: Manga) = db.put() + .`object`(manga) + .withPutResolver(MangaViewerPutResolver()) + .prepare() + fun deleteManga(manga: Manga) = db.delete().`object`(manga).prepare() fun deleteMangas(mangas: List) = db.delete().objects(mangas).prepare() @@ -108,4 +110,4 @@ interface MangaQueries : DbProvider { fun getTotalChapterManga() = db.get().listOfObjects(Manga::class.java) .withQuery(RawQuery.builder().query(getTotalChapterMangaQuery()).observesTables(MangaTable.TABLE).build()).prepare(); -} \ No newline at end of file +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/MangaViewerPutResolver.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/MangaViewerPutResolver.kt new file mode 100644 index 0000000000..86c67f3463 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/MangaViewerPutResolver.kt @@ -0,0 +1,32 @@ +package eu.kanade.tachiyomi.data.database.resolvers + +import android.content.ContentValues +import com.pushtorefresh.storio.sqlite.StorIOSQLite +import com.pushtorefresh.storio.sqlite.operations.put.PutResolver +import com.pushtorefresh.storio.sqlite.operations.put.PutResult +import com.pushtorefresh.storio.sqlite.queries.UpdateQuery +import eu.kanade.tachiyomi.data.database.inTransactionReturn +import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.data.database.tables.MangaTable + +class MangaViewerPutResolver : PutResolver() { + + override fun performPut(db: StorIOSQLite, manga: Manga) = db.inTransactionReturn { + val updateQuery = mapToUpdateQuery(manga) + val contentValues = mapToContentValues(manga) + + val numberOfRowsUpdated = db.lowLevel().update(updateQuery, contentValues) + PutResult.newUpdateResult(numberOfRowsUpdated, updateQuery.table()) + } + + fun mapToUpdateQuery(manga: Manga) = UpdateQuery.builder() + .table(MangaTable.TABLE) + .where("${MangaTable.COL_ID} = ?") + .whereArgs(manga.id) + .build() + + fun mapToContentValues(manga: Manga) = ContentValues(1).apply { + put(MangaTable.COL_VIEWER, manga.viewer) + } + +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index 98ec66ae25..9861e288cb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -99,7 +99,7 @@ class ReaderActivity : BaseRxActivity() { fun newIntent(context: Context, manga: Manga, chapter: Chapter): Intent { val intent = Intent(context, ReaderActivity::class.java) - intent.putExtra("manga", manga) + intent.putExtra("manga", manga.id) intent.putExtra("chapter", chapter.id) return intent } @@ -117,10 +117,10 @@ class ReaderActivity : BaseRxActivity() { setContentView(R.layout.reader_activity) if (presenter.needsInit()) { - val manga = intent.extras.getSerializable("manga") as? Manga + val manga = intent.extras.getLong("manga", -1) val chapter = intent.extras.getLong("chapter", -1) - if (manga == null || chapter == -1L) { + if (manga == -1L || chapter == -1L) { finish() return } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt index 50931553e9..ab8b56ee83 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt @@ -157,11 +157,27 @@ class ReaderPresenter( return manga == null } + /** + * Initializes this presenter with the given [mangaId] and [initialChapterId]. This method will + * fetch the manga from the database and initialize the initial chapter. + */ + fun init(mangaId: Long, initialChapterId: Long) { + if (!needsInit()) return + + db.getManga(mangaId).asRxObservable() + .first() + .observeOn(AndroidSchedulers.mainThread()) + .doOnNext { init(it, initialChapterId) } + .subscribeFirst({ _, _ -> + // Ignore onNext event + }, ReaderActivity::setInitialChapterError) + } + /** * Initializes this presenter with the given [manga] and [initialChapterId]. This method will * set the chapter loader, view subscriptions and trigger an initial load. */ - fun init(manga: Manga, initialChapterId: Long) { + private fun init(manga: Manga, initialChapterId: Long) { if (!needsInit()) return this.manga = manga @@ -180,6 +196,7 @@ class ReaderPresenter( .fromCallable { chapterList.first { chapterId == it.chapter.id } } .flatMap { getLoadObservable(loader!!, it) } .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) .subscribeFirst({ _, _ -> // Ignore onNext event }, ReaderActivity::setInitialChapterError) @@ -374,8 +391,7 @@ class ReaderPresenter( fun setMangaViewer(viewer: Int) { val manga = manga ?: return manga.viewer = viewer - // TODO custom put operation - db.insertManga(manga).executeAsBlocking() + db.updateMangaViewer(manga).executeAsBlocking() Observable.timer(250, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread()) .subscribeFirst({ view, _ ->