From 4c261cdb9d4e80cfe64324995106b8c98d90c8f3 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Tue, 17 Nov 2026 11:00:43 +0100 Subject: [PATCH] Group tasks by due date, day, priority or list (#71) --- CHANGELOG.md | 6 + .../agendula/data/prefs/SettingsPrefs.kt | 66 ++++++++-- .../agendula/domain/TaskGroups.kt | 113 +++++++++++++++++ .../agendula/ui/tasklist/TaskListScreen.kt | 94 ++++++++++++-- .../agendula/ui/tasklist/TaskListViewModel.kt | 22 ++-- app/src/main/res/values/strings.xml | 15 +++ .../data/prefs/TaskGroupingPrefsTest.kt | 42 +++++++ .../agendula/domain/TaskGroupsTest.kt | 119 ++++++++++++++++++ 8 files changed, 447 insertions(+), 30 deletions(-) create mode 100644 app/src/main/java/de/jeanlucmakiola/agendula/domain/TaskGroups.kt create mode 100644 app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/TaskGroupingPrefsTest.kt create mode 100644 app/src/test/java/de/jeanlucmakiola/agendula/domain/TaskGroupsTest.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 67c3d4b..7de47f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ All notable changes to this project are documented here. The format follows ## [Unreleased] +### Added +- Task lists can be grouped by due date (Overdue, Today, Upcoming), by day, + by priority or, in smart lists, tags and filters, by list. Pick it under + "Group by" in the list's sort and filter options; each list and view + remembers its own choice (#71). + ## [1.3.2] - 2026-11-13 ### Added diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/SettingsPrefs.kt b/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/SettingsPrefs.kt index 3022a99..d8dc02f 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/SettingsPrefs.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/SettingsPrefs.kt @@ -15,7 +15,10 @@ import de.jeanlucmakiola.agendula.domain.Task import de.jeanlucmakiola.agendula.domain.TaskFilter import de.jeanlucmakiola.agendula.domain.TaskFormField import de.jeanlucmakiola.agendula.domain.TaskReminder +import de.jeanlucmakiola.agendula.domain.TaskGroupKey +import de.jeanlucmakiola.agendula.domain.TaskGrouping import de.jeanlucmakiola.agendula.domain.TaskSection +import de.jeanlucmakiola.agendula.domain.groupingKey import de.jeanlucmakiola.agendula.domain.asTaskReminder import de.jeanlucmakiola.agendula.domain.asReminderLead import de.jeanlucmakiola.agendula.domain.TaskSortOrder @@ -28,6 +31,7 @@ import de.jeanlucmakiola.floret.reminders.reminderLeadsFor import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.distinctUntilChanged import java.time.DayOfWeek +import java.time.LocalDate import kotlinx.coroutines.flow.map import javax.inject.Inject import javax.inject.Singleton @@ -58,7 +62,8 @@ fun TimeFormatPref.is24Hour(systemIs24Hour: Boolean): Boolean = when (this) { enum class ReminderDescription { HIDDEN, SHORT, FULL } const val DEFAULT_SNOOZE_MINUTES = 10 -/** Done starts folded away, to keep the open tasks in focus. */ val DEFAULT_COLLAPSED_SECTIONS: Set = setOf(TaskSection.COMPLETED) +/** Done starts folded away, to keep the open tasks in focus. */ +val DEFAULT_COLLAPSED_GROUPS: Set = setOf(TaskGroupKey.Section(TaskSection.COMPLETED).id) /** The snooze lengths Settings offers. */ val SNOOZE_PRESETS = listOf(5, 10, 15, 30, 60) @@ -120,9 +125,14 @@ data class Settings( val hiddenFromSmartLists: Set = emptySet(), /** Pre-fill a new task's start with today's date. */ val defaultStartToday: Boolean = false, - /** Task-list sections folded away; Done starts folded. */ - val collapsedSections: Set = DEFAULT_COLLAPSED_SECTIONS, + /** Ids of the task-list groups folded away ([TaskGroupKey.id]); Done starts folded. */ + val collapsedGroups: Set = DEFAULT_COLLAPSED_GROUPS, + /** Each view's chosen grouping, keyed by [groupingKey]; absent = [TaskGrouping.DUE]. */ + val groupings: Map = emptyMap(), ) { + fun groupingFor(filter: TaskFilter): TaskGrouping = + filter.groupingKey()?.let { groupings[it] } ?: TaskGrouping.DUE + /** [tasks] as [filter] shows them: a smart list drops the lists kept out of it. */ fun visibleIn(filter: TaskFilter, tasks: List): List = if (filter is TaskFilter.Smart && hiddenFromSmartLists.isNotEmpty()) { @@ -198,16 +208,22 @@ class SettingsPrefs @Inject constructor( .toSet() }, defaultStartToday = p[DEFAULT_START_TODAY] ?: false, - collapsedSections = p[COLLAPSED_SECTIONS] - ?.mapNotNull { name -> runCatching { TaskSection.valueOf(name) }.getOrNull() } - ?.toSet() - ?: DEFAULT_COLLAPSED_SECTIONS, + collapsedGroups = p[COLLAPSED_SECTIONS] ?: DEFAULT_COLLAPSED_GROUPS, + groupings = decodeGroupings(p[GROUPINGS].orEmpty(), modeOf(p).name), ) } - suspend fun setSectionCollapsed(section: TaskSection, collapsed: Boolean) = dataStore.edit { p -> - val current = p[COLLAPSED_SECTIONS] ?: DEFAULT_COLLAPSED_SECTIONS.mapTo(HashSet()) { it.name } - p[COLLAPSED_SECTIONS] = if (collapsed) current + section.name else current - section.name + /** Folds or unfolds the group with [id]; day groups that have passed are dropped on the way. */ + suspend fun setGroupCollapsed(id: String, collapsed: Boolean) = dataStore.edit { p -> + val current = dropPastDays(p[COLLAPSED_SECTIONS] ?: DEFAULT_COLLAPSED_GROUPS, LocalDate.now()) + p[COLLAPSED_SECTIONS] = if (collapsed) current + id else current - id + } + + /** Remembers [grouping] for [filter]'s view. */ + suspend fun setGrouping(filter: TaskFilter, grouping: TaskGrouping) = dataStore.edit { p -> + val key = storedGroupingKey(filter, modeOf(p).name) ?: return@edit + val kept = p[GROUPINGS].orEmpty().filterTo(HashSet()) { it.substringAfter(GROUPING_SEPARATOR) != key } + p[GROUPINGS] = if (grouping == TaskGrouping.DUE) kept else kept + "${grouping.name}$GROUPING_SEPARATOR$key" } suspend fun setSortOrder(order: TaskSortOrder) = dataStore.edit { it[SORT_ORDER] = order.name } @@ -372,6 +388,7 @@ class SettingsPrefs @Inject constructor( val SMART_LIST_HIDDEN = stringSetPreferencesKey("smart_list_hidden") val DEFAULT_START_TODAY = booleanPreferencesKey("default_start_today") val COLLAPSED_SECTIONS = stringSetPreferencesKey("collapsed_sections") + val GROUPINGS = stringSetPreferencesKey("task_groupings") val SAVED_FILTERS = stringPreferencesKey("saved_filters") val SAVED_FILTER_NEXT_ID = longPreferencesKey("saved_filter_next_id") } @@ -394,3 +411,32 @@ private fun parseLeads(stored: String): List = */ private fun List.inWholeDays(): List = map { it.asTaskReminder().inWholeDays().asReminderLead() }.normalizeReminders() + +private const val GROUPING_SEPARATOR = '|' + +/** List ids belong to one store, so a list's key carries the store's [mode]: `list:OWN:5`. */ +private fun storedGroupingKey(filter: TaskFilter, mode: String): String? { + val key = filter.groupingKey() ?: return null + return if (filter is TaskFilter.OfList) "list:$mode:${filter.listId}" else key +} + +/** `GROUPING|key` entries → view key to grouping, keeping only the lists of the current [mode]. */ +internal fun decodeGroupings(entries: Set, mode: String): Map = + entries.mapNotNull { entry -> + val grouping = runCatching { TaskGrouping.valueOf(entry.substringBefore(GROUPING_SEPARATOR)) } + .getOrNull() ?: return@mapNotNull null + val stored = entry.substringAfter(GROUPING_SEPARATOR, "") + val key = when { + stored.isEmpty() -> null + stored.startsWith("list:") -> stored.removePrefix("list:$mode:").takeIf { it != stored }?.let { "list:$it" } + else -> stored + } + key?.let { it to grouping } + }.toMap() + +/** [collapsed] without the day groups before [today], which can never show again. */ +internal fun dropPastDays(collapsed: Set, today: LocalDate): Set = + collapsed.filterTo(HashSet()) { entry -> + if (!entry.startsWith(TaskGroupKey.DAY_PREFIX)) return@filterTo true + runCatching { !LocalDate.parse(entry.removePrefix(TaskGroupKey.DAY_PREFIX)).isBefore(today) }.getOrDefault(false) + } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/domain/TaskGroups.kt b/app/src/main/java/de/jeanlucmakiola/agendula/domain/TaskGroups.kt new file mode 100644 index 0000000..3230674 --- /dev/null +++ b/app/src/main/java/de/jeanlucmakiola/agendula/domain/TaskGroups.kt @@ -0,0 +1,113 @@ +package de.jeanlucmakiola.agendula.domain + +import java.time.LocalDate +import java.time.ZoneId +import kotlin.time.Instant + +/** How a task list groups its rows (#71). [DUE] is the classic Overdue / Today / Upcoming split. */ +enum class TaskGrouping { DUE, DAY, PRIORITY, LIST } + +/** What a group of rows is headed by. [id] is stable and is what collapse state keys on. */ +sealed interface TaskGroupKey { + val id: String + + /** A due-date bucket; its id is the section's name, as earlier versions stored it. */ + data class Section(val section: TaskSection) : TaskGroupKey { + override val id: String get() = section.name + } + + /** Open tasks due on [date], a day after today. */ + data class Day(val date: LocalDate) : TaskGroupKey { + override val id: String get() = "$DAY_PREFIX$date" + } + + data class OfPriority(val priority: Priority) : TaskGroupKey { + override val id: String get() = "priority:${priority.name}" + } + + data class OfList(val listId: Long, val name: String?) : TaskGroupKey { + override val id: String get() = "list:$listId" + } + + companion object { + const val DAY_PREFIX = "day:" + } +} + +/** A non-empty run of tasks under one [TaskGroupKey], already in display order. */ +data class TaskGroup(val key: TaskGroupKey, val tasks: List) + +/** + * Groups already-sorted tasks for display. Completed tasks always close the list + * in their own group, whatever the grouping; within a group the incoming order is + * kept. [todayStart] / [todayEnd] bound the local day (see [DayWindow]); in the + * Today list ([todayList]) the date groupings put every open task under Today. + */ +object TaskGroups { + + fun of( + tasks: List, + grouping: TaskGrouping, + todayStart: Instant, + todayEnd: Instant, + todayList: Boolean = false, + zone: ZoneId = ZoneId.systemDefault(), + ): List { + val (completed, open) = tasks.partition { it.isCompleted } + val keyed: List>> = when (grouping) { + TaskGrouping.DUE, TaskGrouping.DAY -> open + .groupBy { dueKey(it, grouping == TaskGrouping.DAY, todayStart, todayEnd, todayList, zone) } + .toList() + .sortedWith(compareBy({ dueRank(it.first) }, { (it.first as? TaskGroupKey.Day)?.date })) + TaskGrouping.PRIORITY -> open + .groupBy { TaskGroupKey.OfPriority(it.priority) } + .toList() + .sortedByDescending { it.first.priority.ordinal } + TaskGrouping.LIST -> open + .groupBy { it.listId } + .map { (listId, rows) -> TaskGroupKey.OfList(listId, rows.first().listName) to rows } + .sortedWith(compareBy({ it.first.name.orEmpty().lowercase() }, { it.first.listId })) + } + val groups = keyed.map { (key, rows) -> TaskGroup(key, rows) } + return if (completed.isEmpty()) groups else groups + TaskGroup(TaskGroupKey.Section(TaskSection.COMPLETED), completed) + } + + private fun dueKey( + task: Task, + perDay: Boolean, + todayStart: Instant, + todayEnd: Instant, + todayList: Boolean, + zone: ZoneId, + ): TaskGroupKey { + val window = if (todayList) DueWindow.TODAY else dueWindowOf(task, todayStart, todayEnd, zone) + return when (window) { + DueWindow.OVERDUE -> TaskGroupKey.Section(TaskSection.OVERDUE) + DueWindow.TODAY -> TaskGroupKey.Section(TaskSection.TODAY) + DueWindow.NO_DATE -> TaskGroupKey.Section(TaskSection.NO_DATE) + DueWindow.UPCOMING -> + if (perDay) TaskGroupKey.Day(localDueDate(task, zone)) else TaskGroupKey.Section(TaskSection.UPCOMING) + } + } + + /** Overdue → Today → Upcoming (or each later day) → No date. */ + private fun dueRank(key: TaskGroupKey): Int = when (key) { + is TaskGroupKey.Day -> TaskSection.UPCOMING.ordinal + is TaskGroupKey.Section -> key.section.ordinal + else -> Int.MAX_VALUE + } + + private fun localDueDate(task: Task, zone: ZoneId): LocalDate { + val due = requireNotNull(task.wallClock(task.due, zone)) + return java.time.Instant.ofEpochMilli(due.toEpochMilliseconds()).atZone(zone).toLocalDate() + } +} + +/** The key a view's grouping is remembered under; `null` for views that have no task-list screen. */ +fun TaskFilter.groupingKey(): String? = when (this) { + is TaskFilter.OfList -> "list:$listId" + is TaskFilter.Smart -> "smart:${list.name}" + is TaskFilter.Tagged -> "tag:$tag" + is TaskFilter.Saved -> "saved:$id" + is TaskFilter.OfLists -> null +} diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListScreen.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListScreen.kt index a6efcd5..03abe3f 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListScreen.kt @@ -113,7 +113,9 @@ import de.jeanlucmakiola.agendula.domain.SmartList import de.jeanlucmakiola.agendula.domain.Task import de.jeanlucmakiola.agendula.domain.TaskFilter import de.jeanlucmakiola.agendula.domain.TaskSection -import de.jeanlucmakiola.agendula.domain.TaskSections +import de.jeanlucmakiola.agendula.domain.TaskGroupKey +import de.jeanlucmakiola.agendula.domain.TaskGrouping +import de.jeanlucmakiola.agendula.domain.TaskGroups import de.jeanlucmakiola.agendula.ui.common.LocalUse24HourFormat import de.jeanlucmakiola.agendula.ui.common.SyncPullBox import de.jeanlucmakiola.agendula.ui.common.ignoringEdgeSwipes @@ -128,11 +130,14 @@ import de.jeanlucmakiola.floret.components.SnackChipMargin import de.jeanlucmakiola.floret.time.formatDateTimeCompact import de.jeanlucmakiola.floret.components.pastelize import de.jeanlucmakiola.floret.components.positionOf +import java.time.LocalDate import java.time.ZoneId +import java.time.format.DateTimeFormatter +import java.util.Locale import kotlin.time.Clock /** - * The tasks for one [TaskFilter]: due-date section headers, grouped tonal rows + * The tasks for one [TaskFilter]: collapsible group headers, grouped tonal rows * with a leading checkbox, swipe-to-complete (start→end) / swipe-to-delete * (end→start), and — for a real list — an inline add field. Renders against the * M1 [TaskListViewModel]; actions are fire-and-forget and flow back live. @@ -313,9 +318,13 @@ fun TaskListScreen( if (showSort && content != null) { ViewOptionsPicker( sortOrder = content.sortOrder, + grouping = content.grouping, + // A real list is one list already; grouping it by list would only add a header. + groupings = TaskGrouping.entries.filter { it != TaskGrouping.LIST || filter !is TaskFilter.OfList }, tags = content.availableTags, selectedTags = content.selectedTags, onSortOrder = viewModel::setSortOrder, + onGrouping = viewModel::setGrouping, onToggleTag = viewModel::toggleTagFilter, onClearTags = viewModel::clearTagFilter, onDismiss = { showSort = false }, @@ -379,10 +388,17 @@ private fun TaskListBody( val present = state.tasks.mapTo(HashSet()) { it.taskId } state.tasks.filter { (it.parentId ?: 0L) <= 0L || it.parentId !in present } } - val sections = remember(displayTasks, todayStart) { - TaskSections.of(displayTasks, todayStart, todayEnd, todayList = filter == TaskFilter.Smart(SmartList.TODAY)) + val groups = remember(displayTasks, todayStart, state.grouping) { + TaskGroups.of( + displayTasks, + state.grouping, + todayStart, + todayEnd, + todayList = filter == TaskFilter.Smart(SmartList.TODAY), + ) } - val showHeaders = sections.size > 1 + // A grouping the user picked keeps its header even when everything lands in one group. + val showHeaders = groups.size > 1 || (groups.isNotEmpty() && state.grouping != TaskGrouping.DUE) val listId = (filter as? TaskFilter.OfList)?.listId // In a mixed view (a smart list) the list colour bar + name disambiguate // rows; inside a single real list both are redundant. @@ -436,17 +452,18 @@ private fun TaskListBody( item(key = "empty") { EmptyState(stringResource(R.string.task_list_empty)) } } - sections.forEach { section -> + groups.forEach { group -> // Every header folds its section away (#91); Done starts folded. - val expanded = !showHeaders || section.section !in state.collapsedSections + val groupId = group.key.id + val expanded = !showHeaders || groupId !in state.collapsedGroups if (showHeaders) { - stickyHeader(key = "hdr-${section.section}") { + stickyHeader(key = "hdr-$groupId") { CollapsibleSectionHeader( - label = sectionLabel(section.section), - count = section.tasks.size, + label = groupLabel(group.key, todayStart), + count = group.tasks.size, expanded = expanded, - onToggle = { viewModel.setSectionCollapsed(section.section, expanded) }, - onClear = onClearCompleted.takeIf { section.section == TaskSection.COMPLETED }, + onToggle = { viewModel.setGroupCollapsed(groupId, expanded) }, + onClear = onClearCompleted.takeIf { group.key == TaskGroupKey.Section(TaskSection.COMPLETED) }, ) } } @@ -458,7 +475,7 @@ private fun TaskListBody( // full-width, connecting to the parent above (small top corners), // and the run rounds off (full corners) only at its genuine ends. val rows = buildList { - val topLevel = section.tasks + val topLevel = group.tasks topLevel.forEachIndexed { p, task -> // The full child set — already in this view for a real list, // or fetched on demand for a smart list (childrenOf). A parent @@ -1234,9 +1251,12 @@ private fun CenteredMessage(text: String, inner: PaddingValues) { @Composable private fun ViewOptionsPicker( sortOrder: TaskSortOrder, + grouping: TaskGrouping, + groupings: List, tags: List, selectedTags: Set, onSortOrder: (TaskSortOrder) -> Unit, + onGrouping: (TaskGrouping) -> Unit, onToggleTag: (String) -> Unit, onClearTags: () -> Unit, onDismiss: () -> Unit, @@ -1261,6 +1281,20 @@ private fun ViewOptionsPicker( onClick = { onSortOrder(order) }, ) } + Spacer(Modifier.height(8.dp)) + GroupedSectionHeader(stringResource(R.string.group_title)) + groupings.forEachIndexed { index, option -> + val isSelected = option == grouping + val (title, summary) = groupingLabels(option) + GroupedRow( + title = stringResource(title), + summary = stringResource(summary), + position = positionOf(index, groupings.size), + selected = isSelected, + trailing = if (isSelected) ({ SelectedCheck() }) else null, + onClick = { onGrouping(option) }, + ) + } if (tags.isNotEmpty()) { Spacer(Modifier.height(8.dp)) GroupedSectionHeader(stringResource(R.string.view_filter_tags)) @@ -1309,6 +1343,40 @@ private fun sectionLabel(section: TaskSection): String = stringResource( }, ) +@Composable +private fun groupLabel(key: TaskGroupKey, todayStart: kotlin.time.Instant): String = when (key) { + is TaskGroupKey.Section -> sectionLabel(key.section) + is TaskGroupKey.Day -> { + val today = java.time.Instant.ofEpochMilli(todayStart.toEpochMilliseconds()) + .atZone(ZoneId.systemDefault()).toLocalDate() + if (key.date == today.plusDays(1)) stringResource(R.string.group_tomorrow) else dayLabel(key.date, today) + } + is TaskGroupKey.OfPriority -> stringResource( + when (key.priority) { + Priority.HIGH -> R.string.group_priority_high + Priority.MEDIUM -> R.string.group_priority_medium + Priority.LOW -> R.string.group_priority_low + Priority.NONE -> R.string.group_priority_none + }, + ) + is TaskGroupKey.OfList -> key.name?.takeIf { it.isNotBlank() } ?: stringResource(R.string.group_list_unnamed) +} + +/** A day header in the locale's own order, e.g. "Friday, 21 November"; the year only when it isn't this one's. */ +private fun dayLabel(date: LocalDate, today: LocalDate): String { + val locale = Locale.getDefault() + val skeleton = if (date.year == today.year) "EEEEMMMMd" else "EEEEMMMMdy" + val pattern = android.text.format.DateFormat.getBestDateTimePattern(locale, skeleton) + return date.format(DateTimeFormatter.ofPattern(pattern, locale)) +} + +private fun groupingLabels(grouping: TaskGrouping): Pair = when (grouping) { + TaskGrouping.DUE -> R.string.group_due to R.string.group_due_summary + TaskGrouping.DAY -> R.string.group_day to R.string.group_day_summary + TaskGrouping.PRIORITY -> R.string.group_priority to R.string.group_priority_summary + TaskGrouping.LIST -> R.string.group_list to R.string.group_list_summary +} + @Composable internal fun priorityLabel(priority: Priority): String = stringResource( when (priority) { diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListViewModel.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListViewModel.kt index b74fece..b9bfb7a 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListViewModel.kt @@ -3,7 +3,7 @@ package de.jeanlucmakiola.agendula.ui.tasklist import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel -import de.jeanlucmakiola.agendula.data.prefs.DEFAULT_COLLAPSED_SECTIONS +import de.jeanlucmakiola.agendula.data.prefs.DEFAULT_COLLAPSED_GROUPS import de.jeanlucmakiola.agendula.data.prefs.SettingsPrefs import de.jeanlucmakiola.agendula.data.sync.RemoteLists import de.jeanlucmakiola.agendula.data.tasks.TasksRepository @@ -15,7 +15,7 @@ import de.jeanlucmakiola.agendula.domain.TaskFiltering import de.jeanlucmakiola.agendula.domain.tagsInUse import de.jeanlucmakiola.agendula.domain.TaskForm import de.jeanlucmakiola.agendula.domain.TaskList -import de.jeanlucmakiola.agendula.domain.TaskSection +import de.jeanlucmakiola.agendula.domain.TaskGrouping import de.jeanlucmakiola.agendula.domain.TaskSortOrder import de.jeanlucmakiola.agendula.domain.comparator import de.jeanlucmakiola.agendula.ui.lists.ListWriteFailure @@ -59,8 +59,10 @@ sealed interface TaskListUiState { /** Whether a real list uses the bottom quick-add bar instead of the FAB. */ val bottomAddBar: Boolean = false, val sortOrder: TaskSortOrder = TaskSortOrder.DUE, - /** Sections whose rows are folded under their header. */ - val collapsedSections: Set = DEFAULT_COLLAPSED_SECTIONS, + /** How this view groups its rows. */ + val grouping: TaskGrouping = TaskGrouping.DUE, + /** Ids of the groups whose rows are folded under their header. */ + val collapsedGroups: Set = DEFAULT_COLLAPSED_GROUPS, /** This real list is kept out of the smart lists. */ val hiddenFromSmartLists: Boolean = false, /** Tags the unnarrowed view carries, offered as filter chips. */ @@ -157,7 +159,8 @@ class TaskListViewModel @Inject constructor( showAddSubtaskRow = settings.showAddSubtaskRow, bottomAddBar = settings.bottomAddBar, sortOrder = settings.sortOrder, - collapsedSections = settings.collapsedSections, + grouping = settings.groupingFor(f), + collapsedGroups = settings.collapsedGroups, hiddenFromSmartLists = st.list?.id?.let { it in settings.hiddenFromSmartLists } ?: false, ) } else { @@ -291,8 +294,13 @@ class TaskListViewModel @Inject constructor( fun setHiddenFromSmartLists(listId: Long, hidden: Boolean) = viewModelScope.launch { settingsPrefs.setHiddenFromSmartLists(listId, hidden) } - fun setSectionCollapsed(section: TaskSection, collapsed: Boolean) = - viewModelScope.launch { settingsPrefs.setSectionCollapsed(section, collapsed) } + fun setGroupCollapsed(id: String, collapsed: Boolean) = + viewModelScope.launch { settingsPrefs.setGroupCollapsed(id, collapsed) } + + fun setGrouping(grouping: TaskGrouping) { + val f = filter.value ?: return + viewModelScope.launch { settingsPrefs.setGrouping(f, grouping) } + } fun setSortOrder(order: TaskSortOrder) = viewModelScope.launch { settingsPrefs.setSortOrder(order) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5211b6b..2452da2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -47,6 +47,21 @@ Priority Title Recently added + Group by + Due date + Overdue, today, upcoming + Day + A group for each day + Priority + Highest priority first + List + A group for each list + Tomorrow + High priority + Medium priority + Low priority + No priority + Unnamed list Clear Clear completed tasks? diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/TaskGroupingPrefsTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/TaskGroupingPrefsTest.kt new file mode 100644 index 0000000..317134d --- /dev/null +++ b/app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/TaskGroupingPrefsTest.kt @@ -0,0 +1,42 @@ +package de.jeanlucmakiola.agendula.data.prefs + +import com.google.common.truth.Truth.assertThat +import de.jeanlucmakiola.agendula.domain.SmartList +import de.jeanlucmakiola.agendula.domain.TaskFilter +import de.jeanlucmakiola.agendula.domain.TaskGrouping +import org.junit.jupiter.api.Test +import java.time.LocalDate + +class TaskGroupingPrefsTest { + + @Test + fun `groupings decode per view and keep only the current store's lists`() { + val decoded = decodeGroupings( + setOf("DAY|smart:UPCOMING", "PRIORITY|list:OWN:5", "LIST|list:PROVIDER:5", "DAY|tag:work", "BOGUS|saved:1", "DAY|"), + mode = "OWN", + ) + assertThat(decoded).containsExactly( + "smart:UPCOMING", TaskGrouping.DAY, + "list:5", TaskGrouping.PRIORITY, + "tag:work", TaskGrouping.DAY, + ) + } + + @Test + fun `a view without a stored grouping groups by due date`() { + val settings = Settings(groupings = mapOf("list:5" to TaskGrouping.PRIORITY)) + assertThat(settings.groupingFor(TaskFilter.OfList(5))).isEqualTo(TaskGrouping.PRIORITY) + assertThat(settings.groupingFor(TaskFilter.OfList(6))).isEqualTo(TaskGrouping.DUE) + assertThat(settings.groupingFor(TaskFilter.Smart(SmartList.ALL))).isEqualTo(TaskGrouping.DUE) + } + + @Test + fun `collapsed day groups that have passed are dropped`() { + val today = LocalDate.of(2026, 11, 17) + val kept = dropPastDays( + setOf("COMPLETED", "priority:LOW", "day:2026-11-16", "day:2026-11-18", "day:garbage"), + today, + ) + assertThat(kept).containsExactly("COMPLETED", "priority:LOW", "day:2026-11-18") + } +} diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/domain/TaskGroupsTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/domain/TaskGroupsTest.kt new file mode 100644 index 0000000..633e0ae --- /dev/null +++ b/app/src/test/java/de/jeanlucmakiola/agendula/domain/TaskGroupsTest.kt @@ -0,0 +1,119 @@ +package de.jeanlucmakiola.agendula.domain + +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Test +import java.time.LocalDate +import java.time.ZoneOffset +import kotlin.time.Instant + +class TaskGroupsTest { + + private val dayMs = 86_400_000L + private val todayStart = Instant.fromEpochMilliseconds(1000 * dayMs) + private val todayEnd = Instant.fromEpochMilliseconds(1001 * dayMs) + private val today = LocalDate.ofEpochDay(1000) + + private fun at(millis: Long) = Instant.fromEpochMilliseconds(millis) + + private fun groups(tasks: List, grouping: TaskGrouping, todayList: Boolean = false) = + TaskGroups.of(tasks, grouping, todayStart, todayEnd, todayList, ZoneOffset.UTC) + + private fun keys(tasks: List, grouping: TaskGrouping, todayList: Boolean = false) = + groups(tasks, grouping, todayList).map { it.key } + + @Test + fun `due grouping keeps the classic sections`() { + val tasks = listOf( + testTask(id = 1, due = at(1002 * dayMs)), + testTask(id = 2, due = at(1000 * dayMs - 1)), + testTask(id = 3), + testTask(id = 4, status = TaskStatus.COMPLETED), + testTask(id = 5, due = at(1000 * dayMs + 1)), + ) + assertThat(keys(tasks, TaskGrouping.DUE)).containsExactly( + TaskGroupKey.Section(TaskSection.OVERDUE), + TaskGroupKey.Section(TaskSection.TODAY), + TaskGroupKey.Section(TaskSection.UPCOMING), + TaskGroupKey.Section(TaskSection.NO_DATE), + TaskGroupKey.Section(TaskSection.COMPLETED), + ).inOrder() + } + + @Test + fun `day grouping splits upcoming into one group per day in date order`() { + val tasks = listOf( + testTask(id = 1, due = at(1003 * dayMs + 5)), + testTask(id = 2, due = at(1001 * dayMs + 5)), + testTask(id = 3, due = at(1003 * dayMs + 9)), + testTask(id = 4, due = at(1000 * dayMs + 5)), + testTask(id = 5, due = at(999 * dayMs)), + testTask(id = 6), + ) + val result = groups(tasks, TaskGrouping.DAY) + assertThat(result.map { it.key }).containsExactly( + TaskGroupKey.Section(TaskSection.OVERDUE), + TaskGroupKey.Section(TaskSection.TODAY), + TaskGroupKey.Day(today.plusDays(1)), + TaskGroupKey.Day(today.plusDays(3)), + TaskGroupKey.Section(TaskSection.NO_DATE), + ).inOrder() + assertThat(result[3].tasks.map { it.taskId }).containsExactly(1L, 3L).inOrder() + } + + @Test + fun `day grouping reads an all-day due as its calendar date`() { + val allDay = testTask(due = at(1002 * dayMs), isAllDay = true) + assertThat(keys(listOf(allDay), TaskGrouping.DAY)).containsExactly(TaskGroupKey.Day(today.plusDays(2))) + } + + @Test + fun `the Today list keeps open tasks under Today for date groupings`() { + val tasks = listOf(testTask(id = 1, start = at(1000 * dayMs + 1), due = at(1003 * dayMs))) + assertThat(keys(tasks, TaskGrouping.DAY, todayList = true)) + .containsExactly(TaskGroupKey.Section(TaskSection.TODAY)) + } + + @Test + fun `priority grouping runs from high to none and keeps completed last`() { + val tasks = listOf( + testTask(id = 1, priority = Priority.LOW), + testTask(id = 2, priority = Priority.NONE), + testTask(id = 3, priority = Priority.HIGH, status = TaskStatus.COMPLETED), + testTask(id = 4, priority = Priority.HIGH), + ) + assertThat(keys(tasks, TaskGrouping.PRIORITY)).containsExactly( + TaskGroupKey.OfPriority(Priority.HIGH), + TaskGroupKey.OfPriority(Priority.LOW), + TaskGroupKey.OfPriority(Priority.NONE), + TaskGroupKey.Section(TaskSection.COMPLETED), + ).inOrder() + } + + @Test + fun `list grouping orders lists by name and keeps the incoming order inside`() { + val tasks = listOf( + testTask(id = 1, listId = 2).copy(listName = "work"), + testTask(id = 2, listId = 1).copy(listName = "Home"), + testTask(id = 3, listId = 2).copy(listName = "work"), + ) + val result = groups(tasks, TaskGrouping.LIST) + assertThat(result.map { it.key }).containsExactly( + TaskGroupKey.OfList(1, "Home"), + TaskGroupKey.OfList(2, "work"), + ).inOrder() + assertThat(result[1].tasks.map { it.taskId }).containsExactly(1L, 3L).inOrder() + } + + @Test + fun `group ids stay stable and match the stored section names`() { + assertThat(TaskGroupKey.Section(TaskSection.COMPLETED).id).isEqualTo("COMPLETED") + assertThat(TaskGroupKey.Day(LocalDate.of(2026, 11, 20)).id).isEqualTo("day:2026-11-20") + assertThat(TaskGroupKey.OfPriority(Priority.HIGH).id).isEqualTo("priority:HIGH") + assertThat(TaskGroupKey.OfList(7, "x").id).isEqualTo("list:7") + } + + @Test + fun `empty input yields no groups`() { + TaskGrouping.entries.forEach { assertThat(groups(emptyList(), it)).isEmpty() } + } +}