From 68697e59d71d9875d8a8b8aaaf65e77a084e0b8a Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 3 Jul 2022 13:46:09 -0400 Subject: [PATCH] More domain model usage --- .../data/backup/AbstractBackupManager.kt | 9 +++-- .../tachiyomi/data/cache/ChapterCache.kt | 4 +-- .../source/browse/BrowseSourcePresenter.kt | 2 +- .../ui/manga/chapter/ChaptersSettingsSheet.kt | 3 +- .../manga/chapter/SetChapterSettingsDialog.kt | 2 +- .../tachiyomi/ui/reader/ReaderPresenter.kt | 2 +- .../ui/reader/loader/ChapterLoader.kt | 2 +- .../ui/reader/loader/DownloadPageLoader.kt | 5 +-- .../ui/reader/loader/HttpPageLoader.kt | 5 +-- .../ui/reader/viewer/MissingChapters.kt | 11 +++--- .../util/chapter/ChapterSettingsHelper.kt | 7 ++-- .../util/chapter/ChapterSourceSync.kt | 34 ------------------- 12 files changed, 29 insertions(+), 57 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSourceSync.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupManager.kt index cc7f8f07ff..67daebb1d2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupManager.kt @@ -4,16 +4,18 @@ import android.content.Context import android.net.Uri import eu.kanade.data.DatabaseHandler import eu.kanade.data.toLong +import eu.kanade.domain.chapter.interactor.SyncChaptersWithSource +import eu.kanade.domain.chapter.model.toDbChapter import eu.kanade.domain.manga.interactor.GetFavorites import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.data.database.models.toDomainManga import eu.kanade.tachiyomi.data.database.models.toMangaInfo import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.model.toSChapter -import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get import data.Mangas as DbManga @@ -27,6 +29,7 @@ abstract class AbstractBackupManager(protected val context: Context) { internal val trackManager: TrackManager = Injekt.get() protected val preferences: PreferencesHelper = Injekt.get() private val getFavorites: GetFavorites = Injekt.get() + private val syncChaptersWithSource: SyncChaptersWithSource = Injekt.get() abstract suspend fun createBackup(uri: Uri, flags: Int, isAutoBackup: Boolean): String @@ -50,12 +53,12 @@ abstract class AbstractBackupManager(protected val context: Context) { internal suspend fun restoreChapters(source: Source, manga: Manga, chapters: List): Pair, List> { val fetchedChapters = source.getChapterList(manga.toMangaInfo()) .map { it.toSChapter() } - val syncedChapters = syncChaptersWithSource(fetchedChapters, manga, source) + val syncedChapters = syncChaptersWithSource.await(fetchedChapters, manga.toDomainManga()!!, source) if (syncedChapters.first.isNotEmpty()) { chapters.forEach { it.manga_id = manga.id } updateChapters(chapters) } - return syncedChapters + return syncedChapters.first.map { it.toDbChapter() } to syncedChapters.second.map { it.toDbChapter() } } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt index 4d9444d3ff..bab08dd7e5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt @@ -3,7 +3,7 @@ package eu.kanade.tachiyomi.data.cache import android.content.Context import android.text.format.Formatter import com.jakewharton.disklrucache.DiskLruCache -import eu.kanade.tachiyomi.data.database.models.Chapter +import eu.kanade.domain.chapter.model.Chapter import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.util.storage.DiskUtil import eu.kanade.tachiyomi.util.storage.saveTo @@ -207,6 +207,6 @@ class ChapterCache(private val context: Context) { } private fun getKey(chapter: Chapter): String { - return "${chapter.manga_id}${chapter.url}" + return "${chapter.mangaId}${chapter.url}" } } 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 18b86ae438..9d77dede5e 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 @@ -289,7 +289,7 @@ open class BrowseSourcePresenter( if (!manga.favorite) { manga.removeCovers(coverCache) } else { - ChapterSettingsHelper.applySettingDefaults(manga) + ChapterSettingsHelper.applySettingDefaults(manga.toDomainManga()!!) autoAddTrack(manga) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSettingsSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSettingsSheet.kt index 0a89ffe09a..52ec4d052f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSettingsSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSettingsSheet.kt @@ -7,7 +7,6 @@ import android.view.View import androidx.core.view.isVisible import com.bluelinelabs.conductor.Router import eu.kanade.domain.manga.model.Manga -import eu.kanade.domain.manga.model.toDbManga import eu.kanade.domain.manga.model.toTriStateGroupState import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.ui.manga.MangaPresenter @@ -79,7 +78,7 @@ class ChaptersSettingsSheet( onMenuItemClick = { when (itemId) { R.id.set_as_default -> { - SetChapterSettingsDialog(presenter.manga!!.toDbManga()).showDialog(router) + SetChapterSettingsDialog(presenter.manga!!).showDialog(router) } } }, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/SetChapterSettingsDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/SetChapterSettingsDialog.kt index a94c0fc893..2b82892d67 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/SetChapterSettingsDialog.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/SetChapterSettingsDialog.kt @@ -4,8 +4,8 @@ import android.app.Dialog import android.os.Bundle import androidx.core.os.bundleOf import com.google.android.material.dialog.MaterialAlertDialogBuilder +import eu.kanade.domain.manga.model.Manga import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.util.chapter.ChapterSettingsHelper import eu.kanade.tachiyomi.util.system.toast 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 1fa505417b..1f7cbaf005 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 @@ -267,7 +267,7 @@ class ReaderPresenter( val context = Injekt.get() val source = sourceManager.getOrStub(manga.source) - loader = ChapterLoader(context, downloadManager, manga, source) + loader = ChapterLoader(context, downloadManager, manga.toDomainManga()!!, source) Observable.just(manga).subscribeLatestCache(ReaderActivity::setManga) viewerChaptersRelay.subscribeLatestCache(ReaderActivity::setChapters) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ChapterLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ChapterLoader.kt index 0fa480598d..e3049ae1aa 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ChapterLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ChapterLoader.kt @@ -1,8 +1,8 @@ package eu.kanade.tachiyomi.ui.reader.loader import android.content.Context +import eu.kanade.domain.manga.model.Manga import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.source.LocalSource import eu.kanade.tachiyomi.source.Source diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/DownloadPageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/DownloadPageLoader.kt index 543cdff9f8..4fadbdc9ac 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/DownloadPageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/DownloadPageLoader.kt @@ -3,7 +3,8 @@ package eu.kanade.tachiyomi.ui.reader.loader import android.app.Application import android.net.Uri import com.hippo.unifile.UniFile -import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.domain.manga.model.Manga +import eu.kanade.domain.manga.model.toDbManga import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.model.Page @@ -45,7 +46,7 @@ class DownloadPageLoader( } private fun getPagesFromDirectory(): Observable> { - return downloadManager.buildPageList(source, manga, chapter.chapter) + return downloadManager.buildPageList(source, manga.toDbManga(), chapter.chapter) .map { pages -> pages.map { page -> ReaderPage(page.index, page.url, page.imageUrl) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt index 3c3645f4df..960f69daad 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.ui.reader.loader import eu.kanade.tachiyomi.data.cache.ChapterCache +import eu.kanade.tachiyomi.data.database.models.toDomainChapter import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter @@ -73,7 +74,7 @@ class HttpPageLoader( .fromAction { // Convert to pages without reader information val pagesToSave = pages.map { Page(it.index, it.url, it.imageUrl) } - chapterCache.putPageListToCache(chapter.chapter, pagesToSave) + chapterCache.putPageListToCache(chapter.chapter.toDomainChapter()!!, pagesToSave) } .onErrorComplete() .subscribeOn(Schedulers.io()) @@ -86,7 +87,7 @@ class HttpPageLoader( * the local cache, otherwise fallbacks to network. */ override fun getPages(): Observable> { - return Observable.fromCallable { chapterCache.getPageListFromCache(chapter.chapter) } + return Observable.fromCallable { chapterCache.getPageListFromCache(chapter.chapter.toDomainChapter()!!) } .onErrorResumeNext { source.fetchPageList(chapter.chapter) } .map { pages -> pages.mapIndexed { index, page -> diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/MissingChapters.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/MissingChapters.kt index 64853058c2..6dd9d4c393 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/MissingChapters.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/MissingChapters.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.ui.reader.viewer -import eu.kanade.tachiyomi.data.database.models.Chapter +import eu.kanade.domain.chapter.model.Chapter +import eu.kanade.tachiyomi.data.database.models.toDomainChapter import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter import kotlin.math.floor @@ -8,7 +9,7 @@ private val pattern = Regex("""\d+""") fun hasMissingChapters(higherReaderChapter: ReaderChapter?, lowerReaderChapter: ReaderChapter?): Boolean { if (higherReaderChapter == null || lowerReaderChapter == null) return false - return hasMissingChapters(higherReaderChapter.chapter, lowerReaderChapter.chapter) + return hasMissingChapters(higherReaderChapter.chapter.toDomainChapter(), lowerReaderChapter.chapter.toDomainChapter()) } fun hasMissingChapters(higherChapter: Chapter?, lowerChapter: Chapter?): Boolean { @@ -17,7 +18,7 @@ fun hasMissingChapters(higherChapter: Chapter?, lowerChapter: Chapter?): Boolean if (!pattern.containsMatchIn(higherChapter.name) || !pattern.containsMatchIn(lowerChapter.name)) return false // Check if potential chapter number was recognized as chapter number if (!higherChapter.isRecognizedNumber || !lowerChapter.isRecognizedNumber) return false - return hasMissingChapters(higherChapter.chapter_number, lowerChapter.chapter_number) + return hasMissingChapters(higherChapter.chapterNumber, lowerChapter.chapterNumber) } fun hasMissingChapters(higherChapterNumber: Float, lowerChapterNumber: Float): Boolean { @@ -27,7 +28,7 @@ fun hasMissingChapters(higherChapterNumber: Float, lowerChapterNumber: Float): B fun calculateChapterDifference(higherReaderChapter: ReaderChapter?, lowerReaderChapter: ReaderChapter?): Float { if (higherReaderChapter == null || lowerReaderChapter == null) return 0f - return calculateChapterDifference(higherReaderChapter.chapter, lowerReaderChapter.chapter) + return calculateChapterDifference(higherReaderChapter.chapter.toDomainChapter(), lowerReaderChapter.chapter.toDomainChapter()) } fun calculateChapterDifference(higherChapter: Chapter?, lowerChapter: Chapter?): Float { @@ -36,7 +37,7 @@ fun calculateChapterDifference(higherChapter: Chapter?, lowerChapter: Chapter?): if (!pattern.containsMatchIn(higherChapter.name) || !pattern.containsMatchIn(lowerChapter.name)) return 0f // Check if potential chapter number was recognized as chapter number if (!higherChapter.isRecognizedNumber || !lowerChapter.isRecognizedNumber) return 0f - return calculateChapterDifference(higherChapter.chapter_number, lowerChapter.chapter_number) + return calculateChapterDifference(higherChapter.chapterNumber, lowerChapter.chapterNumber) } fun calculateChapterDifference(higherChapterNumber: Float, lowerChapterNumber: Float): Float { 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 88706dacfb..ec8ec6c04d 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 @@ -2,7 +2,8 @@ package eu.kanade.tachiyomi.util.chapter import eu.kanade.domain.manga.interactor.GetFavorites import eu.kanade.domain.manga.interactor.SetMangaChapterFlags -import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.domain.manga.model.Manga +import eu.kanade.domain.manga.model.toDbManga import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.util.lang.launchIO import uy.kohesive.injekt.injectLazy @@ -17,7 +18,7 @@ object ChapterSettingsHelper { * Updates the global Chapter Settings in Preferences. */ fun setGlobalSettings(manga: Manga) { - prefs.setChapterSettingsDefault(manga) + prefs.setChapterSettingsDefault(manga.toDbManga()) } /** @@ -26,7 +27,7 @@ object ChapterSettingsHelper { fun applySettingDefaults(manga: Manga) { launchIO { setMangaChapterFlags.awaitSetAllFlags( - mangaId = manga.id!!, + mangaId = manga.id, unreadFilter = prefs.filterChapterByRead().toLong(), downloadedFilter = prefs.filterChapterByDownloaded().toLong(), bookmarkedFilter = prefs.filterChapterByBookmarked().toLong(), diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSourceSync.kt b/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSourceSync.kt deleted file mode 100644 index 43ecf2b3a0..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/util/chapter/ChapterSourceSync.kt +++ /dev/null @@ -1,34 +0,0 @@ -package eu.kanade.tachiyomi.util.chapter - -import eu.kanade.domain.chapter.interactor.SyncChaptersWithSource -import eu.kanade.domain.chapter.model.toDbChapter -import eu.kanade.tachiyomi.data.database.models.toDomainManga -import eu.kanade.tachiyomi.source.Source -import eu.kanade.tachiyomi.source.model.SChapter -import uy.kohesive.injekt.Injekt -import uy.kohesive.injekt.api.get -import eu.kanade.tachiyomi.data.database.models.Chapter as DbChapter -import eu.kanade.tachiyomi.data.database.models.Manga as DbManga - -/** - * Helper method for syncing the list of chapters from the source with the ones from the database. - * - * @param rawSourceChapters a list of chapters from the source. - * @param manga the manga of the chapters. - * @param source the source of the chapters. - * @return a pair of new insertions and deletions. - */ -suspend fun syncChaptersWithSource( - rawSourceChapters: List, - manga: DbManga, - source: Source, - syncChaptersWithSource: SyncChaptersWithSource = Injekt.get(), -): Pair, List> { - val domainManga = manga.toDomainManga() ?: return Pair(emptyList(), emptyList()) - val (added, deleted) = syncChaptersWithSource.await(rawSourceChapters, domainManga, source) - - val addedDbChapters = added.map { it.toDbChapter() } - val deletedDbChapters = deleted.map { it.toDbChapter() } - - return Pair(addedDbChapters, deletedDbChapters) -}