Custom rules sources work in every block: a task screen for a bare rule, and progress of any source (#59, #172)

This commit is contained in:
makiolaj committed 2026-11-27 15:16:52 +01:00
1 parent e84e19fd5c
commit e8a73d5e80
17 files changed
+163 -26

No files matched your search

@@ -99,6 +99,9 @@ class TasksRepositoryImpl @Inject constructor(
}
}
}
// Read as a saved filter's rule is, with the week start only when the rule has weeks in it.
is TaskFilter.Matching -> (if (filter.rule.readsWeekStart) settingsPrefs.firstDayOfWeek else flowOf(DayOfWeek.MONDAY))
.flatMapLatest { weekStart -> observing { FilteredTasks(loadTasks(filter, weekStart = weekStart)) } }
else -> observing { FilteredTasks(loadTasks(filter)) }
}
@@ -135,10 +138,11 @@ class TasksRepositoryImpl @Inject constructor(
is TaskFilter.Tagged -> TaskQuery(includeCompleted = true)
is TaskFilter.Saved -> TaskQuery(includeCompleted = true)
is TaskFilter.DueOn -> TaskQuery(includeCompleted = true)
is TaskFilter.Matching -> TaskQuery(includeCompleted = true)
}
val zone = ZoneId.systemDefault()
val (todayStart, todayEnd) = DayWindow.today(Clock.System.now(), zone)
val clock = saved?.let { RuleClock(todayStart, todayEnd, zone, weekStart) }
val clock = if (saved != null || filter is TaskFilter.Matching) RuleClock(todayStart, todayEnd, zone, weekStart) else null
val all = SeriesCollapse.visible(dataSource.tasks(query))
// (done, total) of direct children per parent, over the unfiltered set.
val progress: Map<Long, Pair<Int, Int>> = all
@@ -27,6 +27,12 @@ sealed interface TaskFilter {
/** Every task due on [date], open and completed: a day of the week strip. */
data class DueOn(val date: LocalDate) : TaskFilter
/**
* Every task [rule] admits, open and completed: a block's custom rules,
* read as a [Saved] filter's rule is. [title] names its screen.
*/
data class Matching(val rule: Rule, val title: String? = null) : TaskFilter
}
/**
@@ -58,6 +64,7 @@ object TaskFiltering {
is TaskFilter.Saved -> ruleClock != null &&
saved?.takeIf { it.id == filter.id }?.matches(task, ruleClock) == true
is TaskFilter.DueOn -> task.dateOf(TaskEnd.DUE, ruleClock?.zone ?: ZoneId.systemDefault()) == filter.date
is TaskFilter.Matching -> ruleClock != null && filter.rule.matches(task, ruleClock)
}
/**
@@ -104,6 +104,7 @@ object TaskGroups {
is TaskFilter.Tagged -> emptySet()
// A single day has no later days to split.
is TaskFilter.DueOn -> setOf(TaskGrouping.DAY)
is TaskFilter.Matching -> emptySet()
}
return TaskGrouping.entries - dropped
}
@@ -141,4 +142,6 @@ fun TaskFilter.groupingKey(): String? = when (this) {
is TaskFilter.OfLists -> null
// One grouping for every day the week strip opens.
is TaskFilter.DueOn -> "dueOn"
// One grouping for every block's custom rules.
is TaskFilter.Matching -> "rules"
}
@@ -138,7 +138,8 @@ val BlockSource.taskFilter: TaskFilter?
is BlockSource.List -> TaskFilter.OfList(listId)
is BlockSource.Tag -> TaskFilter.Tagged(tag)
is BlockSource.Filter -> TaskFilter.Saved(filterId)
is BlockSource.ListElsewhere, is BlockSource.Rules -> null
is BlockSource.Rules -> TaskFilter.Matching(rule)
is BlockSource.ListElsewhere -> null
}
/**
@@ -35,6 +35,7 @@ import androidx.compose.material.icons.rounded.FilterList
import androidx.compose.material.icons.rounded.Flag
import androidx.compose.material.icons.rounded.Sell
import androidx.compose.material.icons.rounded.SwapVert
import androidx.compose.material.icons.rounded.TaskAlt
import androidx.compose.material.icons.rounded.Today
import androidx.compose.material.icons.rounded.Upcoming
import androidx.compose.material3.Checkbox
@@ -128,7 +129,7 @@ internal val TILES = setOf(BlockType.PROGRESS, BlockType.COUNT_TILE)
internal fun blockTitle(block: Block, state: ListsUiState.Content): String {
val source = block.source
return when (val options = block.options) {
is BlockOptions.Progress -> options.title ?: stringResource(R.string.smart_today)
is BlockOptions.Progress -> options.title ?: source?.let { sourceLabel(it, state) } ?: stringResource(R.string.smart_today)
is BlockOptions.CountTile -> options.title ?: source?.let { sourceLabel(it, state) }.orEmpty()
is BlockOptions.Preview -> options.title ?: source?.let { sourceLabel(it, state) }.orEmpty()
is BlockOptions.RecentlyCompleted -> options.title ?: stringResource(R.string.block_recent)
@@ -152,6 +153,14 @@ internal fun sourceLabel(source: BlockSource, state: ListsUiState.Content): Stri
is BlockSource.ListElsewhere -> ""
}
/** The task screen [block] opens: its source's, a block's custom rules named after it. */
@Composable
internal fun blockFilter(block: Block, state: ListsUiState.Content): TaskFilter? =
when (val filter = block.source?.taskFilter) {
is TaskFilter.Matching -> filter.copy(title = blockTitle(block, state).ifBlank { null })
else -> filter
}
/** Whether to mark [block] as reading past its view's scope. */
internal fun ListsUiState.Content.marksIgnoredScope(block: Block): Boolean = view.scope != null && block.ignoreScope
@@ -184,7 +193,7 @@ internal fun BlockBody(
when (block.type) {
BlockType.PREVIEW -> {
val tasks = state.previews[block.id].orEmpty()
val filter = block.source?.taskFilter
val filter = blockFilter(block, state)
val options = block.options as BlockOptions.Preview
if (tasks.isEmpty()) {
if (editing) BlockPlaceholder(stringResource(R.string.block_empty))
@@ -273,15 +282,16 @@ internal fun Tile(block: Block, state: ListsUiState.Content, actions: BlockActio
GoneTile(onChoose = { actions.chooseSource(block.id) }, modifier = modifier)
return
}
val filter = block.source?.taskFilter
val filter = blockFilter(block, state)
val open = { filter?.let { actions.openFilter(it, actions.scopeFor(block)) } ?: Unit }
val marked = state.marksIgnoredScope(block)
when (val options = block.options) {
is BlockOptions.Progress -> state.progress[block.id]?.takeIf { filter != null }?.let { progress ->
val today = block.source == BlockSource.Smart(SmartList.TODAY)
if (block.width == BlockWidth.HALF) {
ProgressTile(progress.done, progress.total, blockTitle(block, state), options.wave, marked, open, modifier)
ProgressTile(progress.done, progress.total, blockTitle(block, state), today, options.wave, marked, open, modifier)
} else {
TodayHero(progress.done, progress.total, options.title, options.wave, marked, open, modifier)
TodayHero(progress.done, progress.total, blockTitle(block, state), today, options.wave, marked, open, modifier)
}
}
is BlockOptions.CountTile -> state.counts[block.id]?.takeIf { filter != null }?.let { count ->
@@ -377,6 +387,7 @@ private fun TodayHero(
done: Int,
total: Int,
title: String?,
today: Boolean,
wave: Boolean,
marked: Boolean,
onClick: () -> Unit,
@@ -405,12 +416,12 @@ private fun TodayHero(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
Icon(Icons.Rounded.Today, contentDescription = null, modifier = Modifier.size(20.dp))
Icon(if (today) Icons.Rounded.Today else Icons.Rounded.TaskAlt, contentDescription = null, modifier = Modifier.size(20.dp))
Text(title ?: stringResource(R.string.smart_today), style = MaterialTheme.typography.titleMedium)
if (marked) IgnoredScopeMark(MaterialTheme.colorScheme.onPrimaryContainer)
}
val headline = when {
total == 0 -> stringResource(R.string.home_today_empty)
total == 0 -> stringResource(if (today) R.string.home_today_empty else R.string.block_progress_empty)
left == 0 -> stringResource(R.string.home_today_all_done)
else -> pluralStringResource(R.plurals.home_today_progress_count, total, done, total)
}
@@ -436,6 +447,7 @@ private fun ProgressTile(
done: Int,
total: Int,
title: String,
today: Boolean,
wave: Boolean,
marked: Boolean,
onClick: () -> Unit,
@@ -467,7 +479,11 @@ private fun ProgressTile(
if (marked) IgnoredScopeMark(MaterialTheme.colorScheme.onPrimaryContainer)
}
Text(
if (total == 0) stringResource(R.string.home_today_empty) else stringResource(R.string.block_progress_short, done, total),
if (total == 0) {
stringResource(if (today) R.string.home_today_empty else R.string.block_progress_empty)
} else {
stringResource(R.string.block_progress_short, done, total)
},
style = MaterialTheme.typography.titleLarge,
maxLines = 2,
overflow = TextOverflow.Ellipsis,
@@ -346,8 +346,6 @@ fun ListsScreen(
title = stringResource(R.string.view_choose_source),
state = content,
withSmartLists = true,
// A progress block shows Today only, for now.
todayOnly = choosingBlock.type == BlockType.PROGRESS,
onPick = { source -> viewModel.chooseSource(content.view.id, choosingBlock.id, source) },
onDismiss = { choosingSourceFor = null },
)
@@ -377,7 +375,6 @@ fun ListsScreen(
title = stringResource(R.string.block_source_title),
state = content,
withSmartLists = true,
todayOnly = type == BlockType.PROGRESS,
onPick = { source -> viewModel.addBlock(content.view.id, type, source) },
onDismiss = { addingType = null },
)
@@ -402,7 +399,6 @@ fun ListsScreen(
title = stringResource(R.string.block_source_title),
state = content,
withSmartLists = true,
todayOnly = open.block.type == BlockType.PROGRESS,
onPick = { source -> viewModel.updateDraft { it.copy(source = source) } },
onDismiss = { choosingDraftSource = false },
)
@@ -241,10 +241,10 @@ internal fun homeContent(
}
fun shows(task: Task) = source.counts(task, context, smartHidden) && within.admits(task, clock)
when (block.type) {
BlockType.PROGRESS -> if (source == BlockSource.Smart(SmartList.TODAY)) {
// Today's completed rows against all of its rows.
val inToday = topLevel.filter { source.contains(it, context, smartHidden) && within.admits(it, clock) }
progress[block.id] = BlockProgress(inToday.count { it.isClosed }, inToday.size)
BlockType.PROGRESS -> {
// The source's completed rows against all of its rows: Today's, a list's, a rule's.
val inSource = topLevel.filter { source.contains(it, context, smartHidden) && within.admits(it, clock) }
progress[block.id] = BlockProgress(inSource.count { it.isClosed }, inSource.size)
}
BlockType.COUNT_TILE -> counts[block.id] = if (source is BlockSource.Smart) {
topLevel.count(::shows)
@@ -200,6 +200,8 @@ fun AgendulaNavHost(
tag = args?.getString(Dest.TaskList.ARG_TAG),
filterId = args?.getLong(Dest.TaskList.ARG_FILTER_ID) ?: Dest.NO_ID,
day = args?.getString(Dest.TaskList.ARG_DAY),
rule = args?.getString(Dest.TaskList.ARG_RULE),
title = args?.getString(Dest.TaskList.ARG_TITLE),
)
val scopeViewId = args?.getLong(Dest.TaskList.ARG_VIEW)?.takeIf { it != Dest.NO_ID }
val vm: TaskListViewModel = hiltViewModel()
@@ -2,6 +2,7 @@ package de.jeanlucmakiola.agendula.ui.navigation
import androidx.navigation.NavType
import androidx.navigation.navArgument
import de.jeanlucmakiola.agendula.data.prefs.RuleCodec
import de.jeanlucmakiola.agendula.domain.SmartList
import de.jeanlucmakiola.agendula.domain.TaskFilter
@@ -65,8 +66,14 @@ object Dest {
const val ARG_FILTER_ID = "filterId"
const val ARG_VIEW = "view"
const val ARG_DAY = "day"
const val ARG_RULE = "rule"
const val ARG_TITLE = "title"
/** The store a rule's lists are qualified by in a route: any one, the same both ways. */
private const val ROUTE_STORE = "ROUTE"
const val route = "taskList?$ARG_LIST_ID={$ARG_LIST_ID}&$ARG_SMART={$ARG_SMART}&$ARG_TAG={$ARG_TAG}" +
"&$ARG_FILTER_ID={$ARG_FILTER_ID}&$ARG_VIEW={$ARG_VIEW}&$ARG_DAY={$ARG_DAY}"
"&$ARG_FILTER_ID={$ARG_FILTER_ID}&$ARG_VIEW={$ARG_VIEW}&$ARG_DAY={$ARG_DAY}" +
"&$ARG_RULE={$ARG_RULE}&$ARG_TITLE={$ARG_TITLE}"
val arguments = listOf(
navArgument(ARG_LIST_ID) { type = NavType.LongType; defaultValue = NO_ID },
@@ -75,6 +82,8 @@ object Dest {
navArgument(ARG_FILTER_ID) { type = NavType.LongType; defaultValue = NO_ID },
navArgument(ARG_VIEW) { type = NavType.LongType; defaultValue = NO_ID },
navArgument(ARG_DAY) { type = NavType.StringType; nullable = true; defaultValue = null },
navArgument(ARG_RULE) { type = NavType.StringType; nullable = true; defaultValue = null },
navArgument(ARG_TITLE) { type = NavType.StringType; nullable = true; defaultValue = null },
)
fun build(filter: TaskFilter, scopeViewId: Long? = null): String {
@@ -84,6 +93,8 @@ object Dest {
is TaskFilter.Tagged -> "taskList?$ARG_TAG=${android.net.Uri.encode(filter.tag)}"
is TaskFilter.Saved -> "taskList?$ARG_FILTER_ID=${filter.id}"
is TaskFilter.DueOn -> "taskList?$ARG_DAY=${filter.date}"
is TaskFilter.Matching -> "taskList?$ARG_RULE=${android.net.Uri.encode(RuleCodec.encode(filter.rule, ROUTE_STORE))}" +
filter.title?.let { "&$ARG_TITLE=${android.net.Uri.encode(it)}" }.orEmpty()
// Several lists at once only exists for the widget, which never routes here.
is TaskFilter.OfLists -> error("OfLists has no task-list screen")
}
@@ -91,8 +102,17 @@ object Dest {
}
/** Reconstruct the filter from a destination's arguments. */
fun filterOf(smart: String?, listId: Long, tag: String? = null, filterId: Long = NO_ID, day: String? = null): TaskFilter = when {
fun filterOf(
smart: String?,
listId: Long,
tag: String? = null,
filterId: Long = NO_ID,
day: String? = null,
rule: String? = null,
title: String? = null,
): TaskFilter = when {
smart != null -> TaskFilter.Smart(SmartList.valueOf(smart))
rule != null -> TaskFilter.Matching(RuleCodec.decode(rule, ROUTE_STORE), title)
day != null -> TaskFilter.DueOn(java.time.LocalDate.parse(day))
tag != null -> TaskFilter.Tagged(tag)
filterId != NO_ID -> TaskFilter.Saved(filterId)
@@ -581,7 +581,7 @@ internal fun rememberTaskRows(
// In a mixed view (a smart list) the list colour bar + name disambiguate
// rows; inside a single real list, or a scope of one, both are redundant.
val mixedView = state.scopeLists?.size != 1 && (
filter is TaskFilter.Smart || filter is TaskFilter.Tagged || filter is TaskFilter.DueOn ||
filter is TaskFilter.Smart || filter is TaskFilter.Tagged || filter is TaskFilter.DueOn || filter is TaskFilter.Matching ||
(filter is TaskFilter.Saved && state.savedFilter?.criteria?.listIds?.size != 1)
)
// Parents the user expanded to reveal their subtasks inline. Children are
@@ -1573,6 +1573,7 @@ private fun titleFor(filter: TaskFilter, name: String?): String = when (filter)
is TaskFilter.Tagged -> filter.tag
is TaskFilter.Smart -> stringResource(smartListLabel(filter.list))
is TaskFilter.DueOn -> rememberDayFormat(long = true).format(filter.date, LocalDate.now())
is TaskFilter.Matching -> filter.title ?: stringResource(R.string.block_source_rules)
}
@Composable
@@ -240,7 +240,7 @@ class TaskListViewModel @Inject constructor(
.flatMapLatest { b ->
val f = b.filter
val source: Flow<FilteredTasks> = when (f) {
is TaskFilter.Saved -> repository.filteredTasks(f)
is TaskFilter.Saved, is TaskFilter.Matching -> repository.filteredTasks(f)
is TaskFilter.Smart -> if (b.withCompleted != false && f.list == SmartList.ALL) {
combine(repository.tasks(f), repository.tasks(TaskFilter.Smart(SmartList.COMPLETED))) { open, done ->
FilteredTasks(open + done)
@@ -171,6 +171,7 @@ private fun TaskWidgetBody(
is TaskFilter.Smart -> context.getString(smartListLabel(filter.list))
is TaskFilter.Tagged -> filter.tag
is TaskFilter.DueOn -> filter.date.toString()
is TaskFilter.Matching -> filter.title.orEmpty()
is TaskFilter.Saved -> if (ready == null) {
context.getString(R.string.widget_tasks_label)
} else {
@@ -298,7 +299,7 @@ private fun TaskWidgetHeader(title: String, filter: TaskFilter, singleListId: Lo
// still opens straight to it, otherwise this just raises the app.
is TaskFilter.OfLists -> filter.listIds.singleOrNull()?.let { MainActivity.listIntent(context, it) }
?: Intent(context, MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
is TaskFilter.Tagged, is TaskFilter.DueOn -> Intent(context, MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
is TaskFilter.Tagged, is TaskFilter.DueOn, is TaskFilter.Matching -> Intent(context, MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
is TaskFilter.Saved -> MainActivity.filterIntent(context, filter.id)
}
WidgetHeader(
@@ -222,7 +222,7 @@ private fun WidgetFilterPicker(
when (selected) {
is TaskFilter.OfList -> setOf(selected.listId)
is TaskFilter.OfLists -> selected.listIds
is TaskFilter.Smart, is TaskFilter.Tagged, is TaskFilter.Saved, is TaskFilter.DueOn -> emptySet()
is TaskFilter.Smart, is TaskFilter.Tagged, is TaskFilter.Saved, is TaskFilter.DueOn, is TaskFilter.Matching -> emptySet()
},
)
}
@@ -103,6 +103,7 @@ internal fun TaskFilter.widgetStorageValue(): String = when (this) {
is TaskFilter.Smart -> "smart:${list.name}"
is TaskFilter.Tagged -> error("A tag has no widget")
is TaskFilter.DueOn -> error("A day has no widget")
is TaskFilter.Matching -> error("A block's rules have no widget")
is TaskFilter.Saved -> "filter:$id"
}
@@ -174,7 +175,7 @@ sealed interface TaskWidgetData {
val singleListId: Long? = when (filter) {
is TaskFilter.OfList -> filter.listId
is TaskFilter.OfLists -> filter.listIds.singleOrNull()
is TaskFilter.Smart, is TaskFilter.Tagged, is TaskFilter.Saved, is TaskFilter.DueOn -> null
is TaskFilter.Smart, is TaskFilter.Tagged, is TaskFilter.Saved, is TaskFilter.DueOn, is TaskFilter.Matching -> null
}
/** [singleListId]'s name. */
+1
View File
@@ -1239,6 +1239,7 @@
<string name="block_recent_done_at">Done %1$s</string>
<!-- A half-width progress tile: done of total -->
<string name="block_progress_short">%1$d of %2$d</string>
<string name="block_progress_empty">Nothing to do 🎉</string>
<string name="block_title">Title</string>
<string name="block_heading_text">Text</string>
<string name="block_heading_text_hint">Heading</string>