From 64bfde6c888cd586b83a8506af6e0ea9c123aacc Mon Sep 17 00:00:00 2001 From: Mustafa Ozhan Date: Fri, 13 Sep 2024 17:47:50 +0200 Subject: [PATCH 1/4] [Oztechan/CCC#3903] Move WatchersScreen to SelectCurrencyPurpose --- ...client-viewmodel-selectcurrency.gradle.kts | 1 + .../selectcurrency/SelectCurrencyViewModel.kt | 23 +++++++---- .../di/ClientViewModelSelectCurrencyModule.kt | 2 +- .../model/SelectCurrencyPurpose.kt | 10 +++-- .../SelectCurrencyViewModelTest.kt | 6 ++- .../UI/Calculator/CalculatorRootView.swift | 2 +- ios/CCC/UI/Watchers/WatcherItem.swift | 11 ++++- ios/CCC/UI/Watchers/WatchersRootView.swift | 41 ++++--------------- ios/CCC/UI/Watchers/WatchersView.swift | 9 ++-- 9 files changed, 51 insertions(+), 54 deletions(-) diff --git a/client/viewmodel/selectcurrency/client-viewmodel-selectcurrency.gradle.kts b/client/viewmodel/selectcurrency/client-viewmodel-selectcurrency.gradle.kts index 2995a58f88..3ab104df9e 100644 --- a/client/viewmodel/selectcurrency/client-viewmodel-selectcurrency.gradle.kts +++ b/client/viewmodel/selectcurrency/client-viewmodel-selectcurrency.gradle.kts @@ -27,6 +27,7 @@ kotlin { Modules.Client.DataSource.apply { implementation(project(currency)) + implementation(project(watcher)) } Modules.Client.Storage.apply { implementation(project(calculation)) diff --git a/client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/SelectCurrencyViewModel.kt b/client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/SelectCurrencyViewModel.kt index e077d14076..e23a8f5fb3 100644 --- a/client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/SelectCurrencyViewModel.kt +++ b/client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/SelectCurrencyViewModel.kt @@ -8,15 +8,18 @@ import com.oztechan.ccc.client.core.shared.constants.MINIMUM_ACTIVE_CURRENCY import com.oztechan.ccc.client.core.viewmodel.BaseData import com.oztechan.ccc.client.core.viewmodel.SEEDViewModel import com.oztechan.ccc.client.datasource.currency.CurrencyDataSource +import com.oztechan.ccc.client.datasource.watcher.WatcherDataSource import com.oztechan.ccc.client.storage.calculation.CalculationStorage import com.oztechan.ccc.client.viewmodel.selectcurrency.model.SelectCurrencyPurpose import com.oztechan.ccc.common.core.model.Currency import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.launch class SelectCurrencyViewModel( private val calculationStorage: CalculationStorage, currencyDataSource: CurrencyDataSource, + private val watcherDataSource: WatcherDataSource ) : SEEDViewModel( initialState = SelectCurrencyState() ), @@ -38,14 +41,20 @@ class SelectCurrencyViewModel( // region Event override fun onItemClick(currency: Currency, purpose: SelectCurrencyPurpose) { Logger.d { "SelectCurrencyViewModel onItemClick ${currency.code} $purpose" } - when (purpose) { - SelectCurrencyPurpose.BASE -> { - calculationStorage.currentBase = currency.code - sendEffect { SelectCurrencyEffect.CurrencyChange(currency.code) } - } + viewModelScope.launch { + when (purpose) { + SelectCurrencyPurpose.Base -> calculationStorage.currentBase = currency.code + is SelectCurrencyPurpose.Source -> watcherDataSource.updateWatcherBaseById( + currency.code, + purpose.watcher.id + ) - SelectCurrencyPurpose.SOURCE -> TODO() - SelectCurrencyPurpose.TARGET -> TODO() + is SelectCurrencyPurpose.Target -> watcherDataSource.updateWatcherTargetById( + currency.code, + purpose.watcher.id + ) + } + sendEffect { SelectCurrencyEffect.CurrencyChange(currency.code) } } } diff --git a/client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/di/ClientViewModelSelectCurrencyModule.kt b/client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/di/ClientViewModelSelectCurrencyModule.kt index 03e94aad88..4f554890e5 100644 --- a/client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/di/ClientViewModelSelectCurrencyModule.kt +++ b/client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/di/ClientViewModelSelectCurrencyModule.kt @@ -5,5 +5,5 @@ import com.oztechan.ccc.client.viewmodel.selectcurrency.SelectCurrencyViewModel import org.koin.dsl.module val clientViewModelSelectCurrencyModule = module { - viewModelDefinition { SelectCurrencyViewModel(get(), get()) } + viewModelDefinition { SelectCurrencyViewModel(get(), get(), get()) } } diff --git a/client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/model/SelectCurrencyPurpose.kt b/client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/model/SelectCurrencyPurpose.kt index 0334e16945..6206c68032 100644 --- a/client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/model/SelectCurrencyPurpose.kt +++ b/client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/model/SelectCurrencyPurpose.kt @@ -1,7 +1,9 @@ package com.oztechan.ccc.client.viewmodel.selectcurrency.model -enum class SelectCurrencyPurpose { - BASE, - SOURCE, - TARGET, +import com.oztechan.ccc.common.core.model.Watcher + +sealed class SelectCurrencyPurpose { + data object Base : SelectCurrencyPurpose() + data class Source(val watcher: Watcher) : SelectCurrencyPurpose() + data class Target(val watcher: Watcher) : SelectCurrencyPurpose() } diff --git a/client/viewmodel/selectcurrency/src/commonTest/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/SelectCurrencyViewModelTest.kt b/client/viewmodel/selectcurrency/src/commonTest/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/SelectCurrencyViewModelTest.kt index 6cf1a70ae4..ef50286812 100644 --- a/client/viewmodel/selectcurrency/src/commonTest/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/SelectCurrencyViewModelTest.kt +++ b/client/viewmodel/selectcurrency/src/commonTest/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/SelectCurrencyViewModelTest.kt @@ -6,6 +6,7 @@ package com.oztechan.ccc.client.viewmodel.selectcurrency import co.touchlab.kermit.CommonWriter import co.touchlab.kermit.Logger import com.oztechan.ccc.client.datasource.currency.CurrencyDataSource +import com.oztechan.ccc.client.datasource.watcher.WatcherDataSource import com.oztechan.ccc.client.storage.calculation.CalculationStorage import com.oztechan.ccc.client.viewmodel.selectcurrency.model.SelectCurrencyPurpose import dev.mokkery.MockMode @@ -33,11 +34,12 @@ import com.oztechan.ccc.common.core.model.Currency as CurrencyCommon internal class SelectCurrencyViewModelTest { private val viewModel: SelectCurrencyViewModel by lazy { - SelectCurrencyViewModel(calculationStorage, currencyDataSource) + SelectCurrencyViewModel(calculationStorage, currencyDataSource, watcherDataSource) } private val currencyDataSource = mock() private val calculationStorage = mock(MockMode.autoUnit) + private val watcherDataSource = mock(MockMode.autoUnit) private val currencyDollar = CurrencyCommon("USD", "Dollar", "$", "", true) private val currencyEuro = CurrencyCommon("Eur", "Euro", "", "", true) @@ -101,7 +103,7 @@ internal class SelectCurrencyViewModelTest { fun onItemClick() = runTest { // base viewModel.effect.onSubscription { - viewModel.event.onItemClick(currencyDollar, SelectCurrencyPurpose.BASE) + viewModel.event.onItemClick(currencyDollar, SelectCurrencyPurpose.Base) }.firstOrNull().let { assertNotNull(it) assertIs(it) diff --git a/ios/CCC/UI/Calculator/CalculatorRootView.swift b/ios/CCC/UI/Calculator/CalculatorRootView.swift index ee68ba0103..baa2849d1d 100644 --- a/ios/CCC/UI/Calculator/CalculatorRootView.swift +++ b/ios/CCC/UI/Calculator/CalculatorRootView.swift @@ -83,7 +83,7 @@ struct CalculatorRootView: View { content: { SelectCurrencyRootView( isBarShown: $isBarShown, - purpose: .base + purpose: SelectCurrencyPurpose.Base() ).environmentObject(navigationStack) } ) diff --git a/ios/CCC/UI/Watchers/WatcherItem.swift b/ios/CCC/UI/Watchers/WatcherItem.swift index 22324b5fcb..bc8591e260 100644 --- a/ios/CCC/UI/Watchers/WatcherItem.swift +++ b/ios/CCC/UI/Watchers/WatcherItem.swift @@ -16,7 +16,7 @@ struct WatcherItem: View { @State private var relationSelection = 0 @State private var amount = "" - @Binding var isBaseBarShown: Bool + @Binding var isSourceBarShown: Bool @Binding var isTargetBarShown: Bool let watcher: Provider.Watcher @@ -73,6 +73,13 @@ struct WatcherItem: View { .onAppear { relationSelection = watcher.isGreater ? 1 : 0 amount = "\(watcher.rate)" - } + }.sheet( + isPresented: $isSourceBarShown, + content: { SelectCurrencyRootView(isBarShown: $isSourceBarShown, purpose: SelectCurrencyPurpose.Source(watcher: watcher)) } + ) + .sheet( + isPresented: $isTargetBarShown, + content: { SelectCurrencyRootView(isBarShown: $isTargetBarShown, purpose: SelectCurrencyPurpose.Target(watcher: watcher)) } + ) } } diff --git a/ios/CCC/UI/Watchers/WatchersRootView.swift b/ios/CCC/UI/Watchers/WatchersRootView.swift index 7302ca3b8d..50fd148bc7 100644 --- a/ios/CCC/UI/Watchers/WatchersRootView.swift +++ b/ios/CCC/UI/Watchers/WatchersRootView.swift @@ -21,8 +21,8 @@ struct WatchersRootView: View { WatchersViewModel >() @StateObject var notificationManager = NotificationManager() - @State var sourceBarInfo = BarInfo(isShown: false, watcher: nil) - @State var targetBarInfo = BarInfo(isShown: false, watcher: nil) + @State var isSourceBarShown = false + @State var isTargetBarShown = false @State var isInvalidInputSnackShown = false @State var isMaxWatchersSnackShown = false @State var isTooBigInputSnackShown = false @@ -34,8 +34,8 @@ struct WatchersRootView: View { event: observable.event, state: observable.state, authorizationStatus: notificationManager.authorizationStatus, - sourceBarInfo: $sourceBarInfo, - targetBarInfo: $targetBarInfo + isSourceBarShown: $isSourceBarShown, + isTargetBarShown: $isTargetBarShown ) .snack(isPresented: $isInvalidInputSnackShown) { SnackView(text: String(\.text_invalid_input)) @@ -46,24 +46,6 @@ struct WatchersRootView: View { .snack(isPresented: $isTooBigInputSnackShown) { SnackView(text: String(\.text_too_big_input)) } - .sheet( - isPresented: $sourceBarInfo.isShown, - content: { - SelectCurrencyRootView( - isBarShown: $sourceBarInfo.isShown, - purpose: .source - ).environmentObject(navigationStack) - } - ) - .sheet( - isPresented: $targetBarInfo.isShown, - content: { - SelectCurrencyRootView( - isBarShown: $targetBarInfo.isShown, - purpose: .target - ).environmentObject(navigationStack) - } - ) .onAppear { observable.startObserving() notificationManager.reloadAuthorisationStatus() @@ -87,12 +69,10 @@ struct WatchersRootView: View { switch effect { case is WatchersEffect.Back: navigationStack.pop() - case let selectBaseEffect as WatchersEffect.SelectBase: - sourceBarInfo.watcher = selectBaseEffect.watcher - sourceBarInfo.isShown.toggle() - case let selectTargetEffect as WatchersEffect.SelectTarget: - targetBarInfo.watcher = selectTargetEffect.watcher - targetBarInfo.isShown.toggle() + case is WatchersEffect.SelectBase: + isSourceBarShown.toggle() + case is WatchersEffect.SelectTarget: + isTargetBarShown .toggle() case is WatchersEffect.TooBigInput: isTooBigInputSnackShown.toggle() case is WatchersEffect.InvalidInput: @@ -117,9 +97,4 @@ struct WatchersRootView: View { break } } - - public struct BarInfo { - var isShown: Bool - var watcher: Provider.Watcher? - } } diff --git a/ios/CCC/UI/Watchers/WatchersView.swift b/ios/CCC/UI/Watchers/WatchersView.swift index 148bb10137..a4a94a070a 100644 --- a/ios/CCC/UI/Watchers/WatchersView.swift +++ b/ios/CCC/UI/Watchers/WatchersView.swift @@ -8,6 +8,7 @@ import Provider import SwiftUI +import NavigationStack struct WatchersView: View { @Environment(\.colorScheme) private var colorScheme @@ -16,8 +17,8 @@ struct WatchersView: View { var state: WatchersState var authorizationStatus: UNAuthorizationStatus? - @Binding var sourceBarInfo: WatchersRootView.BarInfo - @Binding var targetBarInfo: WatchersRootView.BarInfo + @Binding var isSourceBarShown: Bool + @Binding var isTargetBarShown: Bool var body: some View { ZStack { @@ -33,8 +34,8 @@ struct WatchersView: View { Form { List(state.watcherList, id: \.id) { watcher in WatcherItem( - isBaseBarShown: $sourceBarInfo.isShown, - isTargetBarShown: $targetBarInfo.isShown, + isSourceBarShown: $isSourceBarShown, + isTargetBarShown: $isTargetBarShown, watcher: watcher, event: event ) From 10487e7c8a66c1e2a008dbf38f6772374dae9b01 Mon Sep 17 00:00:00 2001 From: Mustafa Ozhan Date: Fri, 13 Sep 2024 18:02:35 +0200 Subject: [PATCH 2/4] [Oztechan/CCC#3903] Move WatchersScreen to SelectCurrencyPurpose --- .../ui/mobile/content/selectcurrency/SelectCurrencyAdapter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/selectcurrency/SelectCurrencyAdapter.kt b/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/selectcurrency/SelectCurrencyAdapter.kt index a3b8763789..a9b156c534 100644 --- a/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/selectcurrency/SelectCurrencyAdapter.kt +++ b/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/selectcurrency/SelectCurrencyAdapter.kt @@ -34,7 +34,7 @@ class SelectCurrencyAdapter( root.setOnClickListener { selectCurrencyEvent.onItemClick( item, - SelectCurrencyPurpose.BASE + SelectCurrencyPurpose.Base ) } } From 17401f9cd62f2b9cd6492c7bb243e1961b93b71d Mon Sep 17 00:00:00 2001 From: Mustafa Ozhan Date: Fri, 13 Sep 2024 19:10:03 +0200 Subject: [PATCH 3/4] [Oztechan/CCC#3903] Move WatchersScreen to SelectCurrencyPurpose --- ios/CCC/UI/Watchers/WatcherItem.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ios/CCC/UI/Watchers/WatcherItem.swift b/ios/CCC/UI/Watchers/WatcherItem.swift index bc8591e260..a52306958e 100644 --- a/ios/CCC/UI/Watchers/WatcherItem.swift +++ b/ios/CCC/UI/Watchers/WatcherItem.swift @@ -75,11 +75,15 @@ struct WatcherItem: View { amount = "\(watcher.rate)" }.sheet( isPresented: $isSourceBarShown, - content: { SelectCurrencyRootView(isBarShown: $isSourceBarShown, purpose: SelectCurrencyPurpose.Source(watcher: watcher)) } + content: { + SelectCurrencyRootView(isBarShown: $isSourceBarShown, purpose: SelectCurrencyPurpose.Source(watcher: watcher)) + } ) .sheet( isPresented: $isTargetBarShown, - content: { SelectCurrencyRootView(isBarShown: $isTargetBarShown, purpose: SelectCurrencyPurpose.Target(watcher: watcher)) } + content: { + SelectCurrencyRootView(isBarShown: $isTargetBarShown, purpose: SelectCurrencyPurpose.Target(watcher: watcher)) + } ) } } From 267fc071f05a4860ae251f4bcb803b4460cd7097 Mon Sep 17 00:00:00 2001 From: Mustafa Ozhan Date: Fri, 13 Sep 2024 19:16:55 +0200 Subject: [PATCH 4/4] [Oztechan/CCC#3903] Move WatchersScreen to SelectCurrencyPurpose --- ios/CCC/UI/Watchers/WatcherItem.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ios/CCC/UI/Watchers/WatcherItem.swift b/ios/CCC/UI/Watchers/WatcherItem.swift index a52306958e..3e59ac0fa3 100644 --- a/ios/CCC/UI/Watchers/WatcherItem.swift +++ b/ios/CCC/UI/Watchers/WatcherItem.swift @@ -76,13 +76,19 @@ struct WatcherItem: View { }.sheet( isPresented: $isSourceBarShown, content: { - SelectCurrencyRootView(isBarShown: $isSourceBarShown, purpose: SelectCurrencyPurpose.Source(watcher: watcher)) + SelectCurrencyRootView( + isBarShown: $isSourceBarShown, + purpose: SelectCurrencyPurpose.Source(watcher: watcher) + ) } ) .sheet( isPresented: $isTargetBarShown, content: { - SelectCurrencyRootView(isBarShown: $isTargetBarShown, purpose: SelectCurrencyPurpose.Target(watcher: watcher)) + SelectCurrencyRootView( + isBarShown: $isTargetBarShown, + purpose: SelectCurrencyPurpose.Target(watcher: watcher) + ) } ) }