From 3791d8254034ffcab94ddd65ec290c28384e6526 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 3 Jul 2022 16:25:51 -0400 Subject: [PATCH] More domain model usage --- .../eu/kanade/domain/chapter/model/Chapter.kt | 3 +- .../tachiyomi/data/database/models/Chapter.kt | 4 -- .../migration/search/SearchController.kt | 13 +++--- .../migration/search/SearchPresenter.kt | 42 +++++++++---------- .../search/SourceSearchController.kt | 3 +- .../globalsearch/GlobalSearchCardAdapter.kt | 2 +- .../globalsearch/GlobalSearchCardHolder.kt | 2 +- .../globalsearch/GlobalSearchCardItem.kt | 4 +- .../globalsearch/GlobalSearchController.kt | 4 +- .../source/globalsearch/GlobalSearchHolder.kt | 4 +- .../globalsearch/GlobalSearchPresenter.kt | 4 +- .../tachiyomi/ui/library/LibraryPresenter.kt | 6 +-- .../tachiyomi/ui/manga/MangaController.kt | 2 +- .../tachiyomi/ui/manga/MangaPresenter.kt | 13 +++--- 14 files changed, 46 insertions(+), 60 deletions(-) diff --git a/app/src/main/java/eu/kanade/domain/chapter/model/Chapter.kt b/app/src/main/java/eu/kanade/domain/chapter/model/Chapter.kt index 6b8fc57cd..ba67b6917 100644 --- a/app/src/main/java/eu/kanade/domain/chapter/model/Chapter.kt +++ b/app/src/main/java/eu/kanade/domain/chapter/model/Chapter.kt @@ -1,5 +1,6 @@ package eu.kanade.domain.chapter.model +import eu.kanade.tachiyomi.data.database.models.ChapterImpl import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.data.database.models.Chapter as DbChapter @@ -61,7 +62,7 @@ data class Chapter( } // TODO: Remove when all deps are migrated -fun Chapter.toDbChapter(): DbChapter = DbChapter.create().also { +fun Chapter.toDbChapter(): DbChapter = ChapterImpl().also { it.id = id it.manga_id = mangaId it.url = url diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Chapter.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Chapter.kt index 2437e0a39..90bbfdce9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Chapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Chapter.kt @@ -20,11 +20,7 @@ interface Chapter : SChapter, Serializable { var source_order: Int - val isRecognizedNumber: Boolean - get() = chapter_number >= 0f - companion object { - fun create(): Chapter = ChapterImpl().apply { chapter_number = -1f } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchController.kt index 3362bb06d..e49c58d7b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchController.kt @@ -7,10 +7,8 @@ import com.bluelinelabs.conductor.Controller import com.bluelinelabs.conductor.RouterTransaction import com.google.android.material.dialog.MaterialAlertDialogBuilder import eu.kanade.domain.manga.interactor.GetManga -import eu.kanade.domain.manga.model.toDbManga +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.database.models.toDomainManga import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.ui.base.controller.DialogController @@ -32,7 +30,6 @@ class SearchController( runBlocking { Injekt.get() .await(mangaId) - ?.toDbManga() }, ) @@ -89,7 +86,7 @@ class SearchController( if (!isReplacingManga) { router.popController(this) if (newManga?.id != null) { - val newMangaController = RouterTransaction.with(MangaController(newManga.id!!)) + val newMangaController = RouterTransaction.with(MangaController(newManga.id)) if (router.backstack.lastOrNull()?.controller is MangaController) { // Replace old MangaController router.replaceTopController(newMangaController) @@ -109,7 +106,7 @@ class SearchController( override fun onCreateDialog(savedViewState: Bundle?): Dialog { val prefValue = preferences.migrateFlags().get() val enabledFlagsPositions = MigrationFlags.getEnabledFlagsPositions(prefValue) - val items = MigrationFlags.titles(manga?.toDomainManga()) + val items = MigrationFlags.titles(manga) .map { resources?.getString(it) } .toTypedArray() val selected = items @@ -145,7 +142,7 @@ class SearchController( } .setNeutralButton(activity?.getString(R.string.action_show_manga)) { _, _ -> dismissDialog() - router.pushController(MangaController(newManga!!.id!!)) + router.pushController(MangaController(newManga!!.id)) } .create() } @@ -154,6 +151,6 @@ class SearchController( override fun onTitleClick(source: CatalogueSource) { presenter.preferences.lastUsedSource().set(source.id) - router.pushController(SourceSearchController(manga?.toDomainManga(), source, presenter.query)) + router.pushController(SourceSearchController(manga, source, presenter.query)) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt index 323b0b4eb..79aa1926b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt @@ -9,15 +9,14 @@ import eu.kanade.domain.chapter.interactor.SyncChaptersWithSource import eu.kanade.domain.chapter.interactor.UpdateChapter import eu.kanade.domain.chapter.model.toChapterUpdate import eu.kanade.domain.manga.interactor.UpdateManga +import eu.kanade.domain.manga.model.Manga import eu.kanade.domain.manga.model.MangaUpdate import eu.kanade.domain.manga.model.hasCustomCover import eu.kanade.domain.manga.model.toDbManga +import eu.kanade.domain.manga.model.toMangaInfo import eu.kanade.domain.track.interactor.GetTracks import eu.kanade.domain.track.interactor.InsertTrack import eu.kanade.tachiyomi.data.cache.CoverCache -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.track.EnhancedTrackService import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.source.CatalogueSource @@ -76,7 +75,7 @@ class SearchPresenter( return GlobalSearchItem(source, results, source.id == manga.source) } - override fun networkToLocalManga(sManga: SManga, sourceId: Long): Manga { + override fun networkToLocalManga(sManga: SManga, sourceId: Long): eu.kanade.tachiyomi.data.database.models.Manga { val localManga = super.networkToLocalManga(sManga, sourceId) // For migration, displayed title should always match source rather than local DB localManga.title = sManga.title @@ -118,19 +117,16 @@ class SearchPresenter( val migrateTracks = MigrationFlags.hasTracks(flags) val migrateCustomCover = MigrationFlags.hasCustomCover(flags) - val prevDomainManga = prevManga.toDomainManga() ?: return - val domainManga = manga.toDomainManga() ?: return - try { - syncChaptersWithSource.await(sourceChapters, domainManga, source) + syncChaptersWithSource.await(sourceChapters, manga, source) } catch (e: Exception) { // Worst case, chapters won't be synced } // Update chapters read, bookmark and dateFetch if (migrateChapters) { - val prevMangaChapters = getChapterByMangaId.await(prevDomainManga.id) - val mangaChapters = getChapterByMangaId.await(domainManga.id) + val prevMangaChapters = getChapterByMangaId.await(prevManga.id) + val mangaChapters = getChapterByMangaId.await(manga.id) val maxChapterRead = prevMangaChapters .filter { it.read } @@ -163,41 +159,41 @@ class SearchPresenter( // Update categories if (migrateCategories) { - val categoryIds = getCategories.await(prevDomainManga.id).map { it.id } - setMangaCategories.await(domainManga.id, categoryIds) + val categoryIds = getCategories.await(prevManga.id).map { it.id } + setMangaCategories.await(manga.id, categoryIds) } // Update track if (migrateTracks) { - val tracks = getTracks.await(prevDomainManga.id).mapNotNull { track -> - val updatedTrack = track.copy(mangaId = domainManga.id) + val tracks = getTracks.await(prevManga.id).mapNotNull { track -> + val updatedTrack = track.copy(mangaId = manga.id) val service = enhancedServices - .firstOrNull { it.isTrackFrom(updatedTrack, prevDomainManga, prevSource) } + .firstOrNull { it.isTrackFrom(updatedTrack, prevManga, prevSource) } - if (service != null) service.migrateTrack(updatedTrack, domainManga, source) + if (service != null) service.migrateTrack(updatedTrack, manga, source) else updatedTrack } insertTrack.awaitAll(tracks) } if (replace) { - updateManga.await(MangaUpdate(prevDomainManga.id, favorite = false, dateAdded = 0)) + updateManga.await(MangaUpdate(prevManga.id, favorite = false, dateAdded = 0)) } // Update custom cover (recheck if custom cover exists) - if (migrateCustomCover && prevDomainManga.hasCustomCover()) { + if (migrateCustomCover && prevManga.hasCustomCover()) { @Suppress("BlockingMethodInNonBlockingContext") - coverCache.setCustomCoverToCache(domainManga.toDbManga(), coverCache.getCustomCoverFile(prevDomainManga.id).inputStream()) + coverCache.setCustomCoverToCache(manga.toDbManga(), coverCache.getCustomCoverFile(prevManga.id).inputStream()) } updateManga.await( MangaUpdate( - id = domainManga.id, + id = manga.id, favorite = true, - chapterFlags = prevDomainManga.chapterFlags, - viewerFlags = prevDomainManga.viewerFlags, - dateAdded = if (replace) prevDomainManga.dateAdded else Date().time, + chapterFlags = prevManga.chapterFlags, + viewerFlags = prevManga.viewerFlags, + dateAdded = if (replace) prevManga.dateAdded else Date().time, ), ) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SourceSearchController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SourceSearchController.kt index 16c910221..ce1e0198c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SourceSearchController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SourceSearchController.kt @@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.browse.migration.search import android.os.Bundle import android.view.View import eu.kanade.domain.manga.model.Manga -import eu.kanade.domain.manga.model.toDbManga import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourceController import eu.kanade.tachiyomi.ui.browse.source.browse.SourceItem @@ -29,7 +28,7 @@ class SourceSearchController( newManga = item.manga val searchController = router.backstack.findLast { it.controller.javaClass == SearchController::class.java }?.controller as SearchController? val dialog = - SearchController.MigrationDialog(oldManga?.toDbManga(), newManga?.toDbManga(), this) + SearchController.MigrationDialog(oldManga, newManga, this) dialog.targetController = searchController dialog.showDialog(router) return true diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchCardAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchCardAdapter.kt index d9a8f430c..3c620a6ff 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchCardAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchCardAdapter.kt @@ -1,7 +1,7 @@ package eu.kanade.tachiyomi.ui.browse.source.globalsearch import eu.davidea.flexibleadapter.FlexibleAdapter -import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.domain.manga.model.Manga /** * Adapter that holds the manga items from search results. diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchCardHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchCardHolder.kt index 58888764c..4a5222063 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchCardHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchCardHolder.kt @@ -4,8 +4,8 @@ import android.view.View import androidx.core.view.isVisible import coil.dispose import eu.davidea.viewholders.FlexibleViewHolder +import eu.kanade.domain.manga.model.Manga import eu.kanade.tachiyomi.data.coil.MangaCoverFetcher -import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.databinding.GlobalSearchControllerCardItemBinding import eu.kanade.tachiyomi.util.view.loadAutoPause diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchCardItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchCardItem.kt index 0a03eaf34..5296d2d6d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchCardItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchCardItem.kt @@ -5,8 +5,8 @@ import androidx.recyclerview.widget.RecyclerView import eu.davidea.flexibleadapter.FlexibleAdapter import eu.davidea.flexibleadapter.items.AbstractFlexibleItem import eu.davidea.flexibleadapter.items.IFlexible +import eu.kanade.domain.manga.model.Manga import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.data.database.models.Manga class GlobalSearchCardItem(val manga: Manga) : AbstractFlexibleItem() { @@ -35,6 +35,6 @@ class GlobalSearchCardItem(val manga: Manga) : AbstractFlexibleItem val item = mangaAdapter.getItem(holder.bindingAdapterPosition) - if (item != null && item.manga.id!! == manga.id!!) { + if (item != null && item.manga.id == manga.id) { return holder as GlobalSearchCardHolder } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchPresenter.kt index 616f02663..18c04019e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchPresenter.kt @@ -178,7 +178,7 @@ open class GlobalSearchPresenter( .map { it.mangas } .map { list -> list.map { networkToLocalManga(it, source.id) } } // Convert to local manga .doOnNext { fetchImage(it, source) } // Load manga covers - .map { list -> createCatalogueSearchItem(source, list.map { GlobalSearchCardItem(it) }) } + .map { list -> createCatalogueSearchItem(source, list.map { GlobalSearchCardItem(it.toDomainManga()!!) }) } }, 5, ) @@ -238,7 +238,7 @@ open class GlobalSearchPresenter( .subscribe( { (source, manga) -> @Suppress("DEPRECATION") - view?.onMangaInitialized(source, manga) + view?.onMangaInitialized(source, manga.toDomainManga()!!) }, { error -> logcat(LogPriority.ERROR, error) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index 392ec7d7b..dcb5ed42d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -9,6 +9,7 @@ import eu.kanade.domain.category.interactor.SetMangaCategories import eu.kanade.domain.category.model.Category import eu.kanade.domain.chapter.interactor.GetChapterByMangaId import eu.kanade.domain.chapter.interactor.UpdateChapter +import eu.kanade.domain.chapter.model.Chapter import eu.kanade.domain.chapter.model.ChapterUpdate import eu.kanade.domain.chapter.model.toDbChapter import eu.kanade.domain.manga.interactor.GetLibraryManga @@ -19,7 +20,6 @@ import eu.kanade.domain.manga.model.isLocal import eu.kanade.domain.manga.model.toDbManga import eu.kanade.domain.track.interactor.GetTracks import eu.kanade.tachiyomi.data.cache.CoverCache -import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.toDomainManga import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.preference.PreferencesHelper @@ -547,7 +547,7 @@ class LibraryPresenter( updateChapter.awaitAll(toUpdate) if (read && preferences.removeAfterMarkedAsRead()) { - deleteChapters(manga, chapters.map { it.toDbChapter() }) + deleteChapters(manga, chapters) } } } @@ -555,7 +555,7 @@ class LibraryPresenter( private fun deleteChapters(manga: Manga, chapters: List) { sourceManager.get(manga.source)?.let { source -> - downloadManager.deleteChapters(chapters, manga.toDbManga(), source) + downloadManager.deleteChapters(chapters.map { it.toDbChapter() }, manga.toDbManga(), source) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt index 0663f17cd..d7d5c74af 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt @@ -352,7 +352,7 @@ class MangaController : */ private fun migrateManga() { val manga = presenter.manga ?: return - val controller = SearchController(manga.toDbManga()) + val controller = SearchController(manga) controller.targetController = this router.pushController(controller) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt index def5afc5c..9bd46941b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt @@ -23,9 +23,7 @@ import eu.kanade.domain.track.interactor.GetTracks import eu.kanade.domain.track.interactor.InsertTrack import eu.kanade.domain.track.model.toDbTrack import eu.kanade.domain.track.model.toDomainTrack -import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Track -import eu.kanade.tachiyomi.data.database.models.toDomainChapter import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.download.model.Download import eu.kanade.tachiyomi.data.preference.PreferencesHelper @@ -375,7 +373,7 @@ class MangaPresenter( getTracks.subscribe(manga.id) .catch { logcat(LogPriority.ERROR, it) } .map { tracks -> - val loggedServicesId = loggedServices.map { it.id.toLong() } + val loggedServicesId = loggedServices.map { it.id } tracks.filter { it.syncId in loggedServicesId }.size } .collectLatest { trackingCount -> @@ -464,8 +462,7 @@ class MangaPresenter( ) if (manualFetch) { - val dbChapters = newChapters.map { it.toDbChapter() } - downloadNewChapters(dbChapters) + downloadNewChapters(newChapters) } } } catch (e: Throwable) { @@ -592,12 +589,12 @@ class MangaPresenter( } } - private fun downloadNewChapters(chapters: List) { + private fun downloadNewChapters(chapters: List) { presenterScope.launchIO { val manga = successState?.manga ?: return@launchIO val categories = getCategories.await(manga.id).map { it.id } if (chapters.isEmpty() || !manga.shouldDownloadNewChapters(categories, preferences)) return@launchIO - downloadChapters(chapters.map { it.toDomainChapter()!! }) + downloadChapters(chapters) } } @@ -784,7 +781,7 @@ class MangaPresenter( val manga = successState?.manga ?: return presenterScope.launchIO { - deleteTrack.await(manga.id, service.id.toLong()) + deleteTrack.await(manga.id, service.id) } }