Skip to content

Commit 7aabd41

Browse files
committed
Fix checkpoint/callback examples
1 parent b9d2119 commit 7aabd41

File tree

2 files changed

+120
-116
lines changed

2 files changed

+120
-116
lines changed

NavigatorDemo/NavigatorDemo/NavigatorDemo/Features/Examples/CallbackExampleView.swift

Lines changed: 119 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -8,117 +8,122 @@
88
import Navigator
99
import SwiftUI
1010

11-
//struct CallbackExampleView: View {
12-
// @State var value: Double = 0
13-
// var body: some View {
14-
// ManagedNavigationStack { navigator in
15-
// List {
16-
// Section {
17-
// Text("Callback Value: \(Int(value))")
18-
// }
19-
// Section {
20-
// ExampleListButton(
21-
// title: "Present Callback Sheet w/Dismiss",
22-
// text: "Callback handler dismisses presented views with dismissPresentedViews.",
23-
// action: {
24-
// navigator.navigate(to: CallbackDestinations.presented(value, .init {
25-
// value = $0
26-
// navigator.dismissPresentedViews()
27-
// }))
28-
// })
29-
// ExampleListButton(
30-
// title: "Present Callback Sheet w/Checkpoint",
31-
// text: "Callback handler dismisses presented views with returnToCheckpoint(.home).",
32-
// action: {
33-
// navigator.navigate(to: CallbackDestinations.presented(value, .init {
34-
// value = $0
35-
// navigator.returnToCheckpoint(.home)
36-
// }))
37-
// })
38-
// }
39-
// Section {
40-
// ExampleListButton(
41-
// title: "Push Callback View",
42-
// text: "Callback handler dismisses presented views with returnToCheckpoint(.home).",
43-
// action: {
44-
// navigator.navigate(to: CallbackDestinations.pushed(value, .init {
45-
// value = $0
46-
// navigator.returnToCheckpoint(.home)
47-
// }))
48-
// })
49-
// }
50-
// Section {
51-
// Button("Dismiss Example") {
52-
// navigator.dismiss()
53-
// }
54-
// }
55-
// }
56-
// .navigationDestination(CallbackDestinations.self)
57-
// // illustrates returning to named checkpoint instead of trying to pop or dismiss
58-
// .navigationCheckpoint(KnownCheckpoints.home)
59-
// // illustrates returning to named checkpoint with value instead of using callback handler
60-
// .navigationCheckpoint(KnownCheckpoints.home) { (value: Double) in
61-
// self.value = value
62-
// }
63-
// .navigationTitle("Callback Example")
64-
// }
65-
// }
66-
//}
67-
//
68-
//enum CallbackDestinations: NavigationDestination {
69-
//
70-
// case presented(Double, Callback<Double>)
71-
// case pushed(Double, Callback<Double>)
72-
//
73-
// var view: some View {
74-
// switch self {
75-
// case .presented(let value, let callback):
76-
// CallbackReturnView(value: value, handler: callback.handler)
77-
// case .pushed(let value, let callback):
78-
// CallbackReturnView(value: value, handler: callback.handler)
79-
// }
80-
// }
81-
//
82-
// var method: NavigationMethod {
83-
// switch self {
84-
// case .presented:
85-
// return .managedSheet
86-
// case .pushed:
87-
// return .push
88-
// }
89-
// }
90-
//}
91-
//
92-
//struct CallbackReturnView: View {
93-
// @State var value: Double
94-
// @Environment(\.navigator) var navigator
95-
// let handler: (Double) -> Void
96-
// var body: some View {
97-
// List {
98-
// Section {
99-
// Text("Callback Value: \(Int(value))")
100-
// Slider(value: $value, in: 1...10, step: 1)
101-
// }
102-
// Section {
103-
// ExampleListButton(
104-
// title: "Callback With Value: \(Int(value))",
105-
// text: "Calls passed callback handler with current value.",
106-
// action: {
107-
// handler(value)
108-
// })
109-
// ExampleListButton(
110-
// title: "Return To Checkpoint With Value: \(Int(value))",
111-
// text: "Demonstrates bypassing the callback handler with returnToCheckpoint(:value:).",
112-
// action: {
113-
// navigator.returnToCheckpoint(.home, value: value)
114-
// })
115-
// }
116-
// Section {
117-
// Button("Dismiss") {
118-
// navigator.returnToCheckpoint(.home)
119-
// }
120-
// }
121-
// }
122-
// .navigationTitle("Callback View")
123-
// }
124-
//}
11+
extension KnownCheckpoints {
12+
public static var homeReturningDouble: NavigationCheckpoint<Double> { checkpoint() }
13+
}
14+
15+
16+
struct CallbackExampleView: View {
17+
@State var value: Double = 0
18+
var body: some View {
19+
ManagedNavigationStack { navigator in
20+
List {
21+
Section {
22+
Text("Callback Value: \(Int(value))")
23+
}
24+
Section {
25+
ExampleListButton(
26+
title: "Present Callback Sheet w/Dismiss",
27+
text: "Callback handler dismisses presented views with dismissPresentedViews.",
28+
action: {
29+
navigator.navigate(to: CallbackDestinations.presented(value, .init {
30+
value = $0
31+
navigator.dismissPresentedViews()
32+
}))
33+
})
34+
ExampleListButton(
35+
title: "Present Callback Sheet w/Checkpoint",
36+
text: "Callback handler dismisses presented views with returnToCheckpoint(.home).",
37+
action: {
38+
navigator.navigate(to: CallbackDestinations.presented(value, .init {
39+
value = $0
40+
navigator.returnToCheckpoint(KnownCheckpoints.home)
41+
}))
42+
})
43+
}
44+
Section {
45+
ExampleListButton(
46+
title: "Push Callback View",
47+
text: "Callback handler dismisses presented views with returnToCheckpoint(.home).",
48+
action: {
49+
navigator.navigate(to: CallbackDestinations.pushed(value, .init {
50+
value = $0
51+
navigator.returnToCheckpoint(KnownCheckpoints.home)
52+
}))
53+
})
54+
}
55+
Section {
56+
Button("Dismiss Example") {
57+
navigator.dismiss()
58+
}
59+
}
60+
}
61+
.navigationDestination(CallbackDestinations.self)
62+
// illustrates returning to named checkpoint instead of trying to pop or dismiss
63+
.navigationCheckpoint(KnownCheckpoints.home)
64+
// illustrates returning to named checkpoint with value instead of using callback handler
65+
.navigationCheckpoint(KnownCheckpoints.homeReturningDouble) { (value: Double) in
66+
self.value = value
67+
}
68+
.navigationTitle("Callback Example")
69+
}
70+
}
71+
}
72+
73+
enum CallbackDestinations: NavigationDestination {
74+
75+
case presented(Double, Callback<Double>)
76+
case pushed(Double, Callback<Double>)
77+
78+
var view: some View {
79+
switch self {
80+
case .presented(let value, let callback):
81+
CallbackReturnView(value: value, handler: callback.handler)
82+
case .pushed(let value, let callback):
83+
CallbackReturnView(value: value, handler: callback.handler)
84+
}
85+
}
86+
87+
var method: NavigationMethod {
88+
switch self {
89+
case .presented:
90+
return .managedSheet
91+
case .pushed:
92+
return .push
93+
}
94+
}
95+
}
96+
97+
struct CallbackReturnView: View {
98+
@State var value: Double
99+
@Environment(\.navigator) var navigator
100+
let handler: (Double) -> Void
101+
var body: some View {
102+
List {
103+
Section {
104+
Text("Callback Value: \(Int(value))")
105+
Slider(value: $value, in: 1...10, step: 1)
106+
}
107+
Section {
108+
ExampleListButton(
109+
title: "Callback With Value: \(Int(value))",
110+
text: "Calls passed callback handler with current value.",
111+
action: {
112+
handler(value)
113+
})
114+
ExampleListButton(
115+
title: "Return To Checkpoint With Value: \(Int(value))",
116+
text: "Demonstrates bypassing the callback handler with returnToCheckpoint(:value:).",
117+
action: {
118+
navigator.returnToCheckpoint(KnownCheckpoints.homeReturningDouble, value: value)
119+
})
120+
}
121+
Section {
122+
Button("Dismiss") {
123+
navigator.returnToCheckpoint(KnownCheckpoints.home)
124+
}
125+
}
126+
}
127+
.navigationTitle("Callback View")
128+
}
129+
}

NavigatorDemo/NavigatorDemo/NavigatorDemo/Features/Examples/ExampleDestinations.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public enum ExampleDestinations: String, NavigationDestination, CaseIterable {
1919
case .binding:
2020
BindingExampleView()
2121
case .callback:
22-
NotAvailableView()
23-
// CallbackExampleView()
22+
CallbackExampleView()
2423
case .transition:
2524
if #available(iOS 18.0, *) {
2625
TransitionExampleView()

0 commit comments

Comments
 (0)