From 846a4d7d2990889c2d3f30848ff0c2ba5adb5546 Mon Sep 17 00:00:00 2001 From: egbuna Date: Tue, 16 Aug 2022 00:48:01 +0100 Subject: [PATCH 1/2] Use collectAsStateWithLifecycle to safely collect uiState --- .../apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/feature-bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt b/feature-bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt index 06a9fdacf..aad3db8f6 100644 --- a/feature-bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt +++ b/feature-bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt @@ -44,18 +44,21 @@ import androidx.compose.ui.platform.testTag import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.hilt.navigation.compose.hiltViewModel +import androidx.lifecycle.compose.ExperimentalLifecycleComposeApi +import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaGradientBackground import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaTopAppBar import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState import com.google.samples.apps.nowinandroid.core.ui.newsFeed +@OptIn(ExperimentalLifecycleComposeApi::class) @Composable fun BookmarksRoute( modifier: Modifier = Modifier, viewModel: BookmarksViewModel = hiltViewModel() ) { - val feedState by viewModel.feedState.collectAsState() + val feedState by viewModel.feedState.collectAsStateWithLifecycle() BookmarksScreen( feedState = feedState, removeFromBookmarks = viewModel::removeFromSavedResources, From a5bac4dd8f18572e6b12e1b37ff243e4b937e0da Mon Sep 17 00:00:00 2001 From: egbuna Date: Tue, 16 Aug 2022 07:17:24 +0100 Subject: [PATCH 2/2] remove unused import --- .../apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/feature-bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt b/feature-bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt index aad3db8f6..0716d7be1 100644 --- a/feature-bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt +++ b/feature-bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksScreen.kt @@ -36,7 +36,6 @@ import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Scaffold import androidx.compose.material3.TopAppBarDefaults import androidx.compose.runtime.Composable -import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color