Skip to content

Commit

Permalink
refactor(m3): compute home start destination in ViewModel instead of ui.
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardPaligot committed Oct 8, 2023
1 parent 5a71d17 commit 041c502
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import org.gdglille.devfest.repositories.EventRepository

sealed class MainUiState {
object Loading : MainUiState()
data class Success(val initialized: Boolean) : MainUiState()
data class Success(val startDestination: String) : MainUiState()
}

class MainViewModel(private val repository: EventRepository) : ViewModel() {
val uiState: StateFlow<MainUiState> = flow { emit(repository.isInitialized()) }
.map { MainUiState.Success(it) }
.map { MainUiState.Success(if (it) "home" else "events") }
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@ fun Main(
val uiState = viewModel.uiState.collectAsState()
when (uiState.value) {
is MainUiState.Success -> {
val startDestination = if ((uiState.value as MainUiState.Success).initialized) {
"home"
} else {
"events"
}
NavHost(navController = navController, startDestination = startDestination) {
NavHost(
navController = navController,
startDestination = (uiState.value as MainUiState.Success).startDestination
) {
composable(route = "events") {
EventListVM(
repository = eventRepository,
Expand Down

0 comments on commit 041c502

Please sign in to comment.