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 a0e096e..ce5e043 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 @@ -65,7 +65,7 @@ fun FilterEditScreen( NameField( name = filter.name, placeholder = stringResource(R.string.filter_name_hint), - autoFocus = isNew && !current.choosingTemplate, + autoFocus = isNew && !current.choosingTemplate && filter.name.isEmpty(), onNameChange = viewModel::rename, onImeAction = save, ) 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 9487bdb..fd6502f 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 @@ -85,21 +85,23 @@ class FilterEditViewModel @Inject constructor( * 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 { 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 + // The first pick wins; the picker stays up, and the editor untouched, until its rules are read. + if (_draft.value?.choosingTemplate != true || starting) return + starting = true viewModelScope.launch { - val form = repository.templateForm(template, nextList) - _draft.update { it?.copy(rules = RuleDraft.of(form)) } + val form = template?.let { repository.templateForm(it, nextList) } + _draft.update { draft -> + draft?.copy( + filter = if (name != null && draft.filter.name.isBlank()) draft.filter.copy(name = name) else draft.filter, + rules = form?.let(RuleDraft::of) ?: draft.rules, + choosingTemplate = false, + ) + } } } + private var starting = false + private fun load(filter: SavedFilter) { _draft.value = Draft(filter, RuleDraft.of(filter.rule)) } 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 df2e40b..e73fb36 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 @@ -113,21 +113,23 @@ class RuleEditViewModel @Inject constructor( * [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 { state -> - state?.copy( - filterName = if (name != null && state.filterName.isBlank()) name else state.filterName, - choosingTemplate = false, - ) - } - if (template == null) return + // The first pick wins; the picker stays up, and the editor untouched, until its rules are read. + if (_state.value?.choosingTemplate != true || starting) return + starting = true viewModelScope.launch { - val form = repository.templateForm(template, nextList) - _state.update { it?.copy(rules = RuleDraft.of(form)) } + val form = template?.let { repository.templateForm(it, nextList) } + _state.update { state -> + state?.copy( + rules = form?.let(RuleDraft::of) ?: state.rules, + filterName = if (name != null && state.filterName.isBlank()) name else state.filterName, + choosingTemplate = false, + ) + } } } + private var starting = false + fun setSaveAsFilter(on: Boolean) = _state.update { it?.copy(saveAsFilter = on) } fun renameFilter(name: String) = _state.update { it?.copy(filterName = name) } 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 c60d017..3be6e89 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 @@ -151,7 +151,7 @@ private fun SimpleSections( 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) } }) + OtherTagRow(position = positionOf(tags.size + 1, tags.size + 2), known = tags, 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 8208a08..fcb1c0b 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,22 +230,24 @@ internal fun TagPicker(tags: List, selected: String?, onSelect: (String) onClick = { pick(tag) }, ) } - OtherTagRow(position = positionOf(options.size, count), startExpanded = options.isEmpty(), onAdd = pick) + OtherTagRow(position = positionOf(options.size, count), known = options, 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. + * open task carries yet, and [onAdd] gets the tag typed, trimmed, in the + * spelling of one of [known] that differs only in case. */ @Composable -internal fun OtherTagRow(position: Position, onAdd: (String) -> Unit, startExpanded: Boolean = false) { +internal fun OtherTagRow(position: Position, known: List, 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.trim().takeIf { it.isNotEmpty() }?.let { tag -> + // Tags match exactly, so one differing only in case takes the spelling in use. + onAdd(known.firstOrNull { it.equals(tag, ignoreCase = true) } ?: tag) typed = "" expanded = false } @@ -306,7 +308,7 @@ internal fun TemplateChooser( ) { var askingList by rememberSaveable { mutableStateOf(false) } if (askingList) { - val name = stringResource(templateLabels(FilterTemplate.NEXT_ACTIONS).first) + val name = stringResource(templateName(FilterTemplate.NEXT_ACTIONS)) NextListPicker( lists = lists, onPick = { onStart(FilterTemplate.NEXT_ACTIONS, it, name) }, @@ -314,7 +316,7 @@ internal fun TemplateChooser( ) return } - val names = FilterTemplate.entries.associateWith { stringResource(templateLabels(it).first) } + val names = FilterTemplate.entries.associateWith { stringResource(templateName(it)) } FullScreenPicker(title = title, onDismiss = onDismiss) { GroupedRow( title = stringResource(R.string.filter_template_empty), @@ -355,7 +357,6 @@ private fun NextListPicker(lists: List, onPick: (Long?) -> Unit, onDis ) { 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( @@ -370,6 +371,10 @@ private fun NextListPicker(lists: List, onPick: (Long?) -> Unit, onDis } } +/** The name a filter made from [template] starts with. */ +private fun templateName(template: FilterTemplate): Int = + if (template == FilterTemplate.NEXT_ACTIONS) R.string.filter_template_gtd_name else templateLabels(template).first + 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 973112c..f1c7dac 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -363,6 +363,7 @@ A template fills in the rules and a name. You can change everything afterwards. Next actions (GTD) A list of your choice, or tag nextAction + Next actions Next actions list Which list holds your next actions? Tasks tagged nextAction show too. Skip to go by the tag alone. Skip 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 d33b8ec..27a65c8 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 @@ -19,6 +19,7 @@ import de.jeanlucmakiola.agendula.domain.Task import de.jeanlucmakiola.agendula.domain.TaskFilter import de.jeanlucmakiola.agendula.domain.TaskList import de.jeanlucmakiola.agendula.domain.newView +import de.jeanlucmakiola.agendula.domain.testTask import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.Flow @@ -184,11 +185,12 @@ class RuleEditViewModelTest { } @Test - fun `the GTD template takes the list picked for it and names an unnamed filter`() { + fun `the GTD template takes the list picked for it, the tag as spelt in use, and names an unnamed filter`() { + repository.tasks = listOf(testTask(tags = listOf("NEXTACTION"))) val vm = editor(RuleEditViewModel.Target.Result(null), RulePurpose.SOURCE) 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"))) + .isEqualTo(SimpleRule(match = Match.ANY, listIds = setOf(2L), tags = setOf("NEXTACTION"))) assertThat(vm.state.value!!.filterName).isEqualTo("Next actions") } @@ -201,8 +203,9 @@ class RuleEditViewModelTest { } private class FakeRepository(var lists: List) : TasksRepository by unsupported() { + var tasks: List = emptyList() override fun taskLists() = flowOf(lists) - override fun tasks(filter: TaskFilter): Flow> = flowOf(emptyList()) + override fun tasks(filter: TaskFilter): Flow> = flowOf(tasks) } private class MemoryDataStore : DataStore {