First template pick wins, and a block gone meanwhile isn't reported saved (#172, #154)

This commit is contained in:
makiolaj committed 2026-11-27 14:58:19 +01:00
1 parent 879ae7e244
commit 4aaa1b3a11
3 files changed
+38 -10

No files matched your search

@@ -82,11 +82,13 @@ class FilterEditViewModel @Inject constructor(
/** 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.
if (_draft.value?.choosingTemplate != true) return
_draft.update { it?.copy(choosingTemplate = false) }
if (template == null) return
viewModelScope.launch {
val form = template?.let { repository.templateForm(it) }
_draft.update { draft ->
draft?.copy(rules = form?.let(RuleDraft::of) ?: draft.rules, choosingTemplate = false)
}
val form = repository.templateForm(template)
_draft.update { it?.copy(rules = RuleDraft.of(form)) }
}
}
@@ -110,11 +110,13 @@ class RuleEditViewModel @Inject constructor(
/** 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.
if (_state.value?.choosingTemplate != true) return
_state.update { it?.copy(choosingTemplate = false) }
if (template == null) return
viewModelScope.launch {
val form = template?.let { repository.templateForm(it) }
_state.update { state ->
state?.copy(rules = form?.let(RuleDraft::of) ?: state.rules, choosingTemplate = false)
}
val form = repository.templateForm(template)
_state.update { it?.copy(rules = RuleDraft.of(form)) }
}
}
@@ -159,8 +161,13 @@ class RuleEditViewModel @Inject constructor(
private var savedFilterId: Long? = null
private suspend fun writeSource(target: Target.Block, source: BlockSource): Boolean =
settingsPrefs.updateView(target.viewId) { view ->
/** Points the block at [source]; false when it can't be written or is gone. */
private suspend fun writeSource(target: Target.Block, source: BlockSource): Boolean {
var found = false
val written = settingsPrefs.updateView(target.viewId) { view ->
found = view.blocks.any { it.id == target.blockId }
view.copy(blocks = view.blocks.map { if (it.id == target.blockId) it.copy(source = source) else it })
}
return written && found
}
}
@@ -141,6 +141,25 @@ class RuleEditViewModelTest {
assertThat(runBlocking { prefs.savedFilters.first() }).isEmpty()
}
@Test
fun `a block removed from its view meanwhile isn't reported saved`() {
val viewId = storedView(BlockSource.Smart(SmartList.ALL))
val vm = editor(RuleEditViewModel.Target.Block(viewId, 99), RulePurpose.SOURCE)
vm.startFrom(null)
var done = false
vm.save { done = true }
assertThat(done).isFalse()
assertThat(vm.saveFailed.value).isTrue()
}
@Test
fun `a second template tap while the first is read is ignored`() {
val vm = editor(RuleEditViewModel.Target.Result(null), RulePurpose.SOURCE)
vm.startFrom(null)
vm.startFrom(FilterTemplate.HIGH_PRIORITY)
assertThat(vm.state.value!!.rules.rule).isEqualTo(Rule.Everything)
}
@Test
fun `custom rules opened for a result hand back the filter they were saved as`() {
runBlocking { prefs.setStorageMode(StorageMode.OWN) }