[#465] Migrate TCATextField to ReducerProtocol (#474)

- TCATextField migrated to ReducerProtocol
- unit tests fixed
This commit is contained in:
Lukas Korba 2022-11-05 16:47:11 +01:00 committed by GitHub
parent df61f72459
commit 13061e7a1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 65 additions and 71 deletions

View File

@ -103,8 +103,7 @@ struct SingleLineTextField_Previews: PreviewProvider {
validationType: .floatingPoint, validationType: .floatingPoint,
text: "" text: ""
), ),
reducer: .default, reducer: TCATextFieldReducer()
environment: .init()
) )
) )
.preferredColorScheme(.dark) .preferredColorScheme(.dark)
@ -118,8 +117,7 @@ struct SingleLineTextField_Previews: PreviewProvider {
validationType: .email, validationType: .email,
text: "" text: ""
), ),
reducer: .default, reducer: TCATextFieldReducer()
environment: .init()
) )
) )
.preferredColorScheme(.light) .preferredColorScheme(.light)

View File

@ -7,37 +7,25 @@
import ComposableArchitecture import ComposableArchitecture
typealias TCATextFieldReducer = Reducer<TCATextFieldState, TCATextFieldAction, TCATextFieldEnvironment> typealias TCATextFieldStore = Store<TCATextFieldReducer.State, TCATextFieldReducer.Action>
typealias TCATextFieldStore = Store<TCATextFieldState, TCATextFieldAction>
typealias TCATextFieldViewStore = ViewStore<TCATextFieldState, TCATextFieldAction>
// MARK: - State struct TCATextFieldReducer: ReducerProtocol {
struct State: Equatable {
var validationType: String.ValidationType?
var text: String
var valid = false
struct TCATextFieldState: Equatable { init(validationType: String.ValidationType?, text: String) {
var validationType: String.ValidationType? self.validationType = validationType
var text: String self.text = text
var valid = false }
init(validationType: String.ValidationType?, text: String) {
self.validationType = validationType
self.text = text
} }
}
// MARK: - Action enum Action: Equatable {
case set(String)
enum TCATextFieldAction: Equatable { }
case set(String)
} func reduce(into state: inout State, action: Action) -> ComposableArchitecture.EffectTask<Action> {
// MARK: - Environment
struct TCATextFieldEnvironment { }
// MARK: - Reducer
extension TCATextFieldReducer {
static let `default` = TCATextFieldReducer { state, action, _ in
switch action { switch action {
case .set(let text): case .set(let text):
state.text = text state.text = text
@ -53,29 +41,27 @@ extension TCATextFieldStore {
static var transaction: Self { static var transaction: Self {
.init( .init(
initialState: .init(validationType: .customFloatingPoint(.zcashNumberFormatter), text: ""), initialState: .init(validationType: .customFloatingPoint(.zcashNumberFormatter), text: ""),
reducer: .default, reducer: TCATextFieldReducer()
environment: .init()
) )
} }
static var address: Self { static var address: Self {
.init( .init(
initialState: .init(validationType: .email, text: ""), initialState: .init(validationType: .email, text: ""),
reducer: .default, reducer: TCATextFieldReducer()
environment: .init()
) )
} }
} }
// MARK: - Placeholders // MARK: - Placeholders
extension TCATextFieldState { extension TCATextFieldReducer.State {
static let placeholder = TCATextFieldState( static let placeholder = TCATextFieldReducer.State(
validationType: nil, validationType: nil,
text: "" text: ""
) )
static let amount = TCATextFieldState( static let amount = TCATextFieldReducer.State(
validationType: .floatingPoint, validationType: .floatingPoint,
text: "" text: ""
) )

View File

@ -16,14 +16,16 @@ typealias TransactionAddressTextFieldReducer = Reducer<
typealias TransactionAddressTextFieldStore = Store<TransactionAddressTextFieldState, TransactionAddressTextFieldAction> typealias TransactionAddressTextFieldStore = Store<TransactionAddressTextFieldState, TransactionAddressTextFieldAction>
typealias AnyTCATextFieldReducerAddress = AnyReducer<TCATextFieldReducer.State, TCATextFieldReducer.Action, TransactionAddressTextFieldEnvironment>
struct TransactionAddressTextFieldState: Equatable { struct TransactionAddressTextFieldState: Equatable {
var isValidAddress = false var isValidAddress = false
var textFieldState: TCATextFieldState var textFieldState: TCATextFieldReducer.State
} }
enum TransactionAddressTextFieldAction: Equatable { enum TransactionAddressTextFieldAction: Equatable {
case clearAddress case clearAddress
case textField(TCATextFieldAction) case textField(TCATextFieldReducer.Action)
} }
struct TransactionAddressTextFieldEnvironment { struct TransactionAddressTextFieldEnvironment {
@ -55,10 +57,13 @@ extension TransactionAddressTextFieldReducer {
} }
} }
private static let textFieldReducer: TransactionAddressTextFieldReducer = TCATextFieldReducer.default.pullback( private static let textFieldReducer: TransactionAddressTextFieldReducer = AnyTCATextFieldReducerAddress { _ in
TCATextFieldReducer()
}
.pullback(
state: \TransactionAddressTextFieldState.textFieldState, state: \TransactionAddressTextFieldState.textFieldState,
action: /TransactionAddressTextFieldAction.textField, action: /TransactionAddressTextFieldAction.textField,
environment: { _ in return .init() } environment: { $0 }
) )
} }

View File

@ -17,11 +17,13 @@ typealias TransactionAmountTextFieldReducer = Reducer<
typealias TransactionAmountTextFieldStore = Store<TransactionAmountTextFieldState, TransactionAmountTextFieldAction> typealias TransactionAmountTextFieldStore = Store<TransactionAmountTextFieldState, TransactionAmountTextFieldAction>
typealias AnyTCATextFieldReducerAmount = AnyReducer<TCATextFieldReducer.State, TCATextFieldReducer.Action, TransactionAmountTextFieldEnvironment>
struct TransactionAmountTextFieldState: Equatable { struct TransactionAmountTextFieldState: Equatable {
var amount: Int64 = 0 var amount: Int64 = 0
var currencySelectionState: CurrencySelectionState var currencySelectionState: CurrencySelectionState
var maxValue: Int64 = 0 var maxValue: Int64 = 0
var textFieldState: TCATextFieldState var textFieldState: TCATextFieldReducer.State
// TODO [#311]: - Get the ZEC price from the SDK, https://github.com/zcash/secant-ios-wallet/issues/311 // TODO [#311]: - Get the ZEC price from the SDK, https://github.com/zcash/secant-ios-wallet/issues/311
var zecPrice = Decimal(140.0) var zecPrice = Decimal(140.0)
@ -34,7 +36,7 @@ enum TransactionAmountTextFieldAction: Equatable {
case clearValue case clearValue
case currencySelection(CurrencySelectionAction) case currencySelection(CurrencySelectionAction)
case setMax case setMax
case textField(TCATextFieldAction) case textField(TCATextFieldReducer.Action)
case updateAmount case updateAmount
} }
@ -104,12 +106,15 @@ extension TransactionAmountTextFieldReducer {
} }
} }
private static let textFieldReducer: TransactionAmountTextFieldReducer = TCATextFieldReducer.default.pullback( private static let textFieldReducer: TransactionAmountTextFieldReducer = AnyTCATextFieldReducerAmount { _ in
TCATextFieldReducer()
}
.pullback(
state: \TransactionAmountTextFieldState.textFieldState, state: \TransactionAmountTextFieldState.textFieldState,
action: /TransactionAmountTextFieldAction.textField, action: /TransactionAmountTextFieldAction.textField,
environment: { _ in return .init() } environment: { $0 }
) )
private static let currencySelectionReducer: TransactionAmountTextFieldReducer = CurrencySelectionReducer.default.pullback( private static let currencySelectionReducer: TransactionAmountTextFieldReducer = CurrencySelectionReducer.default.pullback(
state: \TransactionAmountTextFieldState.currencySelectionState, state: \TransactionAmountTextFieldState.currencySelectionState,
action: /TransactionAmountTextFieldAction.currencySelection, action: /TransactionAmountTextFieldAction.currencySelection,

View File

@ -319,7 +319,7 @@ class SendTests: XCTestCase {
currencySelectionState: CurrencySelectionState(), currencySelectionState: CurrencySelectionState(),
maxValue: 501_300, maxValue: 501_300,
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .customFloatingPoint(usNumberFormatter), validationType: .customFloatingPoint(usNumberFormatter),
text: "" text: ""
) )
@ -398,7 +398,7 @@ class SendTests: XCTestCase {
TransactionAmountTextFieldState( TransactionAmountTextFieldState(
currencySelectionState: CurrencySelectionState(), currencySelectionState: CurrencySelectionState(),
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .customFloatingPoint(usNumberFormatter), validationType: .customFloatingPoint(usNumberFormatter),
text: "" text: ""
) )
@ -433,7 +433,7 @@ class SendTests: XCTestCase {
currencySelectionState: CurrencySelectionState(), currencySelectionState: CurrencySelectionState(),
maxValue: 501_302, maxValue: 501_302,
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .customFloatingPoint(usNumberFormatter), validationType: .customFloatingPoint(usNumberFormatter),
text: "0.00501301" text: "0.00501301"
) )
@ -481,7 +481,7 @@ class SendTests: XCTestCase {
currencySelectionState: CurrencySelectionState(), currencySelectionState: CurrencySelectionState(),
maxValue: 501_300, maxValue: 501_300,
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .floatingPoint, validationType: .floatingPoint,
text: "0.00501301" text: "0.00501301"
) )
@ -529,7 +529,7 @@ class SendTests: XCTestCase {
currencySelectionState: CurrencySelectionState(), currencySelectionState: CurrencySelectionState(),
maxValue: 501_302, maxValue: 501_302,
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .floatingPoint, validationType: .floatingPoint,
text: "0.00501301" text: "0.00501301"
) )
@ -577,7 +577,7 @@ class SendTests: XCTestCase {
currencySelectionState: CurrencySelectionState(), currencySelectionState: CurrencySelectionState(),
maxValue: 501_302, maxValue: 501_302,
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .floatingPoint, validationType: .floatingPoint,
text: "0.0.0501301" text: "0.0.0501301"
) )
@ -624,7 +624,7 @@ class SendTests: XCTestCase {
TransactionAddressTextFieldState( TransactionAddressTextFieldState(
isValidAddress: true, isValidAddress: true,
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .none, validationType: .none,
text: "t1gXqfSSQt6WfpwyuCU3Wi7sSVZ66DYQ3Po" text: "t1gXqfSSQt6WfpwyuCU3Wi7sSVZ66DYQ3Po"
) )
@ -635,7 +635,7 @@ class SendTests: XCTestCase {
currencySelectionState: CurrencySelectionState(), currencySelectionState: CurrencySelectionState(),
maxValue: 501_302, maxValue: 501_302,
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .floatingPoint, validationType: .floatingPoint,
text: "0.0.0501301" text: "0.0.0501301"
) )
@ -679,7 +679,7 @@ class SendTests: XCTestCase {
currencySelectionState: CurrencySelectionState(), currencySelectionState: CurrencySelectionState(),
maxValue: 501_302, maxValue: 501_302,
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .floatingPoint, validationType: .floatingPoint,
text: "0.0.0501301" text: "0.0.0501301"
) )

