From e049c27bdfe04d967a396bcc9f2e16a7bb786a44 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Fri, 27 Nov 2026 10:18:42 -0500 Subject: [PATCH] refactor(android): every ask-before-destroying dialog is ConfirmDestructiveDialog (DRY A6) Remove the file / Remove from library, Delete user, Clear cache and Sign out each drew their own AlertDialog, coloured four ways: a plain text button, error-red text, an error-red button and an error-container button. They now use the shared ConfirmDestructiveDialog, whose confirm is the destructive action colour. It gains an optional confirmWord, a word to type before confirming, which Delete user keeps. Co-Authored-By: Claude Opus 5.5 --- .../admin/ui/AdminQuarantineScreen.kt | 20 ++++----- .../minstrel/admin/ui/AdminUsersScreen.kt | 42 ++++--------------- .../minstrel/settings/ui/SettingsScreen.kt | 34 ++++----------- .../minstrel/settings/ui/StorageCard.kt | 34 ++++----------- .../minstrel/shared/widgets/Dialogs.kt | 26 +++++++++++- 5 files changed, 57 insertions(+), 99 deletions(-) diff --git a/android/app/src/main/java/com/fabledsword/minstrel/admin/ui/AdminQuarantineScreen.kt b/android/app/src/main/java/com/fabledsword/minstrel/admin/ui/AdminQuarantineScreen.kt index 5d7ef57e..ecd6b511 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/admin/ui/AdminQuarantineScreen.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/admin/ui/AdminQuarantineScreen.kt @@ -8,7 +8,6 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items -import androidx.compose.material3.AlertDialog import androidx.compose.material3.AssistChip import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.HorizontalDivider @@ -34,6 +33,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.navigation.NavHostController import com.fabledsword.minstrel.models.AdminQuarantineItemRef import com.fabledsword.minstrel.nav.AdminQuarantine +import com.fabledsword.minstrel.shared.widgets.ConfirmDestructiveDialog import com.fabledsword.minstrel.shared.widgets.ShellContentWindowInsets import com.fabledsword.minstrel.shared.widgets.EmptyState import com.fabledsword.minstrel.shared.widgets.ErrorRetry @@ -159,18 +159,12 @@ private fun RemovalConfirm( "Remove from library", ) } - AlertDialog( - onDismissRequest = onDismiss, - title = { Text(heading) }, - text = { - Text( - text = body, - style = MaterialTheme.typography.bodyMedium, - color = MaterialTheme.colorScheme.onSurfaceVariant, - ) - }, - confirmButton = { TextButton(onClick = onConfirm) { Text(action) } }, - dismissButton = { TextButton(onClick = onDismiss) { Text("Cancel") } }, + ConfirmDestructiveDialog( + title = heading, + text = body, + confirmLabel = action, + onConfirm = onConfirm, + onDismiss = onDismiss, ) } diff --git a/android/app/src/main/java/com/fabledsword/minstrel/admin/ui/AdminUsersScreen.kt b/android/app/src/main/java/com/fabledsword/minstrel/admin/ui/AdminUsersScreen.kt index d85f7572..6001ac73 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/admin/ui/AdminUsersScreen.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/admin/ui/AdminUsersScreen.kt @@ -49,6 +49,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.navigation.NavHostController import com.fabledsword.minstrel.models.AdminUserRef import com.fabledsword.minstrel.nav.AdminUsers +import com.fabledsword.minstrel.shared.widgets.ConfirmDestructiveDialog import com.fabledsword.minstrel.shared.widgets.ShellContentWindowInsets import com.fabledsword.minstrel.shared.widgets.MinstrelTopAppBar import com.fabledsword.minstrel.shared.widgets.PullToRefreshScaffold @@ -490,39 +491,14 @@ private fun DeleteConfirmDialog( onDismiss: () -> Unit, onConfirm: () -> Unit, ) { - var typed by remember { mutableStateOf("") } - val confirmed = typed.trim().equals(DELETE_CONFIRM_WORD, ignoreCase = true) - AlertDialog( - onDismissRequest = onDismiss, - title = { Text("Delete user?") }, - text = { - Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { - Text( - "Delete @${user.username}? This removes the account and all " + - "their data. Type $DELETE_CONFIRM_WORD to confirm.", - ) - OutlinedTextField( - value = typed, - onValueChange = { typed = it }, - label = { Text("Type $DELETE_CONFIRM_WORD") }, - singleLine = true, - modifier = Modifier.fillMaxWidth(), - ) - } - }, - confirmButton = { - TextButton(onClick = onConfirm, enabled = confirmed) { - Text( - "Delete", - color = if (confirmed) { - MaterialTheme.colorScheme.error - } else { - MaterialTheme.colorScheme.onSurfaceVariant - }, - ) - } - }, - dismissButton = { TextButton(onClick = onDismiss) { Text("Cancel") } }, + ConfirmDestructiveDialog( + title = "Delete user?", + text = "Delete @${user.username}? This removes the account and all " + + "their data. Type $DELETE_CONFIRM_WORD to confirm.", + confirmLabel = "Delete", + onConfirm = onConfirm, + onDismiss = onDismiss, + confirmWord = DELETE_CONFIRM_WORD, ) } diff --git a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/SettingsScreen.kt b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/SettingsScreen.kt index 2fe65f65..368dd5d3 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/SettingsScreen.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/SettingsScreen.kt @@ -9,13 +9,8 @@ import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll -import androidx.compose.material3.AlertDialog -import androidx.compose.material3.Button -import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold -import androidx.compose.material3.Text -import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue @@ -34,6 +29,7 @@ import com.fabledsword.minstrel.nav.Admin import com.fabledsword.minstrel.nav.NotificationSettings import com.fabledsword.minstrel.nav.ServerUrl import com.fabledsword.minstrel.nav.Settings as SettingsRoute +import com.fabledsword.minstrel.shared.widgets.ConfirmDestructiveDialog import com.fabledsword.minstrel.shared.widgets.MinstrelTopAppBar import com.fabledsword.minstrel.shared.widgets.ShellContentWindowInsets import com.fabledsword.minstrel.theme.ChoiceRow @@ -142,27 +138,13 @@ private fun SettingsList( @Composable private fun SignOutConfirmDialog(onConfirm: () -> Unit, onDismiss: () -> Unit) { - AlertDialog( - onDismissRequest = onDismiss, - title = { Text("Sign out?") }, - text = { - Text( - "Sign out of this server? Your downloaded music and " + - "settings on this device will be removed.", - ) - }, - confirmButton = { - Button( - onClick = onConfirm, - colors = ButtonDefaults.buttonColors( - containerColor = MaterialTheme.colorScheme.errorContainer, - contentColor = MaterialTheme.colorScheme.onErrorContainer, - ), - ) { Text("Sign out") } - }, - dismissButton = { - TextButton(onClick = onDismiss) { Text("Cancel") } - }, + ConfirmDestructiveDialog( + title = "Sign out?", + text = "Sign out of this server? Your downloaded music and " + + "settings on this device will be removed.", + confirmLabel = "Sign out", + onConfirm = onConfirm, + onDismiss = onDismiss, ) } diff --git a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/StorageCard.kt b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/StorageCard.kt index 88d638c9..1c74f378 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/StorageCard.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/StorageCard.kt @@ -1,11 +1,6 @@ package com.fabledsword.minstrel.settings.ui -import androidx.compose.material3.AlertDialog -import androidx.compose.material3.Button -import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.Text -import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -13,6 +8,7 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle +import com.fabledsword.minstrel.shared.widgets.ConfirmDestructiveDialog import com.fabledsword.minstrel.theme.ChoiceRow import com.fabledsword.minstrel.theme.RowSpinner import com.fabledsword.minstrel.theme.Section @@ -95,27 +91,13 @@ private fun CapRow(title: String, used: Long, value: Long, onSet: (Long) -> Unit @Composable private fun ClearCacheConfirmDialog(onConfirm: () -> Unit, onDismiss: () -> Unit) { - AlertDialog( - onDismissRequest = onDismiss, - title = { Text("Clear cache?") }, - text = { - Text( - "This deletes all cached audio. The next play of any track " + - "re-downloads from the server.", - ) - }, - confirmButton = { - Button( - onClick = onConfirm, - colors = ButtonDefaults.buttonColors( - containerColor = MaterialTheme.colorScheme.error, - contentColor = MaterialTheme.colorScheme.onError, - ), - ) { Text("Clear") } - }, - dismissButton = { - TextButton(onClick = onDismiss) { Text("Cancel") } - }, + ConfirmDestructiveDialog( + title = "Clear cache?", + text = "This deletes all cached audio. The next play of any track " + + "re-downloads from the server.", + confirmLabel = "Clear", + onConfirm = onConfirm, + onDismiss = onDismiss, ) } diff --git a/android/app/src/main/java/com/fabledsword/minstrel/shared/widgets/Dialogs.kt b/android/app/src/main/java/com/fabledsword/minstrel/shared/widgets/Dialogs.kt index 9381cb05..5427c37a 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/shared/widgets/Dialogs.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/shared/widgets/Dialogs.kt @@ -1,5 +1,7 @@ package com.fabledsword.minstrel.shared.widgets +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardOptions @@ -17,6 +19,7 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.KeyboardCapitalization +import androidx.compose.ui.unit.dp import com.fabledsword.minstrel.theme.LocalActionColors /** @@ -64,6 +67,10 @@ fun NameDialog( * Asks before an action that can't be undone. The confirm button is the * destructive action colour (oxblood), not the error colour: an error is a * failure that already happened, and this is a choice about to be made. + * + * With [confirmWord], the confirm button stays off until that word is typed + * (case aside), for the few actions that take a lot with them, such as + * deleting an account. */ @Composable fun ConfirmDestructiveDialog( @@ -72,15 +79,32 @@ fun ConfirmDestructiveDialog( confirmLabel: String, onConfirm: () -> Unit, onDismiss: () -> Unit, + confirmWord: String? = null, ) { val actions = LocalActionColors.current + var typed by rememberSaveable { mutableStateOf("") } + val armed = confirmWord == null || typed.trim().equals(confirmWord, ignoreCase = true) AlertDialog( onDismissRequest = onDismiss, title = { Text(title) }, - text = { Text(text) }, + text = { + Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { + Text(text) + if (confirmWord != null) { + OutlinedTextField( + value = typed, + onValueChange = { typed = it }, + label = { Text("Type $confirmWord") }, + singleLine = true, + modifier = Modifier.fillMaxWidth(), + ) + } + } + }, confirmButton = { Button( onClick = onConfirm, + enabled = armed, colors = ButtonDefaults.buttonColors( containerColor = actions.destructive, contentColor = actions.onAction,