From f7f2aa929917ae5b2ec656cfa545709ff3b146d6 Mon Sep 17 00:00:00 2001 From: pachuho Date: Wed, 3 Jul 2024 11:22:25 +0900 Subject: [PATCH] Modify collectAsState to collectAsStateWithLifecycle --- app/build.gradle.kts | 1 + .../apps/sunflower/compose/garden/GardenScreen.kt | 4 ++-- .../compose/plantdetail/PlantDetailView.kt | 5 +++-- .../viewmodels/GardenPlantingListViewModel.kt | 15 ++++++++++++--- .../sunflower/viewmodels/PlantDetailViewModel.kt | 7 +++++++ gradle/libs.versions.toml | 1 + 6 files changed, 26 insertions(+), 7 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 51d6e0480..74880ca30 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -142,6 +142,7 @@ dependencies { implementation(libs.androidx.compose.ui.tooling.preview) implementation(libs.androidx.compose.runtime.livedata) implementation(libs.androidx.lifecycle.viewmodel.compose) + implementation(libs.androidx.lifecycle.runtime.compose) implementation(libs.glide) implementation(libs.accompanist.systemuicontroller) debugImplementation(libs.androidx.compose.ui.tooling) diff --git a/app/src/main/java/com/google/samples/apps/sunflower/compose/garden/GardenScreen.kt b/app/src/main/java/com/google/samples/apps/sunflower/compose/garden/GardenScreen.kt index 642b52c2c..aa9b17f80 100644 --- a/app/src/main/java/com/google/samples/apps/sunflower/compose/garden/GardenScreen.kt +++ b/app/src/main/java/com/google/samples/apps/sunflower/compose/garden/GardenScreen.kt @@ -35,7 +35,6 @@ import androidx.compose.material3.ElevatedCard import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -47,6 +46,7 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.PreviewParameter import androidx.compose.ui.tooling.preview.PreviewParameterProvider import androidx.hilt.navigation.compose.hiltViewModel +import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi import com.bumptech.glide.integration.compose.GlideImage import com.google.samples.apps.sunflower.R @@ -65,7 +65,7 @@ fun GardenScreen( onAddPlantClick: () -> Unit, onPlantClick: (PlantAndGardenPlantings) -> Unit ) { - val gardenPlants by viewModel.plantAndGardenPlantings.collectAsState(initial = emptyList()) + val gardenPlants by viewModel.plantAndGardenPlantings.collectAsStateWithLifecycle() GardenScreen( gardenPlants = gardenPlants, modifier = modifier, diff --git a/app/src/main/java/com/google/samples/apps/sunflower/compose/plantdetail/PlantDetailView.kt b/app/src/main/java/com/google/samples/apps/sunflower/compose/plantdetail/PlantDetailView.kt index 0d8b12f24..20a36a7e2 100644 --- a/app/src/main/java/com/google/samples/apps/sunflower/compose/plantdetail/PlantDetailView.kt +++ b/app/src/main/java/com/google/samples/apps/sunflower/compose/plantdetail/PlantDetailView.kt @@ -55,7 +55,7 @@ import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar import androidx.compose.runtime.Composable -import androidx.compose.runtime.collectAsState + import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.mutableStateOf @@ -82,6 +82,7 @@ import androidx.compose.ui.viewinterop.AndroidViewBinding import androidx.constraintlayout.compose.ConstraintLayout import androidx.core.text.HtmlCompat import androidx.hilt.navigation.compose.hiltViewModel +import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi import com.bumptech.glide.integration.compose.GlideImage import com.bumptech.glide.load.DataSource @@ -116,7 +117,7 @@ fun PlantDetailsScreen( onGalleryClick: (Plant) -> Unit, ) { val plant = plantDetailsViewModel.plant.observeAsState().value - val isPlanted = plantDetailsViewModel.isPlanted.collectAsState(initial = false).value + val isPlanted = plantDetailsViewModel.isPlanted.collectAsStateWithLifecycle().value val showSnackbar = plantDetailsViewModel.showSnackbar.observeAsState().value if (plant != null && showSnackbar != null) { diff --git a/app/src/main/java/com/google/samples/apps/sunflower/viewmodels/GardenPlantingListViewModel.kt b/app/src/main/java/com/google/samples/apps/sunflower/viewmodels/GardenPlantingListViewModel.kt index f6acf5836..b97143cc4 100644 --- a/app/src/main/java/com/google/samples/apps/sunflower/viewmodels/GardenPlantingListViewModel.kt +++ b/app/src/main/java/com/google/samples/apps/sunflower/viewmodels/GardenPlantingListViewModel.kt @@ -17,16 +17,25 @@ package com.google.samples.apps.sunflower.viewmodels import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.google.samples.apps.sunflower.data.GardenPlantingRepository import com.google.samples.apps.sunflower.data.PlantAndGardenPlantings import dagger.hilt.android.lifecycle.HiltViewModel -import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.stateIn import javax.inject.Inject @HiltViewModel class GardenPlantingListViewModel @Inject internal constructor( gardenPlantingRepository: GardenPlantingRepository ) : ViewModel() { - val plantAndGardenPlantings: Flow> = - gardenPlantingRepository.getPlantedGardens() + val plantAndGardenPlantings: StateFlow> = + gardenPlantingRepository + .getPlantedGardens() + .stateIn( + viewModelScope, + SharingStarted.WhileSubscribed(5000), + emptyList() + ) } \ No newline at end of file diff --git a/app/src/main/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModel.kt b/app/src/main/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModel.kt index 50b1af201..d3b581a88 100644 --- a/app/src/main/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModel.kt +++ b/app/src/main/java/com/google/samples/apps/sunflower/viewmodels/PlantDetailViewModel.kt @@ -26,6 +26,8 @@ import com.google.samples.apps.sunflower.BuildConfig import com.google.samples.apps.sunflower.data.GardenPlantingRepository import com.google.samples.apps.sunflower.data.PlantRepository import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch import javax.inject.Inject @@ -42,6 +44,11 @@ class PlantDetailViewModel @Inject constructor( val plantId: String = savedStateHandle.get(PLANT_ID_SAVED_STATE_KEY)!! val isPlanted = gardenPlantingRepository.isPlanted(plantId) + .stateIn( + viewModelScope, + SharingStarted.WhileSubscribed(5000), + false + ) val plant = plantRepository.getPlant(plantId).asLiveData() private val _showSnackbar = MutableLiveData(false) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 151b6f294..7d7a653e3 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -87,6 +87,7 @@ androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", vers androidx-espresso-intents = { module = "androidx.test.espresso:espresso-intents", version.ref = "espresso" } androidx-lifecycle-livedata-ktx = { module = "androidx.lifecycle:lifecycle-livedata-ktx", version.ref = "lifecycle" } androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "viewModelCompose" } +androidx-lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "viewModelCompose" } androidx-lifecycle-viewmodel-ktx = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "lifecycle" } androidx-monitor = { module = "androidx.test:monitor", version.ref = "monitor" } androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "navigation" }