Keep the default due where no date would show the task, tidy up (#107)
This commit is contained in:
1 parent
82f087a73c
commit
2e7b5cf3f8
8 files changed
+45
-34
No files matched your search
@@ -18,6 +18,7 @@ import de.jeanlucmakiola.agendula.domain.newTaskDates
|
||||
import de.jeanlucmakiola.agendula.domain.Task
|
||||
import de.jeanlucmakiola.agendula.domain.TaskFilter
|
||||
import de.jeanlucmakiola.agendula.domain.TaskForm
|
||||
import de.jeanlucmakiola.agendula.domain.calendarDate
|
||||
import de.jeanlucmakiola.agendula.domain.TaskFormField
|
||||
import de.jeanlucmakiola.agendula.domain.TaskReminder
|
||||
import de.jeanlucmakiola.agendula.domain.TaskSection
|
||||
@@ -176,8 +177,8 @@ data class Settings(
|
||||
title: String,
|
||||
listId: Long,
|
||||
parentId: Long? = null,
|
||||
today: LocalDate = LocalDate.now(),
|
||||
now: Instant = Clock.System.now(),
|
||||
today: LocalDate = now.calendarDate(allDay = false),
|
||||
): TaskForm {
|
||||
val dates = defaultNewTaskDates(today, now, subtask = parentId != null)
|
||||
return TaskForm(
|
||||
|
||||
@@ -129,6 +129,15 @@ data class Task(
|
||||
get() = if (occurrenceStart == null) "$taskId" else "$taskId@${occurrenceStart.toEpochMilliseconds()}"
|
||||
}
|
||||
|
||||
/** An open, unsaved task with only these dates, to ask a view whether it would show one. */
|
||||
fun openTaskDated(start: Instant?, due: Instant?, isAllDay: Boolean): Task = Task(
|
||||
taskId = 0L, listId = 0L, title = "", description = null, location = null, url = null,
|
||||
priority = Priority.NONE, status = TaskStatus.NEEDS_ACTION, percentComplete = null,
|
||||
start = start, due = due, isAllDay = isAllDay, timeZone = null, completedAt = null,
|
||||
listColor = 0, taskColor = null, listName = null, accountName = null, parentId = null,
|
||||
isRecurring = false, distanceFromCurrent = null, created = null, lastModified = null,
|
||||
)
|
||||
|
||||
/** Detail bundle: a task, its parent (if it's a subtask), and its direct children. */
|
||||
data class TaskDetail(
|
||||
val task: Task,
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package de.jeanlucmakiola.agendula.domain
|
||||
|
||||
import java.time.LocalDate
|
||||
import java.time.ZoneId
|
||||
import kotlin.time.Instant
|
||||
|
||||
/** The due date a new task starts with, counted from the day it is created. */
|
||||
@@ -42,7 +41,7 @@ data class NewTaskDates(val start: Instant?, val due: Instant?, val isAllDay: Bo
|
||||
* and the default [start]. Without a due date only [DefaultStartDate.TODAY]
|
||||
* applies, as a timed start at [now].
|
||||
*/
|
||||
fun newTaskDates(dueDate: LocalDate?, start: DefaultStartDate, today: LocalDate, now: Instant): NewTaskDates {
|
||||
fun datesForDue(dueDate: LocalDate?, start: DefaultStartDate, today: LocalDate, now: Instant): NewTaskDates {
|
||||
if (dueDate == null) {
|
||||
return NewTaskDates(start = now.takeIf { start == DefaultStartDate.TODAY }, due = null, isAllDay = false)
|
||||
}
|
||||
@@ -52,8 +51,8 @@ fun newTaskDates(dueDate: LocalDate?, start: DefaultStartDate, today: LocalDate,
|
||||
|
||||
/**
|
||||
* A new task's dates from the [due] and [start] defaults, or from [presetDue]
|
||||
* when there is no default due or the view the task is added from would not
|
||||
* [show] it with the default one.
|
||||
* when there is no default due, or when the view the task is added from would
|
||||
* [show] it with the preset but not with the default.
|
||||
*/
|
||||
fun newTaskDates(
|
||||
due: DefaultDueDate,
|
||||
@@ -63,9 +62,10 @@ fun newTaskDates(
|
||||
now: Instant,
|
||||
show: (NewTaskDates) -> Boolean = { true },
|
||||
): NewTaskDates {
|
||||
val preset = newTaskDates(presetDue, start, today, now)
|
||||
val preset = datesForDue(presetDue, start, today, now)
|
||||
val dueDate = due.dateFrom(today) ?: return preset
|
||||
return newTaskDates(dueDate, start, today, now).takeIf(show) ?: preset
|
||||
val dated = datesForDue(dueDate, start, today, now)
|
||||
return listOf(dated, preset).firstOrNull(show) ?: dated
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,17 +77,10 @@ fun NewTaskDates.showsIn(
|
||||
savedFilter: SavedFilter?,
|
||||
todayStart: Instant,
|
||||
todayEnd: Instant,
|
||||
zone: ZoneId = ZoneId.systemDefault(),
|
||||
): Boolean {
|
||||
val probe = Task(
|
||||
taskId = 0L, listId = 0L, title = "", description = null, location = null, url = null,
|
||||
priority = Priority.NONE, status = TaskStatus.NEEDS_ACTION, percentComplete = null,
|
||||
start = start, due = due, isAllDay = isAllDay, timeZone = null, completedAt = null,
|
||||
listColor = 0, taskColor = null, listName = null, accountName = null, parentId = null,
|
||||
isRecurring = false, distanceFromCurrent = null, created = null, lastModified = null,
|
||||
)
|
||||
val probe = openTaskDated(start, due, isAllDay)
|
||||
return when {
|
||||
savedFilter != null -> savedFilter.due.isEmpty() || dueWindowOf(probe, todayStart, todayEnd, zone) in savedFilter.due
|
||||
savedFilter != null -> savedFilter.due.isEmpty() || dueWindowOf(probe, todayStart, todayEnd) in savedFilter.due
|
||||
smartList != null -> TaskFiltering.matches(probe, TaskFilter.Smart(smartList), todayStart, todayEnd)
|
||||
else -> true
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ class TaskDetailViewModel @Inject constructor(
|
||||
runCatching { repository.duplicateTask(task.taskId, task.occurrenceStart) }.onSuccess(onCopied)
|
||||
}
|
||||
|
||||
/** Add a subtask (title only) under the currently-shown task. */
|
||||
/** Add a subtask by title under the currently-shown task, with the default start. */
|
||||
fun addSubtask(title: String) = viewModelScope.launch {
|
||||
val parent = (state.value as? TaskDetailUiState.Content)?.detail?.task ?: return@launch
|
||||
if (title.isBlank()) return@launch
|
||||
|
||||
@@ -189,10 +189,10 @@ class TaskEditViewModel @Inject constructor(
|
||||
this@TaskEditViewModel.settings = settings
|
||||
defaultFields = settings.defaultEditFields
|
||||
val lists = runCatching { repository.taskLists().first() }.getOrElse { emptyList() }
|
||||
val today = LocalDate.now()
|
||||
val filter = presetFilterId?.let { settingsPrefs.savedFilter(it).first() }
|
||||
val preset = filter?.newTaskPreset(today)
|
||||
val now = Clock.System.now()
|
||||
val today = now.calendarDate(allDay = false)
|
||||
val preset = filter?.newTaskPreset(today)
|
||||
val (todayStart, todayEnd) = DayWindow.today(now, ZoneId.systemDefault())
|
||||
val presetDue = preset?.dueDate ?: presetSmartList?.dueWindows?.presetDue(today)
|
||||
val dates = settings.defaultNewTaskDates(today, now, presetDue, subtask = parentId != null) {
|
||||
|
||||
@@ -18,7 +18,6 @@ import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.IntOffset
|
||||
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
|
||||
import de.jeanlucmakiola.agendula.domain.SmartList
|
||||
import de.jeanlucmakiola.agendula.domain.smartListNamed
|
||||
import de.jeanlucmakiola.agendula.domain.Task
|
||||
import kotlin.time.Instant
|
||||
|
||||
@@ -495,7 +495,7 @@
|
||||
<string name="settings_default_list_first">First available list</string>
|
||||
<string name="settings_add_subtask_row">Show \"add a subtask\" row</string>
|
||||
<string name="settings_add_subtask_row_hint">An add field at the end of an expanded task\'s subtasks</string>
|
||||
<!-- Unused since 1.4; kept until Weblate drops their translations. -->
|
||||
<!-- Unused since 1.4; kept while translations still carry them, as an extra translation fails the build. -->
|
||||
<string name="settings_default_start_today">Default start to today</string>
|
||||
<string name="settings_default_start_today_hint">Pre-fill a new task\'s start with today\'s date</string>
|
||||
<string name="settings_default_due_date">Default due date</string>
|
||||
|
||||
@@ -14,7 +14,7 @@ class NewTaskDefaultsTest {
|
||||
private val zone = ZoneId.systemDefault()
|
||||
|
||||
/** Today where the view tests run, as the smart lists read the device zone. */
|
||||
private val localToday = java.time.Instant.ofEpochMilli(now.toEpochMilliseconds()).atZone(zone).toLocalDate()
|
||||
private val localToday = now.calendarDate(allDay = false, zone = zone)
|
||||
|
||||
@Test
|
||||
fun `due defaults count from today`() {
|
||||
@@ -29,31 +29,31 @@ class NewTaskDefaultsTest {
|
||||
@Test
|
||||
fun `a start relative to the due date is all-day`() {
|
||||
val due = today.plusWeeks(2)
|
||||
val dates = newTaskDates(due, DefaultStartDate.WEEK_BEFORE_DUE, today, now)
|
||||
val dates = datesForDue(due, DefaultStartDate.WEEK_BEFORE_DUE, today, now)
|
||||
assertThat(dates).isEqualTo(NewTaskDates(allDayInstantOf(today.plusWeeks(1)), allDayInstantOf(due), true))
|
||||
assertThat(newTaskDates(due, DefaultStartDate.DUE_DATE, today, now).start).isEqualTo(allDayInstantOf(due))
|
||||
assertThat(newTaskDates(due, DefaultStartDate.DAY_BEFORE_DUE, today, now).start)
|
||||
assertThat(datesForDue(due, DefaultStartDate.DUE_DATE, today, now).start).isEqualTo(allDayInstantOf(due))
|
||||
assertThat(datesForDue(due, DefaultStartDate.DAY_BEFORE_DUE, today, now).start)
|
||||
.isEqualTo(allDayInstantOf(due.minusDays(1)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a start counted back from the due date never lands before today`() {
|
||||
val tomorrow = today.plusDays(1)
|
||||
assertThat(newTaskDates(tomorrow, DefaultStartDate.WEEK_BEFORE_DUE, today, now).start)
|
||||
assertThat(datesForDue(tomorrow, DefaultStartDate.WEEK_BEFORE_DUE, today, now).start)
|
||||
.isEqualTo(allDayInstantOf(today))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a start today never lands after the due date`() {
|
||||
val pastDue = today.minusDays(3)
|
||||
assertThat(newTaskDates(pastDue, DefaultStartDate.TODAY, today, now).start).isEqualTo(allDayInstantOf(pastDue))
|
||||
assertThat(datesForDue(pastDue, DefaultStartDate.TODAY, today, now).start).isEqualTo(allDayInstantOf(pastDue))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `without a due date only a start today applies`() {
|
||||
assertThat(newTaskDates(null, DefaultStartDate.TODAY, today, now)).isEqualTo(NewTaskDates(now, null, false))
|
||||
assertThat(datesForDue(null, DefaultStartDate.TODAY, today, now)).isEqualTo(NewTaskDates(now, null, false))
|
||||
DefaultStartDate.entries.filter { it.needsDue }.forEach {
|
||||
assertThat(newTaskDates(null, it, today, now)).isEqualTo(NewTaskDates(null, null, false))
|
||||
assertThat(datesForDue(null, it, today, now)).isEqualTo(NewTaskDates(null, null, false))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,13 +62,13 @@ class NewTaskDefaultsTest {
|
||||
val (start, end) = DayWindow.today(now, zone)
|
||||
val upcoming = SmartList.UPCOMING.dueWindows.presetDue(localToday)
|
||||
fun inUpcoming(due: DefaultDueDate) = newTaskDates(due, DefaultStartDate.NONE, upcoming, localToday, now) {
|
||||
it.showsIn(SmartList.UPCOMING, null, start, end, zone)
|
||||
it.showsIn(SmartList.UPCOMING, null, start, end)
|
||||
}.due
|
||||
assertThat(inUpcoming(DefaultDueDate.NEXT_WEEK)).isEqualTo(allDayInstantOf(localToday.plusWeeks(1)))
|
||||
assertThat(inUpcoming(DefaultDueDate.TODAY)).isEqualTo(allDayInstantOf(localToday.plusDays(1)))
|
||||
assertThat(inUpcoming(DefaultDueDate.NONE)).isEqualTo(allDayInstantOf(localToday.plusDays(1)))
|
||||
val noDate = newTaskDates(DefaultDueDate.TOMORROW, DefaultStartDate.NONE, null, localToday, now) {
|
||||
it.showsIn(SmartList.NO_DATE, null, start, end, zone)
|
||||
it.showsIn(SmartList.NO_DATE, null, start, end)
|
||||
}
|
||||
assertThat(noDate.due).isNull()
|
||||
}
|
||||
@@ -78,7 +78,7 @@ class NewTaskDefaultsTest {
|
||||
val (start, end) = DayWindow.today(now, zone)
|
||||
fun inToday(startDefault: DefaultStartDate) =
|
||||
newTaskDates(DefaultDueDate.NEXT_WEEK, startDefault, localToday, localToday, now) {
|
||||
it.showsIn(SmartList.TODAY, null, start, end, zone)
|
||||
it.showsIn(SmartList.TODAY, null, start, end)
|
||||
}.due
|
||||
assertThat(inToday(DefaultStartDate.TODAY)).isEqualTo(allDayInstantOf(localToday.plusWeeks(1)))
|
||||
assertThat(inToday(DefaultStartDate.NONE)).isEqualTo(allDayInstantOf(localToday))
|
||||
@@ -89,8 +89,17 @@ class NewTaskDefaultsTest {
|
||||
val (start, end) = DayWindow.today(now, zone)
|
||||
val filter = SavedFilter(id = 1L, name = "Soon", due = setOf(DueWindow.TODAY), listIds = setOf(9L))
|
||||
val dates = newTaskDates(DefaultDueDate.TODAY, DefaultStartDate.NONE, localToday, localToday, now)
|
||||
assertThat(dates.showsIn(null, filter, start, end, zone)).isTrue()
|
||||
assertThat(dates.showsIn(null, filter, start, end)).isTrue()
|
||||
val later = newTaskDates(DefaultDueDate.TOMORROW, DefaultStartDate.NONE, localToday, localToday, now)
|
||||
assertThat(later.showsIn(null, filter, start, end, zone)).isFalse()
|
||||
assertThat(later.showsIn(null, filter, start, end)).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a view no new task shows in keeps the default due`() {
|
||||
val (start, end) = DayWindow.today(now, zone)
|
||||
val dates = newTaskDates(DefaultDueDate.TOMORROW, DefaultStartDate.NONE, null, localToday, now) {
|
||||
it.showsIn(SmartList.OVERDUE, null, start, end)
|
||||
}
|
||||
assertThat(dates.due).isEqualTo(allDayInstantOf(localToday.plusDays(1)))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user