is / is not only on sections that pick something; the simple form survives a look at the advanced editor (#172)

This commit is contained in:
makiolaj committed 2026-11-27 14:58:19 +01:00
1 parent 4aaa1b3a11
commit 02f842016f
4 files changed
+55 -17

No files matched your search

@@ -17,27 +17,34 @@ import kotlinx.coroutines.flow.first
* kept apart so a choice the rule can't hold, like *any rule* with nothing
* picked, survives while editing.
*/
data class RuleDraft(val rule: Rule, val simple: SimpleRule?) {
data class RuleDraft(val rule: Rule, val simple: SimpleRule?, private val left: SimpleRule? = null) {
val advanced: Boolean get() = simple == null
/** Whether the simple editor can show the rule as it is now. */
val canSimplify: Boolean get() = simple != null || SimpleRule.of(rule) != null
/** Changes the simple form, and the rule with it; nothing in the advanced editor. */
/** Changes the simple form, and the rule with them; nothing in the advanced editor. */
fun editSimple(transform: (SimpleRule) -> SimpleRule): RuleDraft {
val form = simple?.let(transform) ?: return this
val form = simple?.let(transform)?.tidy() ?: return this
return RuleDraft(form.toRule(), form)
}
/** Changes the rule in the advanced editor; nothing in the simple one. */
fun editAdvanced(transform: (Rule) -> Rule): RuleDraft = if (simple != null) this else RuleDraft(transform(rule), null)
fun editAdvanced(transform: (Rule) -> Rule): RuleDraft = if (simple != null) this else copy(rule = transform(rule))
/** Over to the advanced editor, which can show any rule. */
fun toAdvanced(): RuleDraft = RuleDraft(rule.forAdvanced(), null)
/** Over to the advanced editor, which can show any rule; the simple form is kept for the way back. */
fun toAdvanced(): RuleDraft = RuleDraft(rule.forAdvanced(), null, simple)
/** Over to the simple editor, when it can show the rule; the rule stays as it is. */
fun toSimple(): RuleDraft = if (simple != null) this else SimpleRule.of(rule)?.let { RuleDraft(rule, it) } ?: this
/**
* Over to the simple editor, when it can show the rule; the rule stays as it
* is. Unchanged since leaving it, the form comes back as it was left.
*/
fun toSimple(): RuleDraft = when {
simple != null -> this
left != null && left.toRule() == rule -> RuleDraft(rule, left)
else -> SimpleRule.of(rule)?.let { RuleDraft(rule, it) } ?: this
}
companion object {
/** [rule] in the simple editor when it can show it, else in the advanced one. */
@@ -106,7 +106,7 @@ private fun SimpleSections(
)
if (lists.isNotEmpty()) {
NegatableHeader(stringResource(R.string.filter_section_lists), form.listsNegated) { negated ->
NegatableHeader(stringResource(R.string.filter_section_lists), form.listsNegated, shown = !form.anyList) { negated ->
onChange { it.copy(listsNegated = negated) }
}
val showAccount = lists.mapTo(HashSet()) { it.accountName }.size > 1
@@ -115,7 +115,7 @@ private fun SimpleSections(
position = positionOf(0, lists.size + 1),
selected = form.anyList,
leading = { Icon(Icons.AutoMirrored.Rounded.ListAlt, contentDescription = null) },
onClick = { onChange { it.copy(listIds = emptySet(), listsElsewhere = emptySet()) } },
onClick = { onChange { it.copy(listIds = emptySet(), listsElsewhere = emptySet(), listsNegated = false) } },
)
lists.forEachIndexed { index, list ->
ChoiceRow(
@@ -134,14 +134,14 @@ private fun SimpleSections(
(tagsInUse + form.tags).distinct().sortedWith(String.CASE_INSENSITIVE_ORDER)
}
if (tags.isNotEmpty()) {
NegatableHeader(stringResource(R.string.filter_section_tags), form.tagsNegated) { negated ->
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),
selected = form.tags.isEmpty(),
onClick = { onChange { it.copy(tags = emptySet()) } },
onClick = { onChange { it.copy(tags = emptySet(), tagsNegated = false) } },
)
tags.forEachIndexed { index, tag ->
ChoiceRow(
@@ -165,7 +165,7 @@ private fun SimpleSections(
}
}
NegatableHeader(stringResource(R.string.filter_section_due), form.dueNegated) { negated ->
NegatableHeader(stringResource(R.string.filter_section_due), form.dueNegated, shown = form.due.isNotEmpty()) { negated ->
onChange { it.copy(dueNegated = negated) }
}
val windows = listOf(DueSpec.Window(DueWindow.OVERDUE), DueSpec.Window(DueWindow.TODAY), DueSpec.Window(DueWindow.UPCOMING))
@@ -175,7 +175,7 @@ private fun SimpleSections(
title = stringResource(R.string.filter_any_due),
position = positionOf(0, rows),
selected = form.due.isEmpty(),
onClick = { onChange { it.copy(due = emptySet()) } },
onClick = { onChange { it.copy(due = emptySet(), dueNegated = false) } },
)
(windows + ranges).forEachIndexed { index, spec ->
ChoiceRow(
@@ -229,9 +229,16 @@ private fun SimpleSections(
}
}
/** A section header with an *is / is not* switch at its end. */
/**
* A section header with an *is / is not* switch at its end, [shown] while the
* section picks something; "any" can't be turned round.
*/
@Composable
private fun NegatableHeader(title: String, negated: Boolean, onNegate: (Boolean) -> Unit) {
private fun NegatableHeader(title: String, negated: Boolean, shown: Boolean, onNegate: (Boolean) -> Unit) {
if (!shown) {
GroupedSectionHeader(title)
return
}
Row(verticalAlignment = Alignment.Bottom) {
GroupedSectionHeader(title, Modifier.weight(1f))
ConnectedChoiceRow(
@@ -245,7 +252,7 @@ private fun NegatableHeader(title: String, negated: Boolean, onNegate: (Boolean)
}
/** Room for the *is / is not* pair, its inset included. */
private val NEGATE_WIDTH = 200.dp
private val NEGATE_WIDTH = 240.dp
@Composable
internal fun ChoiceRow(
@@ -56,6 +56,13 @@ data class SimpleRule(
fun withNextDays(spec: DueSpec.NextDays?): SimpleRule =
copy(due = due.filterNot { it is DueSpec.NextDays }.toSet() + listOfNotNull(spec))
/** This form with *is not* dropped from the sections that pick nothing, where it can't apply. */
fun tidy(): SimpleRule = copy(
dueNegated = dueNegated && due.isNotEmpty(),
listsNegated = listsNegated && !anyList,
tagsNegated = tagsNegated && tags.isNotEmpty(),
)
fun toRule(): Rule {
val parts = buildList {
if (due.isNotEmpty()) add(Rule.AnyOf(due.sortedBy(::dueRank).map(Rule::Due)).negatedIf(dueNegated))
@@ -53,6 +53,23 @@ class RuleDraftTest {
assertThat(advanced.editSimple { it.copy(minPriority = Priority.LOW) }).isEqualTo(advanced)
}
@Test
fun `a look at the advanced editor brings the simple form back as it was left`() {
val any = RuleDraft.of(SimpleRule()).editSimple { it.copy(match = Match.ANY) }
assertThat(any.toAdvanced().toSimple().simple!!.match).isEqualTo(Match.ANY)
val changed = any.toAdvanced().editAdvanced { it.addTo(emptyList(), work) }
assertThat(changed.toSimple().simple).isEqualTo(SimpleRule(listIds = setOf(1L)))
}
@Test
fun `is not drops out of a section once nothing in it is picked`() {
val draft = RuleDraft.of(SimpleRule(tags = setOf("a"), tagsNegated = true))
.editSimple { it.copy(tags = emptySet()) }
assertThat(draft.simple!!.tagsNegated).isFalse()
assertThat(draft.editSimple { it.copy(tags = setOf("b")) }.rule)
.isEqualTo(Rule.AllOf(listOf(Rule.AnyOf(listOf(Rule.HasTag("b"))))))
}
@Test
fun `the simple form keeps a choice its rule can't hold`() {
val any = RuleDraft.of(SimpleRule()).editSimple { it.copy(match = Match.ANY) }