Skip to content

Commit

Permalink
[#3729] Simplify WidgetViewModel (#3730)
Browse files Browse the repository at this point in the history
* [#3729] Simplify WidgetViewModel

* [#3729] Simplify WidgetViewModel
  • Loading branch information
mustafaozhan authored Aug 3, 2024
1 parent 7eb451c commit 413fbe4
Showing 1 changed file with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,38 @@ class WidgetViewModel(
}
}

private suspend fun updateBase(isToNext: Boolean) {
val activeCurrencies = currencyDataSource.getActiveCurrencies()
private fun updateBase(isToNext: Boolean) {
viewModelScope.launch {
val activeCurrencies = currencyDataSource.getActiveCurrencies()

val newBaseIndex = activeCurrencies
.map { it.code }
.indexOf(calculationStorage.currentBase)
.let {
if (isToNext) {
it + 1
} else {
it - 1
val newBaseIndex = activeCurrencies
.map { it.code }
.indexOf(calculationStorage.currentBase)
.let {
if (isToNext) {
it + 1
} else {
it - 1
}
}.let {
(it + activeCurrencies.size) % activeCurrencies.size // it handles index -1 and index size +1
}
}.let {
(it + activeCurrencies.size) % activeCurrencies.size // it handles index -1 and index size +1
}

calculationStorage.currentBase = activeCurrencies[newBaseIndex].code
calculationStorage.currentBase = activeCurrencies[newBaseIndex].code

refreshWidgetData()
}
}

// region Event
override fun onPreviousClick() = viewModelScope.launchIgnored {
Logger.d { "WidgetViewModel onPreviousClick" }
updateBase(false)
refreshWidgetData()
}

override fun onNextClick() = viewModelScope.launchIgnored {
Logger.d { "WidgetViewModel onNextClick" }
updateBase(true)
refreshWidgetData()
}

override fun onRefreshClick() = viewModelScope.launchIgnored {
Expand Down

0 comments on commit 413fbe4

Please sign in to comment.