package org.terst.doot.widget.ui import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.ExperimentalLayoutApi import androidx.compose.foundation.layout.FlowRow import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.width import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.material3.AlertDialog import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.FilterChip import androidx.compose.material3.MaterialTheme import androidx.compose.material3.OutlinedTextField 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 import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.dp import org.terst.doot.widget.data.TaskRecurrence internal fun weekdayAbbrev(day: Int): String = when (day) { 0 -> "Sun"; 1 -> "Mon"; 2 -> "Tue"; 3 -> "Wed"; 4 -> "Thu"; 5 -> "Fri"; 6 -> "Sat" else -> "?" } /** Single-letter label for the compact weekday chip row (S M T W T F S). */ private fun weekdayLetter(day: Int): String = when (day) { 0 -> "S"; 1 -> "M"; 2 -> "T"; 3 -> "W"; 4 -> "T"; 5 -> "F"; 6 -> "S" else -> "?" } /** Correctly pluralized unit for a frequency + interval, e.g. "day"/"days". */ private fun unitLabel(freq: String, interval: Int): String { val plural = interval != 1 return when (freq) { "daily" -> if (plural) "days" else "day" "weekly" -> if (plural) "weeks" else "week" "monthly" -> if (plural) "months" else "month" "yearly" -> if (plural) "years" else "year" else -> "" } } @Composable private fun SectionLabel(text: String) { Text( text = text.uppercase(), style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant ) } @OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class) @Composable fun RecurrenceEditDialog( initial: TaskRecurrence?, onDismiss: () -> Unit, onSave: (freq: String, interval: Int, weekdays: List) -> Unit, onClear: () -> Unit ) { var freq by remember { mutableStateOf(initial?.freq ?: "weekly") } var interval by remember { mutableStateOf((initial?.interval ?: 1).toString()) } var weekdays by remember { mutableStateOf(initial?.weekdays?.toSet() ?: emptySet()) } AlertDialog( onDismissRequest = onDismiss, title = { Text("Recurrence") }, text = { Column(verticalArrangement = Arrangement.spacedBy(20.dp)) { Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { SectionLabel("Repeats") FlowRow(horizontalArrangement = Arrangement.spacedBy(6.dp)) { listOf("daily", "weekly", "monthly", "yearly").forEach { option -> FilterChip( selected = freq == option, onClick = { freq = option }, label = { Text(option.replaceFirstChar { it.uppercase() }) } ) } } } Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { SectionLabel("Every") Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) { OutlinedTextField( value = interval, onValueChange = { if (it.all(Char::isDigit)) interval = it }, modifier = Modifier.width(72.dp), singleLine = true, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number) ) Text(unitLabel(freq, interval.toIntOrNull() ?: 1)) } } if (freq == "weekly") { Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { SectionLabel("On these days") FlowRow(horizontalArrangement = Arrangement.spacedBy(4.dp)) { (0..6).forEach { day -> FilterChip( selected = weekdays.contains(day), onClick = { weekdays = if (weekdays.contains(day)) weekdays - day else weekdays + day }, label = { Text(weekdayLetter(day)) } ) } } } } } }, confirmButton = { TextButton(onClick = { onSave(freq, interval.toIntOrNull()?.coerceAtLeast(1) ?: 1, weekdays.sorted()) }) { Text("Save") } }, dismissButton = { Row { if (initial != null) { TextButton(onClick = onClear) { Text("Clear") } } TextButton(onClick = onDismiss) { Text("Cancel") } } } ) }