feat: implement Todo module with database support and UI components

This commit is contained in:
2026-04-23 20:16:20 -04:00
parent 5652e14352
commit 888841e510
9 changed files with 1185 additions and 9 deletions
@@ -36,7 +36,7 @@ import com.shaarit.data.local.entity.TodoEntity
ReadingReminderEntity::class,
TodoEntity::class
],
version = 8,
version = 9,
exportSchema = true
)
@TypeConverters(Converters::class)
@@ -131,6 +131,19 @@ abstract class ShaarliDatabase : RoomDatabase() {
}
}
/**
* Migration v8 → v9 : Ajout de start_date, repeat_mode et priority
*/
val MIGRATION_8_9 = object : Migration(8, 9) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE `todos` ADD COLUMN `start_date` INTEGER DEFAULT NULL")
db.execSQL("ALTER TABLE `todos` ADD COLUMN `repeat_mode` TEXT DEFAULT NULL")
db.execSQL("ALTER TABLE `todos` ADD COLUMN `priority` TEXT DEFAULT NULL")
db.execSQL("CREATE INDEX IF NOT EXISTS `index_todos_start_date` ON `todos` (`start_date`)")
db.execSQL("CREATE INDEX IF NOT EXISTS `index_todos_priority` ON `todos` (`priority`)")
}
}
@Volatile
private var instance: ShaarliDatabase? = null
@@ -146,7 +159,7 @@ abstract class ShaarliDatabase : RoomDatabase() {
ShaarliDatabase::class.java,
DATABASE_NAME
)
.addMigrations(MIGRATION_4_5, MIGRATION_5_6, MIGRATION_6_7, MIGRATION_7_8)
.addMigrations(MIGRATION_4_5, MIGRATION_5_6, MIGRATION_6_7, MIGRATION_7_8, MIGRATION_8_9)
.fallbackToDestructiveMigrationFrom(1, 2, 3)
.build()
}
@@ -11,8 +11,10 @@ import com.shaarit.domain.model.SubTask
indices = [
Index(value = ["shaarli_link_url"], unique = true),
Index(value = ["due_date"]),
Index(value = ["start_date"]),
Index(value = ["is_done"]),
Index(value = ["group_name"])
Index(value = ["group_name"]),
Index(value = ["priority"])
]
)
data class TodoEntity(
@@ -31,6 +33,15 @@ data class TodoEntity(
@ColumnInfo(name = "due_date")
val dueDate: Long? = null,
@ColumnInfo(name = "start_date")
val startDate: Long? = null,
@ColumnInfo(name = "repeat_mode")
val repeatMode: String? = null,
@ColumnInfo(name = "priority")
val priority: String? = null,
@ColumnInfo(name = "tags")
val tags: List<String> = emptyList(),
@@ -181,7 +181,10 @@ class TodoRepositoryImpl @Inject constructor(
shaarliLinkUrl = url,
content = cleanedContent,
isDone = todo.isDone,
startDate = todo.startDate,
dueDate = todo.dueDate,
repeatMode = todo.repeatMode,
priority = todo.priority,
tags = cleanedTags,
isSynced = false,
groupName = todo.groupName?.trim()?.takeIf { it.isNotBlank() },
@@ -207,7 +210,10 @@ class TodoRepositoryImpl @Inject constructor(
shaarliLinkUrl = shaarliLinkUrl,
content = content,
isDone = isDone,
startDate = startDate,
dueDate = dueDate,
repeatMode = repeatMode,
priority = priority,
tags = tags,
isSynced = isSynced,
groupName = groupName,
@@ -5,7 +5,10 @@ data class TodoItem(
val shaarliLinkUrl: String,
val content: String,
val isDone: Boolean = false,
val startDate: Long? = null,
val dueDate: Long? = null,
val repeatMode: String? = null,
val priority: String? = null,
val tags: List<String> = emptyList(),
val isSynced: Boolean = false,
val groupName: String? = null,
@@ -0,0 +1,218 @@
package com.shaarit.presentation.todo
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import org.json.JSONArray
import org.json.JSONObject
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CustomRepeatDialog(
initialRepeatMode: String?,
onDismiss: () -> Unit,
onSave: (String) -> Unit
) {
var interval by remember { mutableStateOf("1") }
var unit by remember { mutableStateOf("semaine") }
val units = listOf("jour", "semaine", "mois", "an")
var expandedUnit by remember { mutableStateOf(false) }
val daysOfWeek = listOf("D", "L", "M", "M", "J", "V", "S")
var selectedDays by remember { mutableStateOf(setOf<Int>()) }
var endMode by remember { mutableStateOf("jamais") } // "jamais", "date", "occurrences"
var endDate by remember { mutableStateOf("20 mai") }
var endOccurrences by remember { mutableStateOf("1") }
// Parse initialRepeatMode if it's CUSTOM
LaunchedEffect(initialRepeatMode) {
if (initialRepeatMode?.startsWith("CUSTOM:") == true) {
try {
val json = JSONObject(initialRepeatMode.substring(7))
interval = json.optString("interval", "1")
unit = json.optString("unit", "semaine")
val daysArr = json.optJSONArray("daysOfWeek")
if (daysArr != null) {
val days = mutableSetOf<Int>()
for (i in 0 until daysArr.length()) {
days.add(daysArr.getInt(i))
}
selectedDays = days
}
endMode = json.optString("endMode", "jamais")
endDate = json.optString("endDate", "")
endOccurrences = json.optString("endOccurrences", "1")
} catch (e: Exception) {
// Default fallback
}
}
}
Dialog(
onDismissRequest = onDismiss,
properties = DialogProperties(
usePlatformDefaultWidth = false,
decorFitsSystemWindows = true
)
) {
Scaffold(
topBar = {
TopAppBar(
title = { Text("Répétition personnalisée") },
navigationIcon = {
IconButton(onClick = onDismiss) {
Icon(Icons.Default.ArrowBack, contentDescription = "Retour")
}
},
actions = {
TextButton(onClick = {
val json = JSONObject()
json.put("interval", interval)
json.put("unit", unit)
if (unit == "semaine") {
json.put("daysOfWeek", JSONArray(selectedDays.toList()))
}
json.put("endMode", endMode)
if (endMode == "date") json.put("endDate", endDate)
if (endMode == "occurrences") json.put("endOccurrences", endOccurrences)
onSave("CUSTOM:${json.toString()}")
}) {
Text("ENREGISTRER", color = MaterialTheme.colorScheme.primary)
}
},
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.surface
)
)
}
) { paddingValues ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
.padding(16.dp)
) {
Text("Répéter par intervalle de", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)
Spacer(modifier = Modifier.height(8.dp))
Row(verticalAlignment = Alignment.CenterVertically) {
OutlinedTextField(
value = interval,
onValueChange = { interval = it },
modifier = Modifier.width(80.dp),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
singleLine = true
)
Spacer(modifier = Modifier.width(16.dp))
Box {
OutlinedButton(onClick = { expandedUnit = true }) {
Text(unit)
}
DropdownMenu(
expanded = expandedUnit,
onDismissRequest = { expandedUnit = false }
) {
units.forEach { u ->
DropdownMenuItem(
text = { Text(u) },
onClick = { unit = u; expandedUnit = false }
)
}
}
}
}
if (unit == "semaine") {
Spacer(modifier = Modifier.height(24.dp))
Text("Répéter le", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)
Spacer(modifier = Modifier.height(8.dp))
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween
) {
daysOfWeek.forEachIndexed { index, day ->
val isSelected = selectedDays.contains(index)
Box(
modifier = Modifier
.size(40.dp)
.background(
color = if (isSelected) MaterialTheme.colorScheme.primary else Color.Transparent,
shape = CircleShape
)
.clickable {
if (isSelected) selectedDays = selectedDays - index
else selectedDays = selectedDays + index
},
contentAlignment = Alignment.Center
) {
Text(
text = day,
color = if (isSelected) MaterialTheme.colorScheme.onPrimary else MaterialTheme.colorScheme.onSurface
)
}
}
}
}
Spacer(modifier = Modifier.height(24.dp))
Text("Se termine", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)
Spacer(modifier = Modifier.height(8.dp))
Row(verticalAlignment = Alignment.CenterVertically) {
RadioButton(
selected = endMode == "jamais",
onClick = { endMode = "jamais" }
)
Text("Jamais", modifier = Modifier.clickable { endMode = "jamais" })
}
Row(verticalAlignment = Alignment.CenterVertically) {
RadioButton(
selected = endMode == "date",
onClick = { endMode = "date" }
)
Text("Le ", modifier = Modifier.clickable { endMode = "date" })
OutlinedTextField(
value = endDate,
onValueChange = { endDate = it },
modifier = Modifier.width(120.dp).padding(start = 8.dp),
enabled = endMode == "date",
singleLine = true
)
}
Row(verticalAlignment = Alignment.CenterVertically) {
RadioButton(
selected = endMode == "occurrences",
onClick = { endMode = "occurrences" }
)
Text("Après ", modifier = Modifier.clickable { endMode = "occurrences" })
OutlinedTextField(
value = endOccurrences,
onValueChange = { endOccurrences = it },
modifier = Modifier.width(80.dp).padding(horizontal = 8.dp),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
enabled = endMode == "occurrences",
singleLine = true
)
Text("occurrence", modifier = Modifier.clickable { endMode = "occurrences" })
}
}
}
}
}
@@ -49,8 +49,10 @@ import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Event
import androidx.compose.material.icons.filled.Flag
import androidx.compose.material.icons.filled.Folder
import androidx.compose.material.icons.filled.List
import androidx.compose.material.icons.filled.Repeat
import androidx.compose.material.icons.filled.Schedule
import androidx.compose.material.icons.filled.Snooze
import androidx.compose.material.icons.outlined.CheckBoxOutlineBlank
@@ -443,6 +445,9 @@ fun TodoScreen(
onDismiss = viewModel::closeEditDialog,
onContentChanged = viewModel::onEditContentChanged,
onDueDateChanged = viewModel::onEditDueDateChanged,
onStartDateChanged = viewModel::onEditStartDateChanged,
onRepeatModeChanged = viewModel::onEditRepeatModeChanged,
onPriorityChanged = viewModel::onEditPriorityChanged,
onGroupChanged = viewModel::onEditGroupChanged,
onNewSubtaskTextChanged = viewModel::onEditNewSubtaskTextChanged,
onAddSubtask = viewModel::addSubtask,
@@ -657,6 +662,9 @@ private fun EditTodoDialog(
onDismiss: () -> Unit,
onContentChanged: (String) -> Unit,
onDueDateChanged: (Long?) -> Unit,
onStartDateChanged: (Long?) -> Unit,
onRepeatModeChanged: (String?) -> Unit,
onPriorityChanged: (String?) -> Unit,
onGroupChanged: (String) -> Unit,
onNewSubtaskTextChanged: (String) -> Unit,
onAddSubtask: () -> Unit,
@@ -669,6 +677,10 @@ private fun EditTodoDialog(
onSave: () -> Unit
) {
var showDatePicker by remember { mutableStateOf(false) }
var showStartDatePicker by remember { mutableStateOf(false) }
var showRepeatMenu by remember { mutableStateOf(false) }
var showCustomRepeatDialog by remember { mutableStateOf(false) }
var showPriorityMenu by remember { mutableStateOf(false) }
androidx.compose.ui.window.Dialog(
onDismissRequest = onDismiss,
@@ -737,6 +749,17 @@ private fun EditTodoDialog(
androidx.compose.material3.Divider(color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f))
// Start Date
val startDateText = state.startDate?.let { formatDateTime(it, timezoneId) } ?: "Pas de date de début"
TaskActionRow(
icon = Icons.Default.Event,
text = startDateText,
onClick = { showStartDatePicker = true },
isSet = state.startDate != null
)
androidx.compose.material3.Divider(color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f))
// Due Date
val dateText = state.dueDate?.let { formatDateTime(it, timezoneId) } ?: "Aucune date d'échéance"
TaskActionRow(
@@ -748,6 +771,105 @@ private fun EditTodoDialog(
androidx.compose.material3.Divider(color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f))
// Repeat Mode
val repeatText = when {
state.repeatMode == null || state.repeatMode == "NONE" -> "Ne pas répéter"
state.repeatMode == "DAILY" -> "Tous les jours"
state.repeatMode == "WEEKLY" -> "Toutes les semaines"
state.repeatMode == "MONTHLY" -> "Tous les mois"
state.repeatMode == "YEARLY" -> "Tous les ans"
state.repeatMode?.startsWith("CUSTOM") == true -> "Personnalisé..."
else -> "Ne pas répéter"
}
androidx.compose.foundation.layout.Box {
TaskActionRow(
icon = Icons.Default.Repeat,
text = repeatText,
onClick = { showRepeatMenu = true },
isSet = state.repeatMode != null && state.repeatMode != "NONE"
)
androidx.compose.material3.DropdownMenu(
expanded = showRepeatMenu,
onDismissRequest = { showRepeatMenu = false }
) {
androidx.compose.material3.DropdownMenuItem(
text = { Text("Ne pas répéter") },
onClick = { onRepeatModeChanged("NONE"); showRepeatMenu = false }
)
androidx.compose.material3.DropdownMenuItem(
text = { Text("Tous les jours") },
onClick = { onRepeatModeChanged("DAILY"); showRepeatMenu = false }
)
androidx.compose.material3.DropdownMenuItem(
text = { Text("Toutes les semaines") },
onClick = { onRepeatModeChanged("WEEKLY"); showRepeatMenu = false }
)
androidx.compose.material3.DropdownMenuItem(
text = { Text("Tous les mois") },
onClick = { onRepeatModeChanged("MONTHLY"); showRepeatMenu = false }
)
androidx.compose.material3.DropdownMenuItem(
text = { Text("Tous les ans") },
onClick = { onRepeatModeChanged("YEARLY"); showRepeatMenu = false }
)
androidx.compose.material3.DropdownMenuItem(
text = { Text("Personnalisé...") },
onClick = { showRepeatMenu = false; showCustomRepeatDialog = true }
)
}
}
androidx.compose.material3.Divider(color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f))
// Priority
val priorityText = when(state.priority) {
"RED" -> "Urgent (Rouge)"
"YELLOW" -> "Élevée (Jaune)"
"BLUE" -> "Normale (Bleu)"
"GREEN" -> "Basse (Vert)"
else -> "Priorité normale"
}
val priorityColor = when(state.priority) {
"RED" -> androidx.compose.ui.graphics.Color.Red
"YELLOW" -> androidx.compose.ui.graphics.Color.Yellow
"BLUE" -> androidx.compose.ui.graphics.Color.Blue
"GREEN" -> androidx.compose.ui.graphics.Color.Green
else -> MaterialTheme.colorScheme.onSurfaceVariant
}
androidx.compose.foundation.layout.Box {
TaskActionRow(
icon = Icons.Default.Flag,
iconTint = priorityColor,
textTint = priorityColor,
text = priorityText,
onClick = { showPriorityMenu = true },
isSet = state.priority != null
)
androidx.compose.material3.DropdownMenu(
expanded = showPriorityMenu,
onDismissRequest = { showPriorityMenu = false }
) {
androidx.compose.material3.DropdownMenuItem(
text = { Text("Urgent (Rouge)", color = androidx.compose.ui.graphics.Color.Red) },
onClick = { onPriorityChanged("RED"); showPriorityMenu = false }
)
androidx.compose.material3.DropdownMenuItem(
text = { Text("Élevée (Jaune)", color = androidx.compose.ui.graphics.Color.Yellow) },
onClick = { onPriorityChanged("YELLOW"); showPriorityMenu = false }
)
androidx.compose.material3.DropdownMenuItem(
text = { Text("Normale (Bleu)", color = androidx.compose.ui.graphics.Color.Blue) },
onClick = { onPriorityChanged("BLUE"); showPriorityMenu = false }
)
androidx.compose.material3.DropdownMenuItem(
text = { Text("Basse (Vert)", color = androidx.compose.ui.graphics.Color.Green) },
onClick = { onPriorityChanged("GREEN"); showPriorityMenu = false }
)
}
}
androidx.compose.material3.Divider(color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f))
// Group (Famille)
TaskGroupRow(
groupName = state.groupName,
@@ -791,6 +913,26 @@ private fun EditTodoDialog(
onDateSelected = onDueDateChanged
)
}
if (showStartDatePicker) {
DateTimePickerBottomSheet(
initialDate = state.startDate,
timezoneId = timezoneId,
onDismiss = { showStartDatePicker = false },
onDateSelected = onStartDateChanged
)
}
if (showCustomRepeatDialog) {
CustomRepeatDialog(
initialRepeatMode = state.repeatMode,
onDismiss = { showCustomRepeatDialog = false },
onSave = {
onRepeatModeChanged(it)
showCustomRepeatDialog = false
}
)
}
}
}
@@ -799,7 +941,9 @@ private fun TaskActionRow(
icon: androidx.compose.ui.graphics.vector.ImageVector,
text: String,
onClick: () -> Unit = {},
isSet: Boolean = false
isSet: Boolean = false,
iconTint: androidx.compose.ui.graphics.Color? = null,
textTint: androidx.compose.ui.graphics.Color? = null
) {
Row(
modifier = Modifier
@@ -811,12 +955,12 @@ private fun TaskActionRow(
Icon(
icon,
contentDescription = null,
tint = if (isSet) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurfaceVariant
tint = iconTint ?: if (isSet) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurfaceVariant
)
Spacer(modifier = Modifier.width(16.dp))
Text(
text,
color = if (isSet) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurfaceVariant,
color = textTint ?: if (isSet) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurfaceVariant,
style = MaterialTheme.typography.bodyLarge
)
}
@@ -44,7 +44,10 @@ data class EditTodoDialogUiState(
val isCreateMode: Boolean = false,
val todoId: Long = 0,
val content: String = "",
val startDate: Long? = null,
val dueDate: Long? = null,
val repeatMode: String? = null,
val priority: String? = null,
val tags: List<String> = emptyList(),
val groupName: String = "",
val subtasks: List<SubTask> = emptyList(),
@@ -310,7 +313,10 @@ class TodoViewModel @Inject constructor(
isVisible = true,
todoId = todo.id,
content = todo.content,
startDate = todo.startDate,
dueDate = todo.dueDate,
repeatMode = todo.repeatMode,
priority = todo.priority,
tags = todo.tags,
groupName = todo.groupName ?: "",
subtasks = todo.subtasks,
@@ -332,6 +338,18 @@ class TodoViewModel @Inject constructor(
_editDialogState.update { it.copy(dueDate = dueDate) }
}
fun onEditStartDateChanged(startDate: Long?) {
_editDialogState.update { it.copy(startDate = startDate) }
}
fun onEditRepeatModeChanged(repeatMode: String?) {
_editDialogState.update { it.copy(repeatMode = repeatMode) }
}
fun onEditPriorityChanged(priority: String?) {
_editDialogState.update { it.copy(priority = priority) }
}
fun onEditGroupChanged(groupName: String) {
_editDialogState.update { it.copy(groupName = groupName) }
}
@@ -411,7 +429,10 @@ class TodoViewModel @Inject constructor(
shaarliLinkUrl = "",
content = content,
isDone = false,
startDate = state.startDate,
dueDate = state.dueDate,
repeatMode = state.repeatMode,
priority = state.priority,
tags = state.tags,
isSynced = false,
groupName = state.groupName.takeIf { it.isNotBlank() },
@@ -423,7 +444,10 @@ class TodoViewModel @Inject constructor(
shaarliLinkUrl = state.shaarliLinkUrl,
content = content,
isDone = state.isDone,
startDate = state.startDate,
dueDate = state.dueDate,
repeatMode = state.repeatMode,
priority = state.priority,
tags = state.tags,
isSynced = false,
groupName = state.groupName.takeIf { it.isNotBlank() },