mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-11-04 08:08:55 +01:00 
			
		
		
		
	Fix BrowseSourceScreen list/grid unnecessary reloads (#8661)
This commit is contained in:
		@@ -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,
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
 
 | 
			
		||||
@@ -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()) }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user