|
| 1 | +/* |
| 2 | + * Copyright 2025 Mifos Initiative |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 7 | + * |
| 8 | + * See https://github.com/openMF/mobile-mobile/blob/master/LICENSE.md |
| 9 | + */ |
| 10 | +package cmp.navigation |
| 11 | + |
| 12 | +import androidx.lifecycle.ViewModel |
| 13 | +import androidx.lifecycle.viewModelScope |
| 14 | +import kotlinx.coroutines.flow.SharingStarted |
| 15 | +import kotlinx.coroutines.flow.StateFlow |
| 16 | +import kotlinx.coroutines.flow.map |
| 17 | +import kotlinx.coroutines.flow.stateIn |
| 18 | +import kotlinx.coroutines.launch |
| 19 | +import org.mifos.mobile.core.common.DataState |
| 20 | +import org.mifos.mobile.core.data.repository.UserDataRepository |
| 21 | +import org.mifos.mobile.core.model.UserData |
| 22 | + |
| 23 | +class ComposeAppViewModel( |
| 24 | + private val userDataRepository: UserDataRepository, |
| 25 | +// private val passcodeManager: PasscodeManager, |
| 26 | +) : ViewModel() { |
| 27 | + |
| 28 | + val uiState: StateFlow<MainUiState> = userDataRepository.userData.map { dataState -> |
| 29 | + when (dataState) { |
| 30 | + is DataState.Success -> MainUiState.Success(dataState.data) |
| 31 | + is DataState.Error -> MainUiState.Error(dataState.exception.message ?: "Unknown error") |
| 32 | + DataState.Loading -> MainUiState.Loading |
| 33 | + } |
| 34 | + }.stateIn( |
| 35 | + scope = viewModelScope, |
| 36 | + initialValue = MainUiState.Loading, |
| 37 | + started = SharingStarted.WhileSubscribed(5_000), |
| 38 | + ) |
| 39 | + |
| 40 | + fun logOut() { |
| 41 | + viewModelScope.launch { |
| 42 | + userDataRepository.logOut() |
| 43 | +// passcodeManager.clearPasscode() |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +sealed interface MainUiState { |
| 49 | + data object Loading : MainUiState |
| 50 | + data class Error(val error: String) : MainUiState |
| 51 | + data class Success(val userData: UserData) : MainUiState |
| 52 | +} |
0 commit comments