View File

@ -15,7 +15,7 @@ class TransactionAddressTextFieldTests: XCTestCase {
initialState: initialState:
TransactionAddressTextFieldState( TransactionAddressTextFieldState(
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: nil, validationType: nil,
text: "t1gXqfSSQt6WfpwyuCU3Wi7sSVZ66DYQ3Po" text: "t1gXqfSSQt6WfpwyuCU3Wi7sSVZ66DYQ3Po"
) )

View File

@ -28,7 +28,7 @@ class TransactionAmountTextFieldTests: XCTestCase {
currencySelectionState: CurrencySelectionState(), currencySelectionState: CurrencySelectionState(),
maxValue: 501_301, maxValue: 501_301,
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .customFloatingPoint(usNumberFormatter), validationType: .customFloatingPoint(usNumberFormatter),
text: "0.002" text: "0.002"
) )
@ -56,7 +56,7 @@ class TransactionAmountTextFieldTests: XCTestCase {
currencySelectionState: CurrencySelectionState(), currencySelectionState: CurrencySelectionState(),
maxValue: 501_301, maxValue: 501_301,
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .floatingPoint, validationType: .floatingPoint,
text: "0.002" text: "0.002"
) )
@ -83,7 +83,7 @@ class TransactionAmountTextFieldTests: XCTestCase {
currencyType: .zec currencyType: .zec
), ),
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .customFloatingPoint(usNumberFormatter), validationType: .customFloatingPoint(usNumberFormatter),
text: "1.0" text: "1.0"
), ),
@ -115,7 +115,7 @@ class TransactionAmountTextFieldTests: XCTestCase {
currencyType: .zec currencyType: .zec
), ),
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .customFloatingPoint(usNumberFormatter), validationType: .customFloatingPoint(usNumberFormatter),
text: "25000" text: "25000"
), ),
@ -147,7 +147,7 @@ class TransactionAmountTextFieldTests: XCTestCase {
currencyType: .usd currencyType: .usd
), ),
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .customFloatingPoint(usNumberFormatter), validationType: .customFloatingPoint(usNumberFormatter),
text: "1 000" text: "1 000"
), ),
@ -180,7 +180,7 @@ class TransactionAmountTextFieldTests: XCTestCase {
), ),
maxValue: 100_000_000, maxValue: 100_000_000,
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .customFloatingPoint(usNumberFormatter), validationType: .customFloatingPoint(usNumberFormatter),
text: "5" text: "5"
), ),
@ -222,7 +222,7 @@ class TransactionAmountTextFieldTests: XCTestCase {
), ),
maxValue: 200_000_000, maxValue: 200_000_000,
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .customFloatingPoint(usNumberFormatter), validationType: .customFloatingPoint(usNumberFormatter),
text: "5" text: "5"
), ),
@ -254,7 +254,7 @@ class TransactionAmountTextFieldTests: XCTestCase {
), ),
maxValue: 200_000_000, maxValue: 200_000_000,
textFieldState: textFieldState:
TCATextFieldState( TCATextFieldReducer.State(
validationType: .customFloatingPoint(usNumberFormatter), validationType: .customFloatingPoint(usNumberFormatter),
text: "5" text: "5"
), ),

