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 <noreply@anthropic.com>
This commit is contained in:
+6
-12
@@ -8,7 +8,6 @@ import androidx.compose.foundation.layout.fillMaxWidth
|
|||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
import androidx.compose.foundation.lazy.LazyColumn
|
||||||
import androidx.compose.foundation.lazy.items
|
import androidx.compose.foundation.lazy.items
|
||||||
import androidx.compose.material3.AlertDialog
|
|
||||||
import androidx.compose.material3.AssistChip
|
import androidx.compose.material3.AssistChip
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
import androidx.compose.material3.HorizontalDivider
|
import androidx.compose.material3.HorizontalDivider
|
||||||
@@ -34,6 +33,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
|||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import com.fabledsword.minstrel.models.AdminQuarantineItemRef
|
import com.fabledsword.minstrel.models.AdminQuarantineItemRef
|
||||||
import com.fabledsword.minstrel.nav.AdminQuarantine
|
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.ShellContentWindowInsets
|
||||||
import com.fabledsword.minstrel.shared.widgets.EmptyState
|
import com.fabledsword.minstrel.shared.widgets.EmptyState
|
||||||
import com.fabledsword.minstrel.shared.widgets.ErrorRetry
|
import com.fabledsword.minstrel.shared.widgets.ErrorRetry
|
||||||
@@ -159,18 +159,12 @@ private fun RemovalConfirm(
|
|||||||
"Remove from library",
|
"Remove from library",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
AlertDialog(
|
ConfirmDestructiveDialog(
|
||||||
onDismissRequest = onDismiss,
|
title = heading,
|
||||||
title = { Text(heading) },
|
|
||||||
text = {
|
|
||||||
Text(
|
|
||||||
text = body,
|
text = body,
|
||||||
style = MaterialTheme.typography.bodyMedium,
|
confirmLabel = action,
|
||||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
onConfirm = onConfirm,
|
||||||
)
|
onDismiss = onDismiss,
|
||||||
},
|
|
||||||
confirmButton = { TextButton(onClick = onConfirm) { Text(action) } },
|
|
||||||
dismissButton = { TextButton(onClick = onDismiss) { Text("Cancel") } },
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
|||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import com.fabledsword.minstrel.models.AdminUserRef
|
import com.fabledsword.minstrel.models.AdminUserRef
|
||||||
import com.fabledsword.minstrel.nav.AdminUsers
|
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.ShellContentWindowInsets
|
||||||
import com.fabledsword.minstrel.shared.widgets.MinstrelTopAppBar
|
import com.fabledsword.minstrel.shared.widgets.MinstrelTopAppBar
|
||||||
import com.fabledsword.minstrel.shared.widgets.PullToRefreshScaffold
|
import com.fabledsword.minstrel.shared.widgets.PullToRefreshScaffold
|
||||||
@@ -490,39 +491,14 @@ private fun DeleteConfirmDialog(
|
|||||||
onDismiss: () -> Unit,
|
onDismiss: () -> Unit,
|
||||||
onConfirm: () -> Unit,
|
onConfirm: () -> Unit,
|
||||||
) {
|
) {
|
||||||
var typed by remember { mutableStateOf("") }
|
ConfirmDestructiveDialog(
|
||||||
val confirmed = typed.trim().equals(DELETE_CONFIRM_WORD, ignoreCase = true)
|
title = "Delete user?",
|
||||||
AlertDialog(
|
text = "Delete @${user.username}? This removes the account and all " +
|
||||||
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.",
|
"their data. Type $DELETE_CONFIRM_WORD to confirm.",
|
||||||
)
|
confirmLabel = "Delete",
|
||||||
OutlinedTextField(
|
onConfirm = onConfirm,
|
||||||
value = typed,
|
onDismiss = onDismiss,
|
||||||
onValueChange = { typed = it },
|
confirmWord = DELETE_CONFIRM_WORD,
|
||||||
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") } },
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,13 +9,8 @@ import androidx.compose.foundation.layout.fillMaxSize
|
|||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.rememberScrollState
|
import androidx.compose.foundation.rememberScrollState
|
||||||
import androidx.compose.foundation.verticalScroll
|
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.MaterialTheme
|
||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.material3.TextButton
|
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.getValue
|
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.NotificationSettings
|
||||||
import com.fabledsword.minstrel.nav.ServerUrl
|
import com.fabledsword.minstrel.nav.ServerUrl
|
||||||
import com.fabledsword.minstrel.nav.Settings as SettingsRoute
|
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.MinstrelTopAppBar
|
||||||
import com.fabledsword.minstrel.shared.widgets.ShellContentWindowInsets
|
import com.fabledsword.minstrel.shared.widgets.ShellContentWindowInsets
|
||||||
import com.fabledsword.minstrel.theme.ChoiceRow
|
import com.fabledsword.minstrel.theme.ChoiceRow
|
||||||
@@ -142,27 +138,13 @@ private fun SettingsList(
|
|||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
private fun SignOutConfirmDialog(onConfirm: () -> Unit, onDismiss: () -> Unit) {
|
private fun SignOutConfirmDialog(onConfirm: () -> Unit, onDismiss: () -> Unit) {
|
||||||
AlertDialog(
|
ConfirmDestructiveDialog(
|
||||||
onDismissRequest = onDismiss,
|
title = "Sign out?",
|
||||||
title = { Text("Sign out?") },
|
text = "Sign out of this server? Your downloaded music and " +
|
||||||
text = {
|
|
||||||
Text(
|
|
||||||
"Sign out of this server? Your downloaded music and " +
|
|
||||||
"settings on this device will be removed.",
|
"settings on this device will be removed.",
|
||||||
)
|
confirmLabel = "Sign out",
|
||||||
},
|
onConfirm = onConfirm,
|
||||||
confirmButton = {
|
onDismiss = onDismiss,
|
||||||
Button(
|
|
||||||
onClick = onConfirm,
|
|
||||||
colors = ButtonDefaults.buttonColors(
|
|
||||||
containerColor = MaterialTheme.colorScheme.errorContainer,
|
|
||||||
contentColor = MaterialTheme.colorScheme.onErrorContainer,
|
|
||||||
),
|
|
||||||
) { Text("Sign out") }
|
|
||||||
},
|
|
||||||
dismissButton = {
|
|
||||||
TextButton(onClick = onDismiss) { Text("Cancel") }
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,6 @@
|
|||||||
package com.fabledsword.minstrel.settings.ui
|
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.MaterialTheme
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.material3.TextButton
|
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
@@ -13,6 +8,7 @@ import androidx.compose.runtime.remember
|
|||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
|
import com.fabledsword.minstrel.shared.widgets.ConfirmDestructiveDialog
|
||||||
import com.fabledsword.minstrel.theme.ChoiceRow
|
import com.fabledsword.minstrel.theme.ChoiceRow
|
||||||
import com.fabledsword.minstrel.theme.RowSpinner
|
import com.fabledsword.minstrel.theme.RowSpinner
|
||||||
import com.fabledsword.minstrel.theme.Section
|
import com.fabledsword.minstrel.theme.Section
|
||||||
@@ -95,27 +91,13 @@ private fun CapRow(title: String, used: Long, value: Long, onSet: (Long) -> Unit
|
|||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
private fun ClearCacheConfirmDialog(onConfirm: () -> Unit, onDismiss: () -> Unit) {
|
private fun ClearCacheConfirmDialog(onConfirm: () -> Unit, onDismiss: () -> Unit) {
|
||||||
AlertDialog(
|
ConfirmDestructiveDialog(
|
||||||
onDismissRequest = onDismiss,
|
title = "Clear cache?",
|
||||||
title = { Text("Clear cache?") },
|
text = "This deletes all cached audio. The next play of any track " +
|
||||||
text = {
|
|
||||||
Text(
|
|
||||||
"This deletes all cached audio. The next play of any track " +
|
|
||||||
"re-downloads from the server.",
|
"re-downloads from the server.",
|
||||||
)
|
confirmLabel = "Clear",
|
||||||
},
|
onConfirm = onConfirm,
|
||||||
confirmButton = {
|
onDismiss = onDismiss,
|
||||||
Button(
|
|
||||||
onClick = onConfirm,
|
|
||||||
colors = ButtonDefaults.buttonColors(
|
|
||||||
containerColor = MaterialTheme.colorScheme.error,
|
|
||||||
contentColor = MaterialTheme.colorScheme.onError,
|
|
||||||
),
|
|
||||||
) { Text("Clear") }
|
|
||||||
},
|
|
||||||
dismissButton = {
|
|
||||||
TextButton(onClick = onDismiss) { Text("Cancel") }
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.fabledsword.minstrel.shared.widgets
|
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.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.text.KeyboardActions
|
import androidx.compose.foundation.text.KeyboardActions
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
import androidx.compose.foundation.text.KeyboardOptions
|
||||||
@@ -17,6 +19,7 @@ import androidx.compose.runtime.setValue
|
|||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.text.input.ImeAction
|
import androidx.compose.ui.text.input.ImeAction
|
||||||
import androidx.compose.ui.text.input.KeyboardCapitalization
|
import androidx.compose.ui.text.input.KeyboardCapitalization
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
import com.fabledsword.minstrel.theme.LocalActionColors
|
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
|
* 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
|
* 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.
|
* 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
|
@Composable
|
||||||
fun ConfirmDestructiveDialog(
|
fun ConfirmDestructiveDialog(
|
||||||
@@ -72,15 +79,32 @@ fun ConfirmDestructiveDialog(
|
|||||||
confirmLabel: String,
|
confirmLabel: String,
|
||||||
onConfirm: () -> Unit,
|
onConfirm: () -> Unit,
|
||||||
onDismiss: () -> Unit,
|
onDismiss: () -> Unit,
|
||||||
|
confirmWord: String? = null,
|
||||||
) {
|
) {
|
||||||
val actions = LocalActionColors.current
|
val actions = LocalActionColors.current
|
||||||
|
var typed by rememberSaveable { mutableStateOf("") }
|
||||||
|
val armed = confirmWord == null || typed.trim().equals(confirmWord, ignoreCase = true)
|
||||||
AlertDialog(
|
AlertDialog(
|
||||||
onDismissRequest = onDismiss,
|
onDismissRequest = onDismiss,
|
||||||
title = { Text(title) },
|
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 = {
|
confirmButton = {
|
||||||
Button(
|
Button(
|
||||||
onClick = onConfirm,
|
onClick = onConfirm,
|
||||||
|
enabled = armed,
|
||||||
colors = ButtonDefaults.buttonColors(
|
colors = ButtonDefaults.buttonColors(
|
||||||
containerColor = actions.destructive,
|
containerColor = actions.destructive,
|
||||||
contentColor = actions.onAction,
|
contentColor = actions.onAction,
|
||||||
|
|||||||
Reference in New Issue
Block a user