From b04d1e5f50b1a3622e8bfde3ddced7e5a7cd8e2e Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Sat, 1 Oct 2022 21:01:25 +0600 Subject: [PATCH] Reduce some usages of `toDbManga` (#8116) Co-Authored-By: stevenyomi <95685115+stevenyomi@users.noreply.github.com> Co-authored-by: stevenyomi <95685115+stevenyomi@users.noreply.github.com> --- .../eu/kanade/domain/manga/model/Manga.kt | 22 +++++++++++++++++++ .../source/browse/BrowseSourcePresenter.kt | 19 ++++++---------- .../kanade/tachiyomi/util/MangaExtensions.kt | 2 +- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/eu/kanade/domain/manga/model/Manga.kt b/app/src/main/java/eu/kanade/domain/manga/model/Manga.kt index 93eddd7bf4..ba577518cd 100644 --- a/app/src/main/java/eu/kanade/domain/manga/model/Manga.kt +++ b/app/src/main/java/eu/kanade/domain/manga/model/Manga.kt @@ -99,6 +99,28 @@ data class Manga( it.initialized = initialized } + fun copyFrom(other: SManga): Manga { + val author = other.author ?: author + val artist = other.artist ?: artist + val description = other.description ?: description + val genres = if (other.genre != null) { + other.getGenres() + } else { + genre + } + val thumbnailUrl = other.thumbnail_url ?: thumbnailUrl + return this.copy( + author = author, + artist = artist, + description = description, + genre = genres, + thumbnailUrl = thumbnailUrl, + status = other.status.toLong(), + updateStrategy = other.update_strategy, + initialized = other.initialized && initialized, + ) + } + companion object { // Generic filter that does not filter anything const val SHOW_ALL = 0x00000000L 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 a0df5444d0..f57ae18c98 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 @@ -66,15 +66,14 @@ import eu.kanade.tachiyomi.ui.browse.source.filter.TriStateItem import eu.kanade.tachiyomi.ui.browse.source.filter.TriStateSectionItem import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.withIOContext +import eu.kanade.tachiyomi.util.lang.withNonCancellableContext import eu.kanade.tachiyomi.util.removeCovers import eu.kanade.tachiyomi.util.system.logcat -import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import logcat.LogPriority import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -212,17 +211,13 @@ open class BrowseSourcePresenter( */ private suspend fun initializeManga(manga: DomainManga) { if (manga.thumbnailUrl != null || manga.initialized) return - withContext(NonCancellable) { - val db = manga.toDbManga() + withNonCancellableContext { try { - val networkManga = source!!.getMangaDetails(db.copy()) - db.copyFrom(networkManga) - db.initialized = true - updateManga.await( - db - .toDomainManga() - ?.toMangaUpdate()!!, - ) + val networkManga = source!!.getMangaDetails(manga.toSManga()) + val updatedManga = manga.copyFrom(networkManga) + .copy(initialized = true) + + updateManga.await(updatedManga.toMangaUpdate()) } catch (e: Exception) { logcat(LogPriority.ERROR, e) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/MangaExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/MangaExtensions.kt index 4c60ab0ca6..c0e5f93e63 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/MangaExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/MangaExtensions.kt @@ -89,7 +89,7 @@ suspend fun DomainManga.editCover( coverCache: CoverCache = Injekt.get(), ) { if (isLocal()) { - LocalSource.updateCover(context, toDbManga(), stream) + LocalSource.updateCover(context, toSManga(), stream) updateManga.awaitUpdateCoverLastModified(id) } else if (favorite) { coverCache.setCustomCoverToCache(toDbManga(), stream)