Files
swift-composable-architectu…/Examples/CaseStudies/SwiftUICaseStudies/04-Navigation-Multiple-Destinations.swift
Stephen Celis f1af33763e Swift 6 updates (#3379)
* Swift 6 updates

  - Soft-deprecate `_SynthesizedConformance` now that Xcode 16 has fixed
    this bug.
    - Update docs accordingly.

  - Document Xcode 16 macro gotcha around custom build configuration
    names.

* wip
2024-09-12 14:11:05 -07:00

86 lines
2.0 KiB
Swift

import ComposableArchitecture
import SwiftUI
private let readMe = """
This screen demonstrates driving 3 kinds of navigation (drill down, sheet, popover) from a single
piece of enum state.
"""
@Reducer
struct MultipleDestinations {
@Reducer
enum Destination {
case drillDown(Counter)
case popover(Counter)
case sheet(Counter)
}
@ObservableState
struct State: Equatable {
@Presents var destination: Destination.State?
}
enum Action {
case destination(PresentationAction<Destination.Action>)
case showDrillDown
case showPopover
case showSheet
}
var body: some Reducer<State, Action> {
Reduce { state, action in
switch action {
case .showDrillDown:
state.destination = .drillDown(Counter.State())
return .none
case .showPopover:
state.destination = .popover(Counter.State())
return .none
case .showSheet:
state.destination = .sheet(Counter.State())
return .none
case .destination:
return .none
}
}
.ifLet(\.$destination, action: \.destination)
}
}
extension MultipleDestinations.Destination.State: Equatable {}
struct MultipleDestinationsView: View {
@Bindable var store: StoreOf<MultipleDestinations>
var body: some View {
Form {
Section {
AboutView(readMe: readMe)
}
Button("Show drill-down") {
store.send(.showDrillDown)
}
Button("Show popover") {
store.send(.showPopover)
}
Button("Show sheet") {
store.send(.showSheet)
}
}
.navigationDestination(
item: $store.scope(state: \.destination?.drillDown, action: \.destination.drillDown)
) { store in
CounterView(store: store)
}
.popover(
item: $store.scope(state: \.destination?.popover, action: \.destination.popover)
) { store in
CounterView(store: store)
}
.sheet(
item: $store.scope(state: \.destination?.sheet, action: \.destination.sheet)
) { store in
CounterView(store: store)
}
}
}