Skip to content

Commit

Permalink
CHANGE expose vm's ui state as stateflow, to not have to set a defaul…
Browse files Browse the repository at this point in the history
…t twice (this also solves a possible flickering default state bug, when re-opening this screen and a state change from the viewmodel is preceded by the default state for a split second)
  • Loading branch information
Frank1234 committed Feb 21, 2024
1 parent 3263df7 commit e3d9a23
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.ramcosta.composedestinations.navigation.DestinationsNavigator
@Composable
fun InitNavigator(destinationsNavigator: DestinationsNavigator, routeNavigator: RouteNavigator) {

val viewState by routeNavigator.navigationState.collectAsStateWithLifecycle(initialValue = NavigationState.Idle)
val viewState by routeNavigator.navigationState.collectAsStateWithLifecycle()
LaunchedEffect(viewState) {
updateNavigationState(destinationsNavigator, viewState, routeNavigator::onNavigated)
}
Expand Down Expand Up @@ -47,4 +47,4 @@ private fun updateNavigationState(
is NavigationState.Idle -> {
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package nl.q42.template.presentation.home
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import nl.q42.template.actionresult.data.handleAction
Expand All @@ -21,7 +21,7 @@ class HomeViewModel @Inject constructor(
) : ViewModel(), RouteNavigator by navigator {

private val _uiState = MutableStateFlow<HomeViewState>(HomeViewState.Empty)
val uiState: Flow<HomeViewState> = _uiState
val uiState: StateFlow<HomeViewState> = _uiState

init {
loadUser()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import nl.q42.template.navigation.viewmodel.RouteNavigator
import javax.inject.Inject

Expand All @@ -17,7 +18,7 @@ class HomeSecondViewModel @Inject constructor(
private val titleParam = savedStateHandle.get<String>("title") ?: throw IllegalArgumentException("title required")

private val _uiState = MutableStateFlow<HomeSecondViewState>(HomeSecondViewState(titleParam))
val uiState: Flow<HomeSecondViewState> = _uiState
val uiState: StateFlow<HomeSecondViewState> = _uiState

fun onBackClicked() {
navigateUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ fun HomeScreen(

OnLifecycleResume(viewModel::onScreenResumed)

val viewState by viewModel.uiState.collectAsStateWithLifecycle(initialValue = HomeViewState.Empty)
val viewState by viewModel.uiState.collectAsStateWithLifecycle()
HomeContent(viewState, viewModel::onLoadClicked, viewModel::onOpenSecondScreenClicked, viewModel::onOpenOnboardingClicked)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import nl.q42.template.navigation.viewmodel.InitNavigator
import nl.q42.template.presentation.home.second.HomeSecondViewModel
import nl.q42.template.presentation.home.second.HomeSecondViewState

@Destination
@Composable
Expand All @@ -33,7 +32,7 @@ fun HomeSecondScreen(

InitNavigator(navigator, viewModel) // enables viewModel to navigate to other screens

val viewState by viewModel.uiState.collectAsStateWithLifecycle(initialValue = HomeSecondViewState(""))
val viewState by viewModel.uiState.collectAsStateWithLifecycle()

Column(
modifier = Modifier.fillMaxSize(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import nl.q42.template.navigation.viewmodel.RouteNavigator
import javax.inject.Inject

Expand All @@ -15,7 +16,7 @@ class OnboardingStartViewModel @Inject constructor(
) : ViewModel(), RouteNavigator by navigator {

private val _uiState = MutableStateFlow(OnboardingStartViewState("start"))
val uiState: Flow<OnboardingStartViewState> = _uiState
val uiState: StateFlow<OnboardingStartViewState> = _uiState

fun onBackClicked() {
navigateUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fun OnboardingStartScreen(

InitNavigator(navigator, viewModel) // enables viewModel to navigate to other screens

val viewState by viewModel.uiState.collectAsStateWithLifecycle(initialValue = OnboardingStartViewState(""))
val viewState by viewModel.uiState.collectAsStateWithLifecycle()

Column(
modifier = Modifier.fillMaxSize(),
Expand Down

0 comments on commit e3d9a23

Please sign in to comment.