Skip to content

Commit aa5a6f8

Browse files
committed
Move nav stack to child view.
#478 (comment) #478 (comment)
1 parent faec145 commit aa5a6f8

File tree

1 file changed

+53
-50
lines changed

1 file changed

+53
-50
lines changed

Shared/Samples/Manage operational layers/ManageOperationalLayersView.swift

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,9 @@ struct ManageOperationalLayersView: View {
4848
manageLayersIsPresented = true
4949
}
5050
.popover(isPresented: $manageLayersIsPresented) {
51-
NavigationStack {
52-
ManageLayersView(map: map, layers: operationalLayers)
53-
.navigationTitle("Manage Layers")
54-
.navigationBarTitleDisplayMode(.inline)
55-
.toolbar {
56-
ToolbarItem(placement: .confirmationAction) {
57-
Button("Done") { manageLayersIsPresented = false }
58-
}
59-
}
60-
}
61-
.presentationDetents([.fraction(0.4)])
62-
.frame(idealWidth: 320, idealHeight: 260)
51+
ManageLayersView(map: map, layers: operationalLayers)
52+
.presentationDetents([.fraction(0.4)])
53+
.frame(idealWidth: 320, idealHeight: 260)
6354
}
6455
}
6556
}
@@ -74,66 +65,78 @@ private struct ManageLayersView: View {
7465
/// The layers to add to and remove from the map.
7566
let layers: [Layer]
7667

68+
/// The action to dismiss the view.
69+
@Environment(\.dismiss) private var dismiss
70+
7771
/// The layers currently added to the map.
7872
@State private var operationalLayers: [Layer] = []
7973

8074
/// The layers removed from the map.
8175
@State private var removedLayers: [Layer] = []
8276

8377
var body: some View {
84-
Form {
85-
Section {
86-
ForEach(operationalLayers, id: \.id) { layer in
78+
NavigationStack {
79+
Form {
80+
Section {
81+
ForEach(operationalLayers, id: \.id) { layer in
82+
HStack {
83+
Button("Remove Layer", systemImage: "minus.circle.fill") {
84+
map.removeOperationalLayer(layer)
85+
86+
withAnimation {
87+
operationalLayers.removeAll { $0.id == layer.id }
88+
removedLayers.append(layer)
89+
}
90+
}
91+
.foregroundStyle(.red)
92+
93+
Text(layer.name)
94+
}
95+
}
96+
} header: {
8797
HStack {
88-
Button("Remove Layer", systemImage: "minus.circle.fill") {
98+
Text("Operational Layers")
99+
Spacer()
100+
Button("Swap Layers", systemImage: "arrow.up.arrow.down.circle") {
101+
let layer = operationalLayers.first!
89102
map.removeOperationalLayer(layer)
103+
map.addOperationalLayer(layer)
90104

91105
withAnimation {
92-
operationalLayers.removeAll { $0.id == layer.id }
93-
removedLayers.append(layer)
106+
operationalLayers.reverse()
94107
}
95108
}
96-
.foregroundStyle(.red)
97-
98-
Text(layer.name)
109+
.disabled(operationalLayers.count < 2)
99110
}
100111
}
101-
} header: {
102-
HStack {
103-
Text("Operational Layers")
104-
Spacer()
105-
Button("Swap Layers", systemImage: "arrow.up.arrow.down.circle") {
106-
let layer = operationalLayers.first!
107-
map.removeOperationalLayer(layer)
108-
map.addOperationalLayer(layer)
109-
110-
withAnimation {
111-
operationalLayers.reverse()
112+
113+
Section("Removed Layers") {
114+
ForEach(removedLayers, id: \.id) { layer in
115+
HStack {
116+
Button("Add Layer", systemImage: "plus.circle.fill") {
117+
map.addOperationalLayer(layer)
118+
119+
withAnimation {
120+
removedLayers.removeAll { $0.id == layer.id }
121+
operationalLayers.append(layer)
122+
}
123+
}
124+
.foregroundStyle(.green)
125+
126+
Text(layer.name)
112127
}
113128
}
114-
.disabled(operationalLayers.count < 2)
115129
}
116130
}
117-
118-
Section("Removed Layers") {
119-
ForEach(removedLayers, id: \.id) { layer in
120-
HStack {
121-
Button("Add Layer", systemImage: "plus.circle.fill") {
122-
map.addOperationalLayer(layer)
123-
124-
withAnimation {
125-
removedLayers.removeAll { $0.id == layer.id }
126-
operationalLayers.append(layer)
127-
}
128-
}
129-
.foregroundStyle(.green)
130-
131-
Text(layer.name)
132-
}
131+
.labelStyle(.iconOnly)
132+
.navigationTitle("Manage Layers")
133+
.navigationBarTitleDisplayMode(.inline)
134+
.toolbar {
135+
ToolbarItem(placement: .confirmationAction) {
136+
Button("Done", action: dismiss.callAsFunction)
133137
}
134138
}
135139
}
136-
.labelStyle(.iconOnly)
137140
.onAppear {
138141
operationalLayers = map.operationalLayers
139142
removedLayers = layers.filter { layer in

0 commit comments

Comments
 (0)