Skip to content

Commit 6b188d2

Browse files
committed
Restore the gallery as a NavigationStack with nav/sheet/tab screens
1 parent 005804d commit 6b188d2

4 files changed

Lines changed: 176 additions & 22 deletions

File tree

Demo/App.swiftpm/Sources/ContentView.swift

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,28 @@ import AndroidSwiftUI
44
import SwiftUI
55
#endif
66

7-
/// Gallery of playground screens. A picker selects the active screen; full
8-
/// `NavigationStack`/`List` navigation returns in a later step.
7+
/// Gallery of playground screens, navigated with a `NavigationStack`. Each row
8+
/// pushes a feature screen; `List`-based navigation returns with lazy
9+
/// containers in the next step.
910
struct ContentView: View {
1011

11-
@State
12-
private var screen = "Controls"
13-
1412
var body: some View {
15-
VStack(spacing: 0) {
16-
Picker("Screen", selection: $screen) {
17-
Text("Text").tag("Text")
18-
Text("Buttons").tag("Buttons")
19-
Text("Stacks").tag("Stacks")
20-
Text("State").tag("State")
21-
Text("Controls").tag("Controls")
22-
Text("Modifiers").tag("Modifiers")
23-
Text("Observation").tag("Observation")
13+
NavigationStack {
14+
ScrollView {
15+
VStack(spacing: 0) {
16+
NavigationLink("Text", destination: TextScreen())
17+
NavigationLink("Buttons", destination: ButtonScreen())
18+
NavigationLink("Stacks", destination: StacksScreen())
19+
NavigationLink("State", destination: StateScreen())
20+
NavigationLink("Controls", destination: ControlsScreen())
21+
NavigationLink("Modifiers", destination: ModifierScreen())
22+
NavigationLink("Observation", destination: ObservationScreen())
23+
NavigationLink("Navigation", destination: NavigationScreen())
24+
NavigationLink("Sheets", destination: SheetScreen())
25+
NavigationLink("Tabs", destination: TabScreen())
26+
}
2427
}
25-
Divider()
26-
if screen == "Text" { TextScreen() }
27-
if screen == "Buttons" { ButtonScreen() }
28-
if screen == "Stacks" { StacksScreen() }
29-
if screen == "State" { StateScreen() }
30-
if screen == "Controls" { ControlsScreen() }
31-
if screen == "Modifiers" { ModifierScreen() }
32-
if screen == "Observation" { ObservationScreen() }
28+
.navigationTitle("Gallery")
3329
}
3430
}
3531
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#if canImport(AndroidSwiftUI)
2+
import AndroidSwiftUI
3+
#else
4+
import SwiftUI
5+
#endif
6+
7+
/// Classic and value-based navigation, plus the dismiss environment action.
8+
struct NavigationScreen: View {
9+
10+
@Environment(\.dismiss)
11+
private var dismiss
12+
13+
@State
14+
private var showsAlert = false
15+
16+
var body: some View {
17+
VStack(spacing: 16) {
18+
Button("Show alert") { showsAlert = true }
19+
Text("Value-based navigation")
20+
NavigationLink("Push value 1", value: 1)
21+
NavigationLink(value: 2) {
22+
Text("Push value 2")
23+
}
24+
Divider()
25+
Text("Classic navigation")
26+
NavigationLink("Push destination view", destination: ClassicDestination())
27+
Divider()
28+
Button("Pop with dismiss") {
29+
dismiss()
30+
}
31+
}
32+
.navigationDestination(for: Int.self) { value in
33+
ValueDestination(value: value)
34+
}
35+
.alert("An alert", isPresented: $showsAlert, message: "This is an alert message.", buttons: [
36+
AlertButton("Cancel", role: .cancel),
37+
AlertButton("OK"),
38+
])
39+
.navigationTitle("Navigation")
40+
}
41+
}
42+
43+
struct ValueDestination: View {
44+
45+
let value: Int
46+
47+
@Environment(\.dismiss)
48+
private var dismiss
49+
50+
var body: some View {
51+
VStack(spacing: 16) {
52+
Text(verbatim: "Destination for value \(value)")
53+
if value < 3 {
54+
NavigationLink("Push value \(value + 1)", value: value + 1)
55+
}
56+
Button("Pop with dismiss") {
57+
dismiss()
58+
}
59+
}
60+
.navigationTitle("Value \(value)")
61+
}
62+
}
63+
64+
struct ClassicDestination: View {
65+
66+
@Environment(\.dismiss)
67+
private var dismiss
68+
69+
var body: some View {
70+
VStack(spacing: 16) {
71+
Text("Classic destination")
72+
Button("Pop with dismiss") {
73+
dismiss()
74+
}
75+
}
76+
}
77+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#if canImport(AndroidSwiftUI)
2+
import AndroidSwiftUI
3+
#else
4+
import SwiftUI
5+
#endif
6+
7+
/// Sheet presentation, detents and the dismiss environment action.
8+
struct SheetScreen: View {
9+
10+
@State
11+
private var showsSheet = false
12+
13+
@State
14+
private var showsDetentSheet = false
15+
16+
var body: some View {
17+
VStack(spacing: 16) {
18+
Button("Present sheet") {
19+
showsSheet = true
20+
}
21+
Button("Present medium detent sheet") {
22+
showsDetentSheet = true
23+
}
24+
}
25+
.sheet(isPresented: $showsSheet) {
26+
SheetContent(title: "Full size sheet")
27+
}
28+
.sheet(isPresented: $showsDetentSheet) {
29+
SheetContent(title: "Medium sheet")
30+
.presentationDetents([.medium])
31+
}
32+
}
33+
}
34+
35+
struct SheetContent: View {
36+
37+
let title: String
38+
39+
@Environment(\.dismiss)
40+
private var dismiss
41+
42+
var body: some View {
43+
VStack(spacing: 16) {
44+
Text(title)
45+
Button("Dismiss") {
46+
dismiss()
47+
}
48+
}
49+
}
50+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#if canImport(AndroidSwiftUI)
2+
import AndroidSwiftUI
3+
#else
4+
import SwiftUI
5+
#endif
6+
7+
/// Tab bar with selection.
8+
struct TabScreen: View {
9+
10+
@State
11+
private var selection = 0
12+
13+
var body: some View {
14+
TabView(selection: $selection) {
15+
VStack(spacing: 16) {
16+
Text("First tab")
17+
Button("Select third tab") {
18+
selection = 2
19+
}
20+
}
21+
.tabItem { Text("One") }
22+
.tag(0)
23+
Text("Second tab")
24+
.tabItem { Text("Two") }
25+
.tag(1)
26+
Text("Third tab")
27+
.tabItem { Text("Three") }
28+
.tag(2)
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)