|
| 1 | +package com.allubie.nana.ui.components |
| 2 | + |
| 3 | +import androidx.compose.foundation.clickable |
| 4 | +import androidx.compose.foundation.layout.* |
| 5 | +import androidx.compose.foundation.rememberScrollState |
| 6 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 7 | +import androidx.compose.foundation.verticalScroll |
| 8 | +import androidx.compose.material3.* |
| 9 | +import androidx.compose.runtime.Composable |
| 10 | +import androidx.compose.ui.Alignment |
| 11 | +import androidx.compose.ui.Modifier |
| 12 | +import androidx.compose.ui.graphics.vector.ImageVector |
| 13 | +import androidx.compose.ui.text.font.FontWeight |
| 14 | +import androidx.compose.ui.unit.dp |
| 15 | + |
| 16 | +/** |
| 17 | + * Material 3 confirmation dialog for destructive and non-destructive actions. |
| 18 | + * |
| 19 | + * Follows M3 guidelines: |
| 20 | + * - Optional hero icon above title |
| 21 | + * - Dismiss button on the left, confirm on the right |
| 22 | + * - Destructive confirm button uses error color |
| 23 | + * - Shape: RoundedCornerShape(28.dp) (M3 default) |
| 24 | + */ |
| 25 | +@Composable |
| 26 | +fun NanaConfirmationDialog( |
| 27 | + onDismiss: () -> Unit, |
| 28 | + onConfirm: () -> Unit, |
| 29 | + title: String, |
| 30 | + message: String, |
| 31 | + confirmText: String = "Confirm", |
| 32 | + dismissText: String = "Cancel", |
| 33 | + isDestructive: Boolean = false, |
| 34 | + icon: ImageVector? = null |
| 35 | +) { |
| 36 | + AlertDialog( |
| 37 | + onDismissRequest = onDismiss, |
| 38 | + icon = icon?.let { |
| 39 | + { |
| 40 | + Icon( |
| 41 | + imageVector = it, |
| 42 | + contentDescription = null, |
| 43 | + tint = if (isDestructive) MaterialTheme.colorScheme.error |
| 44 | + else MaterialTheme.colorScheme.primary |
| 45 | + ) |
| 46 | + } |
| 47 | + }, |
| 48 | + title = { |
| 49 | + Text( |
| 50 | + text = title, |
| 51 | + fontWeight = FontWeight.Bold |
| 52 | + ) |
| 53 | + }, |
| 54 | + text = { Text(message) }, |
| 55 | + confirmButton = { |
| 56 | + TextButton( |
| 57 | + onClick = onConfirm, |
| 58 | + colors = if (isDestructive) ButtonDefaults.textButtonColors( |
| 59 | + contentColor = MaterialTheme.colorScheme.error |
| 60 | + ) else ButtonDefaults.textButtonColors() |
| 61 | + ) { |
| 62 | + Text(confirmText) |
| 63 | + } |
| 64 | + }, |
| 65 | + dismissButton = { |
| 66 | + TextButton(onClick = onDismiss) { |
| 67 | + Text(dismissText) |
| 68 | + } |
| 69 | + } |
| 70 | + ) |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Material 3 single-selection dialog with radio buttons. |
| 75 | + * |
| 76 | + * Follows M3 guidelines for simple dialogs with list items. |
| 77 | + */ |
| 78 | +@Composable |
| 79 | +fun <T> NanaSelectionDialog( |
| 80 | + onDismiss: () -> Unit, |
| 81 | + title: String, |
| 82 | + options: List<T>, |
| 83 | + selectedOption: T, |
| 84 | + optionLabel: (T) -> String, |
| 85 | + onSelect: (T) -> Unit, |
| 86 | + dismissText: String = "Cancel" |
| 87 | +) { |
| 88 | + AlertDialog( |
| 89 | + onDismissRequest = onDismiss, |
| 90 | + title = { |
| 91 | + Text( |
| 92 | + text = title, |
| 93 | + fontWeight = FontWeight.Bold |
| 94 | + ) |
| 95 | + }, |
| 96 | + text = { |
| 97 | + Column { |
| 98 | + options.forEach { option -> |
| 99 | + Row( |
| 100 | + modifier = Modifier |
| 101 | + .fillMaxWidth() |
| 102 | + .clickable { onSelect(option) }, |
| 103 | + verticalAlignment = Alignment.CenterVertically |
| 104 | + ) { |
| 105 | + RadioButton( |
| 106 | + selected = option == selectedOption, |
| 107 | + onClick = { onSelect(option) } |
| 108 | + ) |
| 109 | + Spacer(modifier = Modifier.width(8.dp)) |
| 110 | + Text(text = optionLabel(option)) |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + }, |
| 115 | + confirmButton = { |
| 116 | + TextButton(onClick = onDismiss) { |
| 117 | + Text(dismissText) |
| 118 | + } |
| 119 | + } |
| 120 | + ) |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Material 3 searchable list dialog with radio-button items. |
| 125 | + * |
| 126 | + * Includes an OutlinedTextField for filtering and a scrollable list below. |
| 127 | + */ |
| 128 | +@Composable |
| 129 | +fun <T> NanaSearchableListDialog( |
| 130 | + onDismiss: () -> Unit, |
| 131 | + title: String, |
| 132 | + searchQuery: String, |
| 133 | + onSearchQueryChange: (String) -> Unit, |
| 134 | + searchPlaceholder: String = "Search...", |
| 135 | + items: List<T>, |
| 136 | + isSelected: (T) -> Boolean, |
| 137 | + itemLabel: (T) -> String, |
| 138 | + onSelect: (T) -> Unit, |
| 139 | + dismissText: String = "Cancel" |
| 140 | +) { |
| 141 | + AlertDialog( |
| 142 | + onDismissRequest = onDismiss, |
| 143 | + title = { |
| 144 | + Text( |
| 145 | + text = title, |
| 146 | + fontWeight = FontWeight.Bold |
| 147 | + ) |
| 148 | + }, |
| 149 | + text = { |
| 150 | + Column(modifier = Modifier.fillMaxWidth()) { |
| 151 | + OutlinedTextField( |
| 152 | + value = searchQuery, |
| 153 | + onValueChange = onSearchQueryChange, |
| 154 | + placeholder = { Text(searchPlaceholder) }, |
| 155 | + singleLine = true, |
| 156 | + modifier = Modifier.fillMaxWidth(), |
| 157 | + shape = RoundedCornerShape(12.dp) |
| 158 | + ) |
| 159 | + Spacer(modifier = Modifier.height(8.dp)) |
| 160 | + Column( |
| 161 | + modifier = Modifier |
| 162 | + .fillMaxWidth() |
| 163 | + .heightIn(max = 350.dp) |
| 164 | + .verticalScroll(rememberScrollState()) |
| 165 | + ) { |
| 166 | + items.forEach { item -> |
| 167 | + Row( |
| 168 | + modifier = Modifier |
| 169 | + .fillMaxWidth() |
| 170 | + .clickable { onSelect(item) } |
| 171 | + .padding(vertical = 4.dp), |
| 172 | + verticalAlignment = Alignment.CenterVertically |
| 173 | + ) { |
| 174 | + RadioButton( |
| 175 | + selected = isSelected(item), |
| 176 | + onClick = { onSelect(item) } |
| 177 | + ) |
| 178 | + Spacer(modifier = Modifier.width(8.dp)) |
| 179 | + Text(text = itemLabel(item)) |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + }, |
| 185 | + confirmButton = { |
| 186 | + TextButton(onClick = onDismiss) { |
| 187 | + Text(dismissText) |
| 188 | + } |
| 189 | + } |
| 190 | + ) |
| 191 | +} |
0 commit comments