Skip to content

Commit

Permalink
Rename MainScreenViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
lihenggui committed Oct 18, 2024
1 parent 47187f0 commit c1ce5e9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {

Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<MainActivityUiState> = userDataRepository.userData.map {
val uiState: StateFlow<MainScreenUiState> = userDataRepository.userData.map {
Success(it)
}.stateIn(
scope = viewModelScope,
Expand All @@ -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
}
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down

0 comments on commit c1ce5e9

Please sign in to comment.