Skip to content

Instantly share code, notes, and snippets.

@hallee
Last active October 14, 2022 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hallee/e2c79eb771e6780005d92822981f3df9 to your computer and use it in GitHub Desktop.
Save hallee/e2c79eb771e6780005d92822981f3df9 to your computer and use it in GitHub Desktop.
import ComposableArchitecture
import SwiftUI
struct PopoverReducer: ReducerProtocol {
struct State: Hashable {
var isPopoverVisible: Bool
}
enum Action: Hashable {
case setPopoverVisible(visible: Bool)
}
func reduce(into state: inout State, action: Action) -> Effect<Action, Never> {
switch action {
case .setPopoverVisible(let visible):
state.isPopoverVisible = visible
return .none
}
}
}
struct ContentView: View {
let store: StoreOf<PopoverReducer> = .init(
initialState: .init(isPopoverVisible: false),
reducer: PopoverReducer()
)
var body: some View {
WithViewStore(store) { viewStore in
VStack {
Button {
viewStore.send(.setPopoverVisible(visible: true))
} label: {
{ () -> EmptyView in
print("PARENT RENDER")
return EmptyView()
}()
Text("Open popover")
}
}
.padding()
.popover(
isPresented: viewStore.binding(
get: { $0.isPopoverVisible },
send: PopoverReducer.Action.setPopoverVisible(visible:)
)
) {
{ () -> EmptyView in
print("POPOVER RENDER")
return EmptyView()
}()
Text("Popover")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment