summaryrefslogtreecommitdiff
path: root/android/app/src/main/java/org/terst/doot/widget/ui/RecurrenceEditDialog.kt
blob: c5c8aff7fe35368b44aafc70c72116cbe95b4381 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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<Int>) -> 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") }
            }
        }
    )
}