Template rules and name land together, typed tags take the spelling in use, GTD filters are named Next actions (#154, #172)
This commit is contained in:
1 parent
a1c43d5bb2
commit
28d80cb986
7 files changed
+48
-35
No files matched your search
@@ -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,
|
||||
)
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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) }
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -230,22 +230,24 @@ internal fun TagPicker(tags: List<String>, 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<String>, 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<TaskList>, 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<TaskList>, 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<Int, Int, ImageVector> = 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)
|
||||
|
||||
@@ -363,6 +363,7 @@
|
||||
<string name="filter_templates_note">A template fills in the rules and a name. You can change everything afterwards.</string>
|
||||
<string name="filter_template_gtd">Next actions (GTD)</string>
|
||||
<string name="filter_template_gtd_hint">A list of your choice, or tag nextAction</string>
|
||||
<string name="filter_template_gtd_name">Next actions</string>
|
||||
<string name="filter_template_gtd_list_title">Next actions list</string>
|
||||
<string name="filter_template_gtd_list_hint">Which list holds your next actions? Tasks tagged nextAction show too. Skip to go by the tag alone.</string>
|
||||
<string name="filter_template_gtd_skip">Skip</string>
|
||||
|
||||
@@ -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<TaskList>) : TasksRepository by unsupported() {
|
||||
var tasks: List<Task> = emptyList()
|
||||
override fun taskLists() = flowOf(lists)
|
||||
override fun tasks(filter: TaskFilter): Flow<List<Task>> = flowOf(emptyList())
|
||||
override fun tasks(filter: TaskFilter): Flow<List<Task>> = flowOf(tasks)
|
||||
}
|
||||
|
||||
private class MemoryDataStore : DataStore<Preferences> {
|
||||
|
||||
Reference in new issue
Block a user