This commit is contained in:
1 parent
2499ceb1c7
commit
668a717445
6 files changed
+1061
-196
No files matched your search
@@ -0,0 +1,362 @@
|
||||
package de.jeanlucmakiola.agendula.ui.filters
|
||||
|
||||
import androidx.compose.foundation.BorderStroke
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.Add
|
||||
import androidx.compose.material.icons.rounded.ArrowDropDown
|
||||
import androidx.compose.material.icons.rounded.Close
|
||||
import androidx.compose.material.icons.rounded.Delete
|
||||
import androidx.compose.material.icons.rounded.DoNotDisturbOn
|
||||
import androidx.compose.material.icons.rounded.MoreVert
|
||||
import androidx.compose.material3.DropdownMenu
|
||||
import androidx.compose.material3.DropdownMenuItem
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.semantics.Role
|
||||
import androidx.compose.ui.semantics.role
|
||||
import androidx.compose.ui.semantics.semantics
|
||||
import androidx.compose.ui.semantics.stateDescription
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import de.jeanlucmakiola.agendula.R
|
||||
import de.jeanlucmakiola.agendula.domain.Rule
|
||||
import de.jeanlucmakiola.agendula.domain.TaskList
|
||||
import de.jeanlucmakiola.floret.components.GroupedHint
|
||||
import de.jeanlucmakiola.floret.components.GroupedListInset
|
||||
import de.jeanlucmakiola.floret.components.GroupedRow
|
||||
import de.jeanlucmakiola.floret.components.Position
|
||||
import de.jeanlucmakiola.floret.identity.FloretDensity
|
||||
|
||||
/** A picker the advanced editor has open, for the node at [path]. */
|
||||
private sealed interface Picking {
|
||||
val path: RulePath
|
||||
|
||||
/** [adding] puts a new condition into the group at [path] rather than changing the one there. */
|
||||
data class Field(override val path: RulePath, val adding: Boolean) : Picking
|
||||
data class Value(override val path: RulePath, val field: RuleField, val adding: Boolean) : Picking
|
||||
}
|
||||
|
||||
/**
|
||||
* The advanced editor: the rule as nested groups, each *all of* or *any of*,
|
||||
* maybe negated, holding conditions and further groups. [rule] is a group at the
|
||||
* root (see [forAdvanced]); [onChange] gets each change to it.
|
||||
*/
|
||||
@Composable
|
||||
internal fun AdvancedEditor(
|
||||
rule: Rule,
|
||||
lists: List<TaskList>,
|
||||
tagsInUse: List<String>,
|
||||
onChange: ((Rule) -> Rule) -> Unit,
|
||||
) {
|
||||
var picking by remember { mutableStateOf<Picking?>(null) }
|
||||
val listNames = remember(lists) { lists.associate { it.id to it.name } }
|
||||
|
||||
GroupCard(
|
||||
group = rule,
|
||||
path = emptyList(),
|
||||
listNames = listNames,
|
||||
onChange = onChange,
|
||||
onPick = { picking = it },
|
||||
modifier = Modifier.padding(horizontal = GroupedListInset),
|
||||
)
|
||||
GroupedHint(stringResource(R.string.filter_advanced_note), style = MaterialTheme.typography.bodySmall, verticalPadding = 8.dp)
|
||||
|
||||
when (val open = picking) {
|
||||
null -> Unit
|
||||
is Picking.Field -> FieldPicker(
|
||||
selected = if (open.adding) null else rule.at(open.path)?.field,
|
||||
onSelect = { field -> picking = Picking.Value(open.path, field, open.adding) },
|
||||
onDismiss = { if (picking == open) picking = null },
|
||||
)
|
||||
is Picking.Value -> {
|
||||
val current = if (open.adding) null else rule.at(open.path)?.positive?.takeIf { it.field == open.field }
|
||||
val done = { condition: Rule ->
|
||||
onChange { root ->
|
||||
if (open.adding) {
|
||||
root.addTo(open.path, condition)
|
||||
} else {
|
||||
root.updateAt(open.path) { old -> if (old.negated) Rule.Not(condition) else condition }
|
||||
}
|
||||
}
|
||||
}
|
||||
val dismiss = { picking = null }
|
||||
when (open.field) {
|
||||
RuleField.LIST -> {
|
||||
val list = current as? Rule.InList
|
||||
ListsPicker(
|
||||
lists = lists,
|
||||
selected = list?.listIds.orEmpty(),
|
||||
onDone = { ids -> done(Rule.InList(ids, list?.elsewhere.orEmpty())) },
|
||||
onDismiss = dismiss,
|
||||
)
|
||||
}
|
||||
RuleField.TAG -> TagPicker(
|
||||
tags = tagsInUse,
|
||||
selected = (current as? Rule.HasTag)?.tag,
|
||||
onSelect = { done(Rule.HasTag(it)) },
|
||||
onDismiss = dismiss,
|
||||
)
|
||||
RuleField.PRIORITY -> PriorityPicker(
|
||||
selected = (current as? Rule.MinPriority)?.priority,
|
||||
onSelect = { done(Rule.MinPriority(it)) },
|
||||
onDismiss = dismiss,
|
||||
)
|
||||
RuleField.DUE -> DuePicker(
|
||||
selected = (current as? Rule.Due)?.spec,
|
||||
onSelect = { done(Rule.Due(it)) },
|
||||
onDismiss = dismiss,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** One group: its kind and negation, its children, and the buttons that add to it. */
|
||||
@Composable
|
||||
private fun GroupCard(
|
||||
group: Rule,
|
||||
path: RulePath,
|
||||
listNames: Map<Long, String>,
|
||||
onChange: ((Rule) -> Rule) -> Unit,
|
||||
onPick: (Picking) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
val match = group.groupMatch ?: Match.ALL
|
||||
val children = group.groupChildren
|
||||
val nested = path.isNotEmpty()
|
||||
Surface(
|
||||
shape = RoundedCornerShape(24.dp),
|
||||
color = if (path.size % 2 == 0) MaterialTheme.colorScheme.surfaceContainerLow else MaterialTheme.colorScheme.surface,
|
||||
border = BorderStroke(1.dp, MaterialTheme.colorScheme.outlineVariant),
|
||||
modifier = modifier.fillMaxWidth(),
|
||||
) {
|
||||
Column(Modifier.padding(vertical = 8.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {
|
||||
GroupHeader(
|
||||
match = match,
|
||||
negated = group.negated,
|
||||
deletable = nested,
|
||||
onToggleMatch = {
|
||||
val other = if (match == Match.ALL) Match.ANY else Match.ALL
|
||||
onChange { it.updateAt(path) { g -> g.withGroupMatch(other) } }
|
||||
},
|
||||
onToggleNot = { onChange { it.updateAt(path, Rule::toggleNot) } },
|
||||
onDelete = { onChange { it.removeAt(path) } },
|
||||
)
|
||||
if (children.isEmpty()) {
|
||||
GroupedHint(
|
||||
stringResource(if (match == Match.ALL) R.string.filter_group_empty_all else R.string.filter_group_empty_any),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
modifier = Modifier.padding(horizontal = 4.dp),
|
||||
)
|
||||
}
|
||||
children.forEachIndexed { index, child ->
|
||||
val childPath = path + index
|
||||
if (child.isGroup) {
|
||||
GroupCard(
|
||||
group = child,
|
||||
path = childPath,
|
||||
listNames = listNames,
|
||||
onChange = onChange,
|
||||
onPick = onPick,
|
||||
modifier = Modifier.padding(horizontal = 8.dp),
|
||||
)
|
||||
} else {
|
||||
ConditionRow(
|
||||
condition = child,
|
||||
listNames = listNames,
|
||||
onField = { onPick(Picking.Field(childPath, adding = false)) },
|
||||
onValue = { child.field?.let { onPick(Picking.Value(childPath, it, adding = false)) } },
|
||||
onToggleNot = { onChange { it.updateAt(childPath, Rule::toggleNot) } },
|
||||
onRemove = { onChange { it.removeAt(childPath) } },
|
||||
)
|
||||
}
|
||||
}
|
||||
Row(Modifier.padding(horizontal = 8.dp)) {
|
||||
TextButton(onClick = { onPick(Picking.Field(path, adding = true)) }) {
|
||||
Icon(Icons.Rounded.Add, contentDescription = null, modifier = Modifier.size(18.dp))
|
||||
Spacer(Modifier.width(8.dp))
|
||||
Text(stringResource(R.string.filter_add_rule))
|
||||
}
|
||||
TextButton(onClick = { onChange { it.addTo(path, newGroupUnder(group)) } }) {
|
||||
Icon(Icons.Rounded.Add, contentDescription = null, modifier = Modifier.size(18.dp))
|
||||
Spacer(Modifier.width(8.dp))
|
||||
Text(stringResource(R.string.filter_add_group))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** A group's kind, which a tap switches, its negation, and its ⋮ menu. */
|
||||
@Composable
|
||||
private fun GroupHeader(
|
||||
match: Match,
|
||||
negated: Boolean,
|
||||
deletable: Boolean,
|
||||
onToggleMatch: () -> Unit,
|
||||
onToggleNot: () -> Unit,
|
||||
onDelete: () -> Unit,
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().padding(start = 12.dp, end = 4.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||
) {
|
||||
if (negated) {
|
||||
InlineChip(text = stringResource(R.string.filter_group_not), onClick = onToggleNot, toggles = false)
|
||||
}
|
||||
InlineChip(
|
||||
text = stringResource(if (match == Match.ALL) R.string.filter_group_all else R.string.filter_group_any),
|
||||
onClick = onToggleMatch,
|
||||
)
|
||||
Text(
|
||||
stringResource(
|
||||
when {
|
||||
negated && match == Match.ALL -> R.string.filter_group_not_all_hint
|
||||
negated -> R.string.filter_group_not_any_hint
|
||||
match == Match.ALL -> R.string.filter_group_all_hint
|
||||
else -> R.string.filter_group_any_hint
|
||||
},
|
||||
),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
maxLines = 2,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
modifier = Modifier.weight(1f),
|
||||
)
|
||||
var menuOpen by remember { mutableStateOf(false) }
|
||||
Box {
|
||||
IconButton(onClick = { menuOpen = true }) {
|
||||
Icon(Icons.Rounded.MoreVert, contentDescription = stringResource(R.string.filter_group_menu))
|
||||
}
|
||||
DropdownMenu(expanded = menuOpen, onDismissRequest = { menuOpen = false }) {
|
||||
DropdownMenuItem(
|
||||
text = { Text(stringResource(if (negated) R.string.filter_group_negate_off else R.string.filter_group_negate)) },
|
||||
leadingIcon = { Icon(Icons.Rounded.DoNotDisturbOn, contentDescription = null) },
|
||||
onClick = {
|
||||
menuOpen = false
|
||||
onToggleNot()
|
||||
},
|
||||
)
|
||||
if (deletable) {
|
||||
DropdownMenuItem(
|
||||
text = { Text(stringResource(R.string.filter_group_delete)) },
|
||||
leadingIcon = { Icon(Icons.Rounded.Delete, contentDescription = null) },
|
||||
onClick = {
|
||||
menuOpen = false
|
||||
onDelete()
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* One condition: what it checks, which a tap changes, its operator, which a tap
|
||||
* turns round, and its value, which a tap on the row changes; ✕ removes it.
|
||||
*/
|
||||
@Composable
|
||||
private fun ConditionRow(
|
||||
condition: Rule,
|
||||
listNames: Map<Long, String>,
|
||||
onField: () -> Unit,
|
||||
onValue: () -> Unit,
|
||||
onToggleNot: () -> Unit,
|
||||
onRemove: () -> Unit,
|
||||
) {
|
||||
val field = condition.field ?: return
|
||||
GroupedRow(
|
||||
title = conditionValue(condition.positive, listNames),
|
||||
position = Position.Alone,
|
||||
minHeight = FloretDensity.nestedRowHeight,
|
||||
leading = {
|
||||
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp)) {
|
||||
InlineChip(text = fieldLabel(field), onClick = onField, quiet = true)
|
||||
InlineChip(text = operatorLabel(field, condition.negated), onClick = onToggleNot, toggles = false)
|
||||
}
|
||||
},
|
||||
trailing = {
|
||||
IconButton(onClick = onRemove) {
|
||||
Icon(Icons.Rounded.Close, contentDescription = stringResource(R.string.filter_rule_remove))
|
||||
}
|
||||
},
|
||||
onClick = onValue,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* A small pill inside a row, tapped in place: a group's kind, a condition's field
|
||||
* or operator. [toggles] marks one that switches between two values, which reads
|
||||
* as a switch to accessibility; [quiet] draws it without a container.
|
||||
*/
|
||||
@Composable
|
||||
private fun InlineChip(text: String, onClick: () -> Unit, toggles: Boolean = true, quiet: Boolean = false) {
|
||||
Surface(
|
||||
onClick = onClick,
|
||||
shape = CircleShape,
|
||||
color = if (quiet) MaterialTheme.colorScheme.surfaceContainerHighest else MaterialTheme.colorScheme.secondaryContainer,
|
||||
contentColor = if (quiet) MaterialTheme.colorScheme.onSurface else MaterialTheme.colorScheme.onSecondaryContainer,
|
||||
modifier = Modifier.semantics {
|
||||
role = Role.Button
|
||||
if (toggles) stateDescription = text
|
||||
},
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.heightIn(min = 32.dp).padding(start = 12.dp, end = if (toggles) 4.dp else 12.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Text(text, style = MaterialTheme.typography.labelLarge, maxLines = 1)
|
||||
if (toggles) Icon(Icons.Rounded.ArrowDropDown, contentDescription = null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun operatorLabel(field: RuleField, negated: Boolean): String = stringResource(
|
||||
when {
|
||||
field == RuleField.PRIORITY -> if (negated) R.string.filter_op_below else R.string.filter_op_at_least
|
||||
negated -> R.string.filter_op_is_not
|
||||
else -> R.string.filter_op_is
|
||||
},
|
||||
)
|
||||
|
||||
/** What a condition compares against, as in "Work, Home", "focus", "High" or "Next 7 days". */
|
||||
@Composable
|
||||
private fun conditionValue(condition: Rule, listNames: Map<Long, String>): String = when (condition) {
|
||||
is Rule.InList -> {
|
||||
val deleted = stringResource(R.string.view_scope_list_deleted)
|
||||
val elsewhere = stringResource(R.string.view_scope_list_elsewhere)
|
||||
val names = condition.listIds.map { listNames[it] ?: deleted }.sorted() + List(condition.elsewhere.size) { elsewhere }
|
||||
if (names.isEmpty()) stringResource(R.string.filter_value_no_list) else names.joinToString(", ")
|
||||
}
|
||||
is Rule.HasTag -> condition.tag
|
||||
is Rule.MinPriority -> priorityName(condition.priority)
|
||||
is Rule.Due -> dueLabel(condition.spec)
|
||||
else -> ""
|
||||
}
|
||||
@@ -4,19 +4,16 @@ import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.ChevronRight
|
||||
import androidx.compose.material.icons.rounded.Delete
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
@@ -24,27 +21,16 @@ import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import de.jeanlucmakiola.agendula.R
|
||||
import de.jeanlucmakiola.agendula.domain.DueWindow
|
||||
import de.jeanlucmakiola.agendula.domain.FilterCriteria
|
||||
import de.jeanlucmakiola.agendula.domain.Priority
|
||||
import de.jeanlucmakiola.agendula.domain.SavedFilter
|
||||
import de.jeanlucmakiola.agendula.domain.TagMatch
|
||||
import de.jeanlucmakiola.agendula.domain.TaskList
|
||||
import de.jeanlucmakiola.agendula.ui.common.ListColorChip
|
||||
import de.jeanlucmakiola.agendula.ui.common.NameField
|
||||
import de.jeanlucmakiola.floret.components.CollapsingScaffold
|
||||
import de.jeanlucmakiola.floret.components.GroupedRow
|
||||
import de.jeanlucmakiola.floret.components.GroupedSectionHeader
|
||||
import de.jeanlucmakiola.floret.components.OptionPicker
|
||||
import de.jeanlucmakiola.floret.components.Position
|
||||
import de.jeanlucmakiola.floret.components.SelectedCheck
|
||||
import de.jeanlucmakiola.floret.components.positionOf
|
||||
|
||||
/**
|
||||
* Creates or edits a [SavedFilter]: a name, then due windows, priority, lists and
|
||||
* tags. Each multi-choice section leads with an "any" row that is selected while
|
||||
* nothing in it is, so an untouched section visibly doesn't narrow. A filter
|
||||
* whose rule has no flat form can only be renamed or deleted here.
|
||||
* Creates or edits a [SavedFilter]: a name, then its rules in the simple or the
|
||||
* advanced editor (see [RuleEditorSections]). A new filter starts by picking a
|
||||
* template, or none.
|
||||
*/
|
||||
@Composable
|
||||
fun FilterEditScreen(
|
||||
@@ -58,8 +44,6 @@ fun FilterEditScreen(
|
||||
val lists by viewModel.lists.collectAsStateWithLifecycle()
|
||||
val tagsInUse by viewModel.tags.collectAsStateWithLifecycle()
|
||||
val filter = draft?.filter
|
||||
val criteria = draft?.criteria
|
||||
var choosingPriority by rememberSaveable { mutableStateOf(false) }
|
||||
var confirmDelete by rememberSaveable { mutableStateOf(false) }
|
||||
val save = { viewModel.save(onSaved) }
|
||||
|
||||
@@ -75,23 +59,18 @@ fun FilterEditScreen(
|
||||
) { Text(stringResource(R.string.save)) }
|
||||
},
|
||||
) {
|
||||
if (filter == null) return@CollapsingScaffold
|
||||
val current = draft ?: return@CollapsingScaffold
|
||||
val filter = current.filter
|
||||
|
||||
NameField(
|
||||
name = filter.name,
|
||||
placeholder = stringResource(R.string.filter_name_hint),
|
||||
autoFocus = isNew,
|
||||
autoFocus = isNew && !current.choosingTemplate,
|
||||
onNameChange = viewModel::rename,
|
||||
onImeAction = save,
|
||||
)
|
||||
|
||||
// A rule the flat sections can't show is kept as it is: only the name changes here.
|
||||
if (criteria == null) {
|
||||
Spacer(Modifier.height(16.dp))
|
||||
GroupedRow(title = stringResource(R.string.filter_rules_not_editable), position = Position.Alone)
|
||||
} else {
|
||||
CriteriaSections(criteria, lists, tagsInUse, viewModel, onChoosePriority = { choosingPriority = true })
|
||||
}
|
||||
RuleEditorSections(current.rules, lists, tagsInUse, onChange = viewModel::updateRules)
|
||||
|
||||
if (filter.id > 0) {
|
||||
Spacer(Modifier.height(24.dp))
|
||||
@@ -106,14 +85,11 @@ fun FilterEditScreen(
|
||||
Spacer(Modifier.height(24.dp))
|
||||
}
|
||||
|
||||
if (choosingPriority && criteria != null) {
|
||||
OptionPicker(
|
||||
title = stringResource(R.string.filter_section_priority),
|
||||
options = Priority.entries,
|
||||
selected = criteria.minPriority,
|
||||
label = { minPriorityLabel(it) },
|
||||
onSelect = { priority -> viewModel.updateCriteria { it.copy(minPriority = priority) } },
|
||||
onDismiss = { choosingPriority = false },
|
||||
if (draft?.choosingTemplate == true) {
|
||||
TemplatePicker(
|
||||
title = stringResource(R.string.filter_new_title),
|
||||
onPick = { viewModel.startFrom(it, lists, tagsInUse) },
|
||||
onDismiss = onBack,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -133,149 +109,3 @@ fun FilterEditScreen(
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** The flat criteria: due windows, priority, lists and tags. */
|
||||
@Composable
|
||||
private fun CriteriaSections(
|
||||
criteria: FilterCriteria,
|
||||
lists: List<TaskList>,
|
||||
tagsInUse: List<String>,
|
||||
viewModel: FilterEditViewModel,
|
||||
onChoosePriority: () -> Unit,
|
||||
) {
|
||||
GroupedSectionHeader(stringResource(R.string.filter_section_due))
|
||||
val windows = DueWindow.entries
|
||||
ChoiceRow(
|
||||
title = stringResource(R.string.filter_any_due),
|
||||
position = positionOf(0, windows.size + 1),
|
||||
selected = criteria.due.isEmpty(),
|
||||
onClick = { viewModel.updateCriteria { it.copy(due = emptySet()) } },
|
||||
)
|
||||
windows.forEachIndexed { index, window ->
|
||||
ChoiceRow(
|
||||
title = dueWindowLabel(window),
|
||||
position = positionOf(index + 1, windows.size + 1),
|
||||
selected = window in criteria.due,
|
||||
onClick = { viewModel.updateCriteria { it.copy(due = it.due.toggle(window)) } },
|
||||
)
|
||||
}
|
||||
|
||||
GroupedSectionHeader(stringResource(R.string.filter_section_priority))
|
||||
GroupedRow(
|
||||
title = minPriorityLabel(criteria.minPriority),
|
||||
position = Position.Alone,
|
||||
trailing = { Icon(Icons.Rounded.ChevronRight, contentDescription = null) },
|
||||
onClick = onChoosePriority,
|
||||
)
|
||||
|
||||
if (lists.isNotEmpty()) {
|
||||
GroupedSectionHeader(stringResource(R.string.filter_section_lists))
|
||||
val showAccount = lists.mapTo(HashSet()) { it.accountName }.size > 1
|
||||
ChoiceRow(
|
||||
title = stringResource(R.string.filter_any_list),
|
||||
position = positionOf(0, lists.size + 1),
|
||||
selected = criteria.anyList,
|
||||
onClick = { viewModel.updateCriteria { it.copy(listIds = emptySet(), listsElsewhere = emptySet()) } },
|
||||
)
|
||||
lists.forEachIndexed { index, list ->
|
||||
ChoiceRow(
|
||||
title = list.name,
|
||||
summary = list.accountName.takeIf { showAccount },
|
||||
position = positionOf(index + 1, lists.size + 1),
|
||||
selected = list.id in criteria.listIds,
|
||||
leading = { ListColorChip(list.color) },
|
||||
onClick = { viewModel.updateCriteria { it.copy(listIds = it.listIds.toggle(list.id)) } },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// A tag no open task carries any more stays listed while the filter uses
|
||||
// it, so it can still be unticked.
|
||||
val tags = remember(tagsInUse, criteria.tags) {
|
||||
(tagsInUse + criteria.tags).distinct().sortedWith(String.CASE_INSENSITIVE_ORDER)
|
||||
}
|
||||
if (tags.isNotEmpty()) {
|
||||
GroupedSectionHeader(stringResource(R.string.filter_section_tags))
|
||||
ChoiceRow(
|
||||
title = stringResource(R.string.filter_any_tag),
|
||||
position = positionOf(0, tags.size + 1),
|
||||
selected = criteria.tags.isEmpty(),
|
||||
onClick = { viewModel.updateCriteria { it.copy(tags = emptySet()) } },
|
||||
)
|
||||
tags.forEachIndexed { index, tag ->
|
||||
ChoiceRow(
|
||||
title = tag,
|
||||
position = positionOf(index + 1, tags.size + 1),
|
||||
selected = tag in criteria.tags,
|
||||
onClick = { viewModel.updateCriteria { it.copy(tags = it.tags.toggle(tag)) } },
|
||||
)
|
||||
}
|
||||
if (criteria.tags.size > 1) {
|
||||
Spacer(Modifier.height(8.dp))
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.filter_match_all_tags),
|
||||
summary = stringResource(
|
||||
if (criteria.tagMatch == TagMatch.ALL) R.string.filter_match_all_on else R.string.filter_match_all_off,
|
||||
),
|
||||
position = Position.Alone,
|
||||
trailing = {
|
||||
Switch(
|
||||
checked = criteria.tagMatch == TagMatch.ALL,
|
||||
onCheckedChange = { all ->
|
||||
viewModel.updateCriteria { it.copy(tagMatch = if (all) TagMatch.ALL else TagMatch.ANY) }
|
||||
},
|
||||
)
|
||||
},
|
||||
onClick = {
|
||||
viewModel.updateCriteria { it.copy(tagMatch = if (it.tagMatch == TagMatch.ALL) TagMatch.ANY else TagMatch.ALL) }
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ChoiceRow(
|
||||
title: String,
|
||||
position: Position,
|
||||
selected: Boolean,
|
||||
onClick: () -> Unit,
|
||||
summary: String? = null,
|
||||
leading: @Composable (() -> Unit)? = null,
|
||||
) {
|
||||
GroupedRow(
|
||||
title = title,
|
||||
summary = summary,
|
||||
position = position,
|
||||
selected = selected,
|
||||
leading = leading,
|
||||
trailing = if (selected) {
|
||||
{ SelectedCheck() }
|
||||
} else {
|
||||
null
|
||||
},
|
||||
onClick = onClick,
|
||||
)
|
||||
}
|
||||
|
||||
private fun <T> Set<T>.toggle(item: T): Set<T> = if (item in this) this - item else this + item
|
||||
|
||||
@Composable
|
||||
private fun dueWindowLabel(window: DueWindow): String = stringResource(
|
||||
when (window) {
|
||||
DueWindow.OVERDUE -> R.string.smart_overdue
|
||||
DueWindow.TODAY -> R.string.smart_today
|
||||
DueWindow.UPCOMING -> R.string.smart_upcoming
|
||||
DueWindow.NO_DATE -> R.string.smart_no_date
|
||||
},
|
||||
)
|
||||
|
||||
@Composable
|
||||
internal fun minPriorityLabel(priority: Priority): String = stringResource(
|
||||
when (priority) {
|
||||
Priority.NONE -> R.string.filter_priority_any
|
||||
Priority.LOW -> R.string.filter_priority_low
|
||||
Priority.MEDIUM -> R.string.filter_priority_medium
|
||||
Priority.HIGH -> R.string.filter_priority_high
|
||||
},
|
||||
)
|
||||
@@ -5,7 +5,7 @@ import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import de.jeanlucmakiola.agendula.data.prefs.SettingsPrefs
|
||||
import de.jeanlucmakiola.agendula.data.tasks.TasksRepository
|
||||
import de.jeanlucmakiola.agendula.domain.FilterCriteria
|
||||
import de.jeanlucmakiola.agendula.domain.Rule
|
||||
import de.jeanlucmakiola.agendula.domain.SavedFilter
|
||||
import de.jeanlucmakiola.agendula.domain.SmartList
|
||||
import de.jeanlucmakiola.agendula.domain.TaskFilter
|
||||
@@ -33,12 +33,10 @@ class FilterEditViewModel @Inject constructor(
|
||||
) : ViewModel() {
|
||||
|
||||
/**
|
||||
* The filter being edited and its [criteria], kept apart so a choice the
|
||||
* rule can't hold, like all tags with none picked, survives while editing.
|
||||
* [criteria] is `null` when the rule has no flat form; [filter] then keeps
|
||||
* its rule as loaded.
|
||||
* The filter being edited, by name; its rule is [rules]' and goes into it on
|
||||
* save. A new filter starts [choosingTemplate].
|
||||
*/
|
||||
data class Draft(val filter: SavedFilter, val criteria: FilterCriteria?)
|
||||
data class Draft(val filter: SavedFilter, val rules: RuleDraft, val choosingTemplate: Boolean = false)
|
||||
|
||||
private val _draft = MutableStateFlow<Draft?>(null)
|
||||
|
||||
@@ -62,7 +60,7 @@ class FilterEditViewModel @Inject constructor(
|
||||
if (bound) return
|
||||
bound = true
|
||||
if (filterId == null) {
|
||||
load(SavedFilter(id = 0, name = ""))
|
||||
_draft.value = Draft(SavedFilter(id = 0, name = ""), RuleDraft.of(Rule.Everything), choosingTemplate = true)
|
||||
return
|
||||
}
|
||||
viewModelScope.launch {
|
||||
@@ -77,20 +75,27 @@ class FilterEditViewModel @Inject constructor(
|
||||
_draft.update { it?.copy(filter = it.filter.copy(name = name)) }
|
||||
}
|
||||
|
||||
/** Changes the criteria, and the rule with them; a filter without a flat form is left as it is. */
|
||||
fun updateCriteria(transform: (FilterCriteria) -> FilterCriteria) {
|
||||
/** Changes the rules being edited. */
|
||||
fun updateRules(transform: (RuleDraft) -> RuleDraft) {
|
||||
_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 ->
|
||||
val criteria = draft?.criteria?.let(transform) ?: return@update draft
|
||||
Draft(draft.filter.copy(rule = criteria.toRule()), criteria)
|
||||
draft?.copy(
|
||||
rules = template?.let { RuleDraft.of(it.form(lists, tags)) } ?: draft.rules,
|
||||
choosingTemplate = false,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun load(filter: SavedFilter) {
|
||||
_draft.value = Draft(filter, filter.criteria)
|
||||
_draft.value = Draft(filter, RuleDraft.of(filter.rule))
|
||||
}
|
||||
|
||||
fun save(onSaved: (Long) -> Unit) {
|
||||
val filter = _draft.value?.filter ?: return
|
||||
val filter = _draft.value?.let { it.filter.copy(rule = it.rules.rule) } ?: return
|
||||
if (filter.name.isBlank() || saving) return
|
||||
saving = true
|
||||
viewModelScope.launch {
|
||||
|
||||
@@ -0,0 +1,305 @@
|
||||
package de.jeanlucmakiola.agendula.ui.filters
|
||||
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.rounded.ListAlt
|
||||
import androidx.compose.material.icons.rounded.AccountTree
|
||||
import androidx.compose.material.icons.rounded.ChevronRight
|
||||
import androidx.compose.material.icons.rounded.Tune
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.pluralStringResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import de.jeanlucmakiola.agendula.R
|
||||
import de.jeanlucmakiola.agendula.domain.DueSpec
|
||||
import de.jeanlucmakiola.agendula.domain.DueWindow
|
||||
import de.jeanlucmakiola.agendula.domain.Priority
|
||||
import de.jeanlucmakiola.agendula.domain.TagMatch
|
||||
import de.jeanlucmakiola.agendula.domain.TaskList
|
||||
import de.jeanlucmakiola.agendula.ui.common.ListColorChip
|
||||
import de.jeanlucmakiola.floret.components.ConnectedChoiceRow
|
||||
import de.jeanlucmakiola.floret.components.GroupedRow
|
||||
import de.jeanlucmakiola.floret.components.GroupedSectionHeader
|
||||
import de.jeanlucmakiola.floret.components.GroupedSwitchRow
|
||||
import de.jeanlucmakiola.floret.components.OptionPicker
|
||||
import de.jeanlucmakiola.floret.components.Position
|
||||
import de.jeanlucmakiola.floret.components.positionOf
|
||||
|
||||
/**
|
||||
* The rules of a filter, scope or block: the simple editor, or the advanced one
|
||||
* for a rule it can't show, with a row to switch between them. [onChange] gets
|
||||
* each change to apply to the draft.
|
||||
*/
|
||||
@Composable
|
||||
internal fun RuleEditorSections(
|
||||
draft: RuleDraft,
|
||||
lists: List<TaskList>,
|
||||
tagsInUse: List<String>,
|
||||
onChange: ((RuleDraft) -> RuleDraft) -> Unit,
|
||||
) {
|
||||
val simple = draft.simple
|
||||
if (simple != null) {
|
||||
SimpleSections(simple, lists, tagsInUse, onChange = { transform -> onChange { it.editSimple(transform) } })
|
||||
Spacer(Modifier.height(16.dp))
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.filter_advanced),
|
||||
summary = stringResource(R.string.filter_advanced_hint),
|
||||
position = Position.Alone,
|
||||
leading = { Icon(Icons.Rounded.AccountTree, contentDescription = null) },
|
||||
trailing = { Icon(Icons.Rounded.ChevronRight, contentDescription = null) },
|
||||
onClick = { onChange { it.toAdvanced() } },
|
||||
)
|
||||
} else {
|
||||
AdvancedEditor(draft.rule, lists, tagsInUse, onChange = { transform -> onChange { it.editAdvanced(transform) } })
|
||||
Spacer(Modifier.height(16.dp))
|
||||
val canSimplify = draft.canSimplify
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.filter_simple),
|
||||
summary = stringResource(if (canSimplify) R.string.filter_simple_hint else R.string.filter_simple_unavailable),
|
||||
position = Position.Alone,
|
||||
dimmed = !canSimplify,
|
||||
leading = { Icon(Icons.Rounded.Tune, contentDescription = null) },
|
||||
trailing = if (canSimplify) {
|
||||
{ Icon(Icons.Rounded.ChevronRight, contentDescription = null) }
|
||||
} else {
|
||||
null
|
||||
},
|
||||
onClick = if (canSimplify) ({ onChange { it.toSimple() } }) else null,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The simple editor: *all / any* on top, then lists, tags, due and priority,
|
||||
* each turned round by *is / is not* where that reads. Each multi-choice section
|
||||
* leads with an "any" row that is selected while nothing in it is, so an
|
||||
* untouched section visibly doesn't narrow.
|
||||
*/
|
||||
@Composable
|
||||
private fun SimpleSections(
|
||||
form: SimpleRule,
|
||||
lists: List<TaskList>,
|
||||
tagsInUse: List<String>,
|
||||
onChange: ((SimpleRule) -> SimpleRule) -> Unit,
|
||||
) {
|
||||
var choosingPriority by rememberSaveable { mutableStateOf(false) }
|
||||
var choosingDays by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
GroupedSectionHeader(stringResource(R.string.filter_match_title))
|
||||
ConnectedChoiceRow(
|
||||
options = Match.entries,
|
||||
selected = form.match,
|
||||
label = { stringResource(if (it == Match.ALL) R.string.filter_match_all else R.string.filter_match_any) },
|
||||
onSelect = { match -> onChange { it.copy(match = match) } },
|
||||
)
|
||||
|
||||
if (lists.isNotEmpty()) {
|
||||
NegatableHeader(stringResource(R.string.filter_section_lists), form.listsNegated) { negated ->
|
||||
onChange { it.copy(listsNegated = negated) }
|
||||
}
|
||||
val showAccount = lists.mapTo(HashSet()) { it.accountName }.size > 1
|
||||
ChoiceRow(
|
||||
title = stringResource(R.string.filter_any_list),
|
||||
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()) } },
|
||||
)
|
||||
lists.forEachIndexed { index, list ->
|
||||
ChoiceRow(
|
||||
title = list.name,
|
||||
summary = list.accountName.takeIf { showAccount },
|
||||
position = positionOf(index + 1, lists.size + 1),
|
||||
selected = list.id in form.listIds,
|
||||
leading = { ListColorChip(list.color) },
|
||||
onClick = { onChange { it.copy(listIds = it.listIds.toggle(list.id)) } },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// A tag no open task carries any more stays listed while the form uses it, so it can still be unticked.
|
||||
val tags = remember(tagsInUse, form.tags) {
|
||||
(tagsInUse + form.tags).distinct().sortedWith(String.CASE_INSENSITIVE_ORDER)
|
||||
}
|
||||
if (tags.isNotEmpty()) {
|
||||
NegatableHeader(stringResource(R.string.filter_section_tags), form.tagsNegated) { 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()) } },
|
||||
)
|
||||
tags.forEachIndexed { index, tag ->
|
||||
ChoiceRow(
|
||||
title = tag,
|
||||
position = positionOf(index + 1, tags.size + 1),
|
||||
selected = tag in form.tags,
|
||||
onClick = { onChange { it.copy(tags = it.tags.toggle(tag)) } },
|
||||
)
|
||||
}
|
||||
if (form.tags.size > 1) {
|
||||
Spacer(Modifier.height(8.dp))
|
||||
GroupedSwitchRow(
|
||||
title = stringResource(R.string.filter_match_all_tags),
|
||||
summary = stringResource(
|
||||
if (form.tagMatch == TagMatch.ALL) R.string.filter_match_all_on else R.string.filter_match_all_off,
|
||||
),
|
||||
checked = form.tagMatch == TagMatch.ALL,
|
||||
onCheckedChange = { all -> onChange { it.copy(tagMatch = if (all) TagMatch.ALL else TagMatch.ANY) } },
|
||||
position = Position.Alone,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
NegatableHeader(stringResource(R.string.filter_section_due), form.dueNegated) { negated ->
|
||||
onChange { it.copy(dueNegated = negated) }
|
||||
}
|
||||
val windows = listOf(DueSpec.Window(DueWindow.OVERDUE), DueSpec.Window(DueWindow.TODAY), DueSpec.Window(DueWindow.UPCOMING))
|
||||
val ranges = listOf(DueSpec.ThisWeek, DueSpec.ThisMonth)
|
||||
val rows = 1 + windows.size + ranges.size + 2
|
||||
ChoiceRow(
|
||||
title = stringResource(R.string.filter_any_due),
|
||||
position = positionOf(0, rows),
|
||||
selected = form.due.isEmpty(),
|
||||
onClick = { onChange { it.copy(due = emptySet()) } },
|
||||
)
|
||||
(windows + ranges).forEachIndexed { index, spec ->
|
||||
ChoiceRow(
|
||||
title = dueLabel(spec),
|
||||
position = positionOf(index + 1, rows),
|
||||
selected = spec in form.due,
|
||||
onClick = { onChange { it.toggleDue(spec) } },
|
||||
)
|
||||
}
|
||||
val nextDays = form.nextDays
|
||||
GroupedRow(
|
||||
title = nextDays?.let { dueLabel(it) } ?: stringResource(R.string.filter_due_next_days_any),
|
||||
position = positionOf(rows - 2, rows),
|
||||
selected = nextDays != null,
|
||||
trailing = { Icon(Icons.Rounded.ChevronRight, contentDescription = null) },
|
||||
onClick = { choosingDays = true },
|
||||
)
|
||||
val noDate = DueSpec.Window(DueWindow.NO_DATE)
|
||||
ChoiceRow(
|
||||
title = dueLabel(noDate),
|
||||
position = positionOf(rows - 1, rows),
|
||||
selected = noDate in form.due,
|
||||
onClick = { onChange { it.toggleDue(noDate) } },
|
||||
)
|
||||
|
||||
GroupedSectionHeader(stringResource(R.string.filter_section_priority))
|
||||
GroupedRow(
|
||||
title = minPriorityLabel(form.minPriority),
|
||||
position = Position.Alone,
|
||||
trailing = { Icon(Icons.Rounded.ChevronRight, contentDescription = null) },
|
||||
onClick = { choosingPriority = true },
|
||||
)
|
||||
|
||||
if (choosingPriority) {
|
||||
OptionPicker(
|
||||
title = stringResource(R.string.filter_section_priority),
|
||||
options = Priority.entries,
|
||||
selected = form.minPriority,
|
||||
label = { minPriorityLabel(it) },
|
||||
onSelect = { priority -> onChange { it.copy(minPriority = priority) } },
|
||||
onDismiss = { choosingPriority = false },
|
||||
)
|
||||
}
|
||||
if (choosingDays) {
|
||||
NextDaysPicker(
|
||||
selected = nextDays?.days,
|
||||
withNone = true,
|
||||
onSelect = { days -> onChange { it.withNextDays(days?.let(DueSpec::NextDays)) } },
|
||||
onDismiss = { choosingDays = false },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** A section header with an *is / is not* switch at its end. */
|
||||
@Composable
|
||||
private fun NegatableHeader(title: String, negated: Boolean, onNegate: (Boolean) -> Unit) {
|
||||
Row(verticalAlignment = Alignment.Bottom) {
|
||||
GroupedSectionHeader(title, Modifier.weight(1f))
|
||||
ConnectedChoiceRow(
|
||||
options = listOf(false, true),
|
||||
selected = negated,
|
||||
label = { stringResource(if (it) R.string.filter_op_is_not else R.string.filter_op_is) },
|
||||
onSelect = onNegate,
|
||||
modifier = Modifier.width(NEGATE_WIDTH).padding(bottom = 4.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** Room for the *is / is not* pair, its inset included. */
|
||||
private val NEGATE_WIDTH = 200.dp
|
||||
|
||||
@Composable
|
||||
internal fun ChoiceRow(
|
||||
title: String,
|
||||
position: Position,
|
||||
selected: Boolean,
|
||||
onClick: () -> Unit,
|
||||
summary: String? = null,
|
||||
leading: @Composable (() -> Unit)? = null,
|
||||
) {
|
||||
GroupedRow(
|
||||
title = title,
|
||||
summary = summary,
|
||||
position = position,
|
||||
checked = selected,
|
||||
leading = leading,
|
||||
onClick = onClick,
|
||||
)
|
||||
}
|
||||
|
||||
internal fun <T> Set<T>.toggle(item: T): Set<T> = if (item in this) this - item else this + item
|
||||
|
||||
/** A due condition's value, as in "Overdue" or "Next 7 days". */
|
||||
@Composable
|
||||
internal fun dueLabel(spec: DueSpec): String = when (spec) {
|
||||
is DueSpec.Window -> stringResource(
|
||||
when (spec.window) {
|
||||
DueWindow.OVERDUE -> R.string.smart_overdue
|
||||
DueWindow.TODAY -> R.string.smart_today
|
||||
DueWindow.UPCOMING -> R.string.smart_upcoming
|
||||
DueWindow.NO_DATE -> R.string.smart_no_date
|
||||
},
|
||||
)
|
||||
DueSpec.ThisWeek -> stringResource(R.string.filter_due_this_week)
|
||||
DueSpec.ThisMonth -> stringResource(R.string.filter_due_this_month)
|
||||
is DueSpec.NextDays -> pluralStringResource(R.plurals.filter_due_next_days, spec.days, spec.days)
|
||||
}
|
||||
|
||||
@Composable
|
||||
internal fun minPriorityLabel(priority: Priority): String = stringResource(
|
||||
when (priority) {
|
||||
Priority.NONE -> R.string.filter_priority_any
|
||||
Priority.LOW -> R.string.filter_priority_low
|
||||
Priority.MEDIUM -> R.string.filter_priority_medium
|
||||
Priority.HIGH -> R.string.filter_priority_high
|
||||
},
|
||||
)
|
||||
|
||||
@Composable
|
||||
internal fun priorityName(priority: Priority): String = stringResource(
|
||||
when (priority) {
|
||||
Priority.NONE -> R.string.priority_none
|
||||
Priority.LOW -> R.string.priority_low
|
||||
Priority.MEDIUM -> R.string.priority_medium
|
||||
Priority.HIGH -> R.string.priority_high
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,294 @@
|
||||
package de.jeanlucmakiola.agendula.ui.filters
|
||||
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.rounded.ListAlt
|
||||
import androidx.compose.material.icons.rounded.Add
|
||||
import androidx.compose.material.icons.rounded.Bolt
|
||||
import androidx.compose.material.icons.rounded.CalendarMonth
|
||||
import androidx.compose.material.icons.rounded.CalendarViewWeek
|
||||
import androidx.compose.material.icons.rounded.Event
|
||||
import androidx.compose.material.icons.rounded.EventBusy
|
||||
import androidx.compose.material.icons.rounded.Flag
|
||||
import androidx.compose.material.icons.rounded.Schedule
|
||||
import androidx.compose.material.icons.rounded.Sell
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.res.pluralStringResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import de.jeanlucmakiola.agendula.R
|
||||
import de.jeanlucmakiola.agendula.domain.DueSpec
|
||||
import de.jeanlucmakiola.agendula.domain.Priority
|
||||
import de.jeanlucmakiola.agendula.domain.TaskList
|
||||
import de.jeanlucmakiola.agendula.ui.common.ListColorChip
|
||||
import de.jeanlucmakiola.agendula.ui.settings.CategoryIcon
|
||||
import de.jeanlucmakiola.agendula.ui.settings.ChipAccent
|
||||
import de.jeanlucmakiola.floret.components.CustomAmountEditor
|
||||
import de.jeanlucmakiola.floret.components.CustomExpandRow
|
||||
import de.jeanlucmakiola.floret.components.FullScreenPicker
|
||||
import de.jeanlucmakiola.floret.components.GroupedHint
|
||||
import de.jeanlucmakiola.floret.components.GroupedRow
|
||||
import de.jeanlucmakiola.floret.components.GroupedSectionHeader
|
||||
import de.jeanlucmakiola.floret.components.OptionPicker
|
||||
import de.jeanlucmakiola.floret.components.Position
|
||||
import de.jeanlucmakiola.floret.components.positionOf
|
||||
|
||||
/**
|
||||
* The "next N days" ranges: the presets, then a custom number. [withNone] leads
|
||||
* with a row that drops the range, for the simple editor, where it is optional.
|
||||
*/
|
||||
@Composable
|
||||
internal fun NextDaysPicker(
|
||||
selected: Int?,
|
||||
withNone: Boolean,
|
||||
onSelect: (Int?) -> Unit,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
FullScreenPicker(title = stringResource(R.string.filter_next_days_title), onDismiss = onDismiss) {
|
||||
val pick = { days: Int? ->
|
||||
onSelect(days)
|
||||
onDismiss()
|
||||
}
|
||||
if (withNone) {
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.filter_next_days_off),
|
||||
position = Position.Alone,
|
||||
checked = selected == null,
|
||||
onClick = { pick(null) },
|
||||
)
|
||||
Spacer(Modifier.height(16.dp))
|
||||
}
|
||||
NextDaysRows(selected, onPick = pick, first = 0, count = NEXT_DAYS_PRESETS.size + 1)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The preset "next N days" rows and the custom one, at [first] of a run of
|
||||
* [count] rows; [onPick] gets the number of days.
|
||||
*/
|
||||
@Composable
|
||||
private fun NextDaysRows(selected: Int?, onPick: (Int) -> Unit, first: Int, count: Int) {
|
||||
NEXT_DAYS_PRESETS.forEachIndexed { index, days ->
|
||||
GroupedRow(
|
||||
title = dueLabel(DueSpec.NextDays(days)),
|
||||
position = positionOf(first + index, count),
|
||||
checked = selected == days,
|
||||
onClick = { onPick(days) },
|
||||
)
|
||||
}
|
||||
val custom = selected != null && selected !in NEXT_DAYS_PRESETS
|
||||
var expanded by rememberSaveable { mutableStateOf(false) }
|
||||
var amountText by rememberSaveable { mutableStateOf(selected?.takeIf { custom }?.toString().orEmpty()) }
|
||||
val value = amountText.toIntOrNull()?.takeIf { it in 1..NEXT_DAYS_MAX }
|
||||
CustomExpandRow(
|
||||
title = if (custom) {
|
||||
stringResource(R.string.reminder_custom_with_value, dueLabel(DueSpec.NextDays(selected!!)))
|
||||
} else {
|
||||
stringResource(R.string.reminder_custom)
|
||||
},
|
||||
position = positionOf(first + NEXT_DAYS_PRESETS.size, count),
|
||||
expanded = expanded,
|
||||
onExpandedChange = { expanded = it },
|
||||
checked = custom,
|
||||
) {
|
||||
CustomAmountEditor(
|
||||
amountText = amountText,
|
||||
onAmountChange = { amountText = it },
|
||||
preview = value?.let { pluralStringResource(R.plurals.filter_due_next_days, it, it) }
|
||||
?: stringResource(R.string.filter_next_days_amount),
|
||||
setLabel = stringResource(R.string.reminder_custom_set),
|
||||
confirmEnabled = value != null,
|
||||
onConfirm = { value?.let(onPick) },
|
||||
placeholder = "7",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** A due condition's value: a window, this week or month, or the next N days. */
|
||||
@Composable
|
||||
internal fun DuePicker(selected: DueSpec?, onSelect: (DueSpec) -> Unit, onDismiss: () -> Unit) {
|
||||
FullScreenPicker(title = stringResource(R.string.filter_field_due), onDismiss = onDismiss) {
|
||||
val pick = { spec: DueSpec ->
|
||||
onSelect(spec)
|
||||
onDismiss()
|
||||
}
|
||||
DUE_CHOICES.forEachIndexed { index, spec ->
|
||||
GroupedRow(
|
||||
title = dueLabel(spec),
|
||||
position = positionOf(index, DUE_CHOICES.size),
|
||||
checked = spec == selected,
|
||||
onClick = { pick(spec) },
|
||||
)
|
||||
}
|
||||
GroupedSectionHeader(stringResource(R.string.filter_next_days_title))
|
||||
NextDaysRows(
|
||||
selected = (selected as? DueSpec.NextDays)?.days,
|
||||
onPick = { pick(DueSpec.NextDays(it)) },
|
||||
first = 0,
|
||||
count = NEXT_DAYS_PRESETS.size + 1,
|
||||
)
|
||||
Spacer(Modifier.height(16.dp))
|
||||
}
|
||||
}
|
||||
|
||||
/** What a new or changed condition checks. */
|
||||
@Composable
|
||||
internal fun FieldPicker(selected: RuleField?, onSelect: (RuleField) -> Unit, onDismiss: () -> Unit) {
|
||||
OptionPicker(
|
||||
title = stringResource(R.string.filter_field_title),
|
||||
options = RuleField.entries,
|
||||
selected = selected,
|
||||
label = { fieldLabel(it!!) },
|
||||
leading = { Icon(fieldIcon(it!!), contentDescription = null) },
|
||||
onSelect = { it?.let(onSelect) },
|
||||
onDismiss = onDismiss,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The lists a list condition names, ticked on and off; [onDone] takes them, and
|
||||
* only while there is one.
|
||||
*/
|
||||
@Composable
|
||||
internal fun ListsPicker(
|
||||
lists: List<TaskList>,
|
||||
selected: Set<Long>,
|
||||
onDone: (Set<Long>) -> Unit,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
var picked by remember { mutableStateOf(selected) }
|
||||
FullScreenPicker(
|
||||
title = stringResource(R.string.filter_section_lists),
|
||||
onDismiss = onDismiss,
|
||||
actions = {
|
||||
TextButton(
|
||||
onClick = {
|
||||
onDone(picked)
|
||||
onDismiss()
|
||||
},
|
||||
enabled = picked.isNotEmpty(),
|
||||
modifier = Modifier.padding(end = 8.dp),
|
||||
) { Text(stringResource(R.string.picker_done)) }
|
||||
},
|
||||
) {
|
||||
if (lists.isEmpty()) {
|
||||
GroupedHint(stringResource(R.string.filter_lists_none))
|
||||
return@FullScreenPicker
|
||||
}
|
||||
val showAccount = lists.mapTo(HashSet()) { it.accountName }.size > 1
|
||||
lists.forEachIndexed { index, list ->
|
||||
ChoiceRow(
|
||||
title = list.name,
|
||||
summary = list.accountName.takeIf { showAccount },
|
||||
position = positionOf(index, lists.size),
|
||||
selected = list.id in picked,
|
||||
leading = { ListColorChip(list.color) },
|
||||
onClick = { picked = picked.toggle(list.id) },
|
||||
)
|
||||
}
|
||||
Spacer(Modifier.height(16.dp))
|
||||
}
|
||||
}
|
||||
|
||||
/** The tag a tag condition names, from the ones in use. */
|
||||
@Composable
|
||||
internal fun TagPicker(tags: List<String>, selected: String?, onSelect: (String) -> Unit, onDismiss: () -> Unit) {
|
||||
val options = remember(tags, selected) {
|
||||
(tags + listOfNotNull(selected)).distinct().sortedWith(String.CASE_INSENSITIVE_ORDER)
|
||||
}
|
||||
if (options.isEmpty()) {
|
||||
FullScreenPicker(title = stringResource(R.string.filter_section_tags), onDismiss = onDismiss) {
|
||||
GroupedHint(stringResource(R.string.filter_tags_none))
|
||||
}
|
||||
return
|
||||
}
|
||||
OptionPicker(
|
||||
title = stringResource(R.string.filter_section_tags),
|
||||
options = options,
|
||||
selected = selected,
|
||||
label = { it.orEmpty() },
|
||||
onSelect = { it?.let(onSelect) },
|
||||
onDismiss = onDismiss,
|
||||
)
|
||||
}
|
||||
|
||||
/** The priority a priority condition asks for at least. */
|
||||
@Composable
|
||||
internal fun PriorityPicker(selected: Priority?, onSelect: (Priority) -> Unit, onDismiss: () -> Unit) {
|
||||
OptionPicker(
|
||||
title = stringResource(R.string.filter_section_priority),
|
||||
options = PRIORITY_CHOICES,
|
||||
selected = selected,
|
||||
label = { priorityName(it!!) },
|
||||
onSelect = { it?.let(onSelect) },
|
||||
onDismiss = onDismiss,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Where new rules start: empty, or one of the [FilterTemplate]s. [onPick] gets
|
||||
* `null` for empty; [onDismiss] leaves without starting.
|
||||
*/
|
||||
@Composable
|
||||
internal fun TemplatePicker(title: String, onPick: (FilterTemplate?) -> Unit, onDismiss: () -> Unit) {
|
||||
FullScreenPicker(title = title, onDismiss = onDismiss) {
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.filter_template_empty),
|
||||
summary = stringResource(R.string.filter_template_empty_hint),
|
||||
position = Position.Alone,
|
||||
leading = { CategoryIcon(Icons.Rounded.Add, ChipAccent.Primary) },
|
||||
onClick = { onPick(null) },
|
||||
)
|
||||
GroupedSectionHeader(stringResource(R.string.filter_templates_header))
|
||||
FilterTemplate.entries.forEachIndexed { index, template ->
|
||||
val (name, hint, icon) = templateLabels(template)
|
||||
GroupedRow(
|
||||
title = stringResource(name),
|
||||
summary = stringResource(hint),
|
||||
position = positionOf(index, FilterTemplate.entries.size),
|
||||
leading = { CategoryIcon(icon, ChipAccent.Primary) },
|
||||
onClick = { onPick(template) },
|
||||
)
|
||||
}
|
||||
GroupedHint(stringResource(R.string.filter_templates_note), verticalPadding = 8.dp)
|
||||
Spacer(Modifier.height(16.dp))
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
FilterTemplate.THIS_MONTH -> Triple(R.string.filter_template_month, R.string.filter_template_month_hint, Icons.Rounded.CalendarMonth)
|
||||
FilterTemplate.HIGH_PRIORITY -> Triple(R.string.filter_template_high, R.string.filter_template_high_hint, Icons.Rounded.Flag)
|
||||
FilterTemplate.WAITING_FOR -> Triple(R.string.filter_template_waiting, R.string.filter_template_waiting_hint, Icons.Rounded.Schedule)
|
||||
FilterTemplate.NO_DATE -> Triple(R.string.filter_template_no_date, R.string.filter_template_no_date_hint, Icons.Rounded.EventBusy)
|
||||
}
|
||||
|
||||
@Composable
|
||||
internal fun fieldLabel(field: RuleField): String = stringResource(
|
||||
when (field) {
|
||||
RuleField.LIST -> R.string.filter_field_list
|
||||
RuleField.TAG -> R.string.filter_field_tag
|
||||
RuleField.PRIORITY -> R.string.filter_field_priority
|
||||
RuleField.DUE -> R.string.filter_field_due
|
||||
},
|
||||
)
|
||||
|
||||
internal fun fieldIcon(field: RuleField): ImageVector = when (field) {
|
||||
RuleField.LIST -> Icons.AutoMirrored.Rounded.ListAlt
|
||||
RuleField.TAG -> Icons.Rounded.Sell
|
||||
RuleField.PRIORITY -> Icons.Rounded.Flag
|
||||
RuleField.DUE -> Icons.Rounded.Event
|
||||
}
|
||||
@@ -308,7 +308,73 @@
|
||||
<string name="filter_delete_body">Only the filter goes. Its tasks stay where they are.</string>
|
||||
<string name="filter_delete_confirm">Delete</string>
|
||||
<string name="filter_missing">Filter deleted</string>
|
||||
<string name="filter_rules_not_editable">This filter uses rules that can’t be edited here yet. You can still rename or delete it.</string>
|
||||
<string name="filter_match_title">Show tasks that match</string>
|
||||
<string name="filter_match_all">All rules</string>
|
||||
<string name="filter_match_any">Any rule</string>
|
||||
<string name="filter_op_is">is</string>
|
||||
<string name="filter_op_is_not">is not</string>
|
||||
<string name="filter_op_at_least">at least</string>
|
||||
<string name="filter_op_below">below</string>
|
||||
<string name="filter_due_this_week">This week</string>
|
||||
<string name="filter_due_this_month">This month</string>
|
||||
<plurals name="filter_due_next_days">
|
||||
<item quantity="one">Next %d day</item>
|
||||
<item quantity="other">Next %d days</item>
|
||||
</plurals>
|
||||
<string name="filter_due_next_days_any">Next days…</string>
|
||||
<string name="filter_next_days_off">Off</string>
|
||||
<string name="filter_next_days_title">Within the next days</string>
|
||||
<string name="filter_next_days_amount">Enter a number of days</string>
|
||||
<string name="filter_advanced">Advanced editor</string>
|
||||
<string name="filter_advanced_hint">Nested groups with all / any / not</string>
|
||||
<string name="filter_advanced_note">Rules the simple editor can’t show, like nested groups or “not” on a group, always open here.</string>
|
||||
<string name="filter_simple">Simple editor</string>
|
||||
<string name="filter_simple_hint">Lists, tags, due date and priority</string>
|
||||
<string name="filter_simple_unavailable">These rules need the advanced editor</string>
|
||||
<string name="filter_group_all">All of</string>
|
||||
<string name="filter_group_any">Any of</string>
|
||||
<string name="filter_group_not">Not</string>
|
||||
<string name="filter_group_all_hint">every rule must match</string>
|
||||
<string name="filter_group_any_hint">one is enough</string>
|
||||
<string name="filter_group_not_all_hint">leaves out tasks matching every rule</string>
|
||||
<string name="filter_group_not_any_hint">leaves out tasks matching any rule</string>
|
||||
<string name="filter_group_empty_all">No rules yet: every task matches.</string>
|
||||
<string name="filter_group_empty_any">No rules yet: no task matches.</string>
|
||||
<string name="filter_group_menu">Group options</string>
|
||||
<string name="filter_group_negate">Negate group (not)</string>
|
||||
<string name="filter_group_negate_off">Remove “not”</string>
|
||||
<string name="filter_group_delete">Delete group</string>
|
||||
<string name="filter_add_rule">Rule</string>
|
||||
<string name="filter_add_group">Group</string>
|
||||
<string name="filter_rule_remove">Remove rule</string>
|
||||
<string name="filter_field_title">Rule on</string>
|
||||
<string name="filter_field_list">List</string>
|
||||
<string name="filter_field_tag">Tag</string>
|
||||
<string name="filter_field_priority">Priority</string>
|
||||
<string name="filter_field_due">Due</string>
|
||||
<string name="filter_value_no_list">No list</string>
|
||||
<string name="filter_lists_none">No lists yet.</string>
|
||||
<string name="filter_tags_none">No open task has a tag yet.</string>
|
||||
<string name="filter_template_empty">Start empty</string>
|
||||
<string name="filter_template_empty_hint">Pick every rule yourself</string>
|
||||
<string name="filter_templates_header">Templates</string>
|
||||
<string name="filter_templates_note">A template only fills in the rules. You can change everything afterwards.</string>
|
||||
<string name="filter_template_gtd">Next actions (GTD)</string>
|
||||
<string name="filter_template_gtd_hint">List “Next” or tag nextAction</string>
|
||||
<string name="filter_template_week">This week</string>
|
||||
<string name="filter_template_week_hint">Due this week, overdue included</string>
|
||||
<string name="filter_template_month">This month</string>
|
||||
<string name="filter_template_month_hint">Due this month</string>
|
||||
<string name="filter_template_high">High priority</string>
|
||||
<string name="filter_template_high_hint">Priority high, any list</string>
|
||||
<string name="filter_template_waiting">Waiting for</string>
|
||||
<string name="filter_template_waiting_hint">Tag waiting</string>
|
||||
<string name="filter_template_no_date">No date</string>
|
||||
<string name="filter_template_no_date_hint">Open tasks without a due date</string>
|
||||
<string name="rule_scope_title">Edit scope</string>
|
||||
<string name="rule_source_title">Custom rules</string>
|
||||
<string name="rule_save_as_filter">Save as filter</string>
|
||||
<string name="rule_save_as_filter_hint">Keep these rules as a filter, and show the filter here</string>
|
||||
<string name="new_task">New task</string>
|
||||
<string name="lists_failure">Could not read your tasks.</string>
|
||||
<string name="lists_empty">No task lists yet.</string>
|
||||
@@ -1126,6 +1192,9 @@
|
||||
<string name="view_source_gone">This block’s list or filter was deleted.</string>
|
||||
<string name="view_choose_source">Choose source</string>
|
||||
<string name="view_source_smart">Smart lists</string>
|
||||
<string name="view_source_custom">Custom rules…</string>
|
||||
<string name="view_source_custom_hint">Lists, tags, due dates and priority</string>
|
||||
<string name="view_source_custom_label">Custom rules</string>
|
||||
<string name="view_scope_list_gone">A list in this view’s scope was deleted. That part of the scope now matches nothing.</string>
|
||||
<string name="view_edit_scope">Edit scope</string>
|
||||
<!-- A view's scope in one line, built from these pieces -->
|
||||
|
||||
Reference in new issue
Block a user