Quick-add: the last token wins, a list prefix needs two letters, a lone @time is today's or tomorrow's, and an add no block shows says where it went (#186)

This commit is contained in:
makiolaj committed 2026-11-27 18:41:35 +01:00
1 parent 11d26a23b6
commit 7220e5b418
10 files changed
+181 -41

No files matched your search

+6 -4
View File
@@ -58,14 +58,16 @@ All notable changes to this project are documented here. The format follows
per view in Edit view, which says where its tasks go. A task added there
goes to the list the view's scope keeps to, otherwise to the default list,
and takes the tags, priority and due date that keep it in the view; one
that still falls outside says so, with a button to open its list. The
that still falls outside says so, with a button to open its list, and one
no block of the view shows says where it went. The
bar's expand button opens the full editor with what was typed (#59, #186).
- Quick-add understands `@date` and `+list` while typing, marks them and
shows what they stand for: `@today`, `@tomorrow`, `@nextweek`, `@fri`,
`@3d`, `@2w`, `@1m`, `@2026-11-20` or the short date of your language,
each with an optional time (`@fri@14:30`, `@tomorrow@9am`); `+Errands`, a
unique start of a list's name (`+err`), or `+"Shared household"`. The
words are translated, and the English ones always work (#186).
each with an optional time (`@fri@14:30`, `@tomorrow@9am`), or a time alone
(`@14:30`: today, or tomorrow once it has passed); `+Errands`, a unique
start of a list's name (`+err`), or `+"Shared household"`. The last of each
counts. The words are translated, and the English ones always work (#186).
- A new task from a list or filter opened within a view's scope takes that
scope's list, tags and due date too (#59).
@@ -127,7 +127,7 @@ data class ShortDateFormat(val dayFirst: Boolean, val separator: Char, val yearF
}
}
/** What quick-add reads its input against: [today], where weeks start, the words and the short date form. */
/** What quick-add reads its input against: [today] and [now], where weeks start, the words and the short date form. */
data class QuickAddContext(
val today: LocalDate,
val weekStart: DayOfWeek,
@@ -135,6 +135,8 @@ data class QuickAddContext(
val shortDate: ShortDateFormat = ShortDateFormat(dayFirst = false, separator = '/'),
/** The lists a `+list` can name: the ones a task can be added to. */
val lists: List<TaskList> = emptyList(),
/** The time now, which tells whether a lone `@time` is today's or tomorrow's. */
val now: LocalTime = LocalTime.MIDNIGHT,
)
/** A due day, at [time] or all day when it has none. */
@@ -158,10 +160,12 @@ data class QuickAddParse(
*
* A token starts a word: at the start of the input or after whitespace, so
* `bob@example.com` and `C++` stay text. It runs to the next whitespace. The
* first date and the first list are taken; a later one, or one that names
* last date and the last list are taken; an earlier one, or one that names
* nothing, stays in the title as typed.
*
* `@date[@time]`, the date one of:
* `@date[@time]`, or `@time` alone for today at that time (tomorrow once it has
* passed). A token is read as a date first, and as a lone time only when it is
* none: `@20.11.` is a day, `@20` is 20:00, `@3d` three days. The date is one of:
* - `today`, `tomorrow`, `nextweek` (the first day of next week);
* - a weekday, `fri` or `friday`: the next one, a week ahead on that day itself;
* - `3d`, `2w`, `1m`: that many days, weeks or months ahead (up to 999);
@@ -174,16 +178,16 @@ data class QuickAddParse(
* `+list` names a list the task can be added to, ignoring case:
* - `+"Shared household"`: exactly that name, spaces and all;
* - `+Errands`: the list of that name, also with its spaces left out
* (`+sharedhousehold`), or else the one list whose name, spaces left out, starts with it (`+shared`).
* (`+sharedhousehold`), or else the one list whose name, spaces left out, starts with
* it (`+shared`), given at least two letters.
*
* Two lists of that name, or several starting with it, name none.
*/
object QuickAddSyntax {
fun parse(input: String, context: QuickAddContext): QuickAddParse {
val tokens = ArrayList<QuickToken>(2)
var due: QuickDue? = null
var list: TaskList? = null
var due: Pair<QuickDue, QuickToken>? = null
var list: Pair<TaskList, QuickToken>? = null
var i = 0
while (i < input.length) {
if (input[i].isWhitespace()) {
@@ -192,27 +196,25 @@ object QuickAddSyntax {
}
val start = i
var end = wordEnd(input, start)
if (input[start] == '+' && list == null) {
if (input[start] == '+') {
val quoted = quotedList(input, start, context.lists)
if (quoted != null) {
list = quoted.first
end = quoted.second
tokens += QuickToken(start, end, QuickToken.Kind.LIST)
list = quoted.first to QuickToken(start, end, QuickToken.Kind.LIST)
} else {
listNamed(input.substring(start + 1, end), context.lists)?.let {
list = it
tokens += QuickToken(start, end, QuickToken.Kind.LIST)
list = it to QuickToken(start, end, QuickToken.Kind.LIST)
}
}
} else if (input[start] == '@' && due == null) {
} else if (input[start] == '@') {
parseDue(input.substring(start + 1, end), context)?.let {
due = it
tokens += QuickToken(start, end, QuickToken.Kind.DATE)
due = it to QuickToken(start, end, QuickToken.Kind.DATE)
}
}
i = end
}
return QuickAddParse(title = stripped(input, tokens), due = due, list = list, tokens = tokens)
val tokens = listOfNotNull(due?.second, list?.second).sortedBy { it.start }
return QuickAddParse(title = stripped(input, tokens), due = due?.first, list = list?.first, tokens = tokens)
}
private fun wordEnd(input: String, from: Int): Int {
@@ -240,17 +242,26 @@ object QuickAddSyntax {
if (key.isEmpty()) return null
val exact = lists.filter { it.name.trim().lowercase(Locale.ROOT) == key || compact(it.name) == key }
if (exact.isNotEmpty()) return exact.singleOrNull()
if (key.length < MIN_PREFIX) return null
return lists.filter { compact(it.name).startsWith(key) }.singleOrNull()
}
/** The fewest letters that name a list by the start of its name. */
private const val MIN_PREFIX = 2
private fun compact(name: String) = name.filterNot(Char::isWhitespace).lowercase(Locale.ROOT)
/** The due [token] (what follows the `@`) stands for, or `null` when it reads as none. */
fun parseDue(token: String, context: QuickAddContext): QuickDue? {
val at = token.indexOf('@')
val datePart = if (at < 0) token else token.substring(0, at)
val time = if (at < 0) null else parseTime(token.substring(at + 1)) ?: return null
val date = parseDate(datePart.lowercase(Locale.ROOT), context) ?: return null
if (at < 0) {
parseDate(token.lowercase(Locale.ROOT), context)?.let { return QuickDue(it) }
val time = parseTime(token) ?: return null
val day = if (time.isAfter(context.now)) context.today else context.today.plusDays(1)
return QuickDue(day, time)
}
val time = parseTime(token.substring(at + 1)) ?: return null
val date = parseDate(token.substring(0, at).lowercase(Locale.ROOT), context) ?: return null
return QuickDue(date, time)
}
@@ -350,9 +350,12 @@ fun ListsScreen(
val failure = writeFailure
val receipt = undo
val gone = removed
// An add within the view is only told of when no block shows the task.
val added = quickAddReceipt?.takeIf { it.viewId == content?.view?.id }
?.let { r -> remember(r) { r.takeUnless { it is QuickAddReceipt.Added && viewModel.shows(it.viewId ?: View.HOME_ID, it.task) } } }
val quiet = failure == null && !viewSaveFailed && gone == null && receipt == null
val notShown = (added as? QuickAddReceipt.NotShown)?.takeIf { quiet }
val addedUnseen = (added as? QuickAddReceipt.Added)?.takeIf { quiet }
val addFailed = added is QuickAddReceipt.Failed && quiet
SnackChip(
visible = failure != null || viewSaveFailed || gone != null || added != null || receipt != null,
@@ -361,18 +364,19 @@ fun ListsScreen(
viewSaveFailed -> stringResource(R.string.view_save_failed)
gone != null -> stringResource(R.string.block_removed)
notShown != null -> stringResource(R.string.quick_add_not_shown, notShown.listName)
addedUnseen != null -> stringResource(R.string.quick_add_added, addedUnseen.listName)
addFailed -> stringResource(R.string.edit_save_failed)
receipt is UndoReceipt.Completed -> stringResource(R.string.task_completed)
else -> stringResource(R.string.task_deleted)
},
actionLabel = when {
failure != null || viewSaveFailed || addFailed -> null
failure != null || viewSaveFailed || addFailed || addedUnseen != null -> null
notShown != null -> stringResource(R.string.quick_add_open)
gone != null || receipt != null -> stringResource(R.string.undo)
else -> null
},
onAction = when {
failure != null || viewSaveFailed || addFailed -> null
failure != null || viewSaveFailed || addFailed || addedUnseen != null -> null
gone != null -> ({ viewModel.undoRemove() })
notShown != null -> ({
quickAdd.dismissReceipt()
@@ -36,6 +36,7 @@ import de.jeanlucmakiola.agendula.domain.TaskFilter
import de.jeanlucmakiola.agendula.domain.TaskList
import de.jeanlucmakiola.agendula.domain.View
import de.jeanlucmakiola.agendula.domain.appliedScope
import de.jeanlucmakiola.agendula.domain.TaskSorting
import de.jeanlucmakiola.agendula.domain.contains
import de.jeanlucmakiola.agendula.domain.counts
import de.jeanlucmakiola.agendula.domain.forBlock
@@ -302,6 +303,37 @@ internal fun homeContent(
)
}
/** What home was last laid out from, to ask whether it would show a task. */
internal data class HomeInputs(
val view: View,
val lists: List<TaskList>,
val openTasks: List<Task>,
val completedTasks: List<Task>,
val hidden: Set<Long>,
val savedFilters: List<SavedFilter>,
val clock: RuleClock,
val upcomingEnd: Instant?,
) {
/**
* Whether a block of the view would show [task] as a row: the view's task
* list, or a preview with it among the tasks it shows. A count or a ring
* that only goes up doesn't show it. [task] counts once, whether these
* tasks already hold it or not.
*/
fun shows(task: Task): Boolean {
val open = (openTasks.filterNot { it.taskId == task.taskId } + task).sortedWith(TaskSorting.default())
val content = homeContent(view, listOf(view), lists, open, completedTasks, hidden, savedFilters, clock, upcomingEnd)
if (content.previews.values.any { tasks -> tasks.any { it.taskId == task.taskId } }) return true
val block = view.blocks.lastOrNull { it.type == BlockType.TASK_LIST && it.id !in content.goneSources } ?: return false
val source = block.source ?: return false
val listIds = lists.mapTo(HashSet()) { it.id }
val filters = savedFilters.map { it.withListsIn(listIds) }.associateBy { it.id }
val within = view.appliedScope(listIds).forBlock(block)
return within.admits(task, clock) &&
source.counts(task, SourceContext(clock, upcomingEnd, filters), within.hiddenFromSmart(hidden))
}
}
/**
* The lists, tags and filters within [scope], each with its open count. A scope
* that keeps tasks to some lists shows only those.
@@ -412,6 +444,9 @@ class ListsViewModel @Inject constructor(
val now = Clock.System.now()
val zone = ZoneId.systemDefault()
val (todayStart, todayEnd) = DayWindow.today(now, zone)
val clock = RuleClock(todayStart, todayEnd, zone, weekStart)
val upcomingEnd = upcomingRange.end(now, zone)
inputs = HomeInputs(view, lists, openTasks, completedTasks, hidden, filters, clock, upcomingEnd)
homeContent(
view = view,
views = views,
@@ -420,14 +455,20 @@ class ListsViewModel @Inject constructor(
completedTasks = completedTasks,
hidden = hidden,
savedFilters = filters,
clock = RuleClock(todayStart, todayEnd, zone, weekStart),
upcomingEnd = upcomingRange.end(now, zone),
clock = clock,
upcomingEnd = upcomingEnd,
)
}.recoveringFromProviderFailure { ListsUiState.Failure }
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), ListsUiState.Loading)
fun switchView(viewId: Long) = session.switchTo(viewId)
@Volatile
private var inputs: HomeInputs? = null
/** Whether view [viewId], as last shown, shows [task] in one of its blocks (see [HomeInputs.shows]). */
fun shows(viewId: Long, task: Task): Boolean = inputs?.takeIf { it.view.id == viewId }?.shows(task) == true
private val _viewSaveFailed = MutableStateFlow(false)
/** Set for a moment when a view couldn't be stored: the views are another version's. */
@@ -90,6 +90,8 @@ import de.jeanlucmakiola.floret.identity.byDensity
import de.jeanlucmakiola.floret.identity.rememberReduceMotion
import de.jeanlucmakiola.floret.locale.currentLocale
import java.time.LocalDate
import java.time.LocalTime
import java.time.temporal.ChronoUnit
/**
* The quick-add bar at the bottom of a view or list: a field reading the `@date`
@@ -143,7 +145,9 @@ private fun rememberQuickAddContext(lists: List<TaskList>): QuickAddContext {
}
val shortDate = remember(locale) { ShortDateFormat.of(locale) }
val today = LocalDate.now()
return remember(today, weekStart, keywords, shortDate, lists) { QuickAddContext(today, weekStart, keywords, shortDate, lists) }
// To the minute: a lone @time turns to tomorrow's as its minute passes.
val now = LocalTime.now().truncatedTo(ChronoUnit.MINUTES)
return remember(today, now, weekStart, keywords, shortDate, lists) { QuickAddContext(today, weekStart, keywords, shortDate, lists, now) }
}
private val KEYWORD_RESOURCES: Map<String, Int> = mapOf(
@@ -333,6 +337,7 @@ fun QuickAddReceiptChip(receipt: QuickAddReceipt, onOpenList: (Long) -> Unit) {
actionLabel = stringResource(R.string.quick_add_open),
onAction = { onOpenList(receipt.listId) },
)
is QuickAddReceipt.Added -> SnackChip(visible = true, message = stringResource(R.string.quick_add_added, receipt.listName))
is QuickAddReceipt.Failed -> SnackChip(visible = true, message = stringResource(R.string.edit_save_failed))
}
}
@@ -11,6 +11,7 @@ import de.jeanlucmakiola.agendula.domain.QuickAddParse
import de.jeanlucmakiola.agendula.domain.QuickDue
import de.jeanlucmakiola.agendula.domain.Rule
import de.jeanlucmakiola.agendula.domain.RuleClock
import de.jeanlucmakiola.agendula.domain.Task
import de.jeanlucmakiola.agendula.domain.TaskFilter
import de.jeanlucmakiola.agendula.domain.TaskFiltering
import de.jeanlucmakiola.agendula.domain.TaskList
@@ -58,6 +59,9 @@ sealed interface QuickAddReceipt {
/** Added to list [listId], [listName], which the view doesn't show. */
class NotShown(val listId: Long, val listName: String, override val viewId: Long?) : QuickAddReceipt
/** Added to [listName] within the view; [task] as it was added, to tell whether a block shows it. */
class Added(val listName: String, val task: Task, override val viewId: Long?) : QuickAddReceipt
class Failed(override val viewId: Long?) : QuickAddReceipt
}
@@ -111,7 +115,8 @@ class QuickAddViewModel @Inject constructor(
/**
* Adds what [parse], read from [typed], holds within [scope] (`null` for none),
* from a screen showing [shows], as its new-task action would; says so when
* the task falls outside the scope or what is shown. The write runs in the application scope,
* the task falls outside the scope or what is shown, and otherwise hands the
* screen the task to tell whether it shows it ([QuickAddReceipt.Added]). The write runs in the application scope,
* so leaving the screen right after cannot cancel it; when it fails, an empty
* bar gets [typed] back. [viewId] is the view it is added in, if any.
*/
@@ -129,17 +134,18 @@ class QuickAddViewModel @Inject constructor(
val origin = settingsPrefs.newTaskOrigin(shows)
val form = settings.quickAddTask(parse, scope, writable, clock, now, origin)
?: return@runCatching QuickAddReceipt.Failed(viewId)
repository.createTask(form)
val task = form.asOpenTask()
val id = repository.createTask(form)
val task = form.asOpenTask().copy(taskId = id, title = form.title, created = now)
val inScope = scope == null || scope.matches(task, clock)
val inShown = shows == null || TaskFiltering.matches(
task, shows, todayStart, todayEnd,
saved = origin.filter, upcomingEnd = settings.upcomingRange.end(now, zone), ruleClock = clock,
)
val listName = writable.firstOrNull { it.id == form.listId }?.name.orEmpty()
if (inScope && inShown) {
null
QuickAddReceipt.Added(listName, task, viewId)
} else {
QuickAddReceipt.NotShown(form.listId, writable.firstOrNull { it.id == form.listId }?.name.orEmpty(), viewId)
QuickAddReceipt.NotShown(form.listId, listName, viewId)
}
}.getOrElse { QuickAddReceipt.Failed(viewId) }
}
@@ -111,6 +111,7 @@ import de.jeanlucmakiola.agendula.R
import de.jeanlucmakiola.agendula.domain.Rule
import de.jeanlucmakiola.agendula.ui.quickadd.NewTaskSeed
import de.jeanlucmakiola.agendula.ui.quickadd.QuickAddBar
import de.jeanlucmakiola.agendula.ui.quickadd.QuickAddReceipt
import de.jeanlucmakiola.agendula.ui.quickadd.QuickAddReceiptChip
import de.jeanlucmakiola.agendula.ui.quickadd.QuickAddViewModel
import de.jeanlucmakiola.agendula.domain.Priority
@@ -419,7 +420,8 @@ fun TaskListScreen(
) {
// One chip, in priority order: undo, a refused list write, a quick add, a search outcome, the read-only hint.
val failure = listWriteFailure
val added = quickAddReceipt
// The list shows what was added within it, so only the rest is told of.
val added = quickAddReceipt?.takeUnless { it is QuickAddReceipt.Added }
when {
undo != null -> SnackChip(
visible = true,
+2
View File
@@ -1213,6 +1213,8 @@
<!-- %1$s is a list's name. -->
<string name="quick_add_not_shown">Added to %1$s, not shown in this view</string>
<string name="quick_add_open">Open</string>
<!-- %1$s is a list's name; said when nothing on the screen shows the task just added. -->
<string name="quick_add_added">Added to %1$s</string>
<string name="quick_add_expand">Open in the full editor</string>
<!-- %1$s is a day, maybe with a time. -->
<string name="quick_add_due_chip">Due %1$s</string>
@@ -32,7 +32,8 @@ class QuickAddSyntaxTest {
shortDate: ShortDateFormat = german,
keywords: QuickAddKeywords = QuickAddKeywords.English,
lists: List<TaskList> = this.lists,
) = QuickAddSyntax.parse(input, QuickAddContext(on, weekStart, keywords, shortDate, lists))
now: LocalTime = LocalTime.of(10, 0),
) = QuickAddSyntax.parse(input, QuickAddContext(on, weekStart, keywords, shortDate, lists, now))
private fun due(input: String, on: LocalDate = today, shortDate: ShortDateFormat = german) = parse(input, on, shortDate = shortDate).due
@@ -77,11 +78,23 @@ class QuickAddSyntaxTest {
}
@Test
fun `the first date and the first list are taken, later ones stay text`() {
fun `the last date and the last list are taken, earlier ones stay text`() {
val result = parse("a @today b @tomorrow +work c +errands")
assertThat(result.due).isEqualTo(QuickDue(today))
assertThat(result.due).isEqualTo(QuickDue(today.plusDays(1)))
assertThat(result.list).isEqualTo(errands)
assertThat(result.title).isEqualTo("a @today b +work c")
assertThat(result.tokens).containsExactly(
QuickToken(11, 20, QuickToken.Kind.DATE),
QuickToken(29, 37, QuickToken.Kind.LIST),
).inOrder()
}
@Test
fun `a later token that reads as nothing leaves the earlier one taken`() {
val result = parse("a @tomorrow +work b @someday +garden")
assertThat(result.due).isEqualTo(QuickDue(today.plusDays(1)))
assertThat(result.list).isEqualTo(work)
assertThat(result.title).isEqualTo("a b @tomorrow c +errands")
assertThat(result.title).isEqualTo("a b @someday +garden")
}
@Test
@@ -213,9 +226,29 @@ class QuickAddSyntaxTest {
}
@Test
fun `a time alone is no date`() {
assertThat(due("@14:30")).isNull()
assertThat(due("@9")).isNull()
fun `a time alone is today's, or tomorrow's once it has passed`() {
// It is 10:00.
assertThat(due("@14:30")).isEqualTo(QuickDue(today, LocalTime.of(14, 30)))
assertThat(due("@9pm")).isEqualTo(QuickDue(today, LocalTime.of(21, 0)))
assertThat(due("@20")).isEqualTo(QuickDue(today, LocalTime.of(20, 0)))
assertThat(due("@9")).isEqualTo(QuickDue(today.plusDays(1), LocalTime.of(9, 0)))
assertThat(due("@10")).isEqualTo(QuickDue(today.plusDays(1), LocalTime.of(10, 0)))
assertThat(due("@10:01")).isEqualTo(QuickDue(today, LocalTime.of(10, 1)))
assertThat(parse("@0", on = LocalDate.of(2026, 12, 31)).due).isEqualTo(QuickDue(LocalDate.of(2027, 1, 1), LocalTime.MIDNIGHT))
assertThat(parse("Ring @14:30").title).isEqualTo("Ring")
}
@Test
fun `a date reads before a lone time`() {
// Day and month in the locale's form, not 20 o'clock with a dot.
assertThat(due("@20.11")).isEqualTo(QuickDue(LocalDate.of(2027, 11, 20)))
assertThat(due("@12/1", shortDate = us)).isEqualTo(QuickDue(LocalDate.of(2026, 12, 1)))
assertThat(due("@3d")).isEqualTo(QuickDue(LocalDate.of(2026, 11, 30)))
assertThat(due("@1m")).isEqualTo(QuickDue(LocalDate.of(2026, 12, 27)))
// Neither a date nor a time.
assertThat(due("@14.30")).isNull()
assertThat(due("@25")).isNull()
assertThat(due("@2026")).isNull()
}
@Test
@@ -241,6 +274,15 @@ class QuickAddSyntaxTest {
assertThat(parse("+shop").list).isEqualTo(shopping)
}
@Test
fun `a start of one letter names nothing, a one-letter name does`() {
assertThat(parse("+e").list).isNull()
assertThat(parse("+er").list).isEqualTo(errands)
val x = list(9, "X")
assertThat(parse("+x", lists = lists + x).list).isEqualTo(x)
assertThat(parse("+X", lists = listOf(x, list(10, "Xmas"))).list).isEqualTo(x)
}
@Test
fun `a start several names share, or no name, stays text`() {
assertThat(parse("+sh").list).isNull()
@@ -192,4 +192,29 @@ class HomePreviewsTest {
// Today's work task, then the even upcoming ones (list 1) due by Sunday.
assertThat(week.due).containsExactly(today, 1, today.plusDays(2), 1)
}
private fun inputs(view: View, open: List<Task> = upcoming.map { it.copy(isAllDay = true) } + todayWork + todayHome) =
HomeInputs(view, listOf(work, home), open, listOf(doneToday), emptySet(), emptyList(), clock, upcomingEnd = null)
@Test
fun `a new task shows when the view's task list holds it`() {
val view = scoped(null, listOf(Block(1, BlockType.TASK_LIST, BlockSource.List(1))))
assertThat(inputs(view).shows(testTask(id = 99, listId = 1))).isTrue()
assertThat(inputs(view).shows(testTask(id = 99, listId = 2))).isFalse()
val ignoring = scoped(Rule.HasTag("x"), listOf(Block(1, BlockType.TASK_LIST, BlockSource.List(1), ignoreScope = true)))
assertThat(inputs(ignoring).shows(testTask(id = 99, listId = 1))).isTrue()
val scopedOut = scoped(Rule.HasTag("x"), listOf(Block(1, BlockType.TASK_LIST, BlockSource.List(1))))
assertThat(inputs(scopedOut).shows(testTask(id = 99, listId = 1))).isFalse()
}
@Test
fun `a new task shows when a preview has it among its first, not when only a count goes up`() {
val soon = testTask(id = 99, listId = 1, due = dueOn(today.plusDays(1)), isAllDay = true)
val late = testTask(id = 98, listId = 1, due = dueOn(today.plusDays(30)), isAllDay = true)
assertThat(inputs(View.home()).shows(soon)).isTrue()
assertThat(inputs(View.home()).shows(late)).isFalse()
assertThat(inputs(View.home()).shows(testTask(id = 97, listId = 1))).isFalse()
// Already read back from the store, it still counts once.
assertThat(inputs(View.home(), open = upcoming.map { it.copy(isAllDay = true) } + soon).shows(soon)).isTrue()
}
}