Skip to content

Instantly share code, notes, and snippets.

@pteasima
Created May 30, 2020 20:08
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 pteasima/5d2ef54a7428a3e351065d73b0c719db to your computer and use it in GitHub Desktop.
Save pteasima/5d2ef54a7428a3e351065d73b0c719db to your computer and use it in GitHub Desktop.
MissionControl - Debug tools for ComposableArchitecture
struct MissionControlState<State> {
var currentState: State
}
enum MissionControlAction<Action> {
case app(Action)
case lldb
}
struct MissionControl<State, Action, Environment, Content: View>: View {
let store: Store<MissionControlState<State>, MissionControlAction<Action>>
@ObservedObject var viewStore: ViewStore<ViewState, MissionControlAction<Action>>
let content: Content
init(
initialState: State,
reducer: Reducer<State, Action, Environment>,
environment: Environment,
content: @escaping (Store<State, Action>) -> Content,
lldbHook: @escaping (inout State, Environment, inout [Effect<Action, Never>]) -> Void) {
let store = Store<MissionControlState<State>, MissionControlAction<Action>>(
initialState: MissionControlState(
currentState: initialState),
reducer: .combine([
.init { state, action, environment in
switch action {
case .lldb:
var effects: [Effect<Action, Never>] = []
lldbHook(&state.currentState, environment, &effects)
return Effect.concatenate(effects)
.map(MissionControlAction<Action>.app)
case .app:
return .none
}
},
reducer.pullback(
state: \.currentState,
action: /MissionControlAction.app,
environment: { $0 }),
]),
environment: environment
)
self.store = store
viewStore = ViewStore(store.scope(state: { _ in .init() }))
self.content = content(store.scope(state: \.currentState, action: MissionControlAction.app))
}
var body: some View {
ZStack {
content
VStack(spacing: 20) {
Text("Im a debug overlay.")
Button(action: {
self.viewStore.send(.lldb)
}) {
Text("Enter Script Mode")
.font(.largeTitle)
}
}
.padding()
.background(Color.init(.systemGroupedBackground))
}
}
struct ViewState:Equatable {
}
}
@pteasima
Copy link
Author

Btw I dont think theres a reason not to use WithViewStore, its a property either for historical reasons or because I wrote it at 5am 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment