Skip to content

Commit

Permalink
Fixed debug mode selector handler (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandrsarapulovgl authored Sep 29, 2021
1 parent 7ab324f commit ea25dd1
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,18 @@ class DebugModeSettingsViewModel @Inject constructor(
) : ViewModel() {
private val _countriesData: MutableLiveData<CountriesData> = MutableLiveData()
val countriesData: LiveData<CountriesData> = _countriesData
private val _debugModeState: MutableLiveData<DebugModeState> =
MutableLiveData(DebugModeState.OFF)
val debugModeState: LiveData<DebugModeState> = _debugModeState

init {
viewModelScope.launch {
withContext(Dispatchers.IO) {
preferences.debugModeState?.let { DebugModeState.valueOf(it) } ?: DebugModeState.OFF
}.apply {
_debugModeState.value = this
}

val countriesData: CountriesData = withContext(Dispatchers.IO) {
val selectedCountriesCodes: Set<String> =
preferences.debugModeSelectedCountriesCodes ?: emptySet()
Expand All @@ -61,15 +70,14 @@ class DebugModeSettingsViewModel @Inject constructor(
}

fun saveSelectedDebugMode(debugModeState: DebugModeState) {
preferences.debugModeState = debugModeState.toString()
if (debugModeState != _debugModeState.value) {
_debugModeState.value = debugModeState
preferences.debugModeState = debugModeState.toString()
}
}

fun saveSelectedCountries(countriesData: CountriesData) {
_countriesData.value = countriesData
preferences.debugModeSelectedCountriesCodes = countriesData.selectedCountriesCodes
}

val debugModeState: LiveData<DebugModeState> = liveData {
emit(preferences.debugModeState?.let { DebugModeState.valueOf(it) } ?: DebugModeState.OFF)
}
}

0 comments on commit ea25dd1

Please sign in to comment.