From 8753c2d8839ef27b7f6bbcb96e0ba2b27348e138 Mon Sep 17 00:00:00 2001 From: Lukas Korba Date: Mon, 7 Nov 2022 11:53:39 +0100 Subject: [PATCH] [#468] Migrate CurrencySelection to ReducerProtocol (#478) - CurrencySelection migrated to ReducerProtocol - unit tests fixed --- .../CurrencySelectionStore.swift | 64 ++++++++----------- .../CurrencySelectionView.swift | 4 +- .../TransactionAmountTextFieldStore.swift | 16 +++-- .../SendTests/CurrencySelectionTests.swift | 10 ++- secantTests/SendTests/SendTests.swift | 16 ++--- .../TransactionAmountInputTests.swift | 16 ++--- ...TransactionConfirmationSnapshotTests.swift | 4 +- .../TransactionSendingTests.swift | 2 +- 8 files changed, 62 insertions(+), 70 deletions(-) diff --git a/secant/UI Components/TextFields/Components/CurrencySelection/CurrencySelectionStore.swift b/secant/UI Components/TextFields/Components/CurrencySelection/CurrencySelectionStore.swift index e936cda..680602c 100644 --- a/secant/UI Components/TextFields/Components/CurrencySelection/CurrencySelectionStore.swift +++ b/secant/UI Components/TextFields/Components/CurrencySelection/CurrencySelectionStore.swift @@ -9,47 +9,37 @@ import ComposableArchitecture // TODO [#315]: Reimplement this into multicurrency supporter (https://github.com/zcash/secant-ios-wallet/issues/315) -typealias CurrencySelectionReducer = Reducer< - CurrencySelectionState, - CurrencySelectionAction, - CurrencySelectionEnvironment -> +typealias CurrencySelectionStore = Store -typealias CurrencySelectionStore = Store +struct CurrencySelectionReducer: ReducerProtocol { + struct State: Equatable { + enum Currency: Equatable { + case usd + case zec -struct CurrencySelectionState: Equatable { - enum Currency: Equatable { - case usd - case zec - - var acronym: String { - switch self { - case .usd: return "USD" - case .zec: return "ZEC" - } - } - } - - var currencyType: Currency = .zec -} - -enum CurrencySelectionAction: Equatable { - case swapCurrencyType -} - -struct CurrencySelectionEnvironment { } - -extension CurrencySelectionReducer { - static var `default`: Self { - .init { state, action, _ in - switch action { - case .swapCurrencyType: - switch state.currencyType { - case .usd: state.currencyType = .zec - case .zec: state.currencyType = .usd + var acronym: String { + switch self { + case .usd: return "USD" + case .zec: return "ZEC" } } - return .none } + + var currencyType: Currency = .zec + } + + enum Action: Equatable { + case swapCurrencyType + } + + func reduce(into state: inout State, action: Action) -> ComposableArchitecture.EffectTask { + switch action { + case .swapCurrencyType: + switch state.currencyType { + case .usd: state.currencyType = .zec + case .zec: state.currencyType = .usd + } + } + return .none } } diff --git a/secant/UI Components/TextFields/Components/CurrencySelection/CurrencySelectionView.swift b/secant/UI Components/TextFields/Components/CurrencySelection/CurrencySelectionView.swift index 6d32407..ca4bd43 100644 --- a/secant/UI Components/TextFields/Components/CurrencySelection/CurrencySelectionView.swift +++ b/secant/UI Components/TextFields/Components/CurrencySelection/CurrencySelectionView.swift @@ -17,14 +17,14 @@ struct CurrencySelectionView: View { action: { viewStore.send(.swapCurrencyType) }, label: { HStack { - Text(CurrencySelectionState.Currency.usd.acronym) + Text(CurrencySelectionReducer.State.Currency.usd.acronym) .foregroundColor( viewStore.currencyType == .usd ? .yellow : .white ) Asset.Assets.Icons.swap.image - Text(CurrencySelectionState.Currency.zec.acronym) + Text(CurrencySelectionReducer.State.Currency.zec.acronym) .foregroundColor( viewStore.currencyType == .zec ? .yellow : .white ) diff --git a/secant/UI Components/TextFields/TransactionAmount/TransactionAmountTextFieldStore.swift b/secant/UI Components/TextFields/TransactionAmount/TransactionAmountTextFieldStore.swift index bcb8b96..4517796 100644 --- a/secant/UI Components/TextFields/TransactionAmount/TransactionAmountTextFieldStore.swift +++ b/secant/UI Components/TextFields/TransactionAmount/TransactionAmountTextFieldStore.swift @@ -18,10 +18,11 @@ typealias TransactionAmountTextFieldReducer = Reducer< typealias TransactionAmountTextFieldStore = Store typealias AnyTCATextFieldReducerAmount = AnyReducer +typealias AnyCurrencySelectionReducer = AnyReducer struct TransactionAmountTextFieldState: Equatable { var amount: Int64 = 0 - var currencySelectionState: CurrencySelectionState + var currencySelectionState: CurrencySelectionReducer.State var maxValue: Int64 = 0 var textFieldState: TCATextFieldReducer.State // TODO [#311]: - Get the ZEC price from the SDK, https://github.com/zcash/secant-ios-wallet/issues/311 @@ -34,7 +35,7 @@ struct TransactionAmountTextFieldState: Equatable { enum TransactionAmountTextFieldAction: Equatable { case clearValue - case currencySelection(CurrencySelectionAction) + case currencySelection(CurrencySelectionReducer.Action) case setMax case textField(TCATextFieldReducer.Action) case updateAmount @@ -115,21 +116,24 @@ extension TransactionAmountTextFieldReducer { environment: { $0 } ) - private static let currencySelectionReducer: TransactionAmountTextFieldReducer = CurrencySelectionReducer.default.pullback( + private static let currencySelectionReducer: TransactionAmountTextFieldReducer = AnyCurrencySelectionReducer { _ in + CurrencySelectionReducer() + } + .pullback( state: \TransactionAmountTextFieldState.currencySelectionState, action: /TransactionAmountTextFieldAction.currencySelection, - environment: { _ in return .init() } + environment: { $0 } ) } extension TransactionAmountTextFieldState { static let placeholder = TransactionAmountTextFieldState( - currencySelectionState: CurrencySelectionState(), + currencySelectionState: CurrencySelectionReducer.State(), textFieldState: .placeholder ) static let amount = TransactionAmountTextFieldState( - currencySelectionState: CurrencySelectionState(), + currencySelectionState: CurrencySelectionReducer.State(), textFieldState: .amount ) } diff --git a/secantTests/SendTests/CurrencySelectionTests.swift b/secantTests/SendTests/CurrencySelectionTests.swift index e00851e..106952c 100644 --- a/secantTests/SendTests/CurrencySelectionTests.swift +++ b/secantTests/SendTests/CurrencySelectionTests.swift @@ -12,9 +12,8 @@ import ComposableArchitecture class CurrencySelectionTests: XCTestCase { func testCurrencySwapUsdToZec() throws { let store = TestStore( - initialState: CurrencySelectionState(currencyType: .usd), - reducer: CurrencySelectionReducer.default, - environment: CurrencySelectionEnvironment() + initialState: CurrencySelectionReducer.State(currencyType: .usd), + reducer: CurrencySelectionReducer() ) store.send(.swapCurrencyType) { state in @@ -24,9 +23,8 @@ class CurrencySelectionTests: XCTestCase { func testCurrencySwapZecToUsd() throws { let store = TestStore( - initialState: CurrencySelectionState(currencyType: .zec), - reducer: CurrencySelectionReducer.default, - environment: CurrencySelectionEnvironment() + initialState: CurrencySelectionReducer.State(currencyType: .zec), + reducer: CurrencySelectionReducer() ) store.send(.swapCurrencyType) { state in diff --git a/secantTests/SendTests/SendTests.swift b/secantTests/SendTests/SendTests.swift index c0bce6e..582cf40 100644 --- a/secantTests/SendTests/SendTests.swift +++ b/secantTests/SendTests/SendTests.swift @@ -316,7 +316,7 @@ class SendTests: XCTestCase { transactionAddressInputState: .placeholder, transactionAmountInputState: TransactionAmountTextFieldState( - currencySelectionState: CurrencySelectionState(), + currencySelectionState: CurrencySelectionReducer.State(), maxValue: 501_300, textFieldState: TCATextFieldReducer.State( @@ -396,7 +396,7 @@ class SendTests: XCTestCase { transactionAddressInputState: .placeholder, transactionAmountInputState: TransactionAmountTextFieldState( - currencySelectionState: CurrencySelectionState(), + currencySelectionState: CurrencySelectionReducer.State(), textFieldState: TCATextFieldReducer.State( validationType: .customFloatingPoint(usNumberFormatter), @@ -430,7 +430,7 @@ class SendTests: XCTestCase { transactionAmountInputState: TransactionAmountTextFieldState( amount: 501_301, - currencySelectionState: CurrencySelectionState(), + currencySelectionState: CurrencySelectionReducer.State(), maxValue: 501_302, textFieldState: TCATextFieldReducer.State( @@ -478,7 +478,7 @@ class SendTests: XCTestCase { transactionAddressInputState: .placeholder, transactionAmountInputState: TransactionAmountTextFieldState( - currencySelectionState: CurrencySelectionState(), + currencySelectionState: CurrencySelectionReducer.State(), maxValue: 501_300, textFieldState: TCATextFieldReducer.State( @@ -526,7 +526,7 @@ class SendTests: XCTestCase { transactionAddressInputState: .placeholder, transactionAmountInputState: TransactionAmountTextFieldState( - currencySelectionState: CurrencySelectionState(), + currencySelectionState: CurrencySelectionReducer.State(), maxValue: 501_302, textFieldState: TCATextFieldReducer.State( @@ -574,7 +574,7 @@ class SendTests: XCTestCase { transactionAddressInputState: .placeholder, transactionAmountInputState: TransactionAmountTextFieldState( - currencySelectionState: CurrencySelectionState(), + currencySelectionState: CurrencySelectionReducer.State(), maxValue: 501_302, textFieldState: TCATextFieldReducer.State( @@ -632,7 +632,7 @@ class SendTests: XCTestCase { transactionAmountInputState: TransactionAmountTextFieldState( amount: 100, - currencySelectionState: CurrencySelectionState(), + currencySelectionState: CurrencySelectionReducer.State(), maxValue: 501_302, textFieldState: TCATextFieldReducer.State( @@ -676,7 +676,7 @@ class SendTests: XCTestCase { transactionAddressInputState: .placeholder, transactionAmountInputState: TransactionAmountTextFieldState( - currencySelectionState: CurrencySelectionState(), + currencySelectionState: CurrencySelectionReducer.State(), maxValue: 501_302, textFieldState: TCATextFieldReducer.State( diff --git a/secantTests/SendTests/TransactionAmountInputTests.swift b/secantTests/SendTests/TransactionAmountInputTests.swift index f2dd000..bb7da7d 100644 --- a/secantTests/SendTests/TransactionAmountInputTests.swift +++ b/secantTests/SendTests/TransactionAmountInputTests.swift @@ -25,7 +25,7 @@ class TransactionAmountTextFieldTests: XCTestCase { let store = TestStore( initialState: TransactionAmountTextFieldState( - currencySelectionState: CurrencySelectionState(), + currencySelectionState: CurrencySelectionReducer.State(), maxValue: 501_301, textFieldState: TCATextFieldReducer.State( @@ -53,7 +53,7 @@ class TransactionAmountTextFieldTests: XCTestCase { let store = TestStore( initialState: TransactionAmountTextFieldState( - currencySelectionState: CurrencySelectionState(), + currencySelectionState: CurrencySelectionReducer.State(), maxValue: 501_301, textFieldState: TCATextFieldReducer.State( @@ -79,7 +79,7 @@ class TransactionAmountTextFieldTests: XCTestCase { initialState: TransactionAmountTextFieldState( currencySelectionState: - CurrencySelectionState( + CurrencySelectionReducer.State( currencyType: .zec ), textFieldState: @@ -111,7 +111,7 @@ class TransactionAmountTextFieldTests: XCTestCase { initialState: TransactionAmountTextFieldState( currencySelectionState: - CurrencySelectionState( + CurrencySelectionReducer.State( currencyType: .zec ), textFieldState: @@ -143,7 +143,7 @@ class TransactionAmountTextFieldTests: XCTestCase { initialState: TransactionAmountTextFieldState( currencySelectionState: - CurrencySelectionState( + CurrencySelectionReducer.State( currencyType: .usd ), textFieldState: @@ -175,7 +175,7 @@ class TransactionAmountTextFieldTests: XCTestCase { initialState: TransactionAmountTextFieldState( currencySelectionState: - CurrencySelectionState( + CurrencySelectionReducer.State( currencyType: .usd ), maxValue: 100_000_000, @@ -217,7 +217,7 @@ class TransactionAmountTextFieldTests: XCTestCase { initialState: TransactionAmountTextFieldState( currencySelectionState: - CurrencySelectionState( + CurrencySelectionReducer.State( currencyType: .zec ), maxValue: 200_000_000, @@ -249,7 +249,7 @@ class TransactionAmountTextFieldTests: XCTestCase { initialState: TransactionAmountTextFieldState( currencySelectionState: - CurrencySelectionState( + CurrencySelectionReducer.State( currencyType: .usd ), maxValue: 200_000_000, diff --git a/secantTests/SnapshotTests/SendSnapshotTests/TransactionConfirmationSnapshotTests.swift b/secantTests/SnapshotTests/SendSnapshotTests/TransactionConfirmationSnapshotTests.swift index c073865..0bbfd89 100644 --- a/secantTests/SnapshotTests/SendSnapshotTests/TransactionConfirmationSnapshotTests.swift +++ b/secantTests/SnapshotTests/SendSnapshotTests/TransactionConfirmationSnapshotTests.swift @@ -32,7 +32,7 @@ class TransactionConfirmationSnapshotTests: XCTestCase { ) ) state.transactionAmountInputState = TransactionAmountTextFieldState( - currencySelectionState: CurrencySelectionState(), + currencySelectionState: CurrencySelectionReducer.State(), textFieldState: TCATextFieldReducer.State( validationType: nil, text: "2.91" @@ -69,7 +69,7 @@ class TransactionConfirmationSnapshotTests: XCTestCase { ) ) state.transactionAmountInputState = TransactionAmountTextFieldState( - currencySelectionState: CurrencySelectionState(), + currencySelectionState: CurrencySelectionReducer.State(), textFieldState: TCATextFieldReducer.State( validationType: nil, text: "2.91" diff --git a/secantTests/SnapshotTests/SendSnapshotTests/TransactionSendingTests.swift b/secantTests/SnapshotTests/SendSnapshotTests/TransactionSendingTests.swift index 4295163..7a4e748 100644 --- a/secantTests/SnapshotTests/SendSnapshotTests/TransactionSendingTests.swift +++ b/secantTests/SnapshotTests/SendSnapshotTests/TransactionSendingTests.swift @@ -32,7 +32,7 @@ class TransactionSendingTests: XCTestCase { ) ) state.transactionAmountInputState = TransactionAmountTextFieldState( - currencySelectionState: CurrencySelectionState(), + currencySelectionState: CurrencySelectionReducer.State(), textFieldState: TCATextFieldReducer.State( validationType: nil, text: "2.91"