Apply task defaults to every quick add and the Upcoming list (#107)

This commit is contained in:
makiolaj committed 2026-11-16 19:20:15 +01:00
1 parent 5639124fb0
commit 8fa15c282f
10 files changed
+83 -44

No files matched your search

@@ -209,7 +209,7 @@ class MainActivity : ComponentActivity() {
): Intent {
val path = listOfNotNull(
listId?.toString(),
smartList?.let { if (it == SmartList.TODAY) "today" else it.name.lowercase() },
smartList?.name?.lowercase(),
filterId?.let { "filter$it" },
).joinToString("/")
return Intent(ACTION_NEW_TASK, path.ifEmpty { null }?.let { "agendula://newtask/$it".toUri() }, context, MainActivity::class.java)
@@ -14,9 +14,12 @@ import de.jeanlucmakiola.agendula.domain.DefaultDueDate
import de.jeanlucmakiola.agendula.domain.DefaultStartDate
import de.jeanlucmakiola.agendula.domain.NewTaskDates
import de.jeanlucmakiola.agendula.domain.SavedFilter
import de.jeanlucmakiola.agendula.domain.DueWindow
import de.jeanlucmakiola.agendula.domain.newTaskDates
import de.jeanlucmakiola.agendula.domain.newTaskDue
import de.jeanlucmakiola.agendula.domain.Task
import de.jeanlucmakiola.agendula.domain.TaskFilter
import de.jeanlucmakiola.agendula.domain.TaskForm
import de.jeanlucmakiola.agendula.domain.TaskFormField
import de.jeanlucmakiola.agendula.domain.TaskReminder
import de.jeanlucmakiola.agendula.domain.TaskSection
@@ -31,8 +34,10 @@ import de.jeanlucmakiola.floret.reminders.normalizeReminders
import de.jeanlucmakiola.floret.reminders.reminderLeadsFor
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.first
import java.time.DayOfWeek
import java.time.LocalDate
import kotlin.time.Clock
import kotlin.time.Instant
import kotlinx.coroutines.flow.map
import javax.inject.Inject
@@ -155,9 +160,14 @@ data class Settings(
fun hasOwnReminders(listId: Long, allDay: Boolean): Boolean =
listId in (if (allDay) perListAllDayReminderOverride else perListReminderOverride)
/** The dates a new task starts with where nothing presets its due date. */
fun defaultNewTaskDates(today: LocalDate, now: Instant): NewTaskDates =
newTaskDates(defaultDueDate.dateFrom(today), defaultStartDate, today, now)
/** The dates a new task starts with, added to a view of [viewWindows] that would show it due on [presetDue]. */
fun defaultNewTaskDates(
today: LocalDate,
now: Instant,
presetDue: LocalDate? = null,
viewWindows: Set<DueWindow> = emptySet(),
): NewTaskDates =
newTaskDates(newTaskDue(defaultDueDate, presetDue, viewWindows, today), defaultStartDate, today, now)
/** Whether [listId] has its own default reminders for timed or all-day tasks. */
fun hasOwnReminders(listId: Long): Boolean =
@@ -226,6 +236,19 @@ class SettingsPrefs @Inject constructor(
p[COLLAPSED_SECTIONS] = if (collapsed) current + section.name else current - section.name
}
/** A title-only task for [listId], optionally under [parentId], with the default dates. */
suspend fun quickTask(title: String, listId: Long, parentId: Long? = null): TaskForm {
val dates = settings.first().defaultNewTaskDates(LocalDate.now(), Clock.System.now())
return TaskForm(
title = title,
listId = listId,
parentId = parentId,
start = dates.start,
due = dates.due,
isAllDay = dates.isAllDay,
)
}
suspend fun setSortOrder(order: TaskSortOrder) = dataStore.edit { it[SORT_ORDER] = order.name }
suspend fun setSnoozeMinutes(minutes: Int) = dataStore.edit { it[SNOOZE_MINUTES] = minutes.coerceAtLeast(1) }
@@ -24,6 +24,13 @@ enum class DefaultStartDate {
val needsDue: Boolean get() = this != NONE && this != TODAY
}
/**
* A new task's due date: the default where a view of [viewWindows] still shows
* the task, else the [presetDue] that keeps it there.
*/
fun newTaskDue(default: DefaultDueDate, presetDue: LocalDate?, viewWindows: Set<DueWindow>, today: LocalDate): LocalDate? =
default.dateFrom(today)?.takeIf { viewWindows.admitsDue(it, today) } ?: presetDue
/** A new task's dates; whole-day defaults make it all-day once it has a due date. */
data class NewTaskDates(val start: Instant?, val due: Instant?, val isAllDay: Boolean)
@@ -79,6 +79,17 @@ fun dueWindowOf(date: LocalDate, today: LocalDate): DueWindow = when {
fun Set<DueWindow>.admitsDue(date: LocalDate, today: LocalDate): Boolean =
isEmpty() || dueWindowOf(date, today) in this
/**
* The due date that keeps a new task in a view of these windows: today, then
* tomorrow; a view that admits undated tasks, or only overdue ones, leaves it empty.
*/
fun Set<DueWindow>.presetDue(today: LocalDate): LocalDate? = when {
isEmpty() || DueWindow.NO_DATE in this -> null
DueWindow.TODAY in this -> today
DueWindow.UPCOMING in this -> today.plusDays(1)
else -> null
}
/** The due windows this smart list shows; empty for the lists that don't go by due date. */
val SmartList.dueWindows: Set<DueWindow>
get() = when (this) {
@@ -101,21 +112,13 @@ data class NewTaskPreset(
val priority: Priority = Priority.NONE,
)
/**
* The preset for a new task in this filter. Due prefers today, then tomorrow;
* a filter that admits undated tasks, or only overdue ones, leaves it empty.
*/
/** The preset for a new task in this filter; its due is [presetDue]. */
fun SavedFilter.newTaskPreset(today: LocalDate): NewTaskPreset = NewTaskPreset(
listIds = listIds,
tags = when {
tagMatch == TagMatch.ALL -> tags.sorted()
else -> listOfNotNull(tags.minOrNull())
},
dueDate = when {
due.isEmpty() || DueWindow.NO_DATE in due -> null
DueWindow.TODAY in due -> today
DueWindow.UPCOMING in due -> today.plusDays(1)
else -> null
},
dueDate = due.presetDue(today),
priority = minPriority,
)
@@ -7,7 +7,6 @@ import de.jeanlucmakiola.agendula.data.tasks.TasksRepository
import de.jeanlucmakiola.agendula.data.tasks.recoveringFromProviderFailure
import de.jeanlucmakiola.agendula.domain.Task
import de.jeanlucmakiola.agendula.domain.TaskDetail
import de.jeanlucmakiola.agendula.domain.TaskForm
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
@@ -98,10 +97,6 @@ class TaskDetailViewModel @Inject constructor(
fun addSubtask(title: String) = viewModelScope.launch {
val parent = (state.value as? TaskDetailUiState.Content)?.detail?.task ?: return@launch
if (title.isBlank()) return@launch
runCatching {
repository.createTask(
TaskForm(title = title.trim(), listId = parent.listId, parentId = parent.taskId),
)
}
runCatching { repository.createTask(settingsPrefs.quickTask(title.trim(), parent.listId, parent.taskId)) }
}
}
@@ -23,9 +23,8 @@ import java.time.ZoneId
import de.jeanlucmakiola.agendula.domain.populatedFields
import de.jeanlucmakiola.agendula.domain.htmlToPlainText
import de.jeanlucmakiola.agendula.domain.looksLikeHtml
import de.jeanlucmakiola.agendula.domain.admitsDue
import de.jeanlucmakiola.agendula.domain.dueWindows
import de.jeanlucmakiola.agendula.domain.newTaskDates
import de.jeanlucmakiola.agendula.domain.presetDue
import de.jeanlucmakiola.agendula.domain.newTaskPreset
import de.jeanlucmakiola.agendula.domain.normalizeTags
import de.jeanlucmakiola.agendula.domain.tagsInUse
@@ -160,9 +159,9 @@ class TaskEditViewModel @Inject constructor(
private val setAside = mutableListOf<SetAside>()
/**
* Start a fresh task, optionally pre-selecting a list / parent. A saved filter
* or [presetSmartList] it is added from presets what keeps it in that view, and
* the default due date only applies where the task would still show there.
* Start a fresh task, optionally pre-selecting a list / parent. Added from a
* saved filter or [presetSmartList], it takes the default due date where that
* view still shows it, else a due date that keeps it there.
*/
fun bindNew(
presetListId: Long? = null,
@@ -185,11 +184,8 @@ class TaskEditViewModel @Inject constructor(
val today = LocalDate.now()
val filter = presetFilterId?.let { settingsPrefs.savedFilter(it).first() }
val preset = filter?.newTaskPreset(today)
val presetDue = preset?.dueDate ?: today.takeIf { presetSmartList == SmartList.TODAY }
val viewWindows = filter?.due ?: presetSmartList?.dueWindows.orEmpty()
val dueDate = presetDue
?: settings.defaultDueDate.dateFrom(today)?.takeIf { viewWindows.admitsDue(it, today) }
val dates = newTaskDates(dueDate, settings.defaultStartDate, today, Clock.System.now())
val dates = settings.defaultNewTaskDates(today, Clock.System.now(), viewWindows.presetDue(today), viewWindows)
// A read-only or vanished default — or a widget's preset list, if it was
// since deleted or turned read-only — would give the task a list it
// cannot be saved to.
@@ -94,7 +94,7 @@ internal fun TaskFormScreen(
)
GroupedRow(
title = stringResource(R.string.settings_default_start_date),
summary = stringResource(defaultStartLabel(state.settings.defaultStartDate)),
summary = defaultStartSummary(state.settings.defaultStartDate, state.settings.defaultDueDate),
position = Position.Middle,
onClick = { showDefaultStart = true },
)
@@ -178,6 +178,17 @@ private fun defaultDueLabel(due: DefaultDueDate): Int = when (due) {
DefaultDueDate.NEXT_MONTH -> R.string.settings_default_due_next_month
}
/** The start's label, noting when no default due date gives it one to count from. */
@Composable
private fun defaultStartSummary(start: DefaultStartDate, due: DefaultDueDate): String {
val label = stringResource(defaultStartLabel(start))
return if (start.needsDue && due == DefaultDueDate.NONE) {
stringResource(R.string.settings_default_start_without_due, label)
} else {
label
}
}
private fun defaultStartLabel(start: DefaultStartDate): Int = when (start) {
DefaultStartDate.NONE -> R.string.settings_default_start_none
DefaultStartDate.TODAY -> R.string.settings_default_start_today_option
@@ -13,7 +13,6 @@ import de.jeanlucmakiola.agendula.domain.Task
import de.jeanlucmakiola.agendula.domain.TaskFilter
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.TaskSortOrder
@@ -40,9 +39,7 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import java.time.LocalDate
import javax.inject.Inject
import kotlin.time.Clock
sealed interface TaskListUiState {
data object Loading : TaskListUiState
@@ -319,12 +316,7 @@ class TaskListViewModel @Inject constructor(
/** Inline "add task" — a title into [listId], with the default dates. */
fun quickAdd(title: String, listId: Long) = viewModelScope.launch {
if (title.isBlank() || listId <= 0L) return@launch
val dates = settingsPrefs.settings.first().defaultNewTaskDates(LocalDate.now(), Clock.System.now())
runCatching {
repository.createTask(
TaskForm(title = title, listId = listId, start = dates.start, due = dates.due, isAllDay = dates.isAllDay),
)
}
runCatching { repository.createTask(settingsPrefs.quickTask(title, listId)) }
}
private val _listWriteFailure = MutableStateFlow<ListWriteFailure?>(null)
@@ -386,10 +378,6 @@ class TaskListViewModel @Inject constructor(
/** Inline "add subtask" from an expanded list group — files it under [parent]. */
fun quickAddSubtask(parent: Task, title: String) = viewModelScope.launch {
if (title.isBlank() || parent.listId <= 0L) return@launch
runCatching {
repository.createTask(
TaskForm(title = title.trim(), listId = parent.listId, parentId = parent.taskId),
)
}
runCatching { repository.createTask(settingsPrefs.quickTask(title.trim(), parent.listId, parent.taskId)) }
}
}
+2
View File
@@ -512,6 +512,8 @@
<string name="settings_default_start_day_before_due">A day before the due date</string>
<string name="settings_default_start_week_before_due">A week before the due date</string>
<string name="settings_default_start_needs_due">Only for a task with a due date</string>
<!-- %1$s is the chosen default start, e.g. "A day before the due date" -->
<string name="settings_default_start_without_due">%1$s · only for a task with a due date</string>
<string name="settings_bottom_add_bar">Bottom quick-add bar</string>
<string name="settings_bottom_add_bar_hint">Add tasks from a bar pinned to the bottom of a list, instead of the floating button</string>
@@ -50,4 +50,18 @@ class NewTaskDefaultsTest {
assertThat(newTaskDates(null, it, today, now)).isEqualTo(NewTaskDates(null, null, false))
}
}
@Test
fun `the default due applies where the view still shows the task, else its preset`() {
val upcoming = SmartList.UPCOMING.dueWindows
assertThat(newTaskDue(DefaultDueDate.NEXT_WEEK, upcoming.presetDue(today), upcoming, today))
.isEqualTo(today.plusWeeks(1))
assertThat(newTaskDue(DefaultDueDate.TODAY, upcoming.presetDue(today), upcoming, today))
.isEqualTo(today.plusDays(1))
assertThat(newTaskDue(DefaultDueDate.NONE, upcoming.presetDue(today), upcoming, today))
.isEqualTo(today.plusDays(1))
val noDate = SmartList.NO_DATE.dueWindows
assertThat(newTaskDue(DefaultDueDate.TOMORROW, noDate.presetDue(today), noDate, today)).isNull()
assertThat(newTaskDue(DefaultDueDate.TOMORROW, null, emptySet(), today)).isEqualTo(today.plusDays(1))
}
}