From 9375f2d66b3e93f9706516fda18d5dc44953a2eb Mon Sep 17 00:00:00 2001 From: Hicham Boushaba Date: Tue, 10 Dec 2024 08:37:00 +0100 Subject: [PATCH] Make DB flow collection lifecycle aware --- gradle/libs.versions.toml | 1 + libs/apifaker/build.gradle | 1 + .../com/woocommerce/android/apifaker/ui/home/HomeScreen.kt | 3 ++- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 24214f404fa..e1514ddab76 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -132,6 +132,7 @@ androidx-hilt-navigation-fragment = { group = "androidx.hilt", name = "hilt-navi androidx-hilt-work = { group = "androidx.hilt", name = "hilt-work", version.ref = "androidx-hilt" } androidx-lifecycle-livedata-ktx = { group = "androidx.lifecycle", name = "lifecycle-livedata-ktx", version.ref = "androidx-lifecycle" } androidx-lifecycle-process = { group = "androidx.lifecycle", name = "lifecycle-process", version.ref = "androidx-lifecycle" } +androidx-lifecycle-runtime-compose = { group = "androidx.lifecycle", name = "lifecycle-runtime-compose", version.ref = "androidx-lifecycle" } androidx-lifecycle-viewmodel-compose = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "androidx-lifecycle" } androidx-lifecycle-viewmodel-savedstate = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-savedstate", version.ref = "androidx-lifecycle" } androidx-navigation-common = { group = "androidx.navigation", name = "navigation-common", version.ref = "androidx-navigation" } diff --git a/libs/apifaker/build.gradle b/libs/apifaker/build.gradle index 12c0b345f73..b08f5bba41d 100644 --- a/libs/apifaker/build.gradle +++ b/libs/apifaker/build.gradle @@ -52,6 +52,7 @@ android { dependencies { implementation(libs.androidx.core.ktx) implementation(libs.androidx.navigation.compose) + implementation(libs.androidx.lifecycle.runtime.compose) implementation(platform(libs.androidx.compose.bom)) implementation(libs.androidx.compose.material.main) diff --git a/libs/apifaker/src/main/java/com/woocommerce/android/apifaker/ui/home/HomeScreen.kt b/libs/apifaker/src/main/java/com/woocommerce/android/apifaker/ui/home/HomeScreen.kt index aff58c7b0c0..0a1340d46d0 100644 --- a/libs/apifaker/src/main/java/com/woocommerce/android/apifaker/ui/home/HomeScreen.kt +++ b/libs/apifaker/src/main/java/com/woocommerce/android/apifaker/ui/home/HomeScreen.kt @@ -30,6 +30,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp +import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.navigation.NavController import androidx.navigation.compose.rememberNavController import com.woocommerce.android.apifaker.models.ApiType @@ -46,7 +47,7 @@ internal fun HomeScreen( onExit: () -> Unit ) { HomeScreen( - endpoints = viewModel.endpoints.collectAsState().value, + endpoints = viewModel.endpoints.collectAsStateWithLifecycle().value, isEnabled = viewModel.isEnabled.collectAsState(initial = false).value, navController = navController, onMockingToggleChanged = viewModel::onMockingToggleChanged,