Created
May 22, 2021 13:17
-
-
Save kevinrenskers/d3983e6a5496db63da76ebfd61561c02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ComposableArchitecture | |
struct Environment {} | |
// Account | |
struct AccountState: Equatable { | |
var username: String? | |
} | |
enum AccountAction: Equatable { | |
case login(username: String, password: String) | |
case logout | |
} | |
let accountReducer = Reducer<AccountState, AccountAction, Environment> { state, action, environment in | |
return .none | |
} | |
// Counter | |
struct CounterState: Equatable { | |
var count = 0 | |
} | |
enum CounterAction: Equatable { | |
case increase | |
case decrease | |
} | |
let counterReducer = Reducer<CounterState, CounterAction, Environment> { state, action, environment in | |
return .none | |
} | |
// App | |
struct AppState: Equatable { | |
var accountState = AccountState() | |
var counterState = CounterState() | |
// imagine there are many other substates here as well, one for every feature of the app | |
} | |
enum AppAction: Equatable { | |
case accountAction(AccountAction) | |
case counterAction(CounterAction) | |
} | |
let appReducer = Reducer<AppState, AppAction, Environment>.combine( | |
accountReducer.pullback( | |
state: \.accountState, | |
action: /AppAction.accountAction, | |
environment: { $0 } | |
), | |
counterReducer.pullback( | |
state: \.counterState, | |
action: /AppAction.counterAction, | |
environment: { $0 } | |
) | |
) | |
// ViewController | |
class MyFeatureViewController { | |
private let accountViewStore: ViewStore<AccountState, AccountAction> | |
private let counterViewStore: ViewStore<CounterState, CounterAction> | |
init(accountViewStore: ViewStore<AccountState, AccountAction>, counterViewStore: ViewStore<CounterState, CounterAction>) { | |
self.accountViewStore = accountViewStore | |
self.counterViewStore = counterViewStore | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment