More domain model usage

This commit is contained in:
arkon 2022-07-03 16:25:51 -04:00
parent a3ab8746bf
commit 3791d82540
14 changed files with 46 additions and 60 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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<GetManga>()
.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))
}
}

View File

@ -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,
),
)
}

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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<GlobalSearchCardHolder>() {
@ -35,6 +35,6 @@ class GlobalSearchCardItem(val manga: Manga) : AbstractFlexibleItem<GlobalSearch
}
override fun hashCode(): Int {
return manga.id?.toInt() ?: 0
return manga.id.hashCode()
}
}

View File

@ -10,8 +10,8 @@ import androidx.appcompat.widget.SearchView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import dev.chrisbanes.insetter.applyInsetter
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.preference.PreferencesHelper
import eu.kanade.tachiyomi.databinding.GlobalSearchControllerBinding
import eu.kanade.tachiyomi.source.CatalogueSource
@ -65,7 +65,7 @@ open class GlobalSearchController(
* @param manga clicked item containing manga information.
*/
override fun onMangaClick(manga: Manga) {
router.pushController(MangaController(manga.id!!, true))
router.pushController(MangaController(manga.id, true))
}
/**

View File

@ -4,7 +4,7 @@ import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import eu.davidea.viewholders.FlexibleViewHolder
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.domain.manga.model.Manga
import eu.kanade.tachiyomi.databinding.GlobalSearchControllerCardBinding
import eu.kanade.tachiyomi.source.LocalSource
import eu.kanade.tachiyomi.util.system.LocaleHelper
@ -92,7 +92,7 @@ class GlobalSearchHolder(view: View, val adapter: GlobalSearchAdapter) :
private fun getHolder(manga: Manga): GlobalSearchCardHolder? {
mangaAdapter.allBoundViewHolders.forEach { holder ->
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
}
}

View File

@ -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)

View File

@ -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<Chapter>) {
sourceManager.get(manga.source)?.let { source ->
downloadManager.deleteChapters(chapters, manga.toDbManga(), source)
downloadManager.deleteChapters(chapters.map { it.toDbChapter() }, manga.toDbManga(), source)
}
}

View File

@ -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)
}

View File

@ -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<Chapter>) {
private fun downloadNewChapters(chapters: List<DomainChapter>) {
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)
}
}