Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Oztechan/CCC#3903] Move WatchersScreen to SelectCurrencyPurpose #3904

Merged
merged 4 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
root.setOnClickListener {
selectCurrencyEvent.onItemClick(
item,
SelectCurrencyPurpose.BASE
SelectCurrencyPurpose.Base

Check warning on line 37 in android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/selectcurrency/SelectCurrencyAdapter.kt

View check run for this annotation

Codecov / codecov/patch

android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/selectcurrency/SelectCurrencyAdapter.kt#L37

Added line #L37 was not covered by tests
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ kotlin {

Modules.Client.DataSource.apply {
implementation(project(currency))
implementation(project(watcher))
}
Modules.Client.Storage.apply {
implementation(project(calculation))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
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<SelectCurrencyState, SelectCurrencyEffect, SelectCurrencyEvent, BaseData>(
initialState = SelectCurrencyState()
),
Expand All @@ -38,14 +41,20 @@
// 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

Check warning on line 49 in client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/SelectCurrencyViewModel.kt

View check run for this annotation

Codecov / codecov/patch

client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/SelectCurrencyViewModel.kt#L48-L49

Added lines #L48 - L49 were not covered by tests
)

SelectCurrencyPurpose.SOURCE -> TODO()
SelectCurrencyPurpose.TARGET -> TODO()
is SelectCurrencyPurpose.Target -> watcherDataSource.updateWatcherTargetById(
currency.code,
purpose.watcher.id

Check warning on line 54 in client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/SelectCurrencyViewModel.kt

View check run for this annotation

Codecov / codecov/patch

client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/SelectCurrencyViewModel.kt#L53-L54

Added lines #L53 - L54 were not covered by tests
)
}
sendEffect { SelectCurrencyEffect.CurrencyChange(currency.code) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
import org.koin.dsl.module

val clientViewModelSelectCurrencyModule = module {
viewModelDefinition { SelectCurrencyViewModel(get(), get()) }
viewModelDefinition { SelectCurrencyViewModel(get(), get(), get()) }

Check warning on line 8 in client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/di/ClientViewModelSelectCurrencyModule.kt

View check run for this annotation

Codecov / codecov/patch

client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/di/ClientViewModelSelectCurrencyModule.kt#L8

Added line #L8 was not covered by tests
}
Original file line number Diff line number Diff line change
@@ -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()

Check warning on line 8 in client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/model/SelectCurrencyPurpose.kt

View check run for this annotation

Codecov / codecov/patch

client/viewmodel/selectcurrency/src/commonMain/kotlin/com/oztechan/ccc/client/viewmodel/selectcurrency/model/SelectCurrencyPurpose.kt#L7-L8

Added lines #L7 - L8 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<CurrencyDataSource>()
private val calculationStorage = mock<CalculationStorage>(MockMode.autoUnit)
private val watcherDataSource = mock<WatcherDataSource>(MockMode.autoUnit)

private val currencyDollar = CurrencyCommon("USD", "Dollar", "$", "", true)
private val currencyEuro = CurrencyCommon("Eur", "Euro", "", "", true)
Expand Down Expand Up @@ -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<SelectCurrencyEffect.CurrencyChange>(it)
Expand Down
2 changes: 1 addition & 1 deletion ios/CCC/UI/Calculator/CalculatorRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ struct CalculatorRootView: View {
content: {
SelectCurrencyRootView(
isBarShown: $isBarShown,
purpose: .base
purpose: SelectCurrencyPurpose.Base()
).environmentObject(navigationStack)
}
)
Expand Down
21 changes: 19 additions & 2 deletions ios/CCC/UI/Watchers/WatcherItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,6 +73,23 @@ 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)
)
}
)
}
}
41 changes: 8 additions & 33 deletions ios/CCC/UI/Watchers/WatchersRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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()
Expand All @@ -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:
Expand All @@ -117,9 +97,4 @@ struct WatchersRootView: View {
break
}
}

public struct BarInfo {
var isShown: Bool
var watcher: Provider.Watcher?
}
}
9 changes: 5 additions & 4 deletions ios/CCC/UI/Watchers/WatchersView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Provider
import SwiftUI
import NavigationStack

struct WatchersView: View {
@Environment(\.colorScheme) private var colorScheme
Expand All @@ -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 {
Expand All @@ -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
)
Expand Down