Skip to content

Commit

Permalink
Replace produceState with collectAsStateWithLifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
moyheen authored and JoseAlcerreca committed May 31, 2023
1 parent 4192028 commit fca6e5a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 17 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ dependencies {
kaptTest(libs.hilt.android.compiler)

// Arch Components
implementation(libs.androidx.lifecycle.runtime.compose)
implementation(libs.androidx.lifecycle.viewmodel.compose)
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.hilt.navigation.compose)
Expand Down
20 changes: 3 additions & 17 deletions app/src/main/java/android/template/ui/mymodel/MyModelScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package android.template.ui.mymodel

import android.template.ui.theme.MyApplicationTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -28,31 +29,17 @@ import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.Lifecycle.State.STARTED
import androidx.lifecycle.repeatOnLifecycle
import android.template.ui.theme.MyApplicationTheme
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.lifecycle.compose.collectAsStateWithLifecycle

@Composable
fun MyModelScreen(modifier: Modifier = Modifier, viewModel: MyModelViewModel = hiltViewModel()) {
val lifecycle = LocalLifecycleOwner.current.lifecycle
val items by produceState<MyModelUiState>(
initialValue = MyModelUiState.Loading,
key1 = lifecycle,
key2 = viewModel
) {
lifecycle.repeatOnLifecycle(state = STARTED) {
viewModel.uiState.collect { value = it }
}
}
val items by viewModel.uiState.collectAsStateWithLifecycle()
if (items is MyModelUiState.Success) {
MyModelScreen(
items = (items as MyModelUiState.Success).data,
Expand All @@ -62,7 +49,6 @@ fun MyModelScreen(modifier: Modifier = Modifier, viewModel: MyModelViewModel = h
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
internal fun MyModelScreen(
items: List<String>,
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ androidx-compose-ui-tooling = { group = "androidx.compose.ui", name = "ui-toolin
androidx-compose-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest"}
androidx-hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "androidxHilt" }
androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidxLifecycle" }
androidx-lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "androidxLifecycle" }
androidx-lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "androidxLifecycle" }
androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "androidxNavigation" }
androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = "androidxRoom" }
Expand Down

0 comments on commit fca6e5a

Please sign in to comment.