/
03-Navigation-Multiple-Destinations.swift
84 lines (77 loc) · 1.98 KB
/
03-Navigation-Multiple-Destinations.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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(state: .equatable)
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)
}
}
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)
}
}
}