-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix] Collect paging data with lifecycle https://slack-chats.kotlinla…
- Loading branch information
Showing
3 changed files
with
26 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.skyd.rays.ext | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.lifecycle.compose.LocalLifecycleOwner | ||
import androidx.lifecycle.flowWithLifecycle | ||
import androidx.paging.PagingData | ||
import androidx.paging.compose.LazyPagingItems | ||
import androidx.paging.compose.collectAsLazyPagingItems | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.coroutines.EmptyCoroutineContext | ||
|
||
@Composable | ||
fun <T : Any> Flow<PagingData<T>>.collectAsLazyPagingItemsWithLifecycle( | ||
context: CoroutineContext = EmptyCoroutineContext | ||
): LazyPagingItems<T> { | ||
val lifecycle = LocalLifecycleOwner.current.lifecycle | ||
return remember(this, lifecycle) { | ||
flowWithLifecycle(lifecycle) | ||
}.collectAsLazyPagingItems(context) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters