|
| 1 | +// Copyright 2024 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 EditFeaturesUsingFeatureFormsView: View { |
| 20 | + /// The map that contains the features for editing. |
| 21 | + @State private var map = Map( |
| 22 | + item: PortalItem( |
| 23 | + portal: .arcGISOnline(connection: .anonymous), |
| 24 | + id: .featureFormPlacesWebMap |
| 25 | + ) |
| 26 | + ) |
| 27 | + |
| 28 | + /// The feature form for the selected feature. |
| 29 | + @State private var featureForm: FeatureForm? |
| 30 | + |
| 31 | + /// The point on the screen where the user tapped. |
| 32 | + @State private var tapPoint: CGPoint? |
| 33 | + |
| 34 | + /// A Boolean value indicating whether the feature form panel is presented. |
| 35 | + @State private var isShowingFeatureForm = false |
| 36 | + |
| 37 | + /// A Boolean value indicating whether the discard edits alert is presented. |
| 38 | + @State private var isShowingDiscardEditsAlert = false |
| 39 | + |
| 40 | + /// A Boolean value indicating whether the feature form has any validation errors. |
| 41 | + @State private var hasValidationErrors = false |
| 42 | + |
| 43 | + /// A Boolean value indicating whether the feature form edits are being applied. |
| 44 | + @State private var isApplyingEdits = false |
| 45 | + |
| 46 | + /// The error shown in the error alert. |
| 47 | + @State private var error: Error? |
| 48 | + |
| 49 | + /// The text explaining the next step in the sample's workflow. |
| 50 | + private var instructionText: String { |
| 51 | + if !isShowingFeatureForm { |
| 52 | + "Tap on a feature to edit." |
| 53 | + } else if hasValidationErrors { |
| 54 | + "Fix the errors to apply the edits." |
| 55 | + } else if isApplyingEdits { |
| 56 | + "Applying edits..." |
| 57 | + } else { |
| 58 | + "Use the form to edit the feature's attributes." |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// The feature layer on the map. |
| 63 | + private var featureLayer: FeatureLayer { |
| 64 | + map.operationalLayers.first as! FeatureLayer |
| 65 | + } |
| 66 | + |
| 67 | + var body: some View { |
| 68 | + MapViewReader { mapViewProxy in |
| 69 | + MapView(map: map) |
| 70 | + .onSingleTapGesture { screenPoint, _ in |
| 71 | + if isShowingFeatureForm { |
| 72 | + isShowingDiscardEditsAlert = true |
| 73 | + } else { |
| 74 | + tapPoint = screenPoint |
| 75 | + } |
| 76 | + } |
| 77 | + .task(id: tapPoint) { |
| 78 | + // Identifies the feature at the tapped point and creates a feature form from it. |
| 79 | + guard let tapPoint else { return } |
| 80 | + |
| 81 | + do { |
| 82 | + let identifyLayerResult = try await mapViewProxy.identify( |
| 83 | + on: featureLayer, |
| 84 | + screenPoint: tapPoint, |
| 85 | + tolerance: 10 |
| 86 | + ) |
| 87 | + if let feature = identifyLayerResult.geoElements.first as? ArcGISFeature { |
| 88 | + featureLayer.selectFeature(feature) |
| 89 | + featureForm = FeatureForm(feature: feature) |
| 90 | + isShowingFeatureForm = true |
| 91 | + } |
| 92 | + } catch { |
| 93 | + self.error = error |
| 94 | + } |
| 95 | + } |
| 96 | + .overlay(alignment: .top) { |
| 97 | + Text(instructionText) |
| 98 | + .multilineTextAlignment(.center) |
| 99 | + .frame(maxWidth: .infinity, alignment: .center) |
| 100 | + .padding(8) |
| 101 | + .background(.regularMaterial, ignoresSafeAreaEdges: .horizontal) |
| 102 | + } |
| 103 | + .floatingPanel(isPresented: $isShowingFeatureForm) { [featureForm] in |
| 104 | + if let featureForm { |
| 105 | + VStack { |
| 106 | + featureFormToolbar |
| 107 | + |
| 108 | + // Displays the feature form using the toolkit component. |
| 109 | + FeatureFormView(featureForm: featureForm) |
| 110 | + .padding(.horizontal) |
| 111 | + .task { |
| 112 | + for await validationErrors in featureForm.$validationErrors { |
| 113 | + hasValidationErrors = !validationErrors.isEmpty |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + .alert("Discard Edits", isPresented: $isShowingDiscardEditsAlert) { |
| 120 | + Button("Cancel", role: .cancel) {} |
| 121 | + Button("Discard", role: .destructive) { |
| 122 | + featureForm?.discardEdits() |
| 123 | + endEditing() |
| 124 | + } |
| 125 | + } message: { |
| 126 | + Text("Any changes made within the form will be lost.") |
| 127 | + } |
| 128 | + .errorAlert(presentingError: $error) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /// The toolbar for the feature form panel. |
| 133 | + private var featureFormToolbar: some View { |
| 134 | + HStack { |
| 135 | + Button("Discard Edits", systemImage: "x.circle", role: .destructive) { |
| 136 | + isShowingDiscardEditsAlert = true |
| 137 | + } |
| 138 | + |
| 139 | + Spacer() |
| 140 | + |
| 141 | + Text("Edit Feature") |
| 142 | + |
| 143 | + Spacer() |
| 144 | + |
| 145 | + Button("Done", systemImage: "checkmark") { |
| 146 | + isApplyingEdits = true |
| 147 | + } |
| 148 | + .disabled(hasValidationErrors) |
| 149 | + .task(id: isApplyingEdits) { |
| 150 | + guard isApplyingEdits else { return } |
| 151 | + defer { isApplyingEdits = false } |
| 152 | + |
| 153 | + do { |
| 154 | + try await applyEdits() |
| 155 | + endEditing() |
| 156 | + } catch { |
| 157 | + self.error = error |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + .fontWeight(.medium) |
| 162 | + .labelStyle(.iconOnly) |
| 163 | + .padding() |
| 164 | + } |
| 165 | + |
| 166 | + /// Closes the feature form panel and resets the feature selection. |
| 167 | + private func endEditing() { |
| 168 | + isShowingFeatureForm = false |
| 169 | + featureLayer.clearSelection() |
| 170 | + } |
| 171 | + |
| 172 | + /// Applies the edits made in the feature form to the feature service. |
| 173 | + private func applyEdits() async throws { |
| 174 | + guard let featureForm else { return } |
| 175 | + |
| 176 | + // Saves the feature form edits to the database. |
| 177 | + try await featureForm.finishEditing() |
| 178 | + |
| 179 | + guard let serviceFeatureTable = featureForm.feature.table as? ServiceFeatureTable else { |
| 180 | + return |
| 181 | + } |
| 182 | + |
| 183 | + // Applies the edits to the feature service using either the database or feature table. |
| 184 | + if let serviceGeodatabase = serviceFeatureTable.serviceGeodatabase, |
| 185 | + let serviceInfo = serviceGeodatabase.serviceInfo, |
| 186 | + serviceInfo.canUseServiceGeodatabaseApplyEdits { |
| 187 | + _ = try await serviceGeodatabase.applyEdits() |
| 188 | + } else { |
| 189 | + _ = try await serviceFeatureTable.applyEdits() |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +private extension PortalItem.ID { |
| 195 | + /// The ID to the "Feature Form Places" web map portal item on ArcGIS Online. |
| 196 | + static var featureFormPlacesWebMap: Self { .init("516e4d6aeb4c495c87c41e11274c767f")! } |
| 197 | +} |
| 198 | + |
| 199 | +#Preview { |
| 200 | + EditFeaturesUsingFeatureFormsView() |
| 201 | +} |
0 commit comments