Skip to content

Commit

Permalink
Merge pull request #56 from Q42/feature/expose-vm-state-as-stateflow
Browse files Browse the repository at this point in the history
CHANGE expose vm's ui state as stateflow
  • Loading branch information
Frank1234 authored Feb 27, 2024
2 parents 67a9cc2 + bf14ff4 commit 5c779f4
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 11 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,9 @@ 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.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import nl.q42.template.actionresult.data.handleAction
Expand All @@ -23,7 +24,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.asStateFlow()

init {
loadUser()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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 kotlinx.coroutines.flow.asStateFlow
import nl.q42.template.navigation.viewmodel.RouteNavigator
import javax.inject.Inject

Expand All @@ -17,7 +19,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.asStateFlow()

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 @@ -3,8 +3,9 @@ package nl.q42.template.presentation.onboarding.start
import androidx.lifecycle.SavedStateHandle
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 kotlinx.coroutines.flow.asStateFlow
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.asStateFlow()

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 5c779f4

Please sign in to comment.