The task editor takes a view's scope presets and a due time from its route (#59, #186)

This commit is contained in:
makiolaj committed 2026-11-27 17:57:40 +01:00
1 parent be1b270e5c
commit 3c5ed514df
3 files changed
+90 -18

No files matched your search

@@ -39,6 +39,12 @@ import de.jeanlucmakiola.agendula.domain.dueWindows
import de.jeanlucmakiola.agendula.domain.showsIn
import de.jeanlucmakiola.floret.time.DayWindow
import de.jeanlucmakiola.agendula.domain.presetDue
import de.jeanlucmakiola.agendula.domain.appliedScope
import de.jeanlucmakiola.agendula.domain.datesForTimedDue
import de.jeanlucmakiola.agendula.domain.defaultTaskList
import de.jeanlucmakiola.agendula.domain.dueVerdict
import de.jeanlucmakiola.agendula.domain.openTaskDated
import de.jeanlucmakiola.agendula.domain.scopePreset
import de.jeanlucmakiola.agendula.domain.newTaskPreset
import de.jeanlucmakiola.agendula.domain.normalizeTags
import de.jeanlucmakiola.agendula.domain.tagsInUse
@@ -221,7 +227,9 @@ class TaskEditViewModel @Inject constructor(
/**
* 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.
* view still shows it, else a due date that keeps it there. Added within the
* scope of view [scopeViewId], it also takes what keeps it in that scope (see
* [scopePreset]). [presetDueTime] puts a time on [presetDueDate].
*/
fun bindNew(
presetListId: Long? = null,
@@ -232,6 +240,8 @@ class TaskEditViewModel @Inject constructor(
presetSmartList: SmartList? = null,
presetDueDate: LocalDate? = null,
presetRule: Rule? = null,
scopeViewId: Long? = null,
presetDueTime: LocalTime? = null,
) {
if (bound) return
bound = true
@@ -253,25 +263,30 @@ class TaskEditViewModel @Inject constructor(
val (todayStart, todayEnd) = DayWindow.today(now, zone)
val upcomingEnd = settings.upcomingRange.end(now, zone)
val ruleClock = RuleClock(todayStart, todayEnd, zone, settingsPrefs.firstDayOfWeek.first())
val presetDue = presetDueDate ?: preset?.dueDate ?: presetSmartList?.dueWindows?.presetDue(today)
val dates = settings.defaultNewTaskDates(today, now, presetDue, subtask = parentId != null) { dates ->
// A day's list wants the task on that day, whatever the default due date.
if (presetDueDate != null) {
dates.due?.calendarDate(dates.isAllDay, zone) == presetDueDate
} else {
dates.showsIn(presetSmartList, filter, todayStart, todayEnd, upcomingEnd, ruleClock)
val scope = scopeViewId?.let { id -> settingsPrefs.views.first().firstOrNull { it.id == id } }
?.appliedScope(lists.mapTo(HashSet()) { it.id })?.rule
val scopePreset = scope.scopePreset(ruleClock)
val presetDue = presetDueDate ?: preset?.dueDate ?: scopePreset.dueDate ?: presetSmartList?.dueWindows?.presetDue(today)
val dates = if (presetDueDate != null && presetDueTime != null) {
datesForTimedDue(presetDueDate, presetDueTime, settings.defaultStartDate, today, now, zone)
} else {
settings.defaultNewTaskDates(today, now, presetDue, subtask = parentId != null) { dates ->
// A day's list wants the task on that day, whatever the default due date.
if (presetDueDate != null) {
dates.due?.calendarDate(dates.isAllDay, zone) == presetDueDate
} else {
dates.showsIn(presetSmartList, filter, todayStart, todayEnd, upcomingEnd, ruleClock) &&
(scope == null || scope.dueVerdict(openTaskDated(dates.start, dates.due, dates.isAllDay), ruleClock) != false)
}
}
}
// 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.
val writable = lists.filter { it.acceptsWrites }
val filterLists = preset?.listIds.orEmpty()
val defaultList = presetListId?.takeIf { id -> writable.any { it.id == id } }
?: settings.defaultListId?.takeIf { id -> writable.any { it.id == id } && (filterLists.isEmpty() || id in filterLists) }
?: writable.firstOrNull { it.id in filterLists }?.id
?: writable.firstOrNull { !it.isLocal }?.id
?: writable.firstOrNull()?.id
?: scopePreset.listId?.takeIf { id -> writable.any { it.id == id } }
?: defaultTaskList(writable, settings.defaultListId, within = preset?.listIds.orEmpty())
defaultStart = dates.start?.let { start ->
DefaultStart(
at = start,
@@ -291,13 +306,13 @@ class TaskEditViewModel @Inject constructor(
due = dates.due,
isAllDay = dates.isAllDay,
untimedEnds = repository.supportsUntimedEnds(),
priority = preset?.priority ?: Priority.NONE,
priority = maxOf(preset?.priority ?: Priority.NONE, scopePreset.priority),
parentId = parentId,
lists = lists,
repeatFromAvailable = repository.supportsRepeatFromCompletion(),
parentCandidates = loadParents(defaultList, selfId = null),
title = initialTitle.orEmpty(),
tags = (listOfNotNull(presetTag) + preset?.tags.orEmpty()).distinct(),
tags = (listOfNotNull(presetTag) + preset?.tags.orEmpty() + scopePreset.tags).distinct(),
).withDefaultReminders(),
).also { baseline = it.toForm().copy(title = "") }
}
@@ -210,7 +210,8 @@ object Dest {
/**
* The create / edit form. `taskId` set ([NO_ID] otherwise) ⇒ edit; for a new
* task, `presetListId` / `parentId` optionally seed the form, and `filterId`
* (or `smartList`) fills it so the task lands in that view.
* (or `smartList`) fills it so the task lands in that view; `view` adds the
* presets of that view's scope, and `dueTime` puts a time on `due`.
*/
object TaskEdit {
const val ARG_TASK_ID = "taskId"
@@ -223,6 +224,8 @@ object Dest {
const val ARG_SMART_LIST = "smartList"
const val ARG_DUE = "due"
const val ARG_RULE = "rule"
const val ARG_VIEW = "view"
const val ARG_DUE_TIME = "dueTime"
const val route =
"taskEdit?$ARG_TASK_ID={$ARG_TASK_ID}" +
"&$ARG_PRESET_LIST_ID={$ARG_PRESET_LIST_ID}" +
@@ -233,7 +236,9 @@ object Dest {
"&$ARG_FILTER_ID={$ARG_FILTER_ID}" +
"&$ARG_SMART_LIST={$ARG_SMART_LIST}" +
"&$ARG_DUE={$ARG_DUE}" +
"&$ARG_RULE={$ARG_RULE}"
"&$ARG_RULE={$ARG_RULE}" +
"&$ARG_VIEW={$ARG_VIEW}" +
"&$ARG_DUE_TIME={$ARG_DUE_TIME}"
val arguments = listOf(
navArgument(ARG_TASK_ID) { type = NavType.LongType; defaultValue = NO_ID },
@@ -246,6 +251,8 @@ object Dest {
navArgument(ARG_SMART_LIST) { type = NavType.StringType; nullable = true; defaultValue = null },
navArgument(ARG_DUE) { type = NavType.StringType; nullable = true; defaultValue = null },
navArgument(ARG_RULE) { type = NavType.StringType; nullable = true; defaultValue = null },
navArgument(ARG_VIEW) { type = NavType.LongType; defaultValue = NO_ID },
navArgument(ARG_DUE_TIME) { type = NavType.StringType; nullable = true; defaultValue = null },
)
fun buildEdit(taskId: Long, occurrenceStart: Long? = null): String =
@@ -261,6 +268,8 @@ object Dest {
smartList: SmartList? = null,
due: java.time.LocalDate? = null,
rule: Rule? = null,
scopeViewId: Long? = null,
dueTime: java.time.LocalTime? = null,
): String {
val params = buildList {
if (presetListId != null) add("$ARG_PRESET_LIST_ID=$presetListId")
@@ -271,6 +280,8 @@ object Dest {
if (smartList != null) add("$ARG_SMART_LIST=${smartList.name}")
if (due != null) add("$ARG_DUE=$due")
if (rule != null) add("$ARG_RULE=${android.net.Uri.encode(RuleCodec.encode(rule, ROUTE_STORE))}")
if (scopeViewId != null) add("$ARG_VIEW=$scopeViewId")
if (due != null && dueTime != null) add("$ARG_DUE_TIME=$dueTime")
}
return if (params.isEmpty()) "taskEdit" else "taskEdit?${params.joinToString("&")}"
}
@@ -12,6 +12,11 @@ import de.jeanlucmakiola.agendula.data.tasks.ProviderResolver
import de.jeanlucmakiola.agendula.data.tasks.TasksRepository
import de.jeanlucmakiola.agendula.domain.DefaultDueDate
import de.jeanlucmakiola.agendula.domain.DefaultStartDate
import de.jeanlucmakiola.agendula.domain.DueSpec
import de.jeanlucmakiola.agendula.domain.DueWindow
import de.jeanlucmakiola.agendula.domain.Rule
import de.jeanlucmakiola.agendula.domain.View
import de.jeanlucmakiola.agendula.domain.ViewIcon
import de.jeanlucmakiola.agendula.domain.Priority
import de.jeanlucmakiola.agendula.domain.SmartList
import de.jeanlucmakiola.agendula.domain.allDayInstantOf
@@ -30,6 +35,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.UnconfinedTestDispatcher
@@ -118,6 +124,42 @@ class TaskEditViewModelTest {
assertThat(vm.state.value.start).isEqualTo(allDayInstantOf(day.plusDays(1)))
}
@Test
fun `a new task in a scoped view takes the scope's list, tags and due`() = runTest {
repository.lists = listOf(list(1, "Work"), list(2, "Home"))
val prefs = settingsPrefs().apply { setDefaultListId(2) }
val scope = Rule.AllOf(listOf(Rule.InList(setOf(1L)), Rule.HasTag("x"), Rule.Due(DueSpec.Window(DueWindow.TODAY))))
val viewId = checkNotNull(prefs.saveView(View(0, "Work", ViewIcon.WORK, scope, emptyList())))
val vm = TaskEditViewModel(repository, prefs)
val today = aroundToday { vm.bindNew(presetTag = "y", scopeViewId = viewId) }
// The views are read off the main thread.
val state = vm.state.first { !it.loading }
assertThat(state.listId).isEqualTo(1L)
assertThat(state.tags).containsExactly("y", "x").inOrder()
assertThat(state.due).isIn(today.map { allDayInstantOf(it) })
}
@Test
fun `a list picked for the task wins over the scope's`() = runTest {
repository.lists = listOf(list(1, "Work"), list(2, "Home"))
val prefs = settingsPrefs()
val viewId = checkNotNull(prefs.saveView(View(0, "Work", ViewIcon.WORK, Rule.InList(setOf(1L)), emptyList())))
val vm = TaskEditViewModel(repository, prefs).apply { bindNew(presetListId = 2, scopeViewId = viewId) }
assertThat(vm.state.first { !it.loading }.listId).isEqualTo(2L)
}
@Test
fun `a due time from quick-add makes the task timed`() = runTest {
val vm = TaskEditViewModel(repository, settingsPrefs())
val day = LocalDate.now().plusDays(2)
vm.bindNew(presetDueDate = day, presetDueTime = LocalTime.of(14, 30))
assertThat(vm.state.value.due).isEqualTo(localToInstant(day, LocalTime.of(14, 30)))
assertThat(vm.state.value.isAllDay).isFalse()
}
@Test
fun `a new subtask takes no default due`() = runTest {
val prefs = settingsPrefs().apply { setDefaultDueDate(DefaultDueDate.TOMORROW) }
@@ -420,6 +462,9 @@ class TaskEditViewModelTest {
assertThat(sub.state.value.repeatFromOffered).isFalse()
}
private fun list(id: Long, name: String) =
TaskList(id, name, 0, "me", "bitfire.at.davdroid", isSynced = true, isVisible = true, owner = null)
private fun task(start: Instant?, due: Instant?) = Task(
taskId = TASK_ID,
listId = LIST_ID,
@@ -451,11 +496,12 @@ class TaskEditViewModelTest {
var reminders: List<TaskReminder> = emptyList()
var saved: TaskForm? = null
var untimedEnds = true
var lists: List<TaskList> = emptyList()
override fun supportsUntimedEnds() = untimedEnds
override fun supportsRepeatFromCompletion() = true
override fun taskLists() = flowOf(emptyList<TaskList>())
override fun taskLists() = flowOf(lists)
override fun tasks(filter: TaskFilter): Flow<List<Task>> = flowOf(emptyList())
override fun taskDetail(taskId: Long, occurrenceStart: Instant?) =
flowOf(task?.let { TaskDetail(it, emptyList()) })