Skip to content

Instantly share code, notes, and snippets.

@kevinrenskers
Created May 22, 2021 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinrenskers/d3983e6a5496db63da76ebfd61561c02 to your computer and use it in GitHub Desktop.
Save kevinrenskers/d3983e6a5496db63da76ebfd61561c02 to your computer and use it in GitHub Desktop.
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