Skip to content

Commit

Permalink
[SHARED] Simplify settings VM
Browse files Browse the repository at this point in the history
  • Loading branch information
kamgurgul committed Jan 20, 2025
1 parent 9550541 commit f8c3aa1
Showing 1 changed file with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch

class SettingsViewModel(
private val userPreferencesRepository: IUserPreferencesRepository,
) : ViewModel() {

private val _uiStateFlow = MutableStateFlow(UiState())
val uiStateFlow = combine(
_uiStateFlow,
userPreferencesRepository.userPreferencesFlow,
) { uiState, userPreferences ->
uiState.copy(
temperatureUnit = userPreferences.temperatureUnit,
theme = userPreferences.theme,
)
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), UiState())
val uiStateFlow = userPreferencesRepository.userPreferencesFlow
.map { userPreferences ->
UiState(
temperatureUnit = userPreferences.temperatureUnit,
theme = userPreferences.theme,
)
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), UiState())

fun setTemperatureUnit(temperatureUnit: Int) {
viewModelScope.launch {
Expand Down

0 comments on commit f8c3aa1

Please sign in to comment.