View File

@ -26,14 +26,14 @@ class TransactionConfirmationSnapshotTests: XCTestCase {
var state = SendFlowState.placeholder var state = SendFlowState.placeholder
state.addMemoState = true state.addMemoState = true
state.transactionAddressInputState = TransactionAddressTextFieldState( state.transactionAddressInputState = TransactionAddressTextFieldState(
textFieldState: TCATextFieldState( textFieldState: TCATextFieldReducer.State(
validationType: nil, validationType: nil,
text: "ztestmockeddestinationaddress" text: "ztestmockeddestinationaddress"
) )
) )
state.transactionAmountInputState = TransactionAmountTextFieldState( state.transactionAmountInputState = TransactionAmountTextFieldState(
currencySelectionState: CurrencySelectionState(), currencySelectionState: CurrencySelectionState(),
textFieldState: TCATextFieldState( textFieldState: TCATextFieldReducer.State(
validationType: nil, validationType: nil,
text: "2.91" text: "2.91"
) )
@ -63,14 +63,14 @@ class TransactionConfirmationSnapshotTests: XCTestCase {
var state = SendFlowState.placeholder var state = SendFlowState.placeholder
state.addMemoState = true state.addMemoState = true
state.transactionAddressInputState = TransactionAddressTextFieldState( state.transactionAddressInputState = TransactionAddressTextFieldState(
textFieldState: TCATextFieldState( textFieldState: TCATextFieldReducer.State(
validationType: nil, validationType: nil,
text: "ztestmockeddestinationaddress" text: "ztestmockeddestinationaddress"
) )
) )
state.transactionAmountInputState = TransactionAmountTextFieldState( state.transactionAmountInputState = TransactionAmountTextFieldState(
currencySelectionState: CurrencySelectionState(), currencySelectionState: CurrencySelectionState(),
textFieldState: TCATextFieldState( textFieldState: TCATextFieldReducer.State(
validationType: nil, validationType: nil,
text: "2.91" text: "2.91"
) )

View File

@ -26,14 +26,14 @@ class TransactionSendingTests: XCTestCase {
var state = SendFlowState.placeholder var state = SendFlowState.placeholder
state.addMemoState = true state.addMemoState = true
state.transactionAddressInputState = TransactionAddressTextFieldState( state.transactionAddressInputState = TransactionAddressTextFieldState(
textFieldState: TCATextFieldState( textFieldState: TCATextFieldReducer.State(
validationType: nil, validationType: nil,
text: "ztestmockeddestinationaddress" text: "ztestmockeddestinationaddress"
) )
) )
state.transactionAmountInputState = TransactionAmountTextFieldState( state.transactionAmountInputState = TransactionAmountTextFieldState(
currencySelectionState: CurrencySelectionState(), currencySelectionState: CurrencySelectionState(),
textFieldState: TCATextFieldState( textFieldState: TCATextFieldReducer.State(
validationType: nil, validationType: nil,
text: "2.91" text: "2.91"
) )