Templates match the lists and tags as read; rule saves drop deleted lists and reuse their filter on retry (#172, #154)

This commit is contained in:
makiolaj committed 2026-11-27 14:46:32 +01:00
1 parent 7f3807c8fd
commit 8b17ba9d74
6 files changed
+60 -23

No files matched your search

@@ -88,7 +88,7 @@ fun FilterEditScreen(
if (draft?.choosingTemplate == true) {
TemplatePicker(
title = stringResource(R.string.filter_new_title),
onPick = { viewModel.startFrom(it, lists, tagsInUse) },
onPick = viewModel::startFrom,
onDismiss = onBack,
)
}
@@ -80,13 +80,13 @@ class FilterEditViewModel @Inject constructor(
_draft.update { it?.copy(rules = transform(it.rules)) }
}
/** Starts the new filter from [template]'s rules, or from none. */
fun startFrom(template: FilterTemplate?, lists: List<TaskList>, tags: List<String>) {
_draft.update { draft ->
draft?.copy(
rules = template?.let { RuleDraft.of(it.form(lists, tags)) } ?: draft.rules,
choosingTemplate = false,
)
/** 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?) {
viewModelScope.launch {
val form = template?.let { repository.templateForm(it) }
_draft.update { draft ->
draft?.copy(rules = form?.let(RuleDraft::of) ?: draft.rules, choosingTemplate = false)
}
}
}
@@ -1,10 +1,15 @@
package de.jeanlucmakiola.agendula.ui.filters
import de.jeanlucmakiola.agendula.data.tasks.TasksRepository
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.SmartList
import de.jeanlucmakiola.agendula.domain.TaskFilter
import de.jeanlucmakiola.agendula.domain.TaskList
import de.jeanlucmakiola.agendula.domain.tagsInUse
import kotlinx.coroutines.flow.first
/**
* A rule being edited: [rule] is what saves, and [simple] the simple editor's
@@ -42,6 +47,13 @@ data class RuleDraft(val rule: Rule, val simple: SimpleRule?) {
}
}
/** [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())
val tags = runCatching { tagsInUse(tasks(TaskFilter.Smart(SmartList.ALL)).first()) }.getOrDefault(emptyList())
return template.form(lists, tags)
}
/** The rules a new filter can start from (#154); the user changes them as they like. */
enum class FilterTemplate {
NEXT_ACTIONS,
@@ -93,7 +93,7 @@ fun RuleEditScreen(
if (state?.choosingTemplate == true) {
TemplatePicker(
title = title,
onPick = { viewModel.startFrom(it, lists, tagsInUse) },
onPick = viewModel::startFrom,
onDismiss = onBack,
)
}
@@ -108,13 +108,13 @@ class RuleEditViewModel @Inject constructor(
_state.update { it?.copy(rules = transform(it.rules)) }
}
/** Starts from [template]'s rules, or from none. */
fun startFrom(template: FilterTemplate?, lists: List<TaskList>, tags: List<String>) {
_state.update { state ->
state?.copy(
rules = template?.let { RuleDraft.of(it.form(lists, tags)) } ?: state.rules,
choosingTemplate = false,
)
/** Starts from [template]'s rules, or from none; its lists and tags are matched once they are read. */
fun startFrom(template: FilterTemplate?) {
viewModelScope.launch {
val form = template?.let { repository.templateForm(it) }
_state.update { state ->
state?.copy(rules = form?.let(RuleDraft::of) ?: state.rules, choosingTemplate = false)
}
}
}
@@ -133,8 +133,14 @@ class RuleEditViewModel @Inject constructor(
if (saving || !state.canSave) return
saving = true
viewModelScope.launch {
val rules = RuleEditResult.Rules(state.rules.rule)
suspend fun asFilter() = RuleEditResult.Filter(settingsPrefs.saveFilter(SavedFilter(0, state.filterName.trim(), rules.rule)))
// Lists deleted while this was open don't come back with it.
val existing = runCatching { repository.taskLists().first().mapTo(HashSet()) { it.id } }.getOrNull().orEmpty()
val rule = state.rules.rule.let { if (existing.isEmpty()) it else it.withoutListIds { id -> id !in existing } }
val rules = RuleEditResult.Rules(rule)
// A retry after a failed block write updates the filter made by the first try.
suspend fun asFilter() = RuleEditResult.Filter(
settingsPrefs.saveFilter(SavedFilter(savedFilterId ?: 0, state.filterName.trim(), rule)).also { savedFilterId = it },
)
when (target) {
is Target.Result -> onDone(if (state.saveAsFilter) asFilter() else rules)
is Target.Block -> {
@@ -151,6 +157,8 @@ class RuleEditViewModel @Inject constructor(
}
}
private var savedFilterId: Long? = null
private suspend fun writeSource(target: Target.Block, source: BlockSource): Boolean =
settingsPrefs.updateView(target.viewId) { view ->
view.copy(blocks = view.blocks.map { if (it.id == target.blockId) it.copy(source = source) else it })
@@ -94,7 +94,7 @@ class RuleEditViewModelTest {
val vm = editor(RuleEditViewModel.Target.Block(viewId, 2), RulePurpose.SOURCE)
assertThat(vm.state.value!!.choosingTemplate).isTrue()
vm.startFrom(FilterTemplate.HIGH_PRIORITY, emptyList(), emptyList())
vm.startFrom(FilterTemplate.HIGH_PRIORITY)
assertThat(vm.state.value!!.choosingTemplate).isFalse()
var done = false
vm.save { done = it == null }
@@ -115,7 +115,7 @@ class RuleEditViewModelTest {
fun `saved as a filter, the block shows the new filter`() {
val viewId = storedView(BlockSource.Smart(SmartList.ALL))
val vm = editor(RuleEditViewModel.Target.Block(viewId, 2), RulePurpose.SOURCE)
vm.startFrom(null, emptyList(), emptyList())
vm.startFrom(null)
vm.setSaveAsFilter(true)
assertThat(vm.state.value!!.canSave).isFalse()
vm.renameFilter(" Mine ")
@@ -131,7 +131,7 @@ class RuleEditViewModelTest {
fun `a block that is gone gets no filter and says so`() {
runBlocking { prefs.setStorageMode(StorageMode.OWN) }
val vm = editor(RuleEditViewModel.Target.Block(42, 2), RulePurpose.SOURCE)
vm.startFrom(null, emptyList(), emptyList())
vm.startFrom(null)
vm.setSaveAsFilter(true)
vm.renameFilter("Mine")
var done = false
@@ -146,7 +146,7 @@ class RuleEditViewModelTest {
runBlocking { prefs.setStorageMode(StorageMode.OWN) }
val vm = editor(RuleEditViewModel.Target.Result(null), RulePurpose.SOURCE)
assertThat(vm.state.value!!.choosingTemplate).isTrue()
vm.startFrom(FilterTemplate.NO_DATE, emptyList(), emptyList())
vm.startFrom(FilterTemplate.NO_DATE)
vm.setSaveAsFilter(true)
vm.renameFilter("Undated")
var result: RuleEditResult? = null
@@ -155,7 +155,24 @@ class RuleEditViewModelTest {
assertThat(result).isEqualTo(RuleEditResult.Filter(filter.id))
}
private class FakeRepository(private val lists: List<TaskList>) : TasksRepository by unsupported() {
@Test
fun `lists deleted while the editor was open don't come back with the rules`() {
val vm = editor(RuleEditViewModel.Target.Result(Rule.InList(setOf(1L, 2L))), RulePurpose.SCOPE)
repository.lists = listOf(list(1))
var result: RuleEditResult? = null
vm.save { result = it }
assertThat(result).isEqualTo(RuleEditResult.Rules(Rule.InList(setOf(1L))))
}
@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))
val vm = editor(RuleEditViewModel.Target.Result(null), RulePurpose.SOURCE)
vm.startFrom(FilterTemplate.NEXT_ACTIONS)
assertThat(vm.state.value!!.rules.simple!!.listIds).containsExactly(5L)
}
private class FakeRepository(var lists: List<TaskList>) : TasksRepository by unsupported() {
override fun taskLists() = flowOf(lists)
override fun tasks(filter: TaskFilter): Flow<List<Task>> = flowOf(emptyList())
}