Skip to content

Commit

Permalink
Add deletion confirmation dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetr4 committed Sep 5, 2024
1 parent 9a204eb commit e28ff5e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cloud.mike.divelog.ui.detail.topbar

import android.content.res.Configuration
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import cloud.mike.divelog.R
import cloud.mike.divelog.ui.DiveTheme

@Composable
fun DeletionDialog(
onDismiss: () -> Unit,
onConfirm: () -> Unit,
) {
AlertDialog(
text = { Text(stringResource(R.string.dive_detail_dialog_deletion_title)) },
onDismissRequest = { onDismiss() },
dismissButton = {
TextButton(onClick = { onDismiss() }) {
Text(stringResource(R.string.common_button_cancel))
}
},
confirmButton = {
TextButton(onClick = { onConfirm() }) {
Text(stringResource(R.string.common_button_delete))
}
},
)
}

@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
@Preview(uiMode = Configuration.UI_MODE_NIGHT_NO)
@Composable
private fun Preview() {
DiveTheme {
DeletionDialog(
onDismiss = {},
onConfirm = {},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -40,6 +44,8 @@ fun DetailAppBar(
onDismissDeleteError: () -> Unit,
modifier: Modifier = Modifier,
) {
var showDeletionDialog by remember { mutableStateOf(false) }

LaunchedEffect(deleteState) {
when (deleteState) {
is DeleteState.Success -> onNavigateUp() // leave screen after dive was deleted
Expand Down Expand Up @@ -67,11 +73,21 @@ fun DetailAppBar(
EditButton(onClick = { onShowEdit(dive) })
DeleteButton(
loading = deleteState is DeleteState.Loading,
onClick = onDeleteDive,
onClick = { showDeletionDialog = true },
)
}
},
)

if (showDeletionDialog) {
DeletionDialog(
onDismiss = { showDeletionDialog = false },
onConfirm = {
showDeletionDialog = false
onDeleteDive()
},
)
}
}

@Composable
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<string name="common_button_cancel">Abbrechen</string>
<string name="common_button_clear">Löschen</string>
<string name="common_button_edit">Bearbeiten</string>
<string name="common_button_delete">Löschen</string>
<string name="common_button_navigate_up">Zurück</string>
<string name="common_button_ok">OK</string>
<string name="common_button_retry">Erneut versuchen</string>
Expand Down Expand Up @@ -52,6 +53,7 @@

<!-- Detail -->
<string name="dive_detail_button_delete_dive">Tauchgang löschen</string>
<string name="dive_detail_dialog_deletion_title">Tauchgang löschen?</string>
<string name="dive_detail_error_empty">Tauchgang nicht gefunden.</string>
<string name="dive_detail_label_location">Ort</string>
<string name="dive_detail_label_max_depth">Maximale Tiefe</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<string name="common_button_cancel">Cancel</string>
<string name="common_button_clear">Clear</string>
<string name="common_button_edit">Edit</string>
<string name="common_button_delete">Delete</string>
<string name="common_button_navigate_up">Go back</string>
<string name="common_button_ok">OK</string>
<string name="common_button_retry">Try again</string>
Expand Down Expand Up @@ -52,6 +53,7 @@

<!-- Detail -->
<string name="dive_detail_button_delete_dive">Delete dive</string>
<string name="dive_detail_dialog_deletion_title">Delete dive?</string>
<string name="dive_detail_error_empty">Dive not found.</string>
<string name="dive_detail_label_location">Location</string>
<string name="dive_detail_label_max_depth">Max depth</string>
Expand Down

0 comments on commit e28ff5e

Please sign in to comment.