fix(worker): remove observer after leaving composition

This commit is contained in:
junkfood 2024-02-13 18:24:28 +08:00
parent 6ed42a24e8
commit 8f4d24157e
No known key found for this signature in database
GPG Key ID: 2EA5B648DB112A34
2 changed files with 18 additions and 4 deletions

View File

@ -25,6 +25,7 @@ import androidx.compose.material.icons.rounded.Refresh
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
@ -115,8 +116,14 @@ fun FeedsPage(
val owner = LocalLifecycleOwner.current
var isSyncing by remember { mutableStateOf(false) }
homeViewModel.syncWorkLiveData.observe(owner) {
it?.let { isSyncing = it.any { it.state == WorkInfo.State.RUNNING } }
DisposableEffect(owner) {
homeViewModel.syncWorkLiveData.observe(owner) { workInfoList ->
workInfoList.let {
isSyncing = it.any { workInfo -> workInfo.state == WorkInfo.State.RUNNING }
}
}
onDispose { homeViewModel.syncWorkLiveData.removeObservers(owner) }
}
val infiniteTransition = rememberInfiniteTransition()

View File

@ -69,10 +69,17 @@ fun FlowPage(
val owner = LocalLifecycleOwner.current
var isSyncing by remember { mutableStateOf(false) }
homeViewModel.syncWorkLiveData.observe(owner) {
it?.let { isSyncing = it.any { it.state == WorkInfo.State.RUNNING } }
DisposableEffect(owner) {
homeViewModel.syncWorkLiveData.observe(owner) { workInfoList ->
workInfoList.let {
isSyncing = it.any { workInfo -> workInfo.state == WorkInfo.State.RUNNING }
}
}
onDispose { homeViewModel.syncWorkLiveData.removeObservers(owner) }
}
val onToggleStarred: State<(ArticleWithFeed) -> Unit> = rememberUpdatedState {
flowViewModel.updateStarredStatus(
articleId = it.article.id,