From e23dee2157346b3f208fb894a68295878ac9f9d3 Mon Sep 17 00:00:00 2001 From: Mustafa Ozhan Date: Sun, 15 Sep 2024 11:00:27 +0200 Subject: [PATCH] [Oztechan/CCC#3929] Extract updating the symbol into new method (#3930) --- .../calculator/CalculatorViewModel.kt | 33 ++++++++++--------- .../calculator/CalculatorViewModelTest.kt | 6 ++-- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/client/viewmodel/calculator/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/calculator/CalculatorViewModel.kt b/client/viewmodel/calculator/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/calculator/CalculatorViewModel.kt index c57d3ffe7..3ddeb777c 100644 --- a/client/viewmodel/calculator/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/calculator/CalculatorViewModel.kt +++ b/client/viewmodel/calculator/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/calculator/CalculatorViewModel.kt @@ -84,6 +84,7 @@ class CalculatorViewModel( Logger.d { "CalculatorViewModel observeBase $it" } calculationStorage.currentBase = it currentBaseChanged(it) + updateSymbol() } .launchIn(viewModelScope) @@ -173,23 +174,25 @@ class CalculatorViewModel( } } - private fun currentBaseChanged(newBase: String) = - viewModelScope.launch { - data.conversion = null - val symbol = currencyDataSource.getCurrencyByCode(newBase)?.symbol.orEmpty() - setState { - copy( - base = newBase, - input = input, - symbol = symbol - ) - } + private fun currentBaseChanged(newBase: String) { + data.conversion = null + setState { + copy( + base = newBase, + input = input + ) + } - analyticsManager.trackEvent(Event.BaseChange(Param.Base(newBase))) - analyticsManager.setUserProperty(UserProperty.BaseCurrency(newBase)) + analyticsManager.trackEvent(Event.BaseChange(Param.Base(newBase))) + analyticsManager.setUserProperty(UserProperty.BaseCurrency(newBase)) - updateConversion() - } + updateConversion() + } + + private suspend fun updateSymbol() { + val symbol = currencyDataSource.getCurrencyByCode(state.value.base)?.symbol.orEmpty() + setState { copy(symbol = symbol) } + } // region Event override fun onKeyPress(key: String) { diff --git a/client/viewmodel/calculator/src/commonTest/kotlin/com/oztechan/ccc/client/viewmodel/calculator/CalculatorViewModelTest.kt b/client/viewmodel/calculator/src/commonTest/kotlin/com/oztechan/ccc/client/viewmodel/calculator/CalculatorViewModelTest.kt index cad7a1379..02468da62 100644 --- a/client/viewmodel/calculator/src/commonTest/kotlin/com/oztechan/ccc/client/viewmodel/calculator/CalculatorViewModelTest.kt +++ b/client/viewmodel/calculator/src/commonTest/kotlin/com/oztechan/ccc/client/viewmodel/calculator/CalculatorViewModelTest.kt @@ -326,10 +326,10 @@ internal class CalculatorViewModelTest { ) ) calculationStorage.currentBase = currency1.code - calculationStorage.lastInput = "" - currencyDataSource.getCurrencyByCode(currency1.code) analyticsManager.trackEvent(Event.BaseChange(Param.Base(currency1.code))) analyticsManager.setUserProperty(UserProperty.BaseCurrency(currency1.code)) + currencyDataSource.getCurrencyByCode(currency1.code) + calculationStorage.lastInput = "" calculationStorage.lastInput = input } } @@ -354,9 +354,9 @@ internal class CalculatorViewModelTest { verifySuspend(VerifyMode.order) { calculationStorage.currentBase = currency2.code - currencyDataSource.getCurrencyByCode(currency2.code) analyticsManager.trackEvent(Event.BaseChange(Param.Base(currency2.code))) analyticsManager.setUserProperty(UserProperty.BaseCurrency(currency2.code)) + currencyDataSource.getCurrencyByCode(currency2.code) } } }