One completed section: All shows completed tasks, Clear everywhere, Today's tight header (#140)

This commit is contained in:
makiolaj committed 2026-11-27 14:34:39 +01:00
1 parent 922b3a3107
commit 14bb0d3eb3
4 files changed
+116 -33

No files matched your search

@@ -1,15 +1,10 @@
package de.jeanlucmakiola.agendula.ui.tasklist
import androidx.activity.compose.BackHandler
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.VisibilityThreshold
import androidx.compose.animation.core.spring
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.text.BasicTextField
@@ -123,7 +118,6 @@ 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.TaskSearch
import de.jeanlucmakiola.agendula.domain.TaskGroup
import de.jeanlucmakiola.agendula.domain.TaskGroupKey
@@ -136,7 +130,6 @@ import de.jeanlucmakiola.agendula.ui.common.ignoringEdgeSwipes
import de.jeanlucmakiola.agendula.ui.common.priorityAccent
import de.jeanlucmakiola.agendula.ui.lists.ListEditorSheet
import de.jeanlucmakiola.agendula.ui.lists.listWriteFailureMessage
import de.jeanlucmakiola.agendula.ui.lists.ListWriteFailure
import de.jeanlucmakiola.agendula.ui.views.ScopeChipRow
import de.jeanlucmakiola.agendula.ui.lists.smartListLabel
import de.jeanlucmakiola.agendula.ui.views.viewName
@@ -352,7 +345,7 @@ fun TaskListScreen(
TaskListUiState.Failure -> CenteredMessage(stringResource(R.string.task_list_failure), inner)
is TaskListUiState.Content -> TaskListBody(
s, filter, inner, onOpenTask, onRequestDelete, viewModel,
onClearCompleted = if (listId != null && list?.isReadOnly != true) ({ confirmClear = true }) else null,
onClearCompleted = if (s.clearableCompleted().isNotEmpty()) ({ confirmClear = true }) else null,
)
}
}
@@ -441,20 +434,11 @@ fun TaskListScreen(
)
}
if (confirmClear && listId != null) {
val count = content?.tasks?.count { it.isCompleted } ?: 0
AlertDialog(
onDismissRequest = { confirmClear = false },
title = { Text(stringResource(R.string.clear_completed_title)) },
text = { Text(pluralStringResource(R.plurals.clear_completed_body, count, count)) },
confirmButton = {
TextButton(onClick = { confirmClear = false; viewModel.clearCompleted(listId) }) {
Text(stringResource(R.string.clear_completed_confirm))
}
},
dismissButton = {
TextButton(onClick = { confirmClear = false }) { Text(stringResource(R.string.dialog_cancel)) }
},
if (confirmClear && content != null) {
ClearCompletedDialog(
count = content.clearableCompleted().size,
onConfirm = viewModel::clearCompleted,
onDismiss = { confirmClear = false },
)
}
@@ -1380,8 +1364,14 @@ private fun CollapsibleSectionHeader(
color = MaterialTheme.colorScheme.surface,
modifier = Modifier.fillMaxWidth(),
) {
// Clear's 48dp touch target would otherwise push the rows 24dp further down
// than under a header without it; it takes that room from the padding (#140).
Row(
modifier = Modifier.padding(start = 28.dp, end = 20.dp, top = 16.dp, bottom = 8.dp),
modifier = if (onClear == null) {
Modifier.padding(start = 28.dp, end = 20.dp, top = 16.dp, bottom = 8.dp)
} else {
Modifier.padding(start = 28.dp, end = 20.dp, top = 4.dp)
},
verticalAlignment = Alignment.CenterVertically,
) {
Text(
@@ -1409,6 +1399,24 @@ private fun CollapsibleSectionHeader(
}
}
/** Asks before Clear deletes the [count] completed tasks of a Completed section. */
@Composable
internal fun ClearCompletedDialog(count: Int, onConfirm: () -> Unit, onDismiss: () -> Unit) {
AlertDialog(
onDismissRequest = onDismiss,
title = { Text(stringResource(R.string.clear_completed_title)) },
text = { Text(pluralStringResource(R.plurals.clear_completed_body, count, count)) },
confirmButton = {
TextButton(onClick = { onDismiss(); onConfirm() }) {
Text(stringResource(R.string.clear_completed_confirm))
}
},
dismissButton = {
TextButton(onClick = onDismiss) { Text(stringResource(R.string.dialog_cancel)) }
},
)
}
/** Icon + message for an empty list — the Calendula agenda-empty pattern. */
@Composable
internal fun EmptyState(text: String, icon: ImageVector = Icons.Rounded.Checklist) {
@@ -1552,6 +1560,7 @@ private fun titleFor(filter: TaskFilter, name: String?): String = when (filter)
is TaskFilter.OfLists -> error("OfLists has no task-list screen")
is TaskFilter.Tagged -> filter.tag
is TaskFilter.Smart -> stringResource(smartListLabel(filter.list))
is TaskFilter.DueOn -> rememberDayFormat(long = true).format(filter.date, LocalDate.now())
}
@Composable
@@ -153,8 +153,8 @@ data class ScopeChip(val viewId: Long, val name: String, val icon: ViewIcon, val
/**
* What a task screen shows: [filter]'s tasks, within the scope of view
* [scopeViewId] when set. [withCompleted] true adds the completed tasks to an
* All list, false leaves them out of any; `null` keeps what the filter shows.
* [scopeViewId] when set. [withCompleted] false leaves the completed tasks out;
* otherwise they close the list in their own section, All's included (#140).
* [sort] replaces the app's sort order when set.
*/
data class TaskListBinding(
@@ -241,14 +241,14 @@ class TaskListViewModel @Inject constructor(
val f = b.filter
val source: Flow<FilteredTasks> = when (f) {
is TaskFilter.Saved -> repository.filteredTasks(f)
is TaskFilter.Smart -> if (b.withCompleted == true && f.list == SmartList.ALL) {
is TaskFilter.Smart -> if (b.withCompleted != false && f.list == SmartList.ALL) {
combine(repository.tasks(f), repository.tasks(TaskFilter.Smart(SmartList.COMPLETED))) { open, done ->
FilteredTasks(open + done)
}
} else {
repository.tasks(f).map { FilteredTasks(it) }
}
is TaskFilter.OfList, is TaskFilter.Tagged -> repository.tasks(f).map { FilteredTasks(it) }
is TaskFilter.OfList, is TaskFilter.Tagged, is TaskFilter.DueOn -> repository.tasks(f).map { FilteredTasks(it) }
// Several lists at once only exists for the widget, which never routes here.
is TaskFilter.OfLists -> error("OfLists has no task-list screen")
}
@@ -516,10 +516,9 @@ class TaskListViewModel @Inject constructor(
* own, a series that is closed as a whole goes whole, and a parent that still
* has open subtasks stays — deleting it would take them along.
*/
fun clearCompleted(listId: Long) {
val done = (state.value as? TaskListUiState.Content)?.tasks
?.filter { it.listId == listId && it.isCompleted && it.subtaskDone == it.subtaskTotal }
.orEmpty()
/** Deletes the completed tasks this screen shows that [clearableCompleted] lets go. */
fun clearCompleted() {
val done = (state.value as? TaskListUiState.Content)?.clearableCompleted().orEmpty()
appScope.launch {
done.forEach { task ->
val wholeSeries = task.isOccurrence && task.taskId == task.seriesId
@@ -730,3 +729,10 @@ class TaskListViewModel @Inject constructor(
}
}
}
/**
* The completed tasks a Clear in the Completed section deletes: those shown that
* can be written and have no open subtask left.
*/
fun TaskListUiState.Content.clearableCompleted(): List<Task> =
tasks.filter { it.isCompleted && !it.isReadOnly && it.subtaskDone == it.subtaskTotal }
+2 -2
View File
@@ -74,8 +74,8 @@
<string name="clear_completed_action">Clear</string>
<string name="clear_completed_title">Clear completed tasks?</string>
<plurals name="clear_completed_body">
<item quantity="one">%1$d completed task in this list will be deleted.</item>
<item quantity="other">%1$d completed tasks in this list will be deleted.</item>
<item quantity="one">%1$d completed task shown here will be deleted.</item>
<item quantity="other">%1$d completed tasks shown here will be deleted.</item>
</plurals>
<string name="clear_completed_confirm">Delete</string>
<string name="undo">Undo</string>
@@ -0,0 +1,68 @@
package de.jeanlucmakiola.agendula.ui.tasklist
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.agendula.domain.SmartList
import de.jeanlucmakiola.agendula.domain.TaskEnd
import de.jeanlucmakiola.agendula.domain.TaskFilter
import de.jeanlucmakiola.agendula.domain.TaskFiltering
import de.jeanlucmakiola.agendula.domain.TaskGroupKey
import de.jeanlucmakiola.agendula.domain.TaskGroups
import de.jeanlucmakiola.agendula.domain.TaskGrouping
import de.jeanlucmakiola.agendula.domain.TaskStatus
import de.jeanlucmakiola.agendula.domain.testTask
import org.junit.jupiter.api.Test
import java.time.LocalDate
import java.time.LocalTime
import java.time.ZoneId
import kotlin.time.Instant
import kotlin.time.toKotlinInstant
/** The one Completed section Today, All, the lists and the task list block share (#140). */
class CompletedSectionTest {
private val done = TaskStatus.COMPLETED
@Test
fun `clear takes the completed tasks shown that can go`() {
val content = TaskListUiState.Content(
tasks = listOf(
testTask(id = 1),
testTask(id = 2, status = done),
testTask(id = 3, status = done).copy(isReadOnly = true),
testTask(id = 4, status = done).copy(subtaskDone = 1, subtaskTotal = 2),
testTask(id = 5, status = done, listId = 2).copy(subtaskDone = 2, subtaskTotal = 2),
testTask(id = 6, status = TaskStatus.CANCELLED),
),
)
assertThat(content.clearableCompleted().map { it.taskId }).containsExactly(2L, 5L)
}
@Test
fun `completed tasks close any list in their own section`() {
val tasks = listOf(testTask(id = 1, status = done), testTask(id = 2), testTask(id = 3, status = done))
TaskGrouping.entries.forEach { grouping ->
val groups = TaskGroups.of(tasks, grouping, Instant.DISTANT_PAST, Instant.DISTANT_PAST)
assertThat(groups.last().key).isEqualTo(TaskGroupKey.COMPLETED)
assertThat(groups.last().tasks.map { it.taskId }).containsExactly(1L, 3L).inOrder()
assertThat(groups.dropLast(1).flatMap { it.tasks }.none { it.isCompleted }).isTrue()
}
}
@Test
fun `a day's list holds what is due that day, open or done`() {
val zone = ZoneId.systemDefault()
val day = LocalDate.of(2026, 11, 26)
fun on(date: LocalDate) = date.atTime(LocalTime.NOON).atZone(zone).toInstant().toKotlinInstant()
val filter = TaskFilter.DueOn(day)
val matching = listOf(
testTask(id = 1, due = on(day)),
testTask(id = 2, due = on(day), status = done),
testTask(id = 3, due = on(day.plusDays(1))),
testTask(id = 4),
).filter { TaskFiltering.matches(it, filter, Instant.DISTANT_PAST, Instant.DISTANT_PAST) }
assertThat(matching.map { it.taskId }).containsExactly(1L, 2L)
assertThat(matching.all { it.dateOf(TaskEnd.DUE, zone) == day }).isTrue()
assertThat(TaskGroups.offeredFor(filter)).doesNotContain(TaskGrouping.DAY)
assertThat(TaskGroups.offeredFor(TaskFilter.Smart(SmartList.ALL))).contains(TaskGrouping.DAY)
}
}