Skip to content

Commit e151a66

Browse files
committed
Track presented feature form with preference
1 parent 41698cc commit e151a66

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

Sources/ArcGISToolkit/Components/FeatureFormView/EmbeddedFeatureFormView.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import ArcGIS
1616
import SwiftUI
1717

1818
struct EmbeddedFeatureFormView: View {
19-
/// The environment value to access the closure to call when the presented feature form changes.
20-
@Environment(\.formChangedAction) var formChangedAction
21-
2219
/// A Boolean value indicating whether the deprecated FeatureFormView initializer was used.
2320
@Environment(\.formDeprecatedInitializerWasUsed) var deprecatedInitializerWasUsed
2421

@@ -86,9 +83,6 @@ struct EmbeddedFeatureFormView: View {
8683
.task {
8784
await embeddedFeatureFormViewModel.initialEvaluation()
8885
}
89-
.onAppear {
90-
formChangedAction?(embeddedFeatureFormViewModel.featureForm)
91-
}
9286
.featureFormToolbar(embeddedFeatureFormViewModel.featureForm, isAForm: true)
9387
}
9488
}

Sources/ArcGISToolkit/Components/FeatureFormView/FeatureFormView+EnvironmentValues.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ extension EnvironmentValues /* FeatureFormView */ {
2222
/// An error thrown from a call to `FeatureForm.finishEditing()`.
2323
@Entry var finishEditingError: Binding<(any Error)?> = .constant(nil)
2424

25-
/// The environment value to access the closure to call when the presented feature form changes.
26-
@Entry var formChangedAction: ((FeatureForm) -> Void)?
27-
2825
/// A Boolean value indicating whether the deprecated FeatureFormView initializer was used.
2926
@Entry var formDeprecatedInitializerWasUsed = false
3027

Sources/ArcGISToolkit/Components/FeatureFormView/FeatureFormView.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ public struct FeatureFormView: View {
135135
switch itemType {
136136
case let .form(form):
137137
EmbeddedFeatureFormView(featureForm: form)
138+
.preference(
139+
key: PresentedFeatureFormPreferenceKey.self,
140+
value: .init(featureForm: form)
141+
)
138142
case let .utilityAssociationFilterResultView(result, embeddedFeatureFormViewModel):
139143
UtilityAssociationsFilterResultView(
140144
embeddedFeatureFormViewModel: embeddedFeatureFormViewModel,
@@ -143,9 +147,10 @@ public struct FeatureFormView: View {
143147
.featureFormToolbar(embeddedFeatureFormViewModel.featureForm)
144148
.navigationBarTitleDisplayMode(.inline)
145149
.navigationTitle(result.filter.title, subtitle: embeddedFeatureFormViewModel.title)
146-
.onAppear {
147-
formChangedAction(embeddedFeatureFormViewModel.featureForm)
148-
}
150+
.preference(
151+
key: PresentedFeatureFormPreferenceKey.self,
152+
value: .init(featureForm: embeddedFeatureFormViewModel.featureForm)
153+
)
149154
case let .utilityAssociationGroupResultView(result, embeddedFeatureFormViewModel):
150155
UtilityAssociationGroupResultView(
151156
embeddedFeatureFormViewModel: embeddedFeatureFormViewModel,
@@ -154,11 +159,12 @@ public struct FeatureFormView: View {
154159
.featureFormToolbar(embeddedFeatureFormViewModel.featureForm)
155160
.navigationBarTitleDisplayMode(.inline)
156161
.navigationTitle(result.name, subtitle: embeddedFeatureFormViewModel.title)
157-
.onAppear {
158-
formChangedAction(embeddedFeatureFormViewModel.featureForm)
159-
}
160162
}
161163
}
164+
.preference(
165+
key: PresentedFeatureFormPreferenceKey.self,
166+
value: .init(featureForm: rootFeatureForm)
167+
)
162168
}
163169
// Alert for abandoning unsaved edits
164170
.alert(
@@ -261,7 +267,6 @@ public struct FeatureFormView: View {
261267
)
262268
.environment(\.editingButtonVisibility, editingButtonsVisibility)
263269
.environment(\.finishEditingError, $finishEditingError)
264-
.environment(\.formChangedAction, formChangedAction)
265270
.environment(\.formDeprecatedInitializerWasUsed, deprecatedInitializerWasUsed)
266271
.environment(\.isPresented, isPresented)
267272
.environment(\.navigationIsDisabled, navigationIsDisabled)
@@ -270,6 +275,10 @@ public struct FeatureFormView: View {
270275
.environment(\.setAlertContinuation, setAlertContinuation)
271276
.environment(\.validationErrorVisibilityExternal, validationErrorVisibilityExternal)
272277
.environment(\.validationErrorVisibilityInternal, $validationErrorVisibilityInternal)
278+
.onPreferenceChange(PresentedFeatureFormPreferenceKey.self) { wrappedFeatureForm in
279+
guard let wrappedFeatureForm else { return }
280+
formChangedAction(wrappedFeatureForm.featureForm)
281+
}
273282
}
274283
}
275284
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2025 Esri
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import ArcGIS
16+
import SwiftUI
17+
18+
/// A preference key that specifies the feature form currently presented in the `FeatureFormView` navigation stack.
19+
struct PresentedFeatureFormPreferenceKey: PreferenceKey {
20+
/// A wrapper for making a feature form equatable.
21+
struct EquatableFeatureForm: Equatable {
22+
let featureForm: FeatureForm
23+
24+
static func == (lhs: Self, rhs: Self) -> Bool {
25+
lhs.featureForm === rhs.featureForm
26+
}
27+
}
28+
29+
// MARK: PreferenceKey Conformance
30+
31+
static let defaultValue: EquatableFeatureForm? = nil
32+
33+
static func reduce(value: inout EquatableFeatureForm?, nextValue: () -> EquatableFeatureForm?) {
34+
guard let nextValue = nextValue() else { return }
35+
value = nextValue
36+
}
37+
}

0 commit comments

Comments
 (0)