Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Bug fix (#2943)
Browse files Browse the repository at this point in the history
- Implemented getSettingsWithNextTheme function in SettingsAct. Thanks to this change, codebase is a bit more DRY.
 - Refactored SettingsViewModel and HomeViewModel to use this function when changing the theme.
 - Changed `theme` in HomeViewModel to `currentTheme` to ensure consistency of naming in SettingsViewModel and HomeViewModel.
  • Loading branch information
michalguspiel authored Feb 11, 2024
1 parent 701b00e commit e7d98d9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
25 changes: 11 additions & 14 deletions screen/home/src/main/java/com/ivy/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class HomeViewModel @Inject constructor(
private val updateCategoriesCacheAct: UpdateCategoriesCacheAct,
private val syncExchangeRatesAct: SyncExchangeRatesAct,
) : ComposeViewModel<HomeState, HomeEvent>() {
private val theme = mutableStateOf(Theme.AUTO)
private val currentTheme = mutableStateOf(Theme.AUTO)
private val name = mutableStateOf("")
private val period = mutableStateOf(ivyContext.selectedPeriod)
private val baseData = mutableStateOf(
Expand Down Expand Up @@ -145,7 +145,7 @@ class HomeViewModel @Inject constructor(

@Composable
private fun getTheme(): Theme {
return theme.value
return currentTheme.value
}

@Composable
Expand Down Expand Up @@ -229,7 +229,7 @@ class HomeViewModel @Inject constructor(
is HomeEvent.SetOverdueExpanded -> setOverdueExpanded(event.expanded)
is HomeEvent.SetBuffer -> setBuffer(event.buffer).fixUnit()
is HomeEvent.SetCurrency -> setCurrency(event.currency).fixUnit()
HomeEvent.SwitchTheme -> switchTheme().fixUnit()
HomeEvent.SwitchTheme -> switchTheme()
is HomeEvent.DismissCustomerJourneyCard -> dismissCustomerJourneyCard(event.card)
is HomeEvent.SetExpanded -> setExpanded(event.expanded)
}
Expand All @@ -253,7 +253,7 @@ class HomeViewModel @Inject constructor(
val hideBalance = shouldHideBalanceAct(Unit)
val hideIncome = shouldHideIncomeAct(Unit)

theme.value = settings.theme
currentTheme.value = settings.theme
name.value = settings.name
period.value = timePeriod
this.hideBalance.value = hideBalance
Expand Down Expand Up @@ -408,17 +408,14 @@ class HomeViewModel @Inject constructor(
hideIncome.value = true
}

private fun switchTheme() = settingsAct then {
it.copy(
theme = when (it.theme) {
Theme.LIGHT -> Theme.DARK
Theme.DARK -> Theme.AUTO
Theme.AUTO -> Theme.LIGHT
private fun switchTheme() {
viewModelScope.launch {
settingsAct.getSettingsWithNextTheme().run {
updateSettingsAct(this)
ivyContext.switchTheme(this.theme)
currentTheme.value = this.theme
}
)
} then updateSettingsAct then { newSettings ->
ivyContext.switchTheme(newSettings.theme)
theme.value = newSettings.theme
}
}

private suspend fun setBuffer(newBuffer: Double) = settingsAct then {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,19 +302,11 @@ class SettingsViewModel @Inject constructor(

private fun switchTheme() {
viewModelScope.launch {
val currentSettings = settingsAct(Unit)
val newTheme = when (currentSettings.theme) {
Theme.LIGHT -> Theme.DARK
Theme.DARK -> Theme.AUTO
Theme.AUTO -> Theme.LIGHT
settingsAct.getSettingsWithNextTheme().run {
updateSettingsAct(this)
ivyContext.switchTheme(this.theme)
currentTheme.value = this.theme
}
updateSettingsAct(
currentSettings.copy(
theme = newTheme
)
)
ivyContext.switchTheme(newTheme)
currentTheme.value = newTheme
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ivy.wallet.domain.action.settings

import com.ivy.base.legacy.Theme
import com.ivy.data.db.dao.read.SettingsDao
import com.ivy.frp.action.FPAction
import com.ivy.frp.then
Expand All @@ -13,4 +14,14 @@ class SettingsAct @Inject constructor(
override suspend fun Unit.compose(): suspend () -> Settings = suspend {
io { settingsDao.findFirst() }
} then { it.toDomain() }
}

suspend fun getSettingsWithNextTheme(): Settings {
val currentSettings = this(Unit)
val newTheme = when (currentSettings.theme) {
Theme.LIGHT -> Theme.DARK
Theme.DARK -> Theme.AUTO
Theme.AUTO -> Theme.LIGHT
}
return currentSettings.copy(theme = newTheme)
}
}

0 comments on commit e7d98d9

Please sign in to comment.