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

Commit

Permalink
Modify collectAsState to collectAsStateWithLifecycle (#976)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben Trengrove <[email protected]>
  • Loading branch information
pachuho and bentrengrove committed Jul 3, 2024
1 parent 6ba253c commit 2b60ca9
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<PlantAndGardenPlantings>> =
gardenPlantingRepository.getPlantedGardens()
val plantAndGardenPlantings: StateFlow<List<PlantAndGardenPlantings>> =
gardenPlantingRepository
.getPlantedGardens()
.stateIn(
viewModelScope,
SharingStarted.WhileSubscribed(5000),
emptyList()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -42,6 +44,11 @@ class PlantDetailViewModel @Inject constructor(
val plantId: String = savedStateHandle.get<String>(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)
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down

0 comments on commit 2b60ca9

Please sign in to comment.