Fix BrowseSourceScreen list/grid unnecessary reloads (#8661)

This commit is contained in:
Ivan Iskandar 2022-12-04 02:43:52 +07:00 committed by GitHub
parent 059a79debb
commit 7be9b49143
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 29 deletions

View File

@ -76,15 +76,13 @@ data class SourceSearchScreen(
}, },
snackbarHost = { SnackbarHost(hostState = snackbarHostState) }, snackbarHost = { SnackbarHost(hostState = snackbarHostState) },
) { paddingValues -> ) { paddingValues ->
val mangaList = remember(state.currentFilter) { val pagingFlow by screenModel.mangaPagerFlowFlow.collectAsState()
screenModel.getMangaListFlow(state.currentFilter)
}.collectAsLazyPagingItems()
val openMigrateDialog: (Manga) -> Unit = { val openMigrateDialog: (Manga) -> Unit = {
screenModel.setDialog(BrowseSourceScreenModel.Dialog.Migrate(it)) screenModel.setDialog(BrowseSourceScreenModel.Dialog.Migrate(it))
} }
BrowseSourceContent( BrowseSourceContent(
source = screenModel.source, source = screenModel.source,
mangaList = mangaList, mangaList = pagingFlow.collectAsLazyPagingItems(),
columns = screenModel.getColumnsPreference(LocalConfiguration.current.orientation), columns = screenModel.getColumnsPreference(LocalConfiguration.current.orientation),
displayMode = screenModel.displayMode, displayMode = screenModel.displayMode,
snackbarHostState = snackbarHostState, snackbarHostState = snackbarHostState,

View File

@ -173,13 +173,11 @@ data class BrowseSourceScreen(
}, },
snackbarHost = { SnackbarHost(hostState = snackbarHostState) }, snackbarHost = { SnackbarHost(hostState = snackbarHostState) },
) { paddingValues -> ) { paddingValues ->
val mangaList = remember(state.currentFilter) { val pagingFlow by screenModel.mangaPagerFlowFlow.collectAsState()
screenModel.getMangaListFlow(state.currentFilter)
}.collectAsLazyPagingItems()
BrowseSourceContent( BrowseSourceContent(
source = screenModel.source, source = screenModel.source,
mangaList = mangaList, mangaList = pagingFlow.collectAsLazyPagingItems(),
columns = screenModel.getColumnsPreference(LocalConfiguration.current.orientation), columns = screenModel.getColumnsPreference(LocalConfiguration.current.orientation),
displayMode = screenModel.displayMode, displayMode = screenModel.displayMode,
snackbarHostState = snackbarHostState, snackbarHostState = snackbarHostState,

View File

@ -9,7 +9,6 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.paging.Pager import androidx.paging.Pager
import androidx.paging.PagingConfig import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn import androidx.paging.cachedIn
import androidx.paging.map import androidx.paging.map
import cafe.adriel.voyager.core.model.StateScreenModel 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.lang.withNonCancellableContext
import eu.kanade.tachiyomi.util.removeCovers import eu.kanade.tachiyomi.util.removeCovers
import eu.kanade.tachiyomi.util.system.logcat import eu.kanade.tachiyomi.util.system.logcat
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
@ -114,22 +114,13 @@ class BrowseSourceScreenModel(
*/ */
private var filterSheet: SourceFilterSheet? = null private var filterSheet: SourceFilterSheet? = null
init { /**
mutableState.update { it.copy(filters = source.getFilterList()) } * Flow of Pager flow tied to [State.currentFilter]
} */
val mangaPagerFlowFlow = state.map { it.currentFilter }
fun getColumnsPreference(orientation: Int): GridCells { .distinctUntilChanged()
val isLandscape = orientation == Configuration.ORIENTATION_LANDSCAPE .map { currentFilter ->
val columns = if (isLandscape) { Pager(
libraryPreferences.landscapeColumns()
} else {
libraryPreferences.portraitColumns()
}.get()
return if (columns == 0) GridCells.Adaptive(128.dp) else GridCells.Fixed(columns)
}
fun getMangaListFlow(currentFilter: Filter): Flow<PagingData<StateFlow<Manga>>> {
return Pager(
PagingConfig(pageSize = 25), PagingConfig(pageSize = 25),
) { ) {
getRemoteManga.subscribe(sourceId, currentFilter.query ?: "", currentFilter.filters) getRemoteManga.subscribe(sourceId, currentFilter.query ?: "", currentFilter.filters)
@ -145,6 +136,21 @@ class BrowseSourceScreenModel(
} }
.cachedIn(coroutineScope) .cachedIn(coroutineScope)
} }
.stateIn(coroutineScope, SharingStarted.Lazily, emptyFlow())
init {
mutableState.update { it.copy(filters = source.getFilterList()) }
}
fun getColumnsPreference(orientation: Int): GridCells {
val isLandscape = orientation == Configuration.ORIENTATION_LANDSCAPE
val columns = if (isLandscape) {
libraryPreferences.landscapeColumns()
} else {
libraryPreferences.portraitColumns()
}.get()
return if (columns == 0) GridCells.Adaptive(128.dp) else GridCells.Fixed(columns)
}
fun reset() { fun reset() {
mutableState.update { it.copy(filters = source.getFilterList()) } mutableState.update { it.copy(filters = source.getFilterList()) }