From a1c43d5bb276ab9606277b16f0e47cca57ce9aff Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Fri, 27 Nov 2026 15:12:13 +0100 Subject: [PATCH] =?UTF-8?q?Templates=20name=20an=20unnamed=20filter=20and?= =?UTF-8?q?=20Next=20actions=20asks=20for=20its=20list;=20the=20simple=20e?= =?UTF-8?q?ditor's=20tags=20get=20Other=20tag=E2=80=A6=20(#154,=20#172)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 +- .../agendula/ui/filters/FilterEditScreen.kt | 5 +- .../ui/filters/FilterEditViewModel.kt | 18 ++- .../agendula/ui/filters/RuleDraft.kt | 19 ++- .../agendula/ui/filters/RuleEditScreen.kt | 5 +- .../agendula/ui/filters/RuleEditViewModel.kt | 18 ++- .../agendula/ui/filters/RuleEditor.kt | 7 +- .../agendula/ui/filters/RulePickers.kt | 128 +++++++++++++----- app/src/main/res/values/strings.xml | 7 +- .../agendula/ui/filters/RuleDraftTest.kt | 9 +- .../ui/filters/RuleEditViewModelTest.kt | 17 ++- 11 files changed, 164 insertions(+), 74 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b62054..3b5a444 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,8 +31,9 @@ All notable changes to this project are documented here. The format follows *any of*, each of which can be negated; a filter the simple editor can't show opens there (#172). - A new filter can start from a template: *Next actions (GTD)*, *This week*, - *This month*, *High priority*, *Waiting for* or *No date*. A template only - fills in the rules (#154). + *This month*, *High priority*, *Waiting for* or *No date*. It fills in the + rules and, if the filter has none yet, its name; *Next actions* asks which + list holds them. A tag no task carries yet can be typed in (#154, #172). - A view's scope is edited in the same editor, from *Edit view*. A tile or preview whose list or filter was deleted can show *Custom rules* instead, which can be saved as a filter (#59, #172). diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/FilterEditScreen.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/FilterEditScreen.kt index e948166..a0e096e 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/FilterEditScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/FilterEditScreen.kt @@ -86,9 +86,10 @@ fun FilterEditScreen( } if (draft?.choosingTemplate == true) { - TemplatePicker( + TemplateChooser( + lists = lists, title = stringResource(R.string.filter_new_title), - onPick = viewModel::startFrom, + onStart = viewModel::startFrom, onDismiss = onBack, ) } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/FilterEditViewModel.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/FilterEditViewModel.kt index f153fef..9487bdb 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/FilterEditViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/FilterEditViewModel.kt @@ -80,14 +80,22 @@ class FilterEditViewModel @Inject constructor( _draft.update { it?.copy(rules = transform(it.rules)) } } - /** Starts the new filter from [template]'s rules, or from none; its lists and tags are matched once they are read. */ - fun startFrom(template: FilterTemplate?) { - // The picker closes on the first pick; a later tap while the lists are read is ignored. + /** + * Starts the new filter from [template]'s rules, or from none; *Next actions* + * with [nextList]. An unnamed filter takes the template's [name]. + */ + fun startFrom(template: FilterTemplate?, nextList: Long? = null, name: String? = null) { + // The picker closes on the first pick; a later tap while the tags are read is ignored. if (_draft.value?.choosingTemplate != true) return - _draft.update { it?.copy(choosingTemplate = false) } + _draft.update { draft -> + draft?.copy( + filter = if (name != null && draft.filter.name.isBlank()) draft.filter.copy(name = name) else draft.filter, + choosingTemplate = false, + ) + } if (template == null) return viewModelScope.launch { - val form = repository.templateForm(template) + val form = repository.templateForm(template, nextList) _draft.update { it?.copy(rules = RuleDraft.of(form)) } } } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RuleDraft.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RuleDraft.kt index 72f7169..454dd8b 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RuleDraft.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RuleDraft.kt @@ -7,7 +7,6 @@ import de.jeanlucmakiola.agendula.domain.Priority import de.jeanlucmakiola.agendula.domain.Rule import de.jeanlucmakiola.agendula.domain.SmartList import de.jeanlucmakiola.agendula.domain.TaskFilter -import de.jeanlucmakiola.agendula.domain.TaskList import de.jeanlucmakiola.agendula.domain.tagsInUse import kotlinx.coroutines.flow.first @@ -54,11 +53,10 @@ data class RuleDraft(val rule: Rule, val simple: SimpleRule?, private val left: } } -/** [template]'s form against the lists and tags in use as read now; ones that can't be read count as none. */ -internal suspend fun TasksRepository.templateForm(template: FilterTemplate): SimpleRule { - val lists = runCatching { taskLists().first() }.getOrDefault(emptyList()) +/** [template]'s form against the tags in use as read now (none when they can't be), [nextList] for *Next actions*. */ +internal suspend fun TasksRepository.templateForm(template: FilterTemplate, nextList: Long?): SimpleRule { val tags = runCatching { tagsInUse(tasks(TaskFilter.Smart(SmartList.ALL)).first()) }.getOrDefault(emptyList()) - return template.form(lists, tags) + return template.form(nextList, tags) } /** The rules a new filter can start from (#154); the user changes them as they like. */ @@ -72,16 +70,16 @@ enum class FilterTemplate { ; /** - * The template's rules. Lists and tags are matched by name, ignoring case, - * to [lists] and [tags] (the ones in use); a tag not in use yet is taken as - * written, a list that doesn't exist is left out. + * The template's rules. *Next actions* is [nextList] or the next-action tag, + * the tag alone without a list. Tags are matched by name, ignoring case, to + * [tags] (the ones in use); one not in use yet is taken as written. */ - fun form(lists: List, tags: List): SimpleRule { + fun form(nextList: Long?, tags: List): SimpleRule { fun tag(name: String) = tags.firstOrNull { it.equals(name, ignoreCase = true) } ?: name return when (this) { NEXT_ACTIONS -> SimpleRule( match = Match.ANY, - listIds = lists.filter { it.name.trim().equals(NEXT_LIST, ignoreCase = true) }.mapTo(HashSet()) { it.id }, + listIds = setOfNotNull(nextList), tags = setOf(tag(NEXT_ACTION_TAG)), ) THIS_WEEK -> SimpleRule(due = setOf(DueSpec.Window(DueWindow.OVERDUE), DueSpec.ThisWeek)) @@ -93,7 +91,6 @@ enum class FilterTemplate { } private companion object { - const val NEXT_LIST = "Next" const val NEXT_ACTION_TAG = "nextAction" const val WAITING_TAG = "waiting" } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RuleEditScreen.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RuleEditScreen.kt index 6923052..fa93fdb 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RuleEditScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RuleEditScreen.kt @@ -91,9 +91,10 @@ fun RuleEditScreen( } if (state?.choosingTemplate == true) { - TemplatePicker( + TemplateChooser( + lists = lists, title = title, - onPick = viewModel::startFrom, + onStart = viewModel::startFrom, onDismiss = onBack, ) } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RuleEditViewModel.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RuleEditViewModel.kt index 85442e2..df2e40b 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RuleEditViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RuleEditViewModel.kt @@ -108,14 +108,22 @@ class RuleEditViewModel @Inject constructor( _state.update { it?.copy(rules = transform(it.rules)) } } - /** Starts from [template]'s rules, or from none; its lists and tags are matched once they are read. */ - fun startFrom(template: FilterTemplate?) { - // The picker closes on the first pick; a later tap while the lists are read is ignored. + /** + * Starts from [template]'s rules, or from none; *Next actions* with + * [nextList]. [name] goes to an unnamed *Save as filter*. + */ + fun startFrom(template: FilterTemplate?, nextList: Long? = null, name: String? = null) { + // The picker closes on the first pick; a later tap while the tags are read is ignored. if (_state.value?.choosingTemplate != true) return - _state.update { it?.copy(choosingTemplate = false) } + _state.update { state -> + state?.copy( + filterName = if (name != null && state.filterName.isBlank()) name else state.filterName, + choosingTemplate = false, + ) + } if (template == null) return viewModelScope.launch { - val form = repository.templateForm(template) + val form = repository.templateForm(template, nextList) _state.update { it?.copy(rules = RuleDraft.of(form)) } } } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RuleEditor.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RuleEditor.kt index 32b2235..c60d017 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RuleEditor.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RuleEditor.kt @@ -133,24 +133,25 @@ private fun SimpleSections( val tags = remember(tagsInUse, form.tags) { (tagsInUse + form.tags).distinct().sortedWith(String.CASE_INSENSITIVE_ORDER) } - if (tags.isNotEmpty()) { + run { NegatableHeader(stringResource(R.string.filter_section_tags), form.tagsNegated, shown = form.tags.isNotEmpty()) { negated -> onChange { it.copy(tagsNegated = negated) } } ChoiceRow( title = stringResource(R.string.filter_any_tag), - position = positionOf(0, tags.size + 1), + position = positionOf(0, tags.size + 2), selected = form.tags.isEmpty(), onClick = { onChange { it.copy(tags = emptySet(), tagsNegated = false) } }, ) tags.forEachIndexed { index, tag -> ChoiceRow( title = tag, - position = positionOf(index + 1, tags.size + 1), + position = positionOf(index + 1, tags.size + 2), selected = tag in form.tags, onClick = { onChange { it.copy(tags = it.tags.toggle(tag)) } }, ) } + OtherTagRow(position = positionOf(tags.size + 1, tags.size + 2), onAdd = { tag -> onChange { it.copy(tags = it.tags + tag) } }) if (form.tags.size > 1) { Spacer(Modifier.height(8.dp)) GroupedSwitchRow( diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RulePickers.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RulePickers.kt index daa5959..8208a08 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RulePickers.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/filters/RulePickers.kt @@ -230,36 +230,51 @@ internal fun TagPicker(tags: List, selected: String?, onSelect: (String) onClick = { pick(tag) }, ) } - var expanded by rememberSaveable { mutableStateOf(options.isEmpty()) } - var typed by rememberSaveable { mutableStateOf("") } - CustomExpandRow( - title = stringResource(R.string.filter_tag_other), - position = positionOf(options.size, count), - expanded = expanded, - onExpandedChange = { expanded = it }, - ) { - GroupedSurface(position = Position.Bottom, modifier = Modifier.padding(horizontal = GroupedListInset)) { - Row( - modifier = Modifier.padding(start = 16.dp, end = 8.dp), - verticalAlignment = Alignment.CenterVertically, - ) { - InlineTextField( - value = typed, - onValueChange = { typed = it }, - placeholder = stringResource(R.string.filter_tag_other_hint), - capitalization = KeyboardCapitalization.None, - imeAction = ImeAction.Done, - onImeAction = { typed.trim().takeIf { it.isNotEmpty() }?.let(pick) }, - modifier = Modifier.weight(1f).padding(vertical = 12.dp), - ) - TextButton( - onClick = { pick(typed.trim()) }, - enabled = typed.isNotBlank(), - ) { Text(stringResource(R.string.reminder_custom_set)) } + OtherTagRow(position = positionOf(options.size, count), startExpanded = options.isEmpty(), onAdd = pick) + Spacer(Modifier.height(16.dp)) + } +} + +/** + * The "Other tag…" row closing a run of tag rows: it opens a field for a tag no + * open task carries yet, and [onAdd] gets the tag typed, trimmed. + */ +@Composable +internal fun OtherTagRow(position: Position, onAdd: (String) -> Unit, startExpanded: Boolean = false) { + var expanded by rememberSaveable { mutableStateOf(startExpanded) } + var typed by rememberSaveable { mutableStateOf("") } + val add = { + typed.trim().takeIf { it.isNotEmpty() }?.let { + onAdd(it) + typed = "" + expanded = false + } + } + CustomExpandRow( + title = stringResource(R.string.filter_tag_other), + position = position, + expanded = expanded, + onExpandedChange = { expanded = it }, + ) { + GroupedSurface(position = Position.Bottom, modifier = Modifier.padding(horizontal = GroupedListInset)) { + Row( + modifier = Modifier.padding(start = 16.dp, end = 8.dp), + verticalAlignment = Alignment.CenterVertically, + ) { + InlineTextField( + value = typed, + onValueChange = { typed = it }, + placeholder = stringResource(R.string.filter_tag_other_hint), + capitalization = KeyboardCapitalization.None, + imeAction = ImeAction.Done, + onImeAction = { add() }, + modifier = Modifier.weight(1f).padding(vertical = 12.dp), + ) + TextButton(onClick = { add() }, enabled = typed.isNotBlank()) { + Text(stringResource(R.string.reminder_custom_set)) } } } - Spacer(Modifier.height(16.dp)) } } @@ -277,18 +292,36 @@ internal fun PriorityPicker(selected: Priority?, onSelect: (Priority) -> Unit, o } /** - * Where new rules start: empty, or one of the [FilterTemplate]s. [onPick] gets - * `null` for empty; [onDismiss] leaves without starting. + * Where new rules start: empty, or one of the [FilterTemplate]s. *Next actions* + * first asks which of [lists] holds them, which can be skipped. [onStart] gets + * the template (`null` for empty), that list, and the template's name for an + * unnamed filter; [onDismiss] leaves without starting. */ @Composable -internal fun TemplatePicker(title: String, onPick: (FilterTemplate?) -> Unit, onDismiss: () -> Unit) { +internal fun TemplateChooser( + title: String, + lists: List, + onStart: (template: FilterTemplate?, nextList: Long?, name: String?) -> Unit, + onDismiss: () -> Unit, +) { + var askingList by rememberSaveable { mutableStateOf(false) } + if (askingList) { + val name = stringResource(templateLabels(FilterTemplate.NEXT_ACTIONS).first) + NextListPicker( + lists = lists, + onPick = { onStart(FilterTemplate.NEXT_ACTIONS, it, name) }, + onDismiss = { askingList = false }, + ) + return + } + val names = FilterTemplate.entries.associateWith { stringResource(templateLabels(it).first) } FullScreenPicker(title = title, onDismiss = onDismiss) { GroupedRow( title = stringResource(R.string.filter_template_empty), summary = stringResource(R.string.filter_template_empty_hint), position = Position.Alone, leading = { CategoryIcon(Icons.Rounded.Add, ChipAccent.Primary) }, - onClick = { onPick(null) }, + onClick = { onStart(null, null, null) }, ) GroupedSectionHeader(stringResource(R.string.filter_templates_header)) FilterTemplate.entries.forEachIndexed { index, template -> @@ -298,7 +331,9 @@ internal fun TemplatePicker(title: String, onPick: (FilterTemplate?) -> Unit, on summary = stringResource(hint), position = positionOf(index, FilterTemplate.entries.size), leading = { CategoryIcon(icon, ChipAccent.Primary) }, - onClick = { onPick(template) }, + onClick = { + if (template == FilterTemplate.NEXT_ACTIONS) askingList = true else onStart(template, null, names[template]) + }, ) } GroupedHint(stringResource(R.string.filter_templates_note), verticalPadding = 8.dp) @@ -306,6 +341,35 @@ internal fun TemplatePicker(title: String, onPick: (FilterTemplate?) -> Unit, on } } +/** Which list holds the next actions; [onPick] gets `null` when skipped, for the tag alone. */ +@Composable +private fun NextListPicker(lists: List, onPick: (Long?) -> Unit, onDismiss: () -> Unit) { + FullScreenPicker( + title = stringResource(R.string.filter_template_gtd_list_title), + onDismiss = onDismiss, + actions = { + TextButton(onClick = { onPick(null) }, modifier = Modifier.padding(end = 8.dp)) { + Text(stringResource(R.string.filter_template_gtd_skip)) + } + }, + ) { + GroupedHint(stringResource(R.string.filter_template_gtd_list_hint)) + Spacer(Modifier.height(8.dp)) + if (lists.isEmpty()) GroupedHint(stringResource(R.string.filter_lists_none)) + val showAccount = lists.mapTo(HashSet()) { it.accountName }.size > 1 + lists.forEachIndexed { index, list -> + GroupedRow( + title = list.name, + summary = list.accountName.takeIf { showAccount }, + position = positionOf(index, lists.size), + leading = { ListColorChip(list.color) }, + onClick = { onPick(list.id) }, + ) + } + Spacer(Modifier.height(16.dp)) + } +} + private fun templateLabels(template: FilterTemplate): Triple = when (template) { FilterTemplate.NEXT_ACTIONS -> Triple(R.string.filter_template_gtd, R.string.filter_template_gtd_hint, Icons.Rounded.Bolt) FilterTemplate.THIS_WEEK -> Triple(R.string.filter_template_week, R.string.filter_template_week_hint, Icons.Rounded.CalendarViewWeek) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b3df0b7..973112c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -360,9 +360,12 @@ Start empty Pick every rule yourself Templates - A template only fills in the rules. You can change everything afterwards. + A template fills in the rules and a name. You can change everything afterwards. Next actions (GTD) - List “Next” or tag nextAction + A list of your choice, or tag nextAction + Next actions list + Which list holds your next actions? Tasks tagged nextAction show too. Skip to go by the tag alone. + Skip This week Due this week, overdue included This month diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/ui/filters/RuleDraftTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/ui/filters/RuleDraftTest.kt index d0cc62a..561a971 100644 --- a/app/src/test/java/de/jeanlucmakiola/agendula/ui/filters/RuleDraftTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/agendula/ui/filters/RuleDraftTest.kt @@ -5,7 +5,6 @@ import de.jeanlucmakiola.agendula.domain.DueSpec import de.jeanlucmakiola.agendula.domain.DueWindow import de.jeanlucmakiola.agendula.domain.Priority import de.jeanlucmakiola.agendula.domain.Rule -import de.jeanlucmakiola.agendula.domain.TaskList import org.junit.jupiter.api.Test class RuleDraftTest { @@ -120,9 +119,8 @@ class RuleDraftTest { @Test fun `templates fill in the rules they name`() { - val lists = listOf(list(1, "Inbox"), list(2, " next "), list(3, "Work")) val tags = listOf("Waiting", "nextaction") - fun rule(template: FilterTemplate) = template.form(lists, tags).toRule() + fun rule(template: FilterTemplate) = template.form(nextList = 2L, tags = tags).toRule() assertThat(rule(FilterTemplate.NEXT_ACTIONS)).isEqualTo( Rule.AnyOf(listOf(Rule.InList(setOf(2L)), Rule.AnyOf(listOf(Rule.HasTag("nextaction"))))), @@ -140,8 +138,8 @@ class RuleDraftTest { } @Test - fun `without a Next list or tags in use the GTD template still asks for the tag`() { - assertThat(FilterTemplate.NEXT_ACTIONS.form(emptyList(), emptyList())) + fun `with the list skipped and no tags in use the GTD template still asks for the tag`() { + assertThat(FilterTemplate.NEXT_ACTIONS.form(nextList = null, tags = emptyList())) .isEqualTo(SimpleRule(match = Match.ANY, tags = setOf("nextAction"))) } @@ -169,5 +167,4 @@ class RuleDraftTest { assertThat(RuleEditResult.decode("other")).isNull() } - private fun list(id: Long, name: String) = TaskList(id, name, 0, "Local", "local", isSynced = false, isVisible = true, owner = null) } diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/ui/filters/RuleEditViewModelTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/ui/filters/RuleEditViewModelTest.kt index 9c10cd6..d33b8ec 100644 --- a/app/src/test/java/de/jeanlucmakiola/agendula/ui/filters/RuleEditViewModelTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/agendula/ui/filters/RuleEditViewModelTest.kt @@ -184,11 +184,20 @@ class RuleEditViewModelTest { } @Test - fun `a template matches the lists and tags as read, not as shown so far`() { - repository.lists = listOf(list(1), TaskList(5, "Next", 0, "Local", "local", isSynced = false, isVisible = true, owner = null)) + fun `the GTD template takes the list picked for it and names an unnamed filter`() { val vm = editor(RuleEditViewModel.Target.Result(null), RulePurpose.SOURCE) - vm.startFrom(FilterTemplate.NEXT_ACTIONS) - assertThat(vm.state.value!!.rules.simple!!.listIds).containsExactly(5L) + vm.startFrom(FilterTemplate.NEXT_ACTIONS, nextList = 2L, name = "Next actions") + assertThat(vm.state.value!!.rules.simple) + .isEqualTo(SimpleRule(match = Match.ANY, listIds = setOf(2L), tags = setOf("nextAction"))) + assertThat(vm.state.value!!.filterName).isEqualTo("Next actions") + } + + @Test + fun `a template's name doesn't replace one already typed`() { + val vm = editor(RuleEditViewModel.Target.Result(null), RulePurpose.SOURCE) + vm.renameFilter("Mine") + vm.startFrom(FilterTemplate.NO_DATE, name = "No date") + assertThat(vm.state.value!!.filterName).isEqualTo("Mine") } private class FakeRepository(var lists: List) : TasksRepository by unsupported() {