Skip to content

Forms : Added discardEdits API and UI #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
Expand All @@ -22,7 +24,10 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
Expand Down Expand Up @@ -51,6 +56,7 @@ fun MapScreen(mapViewModel: MapViewModel = hiltViewModel(), onBackPressed: () ->
val uiState by mapViewModel.uiState
val context = LocalContext.current
val windowSize = getWindowSize(context)
var showDiscardEditsDialog by remember { mutableStateOf(false) }

Scaffold(
modifier = Modifier.fillMaxSize(),
Expand All @@ -62,7 +68,7 @@ fun MapScreen(mapViewModel: MapViewModel = hiltViewModel(), onBackPressed: () ->
title = mapViewModel.portalItem.title,
editingMode = uiState is UIState.Editing,
onClose = {
scope.launch { mapViewModel.rollbackEdits() }
showDiscardEditsDialog = true
},
onSave = {
scope.launch {
Expand Down Expand Up @@ -130,6 +136,40 @@ fun MapScreen(mapViewModel: MapViewModel = hiltViewModel(), onBackPressed: () ->
}
}
}
if (showDiscardEditsDialog) {
DiscardEditsDialog(
onConfirm = {
mapViewModel.rollbackEdits()
showDiscardEditsDialog = false
},
onCancel = {
showDiscardEditsDialog = false
}
)
}
}

@Composable
fun DiscardEditsDialog(onConfirm: () -> Unit, onCancel: () -> Unit) {
AlertDialog(
onDismissRequest = onCancel,
confirmButton = {
Button(onClick = onConfirm) {
Text(text = stringResource(R.string.discard))
}
},
dismissButton = {
Button(onClick = onCancel) {
Text(text = stringResource(R.string.cancel))
}
},
title = {
Text(text = stringResource(R.string.discard_edits))
},
text = {
Text(text = stringResource(R.string.all_changes_will_be_lost))
}
)
}

@OptIn(ExperimentalMaterial3Api::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,58 +93,59 @@ class MapViewModel @Inject constructor(
}
}

suspend fun rollbackEdits(): Result<Unit> {
fun rollbackEdits(): Result<Unit> {
(_uiState.value as? UIState.Editing)?.let {
val feature = it.featureForm.feature
(feature.featureTable as? ServiceFeatureTable)?.undoLocalEdits()
feature.refresh()
it.featureForm.discardEdits()
_uiState.value = UIState.NotEditing
return Result.success(Unit)
} ?: return Result.failure(IllegalStateException("Not in editing state"))
}

context(MapView, CoroutineScope) override fun onSingleTapConfirmed(singleTapEvent: SingleTapConfirmedEvent) {
launch {
[email protected](
screenCoordinate = singleTapEvent.screenCoordinate,
tolerance = 22.0,
returnPopupsOnly = false
).onSuccess { results ->
results.firstNotNullOfOrNull { result ->
try {
result.geoElements.filterIsInstance<ArcGISFeature>()
.firstOrNull { feature ->
(feature.featureTable?.layer as? FeatureLayer)?.featureFormDefinition != null
}
} catch (e: Exception) {
e.printStackTrace()
Toast.makeText(
context,
"failed to load the FeatureFormDefinition for the feature",
Toast.LENGTH_LONG
).show()
null
}
}?.let { feature ->
feature.load().onSuccess {
// do not process any taps on a different feature when a feature is being edited
if (_uiState.value is UIState.NotEditing) {
launch {
[email protected](
screenCoordinate = singleTapEvent.screenCoordinate,
tolerance = 22.0,
returnPopupsOnly = false
).onSuccess { results ->
results.firstNotNullOfOrNull { result ->
try {
val featureForm = FeatureForm(
feature,
(feature.featureTable?.layer as FeatureLayer).featureFormDefinition!!
)
// set the UI to an editing state and set the FeatureForm
_uiState.value = UIState.Editing(featureForm)
result.geoElements.filterIsInstance<ArcGISFeature>()
.firstOrNull { feature ->
(feature.featureTable?.layer as? FeatureLayer)?.featureFormDefinition != null
}
} catch (e: Exception) {
e.printStackTrace() // for debugging core issues
e.printStackTrace()
Toast.makeText(
context,
"failed to create a FeatureForm for the feature and layer",
"failed to load the FeatureFormDefinition for the feature",
Toast.LENGTH_LONG
).show()
null
}
}.onFailure { println("failed to load tapped Feature") }
} ?: println("identified features do not have feature forms defined")
}.onFailure { println("tap was not on a feature") }
}?.let { feature ->
feature.load().onSuccess {
try {
val featureForm = FeatureForm(
feature,
(feature.featureTable?.layer as FeatureLayer).featureFormDefinition!!
)
// set the UI to an editing state and set the FeatureForm
_uiState.value = UIState.Editing(featureForm)
} catch (e: Exception) {
e.printStackTrace() // for debugging core issues
Toast.makeText(
context,
"failed to create a FeatureForm for the feature and layer",
Toast.LENGTH_LONG
).show()
}
}.onFailure { println("failed to load tapped Feature") }
} ?: println("identified features do not have feature forms defined")
}.onFailure { println("tap was not on a feature") }
}
}
}
}
3 changes: 3 additions & 0 deletions microapps/FeatureFormsApp/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@
<string name="sign_in_with_default_credentials">Sign in using built-in Credentials</string>
<string name="sign_in_with_enterprise">Sign in with ArcGIS Enterprise</string>
<string name="skin_sign_in">Skip sign in</string>
<string name="discard">Discard</string>
<string name="discard_edits">Discard Edits?</string>
<string name="all_changes_will_be_lost">All changes made within the form will be lost.</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
Expand Down