From c5ca739b4926362967d02323ceda56072d992134 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 13 Dec 2020 20:47:48 -0500 Subject: [PATCH] Fix loading fallback thumbnails in browse view (closes #4127) --- .../java/eu/kanade/tachiyomi/source/Source.kt | 6 +- .../source/browse/BrowseSourceController.kt | 1 - .../source/browse/BrowseSourcePresenter.kt | 72 +++++++------------ 3 files changed, 29 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/source/Source.kt b/app/src/main/java/eu/kanade/tachiyomi/source/Source.kt index 5b1dfbdee..17ae6b7f4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/source/Source.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/source/Source.kt @@ -64,8 +64,10 @@ interface Source : tachiyomi.source.Source { */ @Suppress("DEPRECATION") override suspend fun getMangaDetails(manga: MangaInfo): MangaInfo { - return fetchMangaDetails(manga.toSManga()).awaitSingle() - .toMangaInfo() + val sManga = manga.toSManga() + val networkManga = fetchMangaDetails(sManga).awaitSingle() + sManga.copyFrom(networkManga) + return sManga.toMangaInfo() } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt index d2ab129c2..6dbbd4732 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt @@ -459,7 +459,6 @@ open class BrowseSourceController(bundle: Bundle) : val adapter = adapter ?: return preferences.sourceDisplayMode().set(mode) - presenter.refreshDisplayMode() activity?.invalidateOptionsMenu() setupRecycler(view) 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 13f54b414..739fb8c51 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 @@ -33,10 +33,13 @@ import eu.kanade.tachiyomi.util.chapter.ChapterSettingsHelper import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.launchUI import eu.kanade.tachiyomi.util.removeCovers -import kotlinx.coroutines.Job import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.asFlow +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach -import kotlinx.coroutines.isActive import rx.Observable import rx.Subscription import rx.android.schedulers.AndroidSchedulers @@ -105,11 +108,6 @@ open class BrowseSourcePresenter( */ private var pageSubscription: Subscription? = null - /** - * Job to initialize manga details. - */ - private var initializerJob: Job? = null - override fun onCreate(savedState: Bundle?) { super.onCreate(savedState) @@ -139,8 +137,6 @@ open class BrowseSourcePresenter( this.query = query this.appliedFilters = filters - initializeManga() - // Create a new pager. pager = createPager(query, filters) @@ -192,27 +188,6 @@ open class BrowseSourcePresenter( return pager.hasNextPage } - /** - * Subscribes to the initializer of manga details and updates the view if needed. - */ - private fun initializeManga() { - initializerJob?.cancel() - initializerJob = launchIO { - mangaDetailsFlow - .onEach { mangas -> - if (!isActive) return@onEach - - try { - mangas.filter { it.thumbnail_url == null && !it.initialized } - .map { getMangaDetails(it) } - .forEach { launchUI { view?.onMangaInitialized(it) } } - } catch (error: Exception) { - launchUI { Timber.e(error) } - } - } - } - } - /** * Returns a manga from the database for the given manga from network. It creates a new entry * if the manga is not yet in the database. @@ -238,7 +213,19 @@ open class BrowseSourcePresenter( * @param mangas the list of manga to initialize. */ fun initializeMangas(mangas: List) { - launchIO { mangaDetailsFlow.emit(mangas) } + launchIO { + mangas.asFlow() + .filter { it.thumbnail_url == null && !it.initialized } + .map { getMangaDetails(it) } + .onEach { + launchUI { + @Suppress("DEPRECATION") + view?.onMangaInitialized(it) + } + } + .catch { e -> Timber.e(e) } + .collect() + } } /** @@ -248,17 +235,15 @@ open class BrowseSourcePresenter( * @return the initialized manga */ private suspend fun getMangaDetails(manga: Manga): Manga { - return try { - source.getMangaDetails(manga.toMangaInfo()) - .let { networkManga -> - manga.copyFrom(networkManga.toSManga()) - manga.initialized = true - db.insertManga(manga).executeAsBlocking() - manga - } + try { + val networkManga = source.getMangaDetails(manga.toMangaInfo()) + manga.copyFrom(networkManga.toSManga()) + manga.initialized = true + db.insertManga(manga).executeAsBlocking() } catch (e: Exception) { - manga + Timber.e(e) } + return manga } /** @@ -282,13 +267,6 @@ open class BrowseSourcePresenter( db.insertManga(manga).executeAsBlocking() } - /** - * Refreshes the active display mode. - */ - fun refreshDisplayMode() { - initializeManga() - } - /** * Set the filter states for the current source. *