From 5662c4f584f1a977e6569c9fc9a19a963260dd5a Mon Sep 17 00:00:00 2001 From: Ryan Conway Date: Fri, 3 Nov 2023 14:39:49 +0700 Subject: [PATCH] [#541] Handle unstable collections detekt warning --- .../template/compose/ui/screens/home/HomeScreen.kt | 7 ++++--- .../template/compose/ui/screens/home/HomeViewModel.kt | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/screens/home/HomeScreen.kt b/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/screens/home/HomeScreen.kt index 835517537..cdb5ce2e8 100644 --- a/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/screens/home/HomeScreen.kt +++ b/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/screens/home/HomeScreen.kt @@ -18,6 +18,7 @@ import co.nimblehq.template.compose.ui.models.UiModel import co.nimblehq.template.compose.ui.showToast import co.nimblehq.template.compose.ui.theme.AppTheme.dimensions import co.nimblehq.template.compose.ui.theme.ComposeTheme +import kotlinx.collections.immutable.* import timber.log.Timber @Composable @@ -29,7 +30,7 @@ fun HomeScreen( viewModel.error.collectAsEffect { e -> e.showToast(context) } viewModel.navigator.collectAsEffect { destination -> navigator(destination) } - val uiModels: List by viewModel.uiModels.collectAsStateWithLifecycle() + val uiModels: ImmutableList by viewModel.uiModels.collectAsStateWithLifecycle() HomeScreenContent( title = stringResource(id = R.string.app_name), @@ -40,7 +41,7 @@ fun HomeScreen( @Composable private fun HomeScreenContent( title: String, - uiModels: List + uiModels: ImmutableList ) { Column( modifier = Modifier.fillMaxSize(), @@ -63,7 +64,7 @@ private fun HomeScreenPreview() { ComposeTheme { HomeScreenContent( title = stringResource(id = R.string.app_name), - uiModels = listOf(UiModel(1), UiModel(2), UiModel(3)) + uiModels = persistentListOf(UiModel(1), UiModel(2), UiModel(3)) ) } } diff --git a/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/screens/home/HomeViewModel.kt b/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/screens/home/HomeViewModel.kt index c906eb929..8416c71e2 100644 --- a/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/screens/home/HomeViewModel.kt +++ b/template-compose/app/src/main/java/co/nimblehq/template/compose/ui/screens/home/HomeViewModel.kt @@ -7,6 +7,7 @@ import co.nimblehq.template.compose.ui.models.UiModel import co.nimblehq.template.compose.ui.models.toUiModel import co.nimblehq.template.compose.util.DispatchersProvider import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.collections.immutable.* import kotlinx.coroutines.flow.* import javax.inject.Inject @@ -16,7 +17,7 @@ class HomeViewModel @Inject constructor( useCase: UseCase, ) : BaseViewModel() { - private val _uiModels = MutableStateFlow>(emptyList()) + private val _uiModels = MutableStateFlow>(persistentListOf()) val uiModels = _uiModels.asStateFlow() init { @@ -24,7 +25,7 @@ class HomeViewModel @Inject constructor( .injectLoading() .onEach { result -> val uiModels = result.map { it.toUiModel() } - _uiModels.emit(uiModels) + _uiModels.emit(uiModels.toImmutableList()) } .flowOn(dispatchersProvider.io) .catch { e -> _error.emit(e) }