Fix BrowseSourceScreen list/grid unnecessary reloads (#8661)

(cherry picked from commit 7be9b49143d4634478e42e245f927226ea762237)

# Conflicts:
#	app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceScreenModel.kt
This commit is contained in:
Ivan Iskandar 2022-12-04 02:43:52 +07:00 committed by Jobobby04
parent 988e2d5343
commit b89c2f76a1
6 changed files with 42 additions and 45 deletions

View File

@ -64,9 +64,7 @@ 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 = {
// SY --> // SY -->
navigator.items navigator.items
@ -78,7 +76,7 @@ data class SourceSearchScreen(
} }
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),
// SY --> // SY -->
ehentaiBrowseDisplayMode = screenModel.ehentaiBrowseDisplayMode, ehentaiBrowseDisplayMode = screenModel.ehentaiBrowseDisplayMode,

View File

@ -203,13 +203,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),
// SY --> // SY -->
ehentaiBrowseDisplayMode = screenModel.ehentaiBrowseDisplayMode, ehentaiBrowseDisplayMode = screenModel.ehentaiBrowseDisplayMode,

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
@ -83,8 +82,10 @@ import exh.savedsearches.models.SavedSearch
import exh.source.getMainSource import exh.source.getMainSource
import exh.util.nullIfBlank import exh.util.nullIfBlank
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
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.launchIn import kotlinx.coroutines.flow.launchIn
@ -152,6 +153,35 @@ open class BrowseSourceScreenModel(
*/ */
private var filterSheet: SourceFilterSheet? = null 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),
) {
// SY -->
createSourcePagingSource(currentFilter.query ?: "", currentFilter.filters)
// SY <--
}.flow
.map { pagingData ->
pagingData.map { (sManga, metadata) ->
val dbManga = withIOContext { networkToLocalManga.await(sManga.toDomainManga(sourceId)) }
getManga.subscribe(dbManga.url, dbManga.source)
.filterNotNull()
.onEach { initializeManga(it) }
// SY -->
.combineMetadata(dbManga, metadata)
// SY <--
.stateIn(coroutineScope)
}
}
.cachedIn(coroutineScope)
}
.stateIn(coroutineScope, SharingStarted.Lazily, emptyFlow())
// SY --> // SY -->
val ehentaiBrowseDisplayMode by unsortedPreferences.enhancedEHentaiView().asState(coroutineScope) val ehentaiBrowseDisplayMode by unsortedPreferences.enhancedEHentaiView().asState(coroutineScope)
@ -198,29 +228,6 @@ open class BrowseSourceScreenModel(
return if (columns == 0) GridCells.Adaptive(128.dp) else GridCells.Fixed(columns) return if (columns == 0) GridCells.Adaptive(128.dp) else GridCells.Fixed(columns)
} }
fun getMangaListFlow(currentFilter: Filter): Flow<PagingData<StateFlow</* SY --> */Pair<Manga, RaisedSearchMetadata?>/* SY <-- */>>> {
return Pager(
PagingConfig(pageSize = 25),
) {
// SY -->
createSourcePagingSource(currentFilter.query ?: "", currentFilter.filters)
// SY <--
}.flow
.map { pagingData ->
pagingData.map { (sManga, metadata) ->
val dbManga = withIOContext { networkToLocalManga.await(sManga.toDomainManga(sourceId)) }
getManga.subscribe(dbManga.url, dbManga.source)
.filterNotNull()
.onEach { initializeManga(it) }
// SY -->
.combineMetadata(dbManga, metadata)
// SY <--
.stateIn(coroutineScope)
}
}
.cachedIn(coroutineScope)
}
// SY --> // SY -->
open fun Flow<Manga>.combineMetadata(dbManga: Manga, metadata: RaisedSearchMetadata?): Flow<Pair<Manga, RaisedSearchMetadata?>> { open fun Flow<Manga>.combineMetadata(dbManga: Manga, metadata: RaisedSearchMetadata?): Flow<Pair<Manga, RaisedSearchMetadata?>> {
val metadataSource = source.getMainSource<MetadataSource<*, *>>() val metadataSource = source.getMainSource<MetadataSource<*, *>>()

View File

@ -54,13 +54,11 @@ class MangaDexFollowsScreen(private val sourceId: Long) : Screen {
SnackbarHost(hostState = snackbarHostState) 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),
// SY --> // SY -->
ehentaiBrowseDisplayMode = screenModel.ehentaiBrowseDisplayMode, ehentaiBrowseDisplayMode = screenModel.ehentaiBrowseDisplayMode,

View File

@ -46,13 +46,11 @@ class MangaDexSimilarScreen(val mangaId: Long, val sourceId: Long) : Screen {
}, },
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),
// SY --> // SY -->
ehentaiBrowseDisplayMode = false, ehentaiBrowseDisplayMode = false,

View File

@ -45,13 +45,11 @@ class RecommendsScreen(val mangaId: Long, val sourceId: Long) : Screen {
}, },
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),
// SY --> // SY -->
ehentaiBrowseDisplayMode = false, ehentaiBrowseDisplayMode = false,