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 9cf50b2af..58f4a91bb 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 @@ -14,7 +14,6 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.platform.LocalUriHandler -import androidx.paging.compose.collectAsLazyPagingItems import cafe.adriel.voyager.core.model.rememberScreenModel import cafe.adriel.voyager.navigator.LocalNavigator import cafe.adriel.voyager.navigator.currentOrThrow @@ -29,6 +28,7 @@ import eu.kanade.tachiyomi.ui.home.HomeScreen import eu.kanade.tachiyomi.ui.manga.MangaScreen import eu.kanade.tachiyomi.ui.webview.WebViewScreen import kotlinx.coroutines.launch +import mihon.presentation.core.util.collectAsLazyPagingItems import tachiyomi.core.common.Constants import tachiyomi.domain.manga.model.Manga import tachiyomi.i18n.MR @@ -86,7 +86,7 @@ data class SourceSearchScreen( } BrowseSourceContent( source = screenModel.source, - mangaList = screenModel.mangaPagerFlow.collectAsLazyPagingItems(), + mangaList = screenModel.mangaPagerFlowFlow.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 283351093..af517409f 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 @@ -31,7 +31,6 @@ import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.platform.LocalUriHandler -import androidx.paging.compose.collectAsLazyPagingItems import cafe.adriel.voyager.core.model.rememberScreenModel import cafe.adriel.voyager.navigator.LocalNavigator import cafe.adriel.voyager.navigator.currentOrThrow @@ -56,6 +55,7 @@ import eu.kanade.tachiyomi.ui.webview.WebViewScreen import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.receiveAsFlow +import mihon.presentation.core.util.collectAsLazyPagingItems import tachiyomi.core.common.Constants import tachiyomi.core.common.util.lang.launchIO import tachiyomi.domain.source.model.StubSource @@ -208,7 +208,7 @@ data class BrowseSourceScreen( ) { paddingValues -> BrowseSourceContent( source = screenModel.source, - mangaList = screenModel.mangaPagerFlow.collectAsLazyPagingItems(), + mangaList = screenModel.mangaPagerFlowFlow.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 5761c5320..dff062503 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 @@ -26,10 +26,11 @@ import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.util.removeCovers import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.toImmutableList +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.flatMapLatest import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.update @@ -105,9 +106,9 @@ class BrowseSourceScreenModel( * Flow of Pager flow tied to [State.listing] */ private val hideInLibraryItems = sourcePreferences.hideInLibraryItems().get() - val mangaPagerFlow = state.map { it.listing } + val mangaPagerFlowFlow = state.map { it.listing } .distinctUntilChanged() - .flatMapLatest { listing -> + .map { listing -> Pager(PagingConfig(pageSize = 25)) { getRemoteManga.subscribe(sourceId, listing.query ?: "", listing.filters) }.flow.map { pagingData -> @@ -119,8 +120,9 @@ class BrowseSourceScreenModel( } .filter { !hideInLibraryItems || !it.value.favorite } } + .cachedIn(ioCoroutineScope) } - .cachedIn(ioCoroutineScope) + .stateIn(ioCoroutineScope, SharingStarted.Lazily, emptyFlow()) fun getColumnsPreference(orientation: Int): GridCells { val isLandscape = orientation == Configuration.ORIENTATION_LANDSCAPE diff --git a/presentation-core/build.gradle.kts b/presentation-core/build.gradle.kts index f55a8c060..332c1fffe 100644 --- a/presentation-core/build.gradle.kts +++ b/presentation-core/build.gradle.kts @@ -42,5 +42,7 @@ dependencies { implementation(compose.ui.tooling.preview) implementation(compose.ui.util) + implementation(androidx.paging.runtime) + implementation(androidx.paging.compose) implementation(kotlinx.immutables) } diff --git a/presentation-core/src/main/java/mihon/presentation/core/util/PagingDataUtil.kt b/presentation-core/src/main/java/mihon/presentation/core/util/PagingDataUtil.kt new file mode 100644 index 000000000..2ca50f9b7 --- /dev/null +++ b/presentation-core/src/main/java/mihon/presentation/core/util/PagingDataUtil.kt @@ -0,0 +1,16 @@ +package mihon.presentation.core.util + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.paging.PagingData +import androidx.paging.compose.LazyPagingItems +import androidx.paging.compose.collectAsLazyPagingItems +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.StateFlow + +@Composable +fun StateFlow>>.collectAsLazyPagingItems(): LazyPagingItems { + val flow by collectAsState() + return flow.collectAsLazyPagingItems() +}