|
| 1 | +// Copyright 2023 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 ArcGISToolkit |
| 17 | +import SwiftUI |
| 18 | + |
| 19 | +struct FeatureFormExampleView: View { |
| 20 | + /// The height to present the form at. |
| 21 | + @State private var detent: FloatingPanelDetent = .full |
| 22 | + |
| 23 | + /// The `Map` displayed in the `MapView`. |
| 24 | + @State private var map = Map(url: .sampleData)! |
| 25 | + |
| 26 | + /// The point on the screen the user tapped on to identify a feature. |
| 27 | + @State private var identifyScreenPoint: CGPoint? |
| 28 | + |
| 29 | + /// A Boolean value indicating whether the alert confirming the user's intent to cancel is displayed. |
| 30 | + @State private var isCancelConfirmationPresented = false |
| 31 | + |
| 32 | + /// The validation error visibility configuration of the form. |
| 33 | + @State var validationErrorVisibility = FeatureFormView.ValidationErrorVisibility.automatic |
| 34 | + |
| 35 | + /// The form view model provides a channel of communication between the form view and its host. |
| 36 | + @StateObject private var model = Model() |
| 37 | + |
| 38 | + /// The height of the map view's attribution bar. |
| 39 | + @State private var attributionBarHeight: CGFloat = 0 |
| 40 | + |
| 41 | + var body: some View { |
| 42 | + MapViewReader { mapViewProxy in |
| 43 | + MapView(map: map) |
| 44 | + .onAttributionBarHeightChanged { |
| 45 | + attributionBarHeight = $0 |
| 46 | + } |
| 47 | + .onSingleTapGesture { screenPoint, _ in |
| 48 | + if model.isFormPresented { |
| 49 | + isCancelConfirmationPresented = true |
| 50 | + } else { |
| 51 | + identifyScreenPoint = screenPoint |
| 52 | + } |
| 53 | + } |
| 54 | + .task(id: identifyScreenPoint) { |
| 55 | + if let feature = await identifyFeature(with: mapViewProxy), |
| 56 | + let formDefinition = (feature.table?.layer as? FeatureLayer)?.featureFormDefinition, |
| 57 | + let featureForm = FeatureForm(feature: feature, definition: formDefinition) { |
| 58 | + model.featureForm = featureForm |
| 59 | + } |
| 60 | + } |
| 61 | + .ignoresSafeArea(.keyboard) |
| 62 | + .floatingPanel( |
| 63 | + attributionBarHeight: attributionBarHeight, |
| 64 | + selectedDetent: $detent, |
| 65 | + horizontalAlignment: .leading, |
| 66 | + isPresented: $model.isFormPresented |
| 67 | + ) { |
| 68 | + if let featureForm = model.featureForm { |
| 69 | + FeatureFormView(featureForm: featureForm) |
| 70 | + .validationErrors(validationErrorVisibility) |
| 71 | + .padding([.horizontal]) |
| 72 | + } |
| 73 | + } |
| 74 | + .onChange(of: model.isFormPresented) { isFormPresented in |
| 75 | + if !isFormPresented { validationErrorVisibility = .automatic } |
| 76 | + } |
| 77 | + .alert("Discard edits", isPresented: $isCancelConfirmationPresented) { |
| 78 | + Button("Discard edits", role: .destructive) { |
| 79 | + model.discardEdits() |
| 80 | + } |
| 81 | + Button("Continue editing", role: .cancel) { } |
| 82 | + } message: { |
| 83 | + Text("Updates to this feature will be lost.") |
| 84 | + } |
| 85 | + .navigationBarBackButtonHidden(model.isFormPresented) |
| 86 | + .toolbar { |
| 87 | + // Once iOS 16.0 is the minimum supported, the two conditionals to show the |
| 88 | + // buttons can be merged and hoisted up as the root content of the toolbar. |
| 89 | + |
| 90 | + ToolbarItem(placement: .navigationBarLeading) { |
| 91 | + if model.isFormPresented { |
| 92 | + Button("Cancel", role: .cancel) { |
| 93 | + isCancelConfirmationPresented = true |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + ToolbarItem(placement: .navigationBarTrailing) { |
| 99 | + if model.isFormPresented { |
| 100 | + Button("Submit") { |
| 101 | + validationErrorVisibility = .visible |
| 102 | + Task { |
| 103 | + await model.submitChanges() |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +extension FeatureFormExampleView { |
| 114 | + /// Identifies features, if any, at the current screen point. |
| 115 | + /// - Parameter proxy: The proxy to use for identification. |
| 116 | + /// - Returns: The first identified feature in a layer with |
| 117 | + /// a feature form definition. |
| 118 | + func identifyFeature(with proxy: MapViewProxy) async -> ArcGISFeature? { |
| 119 | + guard let identifyScreenPoint else { return nil } |
| 120 | + let identifyResult = try? await proxy.identifyLayers( |
| 121 | + screenPoint: identifyScreenPoint, |
| 122 | + tolerance: 10 |
| 123 | + ) |
| 124 | + .first(where: { result in |
| 125 | + if let feature = result.geoElements.first as? ArcGISFeature, |
| 126 | + (feature.table?.layer as? FeatureLayer)?.featureFormDefinition != nil { |
| 127 | + return true |
| 128 | + } else { |
| 129 | + return false |
| 130 | + } |
| 131 | + }) |
| 132 | + return identifyResult?.geoElements.first as? ArcGISFeature |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +private extension URL { |
| 137 | + static var sampleData: Self { |
| 138 | + .init(string: "<#URL#>")! |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +/// The model class for the form example view |
| 143 | +@MainActor |
| 144 | +class Model: ObservableObject { |
| 145 | + /// The feature form. |
| 146 | + @Published var featureForm: FeatureForm? { |
| 147 | + willSet { |
| 148 | + if let featureForm = newValue { |
| 149 | + featureForm.featureLayer?.selectFeature(featureForm.feature) |
| 150 | + } else if let featureForm = self.featureForm { |
| 151 | + featureForm.featureLayer?.unselectFeature(featureForm.feature) |
| 152 | + } |
| 153 | + } |
| 154 | + didSet { |
| 155 | + isFormPresented = featureForm != nil |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /// A Boolean value indicating whether or not the form is displayed. |
| 160 | + @Published var isFormPresented = false |
| 161 | + |
| 162 | + /// Reverts any local edits that haven't yet been saved to service geodatabase. |
| 163 | + func discardEdits() { |
| 164 | + featureForm?.discardEdits() |
| 165 | + featureForm = nil |
| 166 | + } |
| 167 | + |
| 168 | + /// Submit the changes made to the form. |
| 169 | + func submitChanges() async { |
| 170 | + guard let featureForm = featureForm, |
| 171 | + let table = featureForm.feature.table as? ServiceFeatureTable, |
| 172 | + table.isEditable, |
| 173 | + let database = table.serviceGeodatabase else { |
| 174 | + print("A precondition to submit the changes wasn't met.") |
| 175 | + return |
| 176 | + } |
| 177 | + |
| 178 | + guard featureForm.validationErrors.isEmpty else { return } |
| 179 | + |
| 180 | + try? await table.update(featureForm.feature) |
| 181 | + |
| 182 | + guard database.hasLocalEdits else { |
| 183 | + print("No submittable changes found.") |
| 184 | + return |
| 185 | + } |
| 186 | + |
| 187 | + let results = try? await database.applyEdits() |
| 188 | + |
| 189 | + if results?.first?.editResults.first?.didCompleteWithErrors ?? false { |
| 190 | + print("An error occurred while submitting the changes.") |
| 191 | + } |
| 192 | + |
| 193 | + self.featureForm = nil |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +private extension FeatureForm { |
| 198 | + /// The layer to which the feature belongs. |
| 199 | + var featureLayer: FeatureLayer? { |
| 200 | + feature.table?.layer as? FeatureLayer |
| 201 | + } |
| 202 | +} |
0 commit comments