From c1ce5e9c841b3d850e5d3a47cb07efb009204aa5 Mon Sep 17 00:00:00 2001 From: lihenggui Date: Fri, 18 Oct 2024 16:25:45 -0700 Subject: [PATCH] Rename MainScreenViewModel --- .../samples/apps/nowinandroid/MainActivity.kt | 15 +++++++-------- ...ctivityViewModel.kt => MainScreenViewModel.kt} | 14 +++++++------- gradle/libs.versions.toml | 2 -- 3 files changed, 14 insertions(+), 17 deletions(-) rename app/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/{MainActivityViewModel.kt => MainScreenViewModel.kt} (76%) diff --git a/app/src/androidMain/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt b/app/src/androidMain/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt index 44200a243d..dc660aa773 100644 --- a/app/src/androidMain/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt +++ b/app/src/androidMain/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt @@ -35,8 +35,8 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle import androidx.metrics.performance.JankStats -import com.google.samples.apps.nowinandroid.MainActivityUiState.Loading -import com.google.samples.apps.nowinandroid.MainActivityUiState.Success +import com.google.samples.apps.nowinandroid.MainScreenUiState.Loading +import com.google.samples.apps.nowinandroid.MainScreenUiState.Success import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsHelper import com.google.samples.apps.nowinandroid.core.analytics.LocalAnalyticsHelper import com.google.samples.apps.nowinandroid.core.data.repository.UserNewsResourceRepository @@ -52,7 +52,6 @@ import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import org.koin.android.ext.android.inject -import org.koin.core.context.GlobalContext.get class MainActivity : ComponentActivity() { @@ -69,13 +68,13 @@ class MainActivity : ComponentActivity() { private val userNewsResourceRepository: UserNewsResourceRepository by inject() - private val viewModel: MainActivityViewModel by viewModels() + private val viewModel: MainScreenViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { val splashScreen = installSplashScreen() super.onCreate(savedInstanceState) - var uiState: MainActivityUiState by mutableStateOf(Loading) + var uiState: MainScreenUiState by mutableStateOf(Loading) // Update the uiState lifecycleScope.launch { @@ -161,7 +160,7 @@ class MainActivity : ComponentActivity() { */ @Composable private fun shouldUseAndroidTheme( - uiState: MainActivityUiState, + uiState: MainScreenUiState, ): Boolean = when (uiState) { Loading -> false is Success -> when (uiState.userData.themeBrand) { @@ -175,7 +174,7 @@ private fun shouldUseAndroidTheme( */ @Composable private fun shouldDisableDynamicTheming( - uiState: MainActivityUiState, + uiState: MainScreenUiState, ): Boolean = when (uiState) { Loading -> false is Success -> !uiState.userData.useDynamicColor @@ -187,7 +186,7 @@ private fun shouldDisableDynamicTheming( */ @Composable private fun shouldUseDarkTheme( - uiState: MainActivityUiState, + uiState: MainScreenUiState, ): Boolean = when (uiState) { Loading -> isSystemInDarkTheme() is Success -> when (uiState.userData.darkThemeConfig) { diff --git a/app/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/MainActivityViewModel.kt b/app/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/MainScreenViewModel.kt similarity index 76% rename from app/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/MainActivityViewModel.kt rename to app/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/MainScreenViewModel.kt index c5862519a5..5d21e5edea 100644 --- a/app/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/MainActivityViewModel.kt +++ b/app/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/MainScreenViewModel.kt @@ -18,8 +18,8 @@ package com.google.samples.apps.nowinandroid import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.google.samples.apps.nowinandroid.MainActivityUiState.Loading -import com.google.samples.apps.nowinandroid.MainActivityUiState.Success +import com.google.samples.apps.nowinandroid.MainScreenUiState.Loading +import com.google.samples.apps.nowinandroid.MainScreenUiState.Success import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository import com.google.samples.apps.nowinandroid.core.model.data.UserData import kotlinx.coroutines.flow.SharingStarted @@ -29,10 +29,10 @@ import kotlinx.coroutines.flow.stateIn import org.koin.android.annotation.KoinViewModel @KoinViewModel -class MainActivityViewModel( +class MainScreenViewModel( userDataRepository: UserDataRepository, ) : ViewModel() { - val uiState: StateFlow = userDataRepository.userData.map { + val uiState: StateFlow = userDataRepository.userData.map { Success(it) }.stateIn( scope = viewModelScope, @@ -41,7 +41,7 @@ class MainActivityViewModel( ) } -sealed interface MainActivityUiState { - data object Loading : MainActivityUiState - data class Success(val userData: UserData) : MainActivityUiState +sealed interface MainScreenUiState { + data object Loading : MainScreenUiState + data class Success(val userData: UserData) : MainScreenUiState } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 071ef717c4..7f91857d82 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -153,8 +153,6 @@ koin-bom = { group = "io.insert-koin", name = "koin-bom", version.ref = "koin" } koin-android = { group = "io.insert-koin", name = "koin-android"} koin-core = { group = "io.insert-koin", name = "koin-core"} koin-core-coroutines = { group = "io.insert-koin", name = "koin-android-coroutines"} -koin-core-viewmodel = { group = "io.insert-koin", name = "koin-android-viewmodel"} -koin-core-viewmodel-navigation = { group = "io.insert-koin", name = "koin-android-viewmodel-navigation"} koin-compose = { group = "io.insert-koin", name = "koin-compose"} koin-compose-viewmodel = { group = "io.insert-koin", name = "koin-compose-viewmodel"} koin-compose-viewmodel-navigation = { group = "io.insert-koin", name = "koin-compose-viewmodel-navigation"}