Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Experiments with rememberRetainedCoroutineScope
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbanes committed Mar 21, 2024
1 parent 11d7d02 commit 3f2cc71
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
package app.tivi.common.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import app.cash.paging.CombinedLoadStates
import app.cash.paging.LoadStateError
import app.cash.paging.PagingData
import app.cash.paging.cachedIn
import com.slack.circuit.retained.rememberRetained
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow

Expand All @@ -31,5 +31,5 @@ fun CombinedLoadStates.refreshErrorOrNull(): UiMessage? {

@Composable
inline fun <T : Any> Flow<PagingData<T>>.rememberCachedPagingFlow(
scope: CoroutineScope = rememberCoroutineScope(),
): Flow<PagingData<T>> = remember(this, scope) { cachedIn(scope) }
scope: CoroutineScope = rememberRetainedCoroutineScope(),
): Flow<PagingData<T>> = rememberRetained(scope) { cachedIn(scope) }
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
package app.tivi.common.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.RememberObserver
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import com.slack.circuit.retained.rememberRetained
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel

/**
* Returns a [StableCoroutineScope] around a [androidx.compose.runtime.rememberCoroutineScope].
Expand All @@ -22,3 +27,23 @@ fun rememberCoroutineScope(): StableCoroutineScope {
/** @see rememberCoroutineScope */
@Stable
class StableCoroutineScope(scope: CoroutineScope) : CoroutineScope by scope

@Composable
fun rememberRetainedCoroutineScope(): StableCoroutineScope {
return rememberRetained("coroutine_scope") {
object : RememberObserver {
val scope = StableCoroutineScope(CoroutineScope(Dispatchers.Main + Job()))

override fun onAbandoned() = onForgotten()

override fun onForgotten() {
println("[RetainedCoroutineScope] Forgotten: Cancelling scope")
scope.cancel()
}

override fun onRemembered() {
println("[RetainedCoroutineScope] Remembered")
}
}
}.scope
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class LibraryPresenter(
val uiMessageManager = remember { UiMessageManager() }

val items = observePagedLibraryShows.value.flow
.rememberCachedPagingFlow(scope)
.rememberCachedPagingFlow()
.collectAsLazyPagingItems()

var filter by remember { mutableStateOf<String?>(null) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.runtime.LaunchedEffect
import app.cash.paging.PagingConfig
import app.cash.paging.compose.collectAsLazyPagingItems
import app.tivi.common.compose.rememberCachedPagingFlow
import app.tivi.common.compose.rememberRetainedCoroutineScope
import app.tivi.domain.observers.ObservePagedPopularShows
import app.tivi.screens.PopularShowsScreen
import app.tivi.screens.ShowDetailsScreen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.runtime.LaunchedEffect
import app.cash.paging.PagingConfig
import app.cash.paging.compose.collectAsLazyPagingItems
import app.tivi.common.compose.rememberCachedPagingFlow
import app.tivi.common.compose.rememberRetainedCoroutineScope
import app.tivi.domain.observers.ObservePagedRecommendedShows
import app.tivi.screens.RecommendedShowsScreen
import app.tivi.screens.ShowDetailsScreen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.runtime.LaunchedEffect
import app.cash.paging.PagingConfig
import app.cash.paging.compose.collectAsLazyPagingItems
import app.tivi.common.compose.rememberCachedPagingFlow
import app.tivi.common.compose.rememberRetainedCoroutineScope
import app.tivi.domain.observers.ObservePagedTrendingShows
import app.tivi.screens.ShowDetailsScreen
import app.tivi.screens.TrendingShowsScreen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import app.tivi.common.compose.UiMessage
import app.tivi.common.compose.UiMessageManager
import app.tivi.common.compose.rememberCachedPagingFlow
import app.tivi.common.compose.rememberCoroutineScope
import app.tivi.common.compose.rememberRetainedCoroutineScope
import app.tivi.data.models.SortOption
import app.tivi.data.traktauth.TraktAuthState
import app.tivi.domain.interactors.GetTraktAuthState
Expand Down Expand Up @@ -72,7 +73,7 @@ class UpNextPresenter(
val uiMessageManager = remember { UiMessageManager() }

val items = observePagedUpNextShows.value.flow
.rememberCachedPagingFlow(scope)
.rememberCachedPagingFlow()
.collectAsLazyPagingItems()

var sort by remember { mutableStateOf(SortOption.LAST_WATCHED) }
Expand Down

0 comments on commit 3f2cc71

Please sign in to comment.