Skip to content

Commit eb66812

Browse files
authored
Forms: Select Feature (#298)
1 parent 8bd59c5 commit eb66812

File tree

1 file changed

+32
-35
lines changed
  • microapps/FeatureFormsApp/app/src/main/java/com/arcgismaps/toolkit/featureformsapp/screens/map

1 file changed

+32
-35
lines changed

microapps/FeatureFormsApp/app/src/main/java/com/arcgismaps/toolkit/featureformsapp/screens/map/MapViewModel.kt

+32-35
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ class MapViewModel @Inject constructor(
100100
entry.value.forEach { error ->
101101
featureForm.getFormElement(entry.key)?.let { formElement ->
102102
if (formElement.isEditable.value) {
103-
errors.add(ErrorInfo(formElement.label, error as FeatureFormValidationException))
103+
errors.add(
104+
ErrorInfo(
105+
formElement.label,
106+
error as FeatureFormValidationException
107+
)
108+
)
104109
}
105110
}
106111
}
@@ -121,7 +126,8 @@ class MapViewModel @Inject constructor(
121126
serviceFeatureTable.serviceGeodatabase?.applyEdits()
122127
?: throw IllegalStateException("cannot apply feature edit without a ServiceGeodatabase")
123128
feature.refresh()
124-
Unit
129+
// unselect the feature after the edits have been saved
130+
(feature.featureTable?.layer as FeatureLayer).clearSelection()
125131
}
126132
// set the state to not editing since the feature was updated successfully
127133
_uiState.value = UIState.NotEditing
@@ -153,6 +159,8 @@ class MapViewModel @Inject constructor(
153159
fun rollbackEdits(): Result<Unit> {
154160
(_uiState.value as? UIState.Editing)?.let {
155161
it.featureForm.discardEdits()
162+
// unselect the feature
163+
(it.featureForm.feature.featureTable?.layer as FeatureLayer).clearSelection()
156164
_uiState.value = UIState.NotEditing
157165
return Result.success(Unit)
158166
} ?: return Result.failure(IllegalStateException("Not in editing state"))
@@ -167,41 +175,30 @@ class MapViewModel @Inject constructor(
167175
tolerance = 22.0,
168176
returnPopupsOnly = false
169177
).onSuccess { results ->
170-
results.firstNotNullOfOrNull { result ->
171-
try {
172-
result.geoElements.filterIsInstance<ArcGISFeature>()
173-
.firstOrNull { feature ->
174-
(feature.featureTable?.layer as? FeatureLayer)?.featureFormDefinition != null
175-
}
176-
} catch (e: Exception) {
177-
e.printStackTrace()
178-
Toast.makeText(
179-
context,
180-
"failed to load the FeatureFormDefinition for the feature",
181-
Toast.LENGTH_LONG
182-
).show()
183-
null
184-
}
185-
}?.let { feature ->
186-
feature.load().onSuccess {
187-
try {
188-
val featureForm = FeatureForm(
189-
feature,
190-
(feature.featureTable?.layer as FeatureLayer).featureFormDefinition!!
191-
)
192-
// set the UI to an editing state and set the FeatureForm
178+
try {
179+
results.forEach { result ->
180+
result.geoElements.firstOrNull {
181+
it is ArcGISFeature && (it.featureTable?.layer as? FeatureLayer)?.featureFormDefinition != null
182+
}?.let {
183+
val feature = it as ArcGISFeature
184+
val layer = feature.featureTable!!.layer as FeatureLayer
185+
val featureForm =
186+
FeatureForm(feature, layer.featureFormDefinition!!)
187+
// select the feature
188+
layer.selectFeature(feature)
189+
// set the UI to an editing state with the FeatureForm
193190
_uiState.value = UIState.Editing(featureForm)
194-
} catch (e: Exception) {
195-
e.printStackTrace() // for debugging core issues
196-
Toast.makeText(
197-
context,
198-
"failed to create a FeatureForm for the feature and layer",
199-
Toast.LENGTH_LONG
200-
).show()
201191
}
202-
}.onFailure { println("failed to load tapped Feature") }
203-
} ?: println("identified features do not have feature forms defined")
204-
}.onFailure { println("tap was not on a feature") }
192+
}
193+
} catch (e: Exception) {
194+
e.printStackTrace()
195+
Toast.makeText(
196+
context,
197+
"failed to create a FeatureForm for the feature",
198+
Toast.LENGTH_LONG
199+
).show()
200+
}
201+
}
205202
}
206203
}
207204
}

0 commit comments

Comments
 (0)