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

View File

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

View File

@ -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<PagingData<StateFlow<Manga>>> {
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()) }
}