From 7be9b49143d4634478e42e245f927226ea762237 Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Sun, 4 Dec 2022 02:43:52 +0700 Subject: [PATCH] Fix BrowseSourceScreen list/grid unnecessary reloads (#8661) --- .../migration/search/SourceSearchScreen.kt | 6 +-- .../source/browse/BrowseSourceScreen.kt | 6 +-- .../source/browse/BrowseSourceScreenModel.kt | 48 +++++++++++-------- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SourceSearchScreen.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SourceSearchScreen.kt index 04c71800e9..84bfe77cfc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SourceSearchScreen.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SourceSearchScreen.kt @@ -76,15 +76,13 @@ data class SourceSearchScreen( }, snackbarHost = { SnackbarHost(hostState = snackbarHostState) }, ) { paddingValues -> - val mangaList = remember(state.currentFilter) { - screenModel.getMangaListFlow(state.currentFilter) - }.collectAsLazyPagingItems() + val pagingFlow by screenModel.mangaPagerFlowFlow.collectAsState() val openMigrateDialog: (Manga) -> Unit = { screenModel.setDialog(BrowseSourceScreenModel.Dialog.Migrate(it)) } BrowseSourceContent( source = screenModel.source, - mangaList = mangaList, + mangaList = pagingFlow.collectAsLazyPagingItems(), columns = screenModel.getColumnsPreference(LocalConfiguration.current.orientation), displayMode = screenModel.displayMode, snackbarHostState = snackbarHostState, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreen.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreen.kt index 1c37126b1c..fc6ae5ea07 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreen.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreen.kt @@ -173,13 +173,11 @@ data class BrowseSourceScreen( }, snackbarHost = { SnackbarHost(hostState = snackbarHostState) }, ) { paddingValues -> - val mangaList = remember(state.currentFilter) { - screenModel.getMangaListFlow(state.currentFilter) - }.collectAsLazyPagingItems() + val pagingFlow by screenModel.mangaPagerFlowFlow.collectAsState() BrowseSourceContent( source = screenModel.source, - mangaList = mangaList, + mangaList = pagingFlow.collectAsLazyPagingItems(), columns = screenModel.getColumnsPreference(LocalConfiguration.current.orientation), displayMode = screenModel.displayMode, snackbarHostState = snackbarHostState, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreenModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreenModel.kt index 45c6f817a3..252a58de93 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreenModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreenModel.kt @@ -9,7 +9,6 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.unit.dp import androidx.paging.Pager import androidx.paging.PagingConfig -import androidx.paging.PagingData import androidx.paging.cachedIn import androidx.paging.map import cafe.adriel.voyager.core.model.StateScreenModel @@ -64,8 +63,9 @@ 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.flow.Flow -import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.map @@ -114,6 +114,30 @@ class BrowseSourceScreenModel( */ private var filterSheet: SourceFilterSheet? = null + /** + * Flow of Pager flow tied to [State.currentFilter] + */ + val mangaPagerFlowFlow = state.map { it.currentFilter } + .distinctUntilChanged() + .map { currentFilter -> + Pager( + PagingConfig(pageSize = 25), + ) { + getRemoteManga.subscribe(sourceId, currentFilter.query ?: "", currentFilter.filters) + }.flow + .map { pagingData -> + pagingData.map { sManga -> + val dbManga = withIOContext { networkToLocalManga.await(sManga.toDomainManga(sourceId)) } + getManga.subscribe(dbManga.url, dbManga.source) + .filterNotNull() + .onEach { initializeManga(it) } + .stateIn(coroutineScope) + } + } + .cachedIn(coroutineScope) + } + .stateIn(coroutineScope, SharingStarted.Lazily, emptyFlow()) + init { mutableState.update { it.copy(filters = source.getFilterList()) } } @@ -128,24 +152,6 @@ class BrowseSourceScreenModel( return if (columns == 0) GridCells.Adaptive(128.dp) else GridCells.Fixed(columns) } - fun getMangaListFlow(currentFilter: Filter): Flow>> { - return Pager( - PagingConfig(pageSize = 25), - ) { - getRemoteManga.subscribe(sourceId, currentFilter.query ?: "", currentFilter.filters) - }.flow - .map { pagingData -> - pagingData.map { sManga -> - val dbManga = withIOContext { networkToLocalManga.await(sManga.toDomainManga(sourceId)) } - getManga.subscribe(dbManga.url, dbManga.source) - .filterNotNull() - .onEach { initializeManga(it) } - .stateIn(coroutineScope) - } - } - .cachedIn(coroutineScope) - } - fun reset() { mutableState.update { it.copy(filters = source.getFilterList()) } }