From 53decfd47b2fba53cebcaa2350fb8cf6a2385e9f Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 26 Jun 2022 21:34:54 -0400 Subject: [PATCH] Migrate usages of SyncChaptersWithTrackServiceTwoWay --- .../data/database/queries/ChapterQueries.kt | 4 -- .../data/database/queries/MangaQueries.kt | 26 +++++------- .../data/database/queries/TrackQueries.kt | 2 - .../data/library/LibraryUpdateService.kt | 8 +++- .../tachiyomi/data/track/TrackManager.kt | 6 --- .../kanade/tachiyomi/network/NetworkHelper.kt | 1 + .../source/browse/BrowseSourcePresenter.kt | 20 ++++++---- .../util/chapter/ChapterSettingsHelper.kt | 2 +- .../util/chapter/ChapterTrackSync.kt | 40 ------------------- 9 files changed, 30 insertions(+), 79 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterTrackSync.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/ChapterQueries.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/ChapterQueries.kt index a2a8a96cc..d49d7a067 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/ChapterQueries.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/ChapterQueries.kt @@ -58,10 +58,6 @@ interface ChapterQueries : DbProvider { ) .prepare() - fun insertChapters(chapters: List) = db.put().objects(chapters).prepare() - - fun deleteChapters(chapters: List) = db.delete().objects(chapters).prepare() - fun updateChapterProgress(chapter: Chapter) = db.put() .`object`(chapter) .withPutResolver(ChapterProgressPutResolver()) 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 6663ce8d9..35ab51f44 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 @@ -1,6 +1,5 @@ package eu.kanade.tachiyomi.data.database.queries -import com.pushtorefresh.storio.sqlite.operations.get.PreparedGetListOfObjects import com.pushtorefresh.storio.sqlite.queries.Query import com.pushtorefresh.storio.sqlite.queries.RawQuery import eu.kanade.tachiyomi.data.database.DbProvider @@ -26,21 +25,16 @@ interface MangaQueries : DbProvider { .withGetResolver(LibraryMangaGetResolver.INSTANCE) .prepare() - fun getFavoriteMangas(sortByTitle: Boolean = true): PreparedGetListOfObjects { - var queryBuilder = Query.builder() - .table(MangaTable.TABLE) - .where("${MangaTable.COL_FAVORITE} = ?") - .whereArgs(1) - - if (sortByTitle) { - queryBuilder = queryBuilder.orderBy(MangaTable.COL_TITLE) - } - - return db.get() - .listOfObjects(Manga::class.java) - .withQuery(queryBuilder.build()) - .prepare() - } + fun getFavoriteMangas() = db.get() + .listOfObjects(Manga::class.java) + .withQuery( + Query.builder() + .table(MangaTable.TABLE) + .where("${MangaTable.COL_FAVORITE} = ?") + .whereArgs(1) + .build(), + ) + .prepare() fun getManga(url: String, sourceId: Long) = db.get() .`object`(Manga::class.java) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/TrackQueries.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/TrackQueries.kt index c6afd2977..a0a917ab5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/TrackQueries.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/TrackQueries.kt @@ -15,6 +15,4 @@ interface TrackQueries : DbProvider { .build(), ) .prepare() - - fun insertTrack(track: Track) = db.put().`object`(track).prepare() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt index 7a91cf809..805dcb851 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt @@ -7,7 +7,9 @@ import android.os.IBinder import android.os.PowerManager import androidx.core.content.ContextCompat import eu.kanade.data.chapter.NoChaptersException +import eu.kanade.domain.chapter.interactor.GetChapterByMangaId import eu.kanade.domain.chapter.interactor.SyncChaptersWithSource +import eu.kanade.domain.chapter.interactor.SyncChaptersWithTrackServiceTwoWay import eu.kanade.domain.chapter.model.toDbChapter import eu.kanade.domain.manga.interactor.GetMangaById import eu.kanade.domain.manga.interactor.UpdateManga @@ -41,7 +43,6 @@ import eu.kanade.tachiyomi.source.UnmeteredSource import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.model.toSChapter import eu.kanade.tachiyomi.source.model.toSManga -import eu.kanade.tachiyomi.util.chapter.syncChaptersWithTrackServiceTwoWay import eu.kanade.tachiyomi.util.lang.withIOContext import eu.kanade.tachiyomi.util.prepUpdateCover import eu.kanade.tachiyomi.util.shouldDownloadNewChapters @@ -90,9 +91,11 @@ class LibraryUpdateService( val coverCache: CoverCache = Injekt.get(), private val getMangaById: GetMangaById = Injekt.get(), private val updateManga: UpdateManga = Injekt.get(), + private val getChapterByMangaId: GetChapterByMangaId = Injekt.get(), private val syncChaptersWithSource: SyncChaptersWithSource = Injekt.get(), private val getTracks: GetTracks = Injekt.get(), private val insertTrack: InsertTrack = Injekt.get(), + private val syncChaptersWithTrackServiceTwoWay: SyncChaptersWithTrackServiceTwoWay = Injekt.get(), ) : Service() { private lateinit var wakeLock: PowerManager.WakeLock @@ -517,7 +520,8 @@ class LibraryUpdateService( insertTrack.await(updatedTrack.toDomainTrack()!!) if (service is EnhancedTrackService) { - syncChaptersWithTrackServiceTwoWay(db, db.getChapters(manga).executeAsBlocking(), track.toDbTrack(), service) + val chapters = getChapterByMangaId.await(manga.id!!) + syncChaptersWithTrackServiceTwoWay.await(chapters, track, service) } } catch (e: Throwable) { // Ignore errors and continue diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/TrackManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/TrackManager.kt index b13544d02..e37be3622 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/TrackManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/TrackManager.kt @@ -22,17 +22,11 @@ class TrackManager(context: Context) { } val myAnimeList = MyAnimeList(context, MYANIMELIST) - val aniList = Anilist(context, ANILIST) - val kitsu = Kitsu(context, KITSU) - val shikimori = Shikimori(context, SHIKIMORI) - val bangumi = Bangumi(context, BANGUMI) - val komga = Komga(context, KOMGA) - val mangaUpdates = MangaUpdates(context, MANGA_UPDATES) val services = listOf(myAnimeList, aniList, kitsu, shikimori, bangumi, komga, mangaUpdates) diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt index ea3106cb8..ec7af77f1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/NetworkHelper.kt @@ -54,6 +54,7 @@ class NetworkHelper(context: Context) { val client by lazy { baseClientBuilder.cache(Cache(cacheDir, cacheSize)).build() } + @Suppress("UNUSED") val cloudflareClient by lazy { client.newBuilder() .addInterceptor(CloudflareInterceptor(context)) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt index db91fa508..cadb468ec 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt @@ -3,6 +3,8 @@ package eu.kanade.tachiyomi.ui.browse.source.browse import android.os.Bundle import eu.davidea.flexibleadapter.items.IFlexible import eu.kanade.domain.category.interactor.GetCategories +import eu.kanade.domain.chapter.interactor.GetChapterByMangaId +import eu.kanade.domain.chapter.interactor.SyncChaptersWithTrackServiceTwoWay import eu.kanade.domain.manga.interactor.GetDuplicateLibraryManga import eu.kanade.domain.manga.model.toDbManga import eu.kanade.domain.track.interactor.InsertTrack @@ -38,7 +40,6 @@ import eu.kanade.tachiyomi.ui.browse.source.filter.TextSectionItem import eu.kanade.tachiyomi.ui.browse.source.filter.TriStateItem import eu.kanade.tachiyomi.ui.browse.source.filter.TriStateSectionItem import eu.kanade.tachiyomi.util.chapter.ChapterSettingsHelper -import eu.kanade.tachiyomi.util.chapter.syncChaptersWithTrackServiceTwoWay import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.removeCovers @@ -69,7 +70,9 @@ open class BrowseSourcePresenter( private val coverCache: CoverCache = Injekt.get(), private val getDuplicateLibraryManga: GetDuplicateLibraryManga = Injekt.get(), private val getCategories: GetCategories = Injekt.get(), + private val getChapterByMangaId: GetChapterByMangaId = Injekt.get(), private val insertTrack: InsertTrack = Injekt.get(), + private val syncChaptersWithTrackServiceTwoWay: SyncChaptersWithTrackServiceTwoWay = Injekt.get(), ) : BasePresenter() { /** @@ -280,24 +283,25 @@ open class BrowseSourcePresenter( } private fun autoAddTrack(manga: Manga) { - loggedServices - .filterIsInstance() - .filter { it.accept(source) } - .forEach { service -> - launchIO { + launchIO { + loggedServices + .filterIsInstance() + .filter { it.accept(source) } + .forEach { service -> try { service.match(manga)?.let { track -> track.manga_id = manga.id!! (service as TrackService).bind(track) insertTrack.await(track.toDomainTrack()!!) - syncChaptersWithTrackServiceTwoWay(db, db.getChapters(manga).executeAsBlocking(), track, service as TrackService) + val chapters = getChapterByMangaId.await(manga.id!!) + syncChaptersWithTrackServiceTwoWay.await(chapters, track.toDomainTrack()!!, service) } } catch (e: Exception) { logcat(LogPriority.WARN, e) { "Could not match manga: ${manga.title} with service $service" } } } - } + } } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSettingsHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSettingsHelper.kt index 5d1e6da34..fafe771a8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSettingsHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSettingsHelper.kt @@ -52,7 +52,7 @@ object ChapterSettingsHelper { */ fun updateAllMangasWithGlobalDefaults() { launchIO { - val updatedMangas = db.getFavoriteMangas(sortByTitle = false) + val updatedMangas = db.getFavoriteMangas() .executeAsBlocking() .map { manga -> with(manga) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterTrackSync.kt b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterTrackSync.kt deleted file mode 100644 index eb07a969d..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterTrackSync.kt +++ /dev/null @@ -1,40 +0,0 @@ -package eu.kanade.tachiyomi.util.chapter - -import eu.kanade.tachiyomi.data.database.DatabaseHelper -import eu.kanade.tachiyomi.data.database.models.Chapter -import eu.kanade.tachiyomi.data.database.models.Track -import eu.kanade.tachiyomi.data.track.TrackService -import eu.kanade.tachiyomi.util.lang.launchIO -import eu.kanade.tachiyomi.util.system.logcat -import logcat.LogPriority - -/** - * Helper method for syncing a remote track with the local chapters, and back - * - * @param db the database. - * @param chapters a list of chapters from the source. - * @param remoteTrack the remote Track object. - * @param service the tracker service. - */ -fun syncChaptersWithTrackServiceTwoWay(db: DatabaseHelper, chapters: List, remoteTrack: Track, service: TrackService) { - val sortedChapters = chapters.sortedBy { it.chapter_number } - sortedChapters - .filter { chapter -> chapter.chapter_number <= remoteTrack.last_chapter_read && !chapter.read } - .forEach { it.read = true } - db.updateChaptersProgress(sortedChapters).executeAsBlocking() - - // only take into account continuous reading - val localLastRead = sortedChapters.takeWhile { it.read }.lastOrNull()?.chapter_number ?: 0F - - // update remote - remoteTrack.last_chapter_read = localLastRead - - launchIO { - try { - service.update(remoteTrack) - db.insertTrack(remoteTrack).executeAsBlocking() - } catch (e: Throwable) { - logcat(LogPriority.WARN, e) - } - } -}