From e8a73d5e80f13e97defb3836380adecd1f9c89e2 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Fri, 27 Nov 2026 15:16:52 +0100 Subject: [PATCH] Custom rules sources work in every block: a task screen for a bare rule, and progress of any source (#59, #172) --- CHANGELOG.md | 2 +- .../data/tasks/TasksRepositoryImpl.kt | 6 +- .../agendula/domain/TaskFilter.kt | 7 ++ .../agendula/domain/TaskGroups.kt | 3 + .../agendula/domain/ViewScope.kt | 3 +- .../agendula/ui/lists/HomeBlocks.kt | 32 +++++-- .../agendula/ui/lists/ListsScreen.kt | 4 - .../agendula/ui/lists/ListsViewModel.kt | 8 +- .../agendula/ui/navigation/AgendulaNavHost.kt | 2 + .../agendula/ui/navigation/Destinations.kt | 24 +++++- .../agendula/ui/tasklist/TaskListScreen.kt | 3 +- .../agendula/ui/tasklist/TaskListViewModel.kt | 2 +- .../agendula/widget/TaskWidget.kt | 3 +- .../widget/TaskWidgetConfigActivity.kt | 2 +- .../agendula/widget/WidgetData.kt | 3 +- app/src/main/res/values/strings.xml | 1 + .../ui/lists/CustomRulesBlocksTest.kt | 84 +++++++++++++++++++ 17 files changed, 163 insertions(+), 26 deletions(-) create mode 100644 app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/CustomRulesBlocksTest.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 4345d99..b89ece9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,7 +37,7 @@ All notable changes to this project are documented here. The format follows title, what it shows, its own options, and whether it ignores the view's scope, which a small mark on the block then shows (#59). - New blocks: count tiles of any list, tag, filter or smart list with their - own title, colour and icon; a half-width progress tile; previews of 3, 5 or + own title, colour and icon; progress of any of those, also half width; previews of 3, 5 or 10 tasks that can be checked off in place; recently completed, unchecked to undo; a week strip with a dot per task due, opening a day's tasks; and section headings. Tiles follow the compact density too (#59). diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/TasksRepositoryImpl.kt b/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/TasksRepositoryImpl.kt index 0307689..ceebe2e 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/TasksRepositoryImpl.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/TasksRepositoryImpl.kt @@ -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> = all diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/domain/TaskFilter.kt b/app/src/main/java/de/jeanlucmakiola/agendula/domain/TaskFilter.kt index ad4ce52..cfca252 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/domain/TaskFilter.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/domain/TaskFilter.kt @@ -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) } /** diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/domain/TaskGroups.kt b/app/src/main/java/de/jeanlucmakiola/agendula/domain/TaskGroups.kt index db3c901..1410f61 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/domain/TaskGroups.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/domain/TaskGroups.kt @@ -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" } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/domain/ViewScope.kt b/app/src/main/java/de/jeanlucmakiola/agendula/domain/ViewScope.kt index cc1a4aa..b8852b3 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/domain/ViewScope.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/domain/ViewScope.kt @@ -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 } /** diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/HomeBlocks.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/HomeBlocks.kt index 1d0a21f..a47fd42 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/HomeBlocks.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/HomeBlocks.kt @@ -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, diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsScreen.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsScreen.kt index 414fd4a..ea2b462 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsScreen.kt @@ -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 }, ) diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsViewModel.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsViewModel.kt index 0436f3b..17487a4 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsViewModel.kt @@ -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) diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/navigation/AgendulaNavHost.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/navigation/AgendulaNavHost.kt index 8c5de33..2fcb205 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/navigation/AgendulaNavHost.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/navigation/AgendulaNavHost.kt @@ -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() diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/navigation/Destinations.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/navigation/Destinations.kt index 46a4e77..67bc590 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/navigation/Destinations.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/navigation/Destinations.kt @@ -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) diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListScreen.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListScreen.kt index 421102d..3d42952 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListScreen.kt @@ -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 diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListViewModel.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListViewModel.kt index 350c580..af46acb 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListViewModel.kt @@ -240,7 +240,7 @@ class TaskListViewModel @Inject constructor( .flatMapLatest { b -> val f = b.filter val source: Flow = 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) diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/widget/TaskWidget.kt b/app/src/main/java/de/jeanlucmakiola/agendula/widget/TaskWidget.kt index ce6e0bd..eb85c02 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/widget/TaskWidget.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/widget/TaskWidget.kt @@ -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( diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/widget/TaskWidgetConfigActivity.kt b/app/src/main/java/de/jeanlucmakiola/agendula/widget/TaskWidgetConfigActivity.kt index 8a528a2..70902aa 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/widget/TaskWidgetConfigActivity.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/widget/TaskWidgetConfigActivity.kt @@ -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() }, ) } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/widget/WidgetData.kt b/app/src/main/java/de/jeanlucmakiola/agendula/widget/WidgetData.kt index 2dec836..e4020f5 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/widget/WidgetData.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/widget/WidgetData.kt @@ -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. */ diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c81cc47..b2d72b9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1239,6 +1239,7 @@ Done %1$s %1$d of %2$d + Nothing to do 🎉 Title Text Heading diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/CustomRulesBlocksTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/CustomRulesBlocksTest.kt new file mode 100644 index 0000000..e36a93d --- /dev/null +++ b/app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/CustomRulesBlocksTest.kt @@ -0,0 +1,84 @@ +package de.jeanlucmakiola.agendula.ui.lists + +import com.google.common.truth.Truth.assertThat +import de.jeanlucmakiola.agendula.data.prefs.RuleCodec +import de.jeanlucmakiola.agendula.domain.Block +import de.jeanlucmakiola.agendula.domain.BlockSource +import de.jeanlucmakiola.agendula.domain.BlockType +import de.jeanlucmakiola.agendula.domain.BlockWidth +import de.jeanlucmakiola.agendula.domain.Priority +import de.jeanlucmakiola.agendula.domain.Rule +import de.jeanlucmakiola.agendula.domain.RuleClock +import de.jeanlucmakiola.agendula.domain.TaskFilter +import de.jeanlucmakiola.agendula.domain.TaskFiltering +import de.jeanlucmakiola.agendula.domain.TaskGroups +import de.jeanlucmakiola.agendula.domain.TaskList +import de.jeanlucmakiola.agendula.domain.TaskStatus +import de.jeanlucmakiola.agendula.domain.View +import de.jeanlucmakiola.agendula.domain.ViewIcon +import de.jeanlucmakiola.agendula.domain.groupingKey +import de.jeanlucmakiola.agendula.domain.startOfDay +import de.jeanlucmakiola.agendula.domain.taskFilter +import de.jeanlucmakiola.agendula.domain.testTask +import org.junit.jupiter.api.Test +import java.time.DayOfWeek +import java.time.LocalDate +import java.time.ZoneId + +/** A block's custom rules source works in every block that takes a source. */ +class CustomRulesBlocksTest { + + private val zone = ZoneId.systemDefault() + private val today = LocalDate.of(2026, 11, 26) + private val clock = RuleClock(startOfDay(today, zone), startOfDay(today.plusDays(1), zone), zone, DayOfWeek.MONDAY) + private fun list(id: Long) = TaskList(id, "L$id", 0, "Local", "local", isSynced = false, isVisible = true, owner = null) + + private val urgent = Rule.AnyOf(listOf(Rule.HasTag("next"), Rule.MinPriority(Priority.HIGH))) + private val open = listOf( + testTask(id = 1, listId = 1, tags = listOf("next")), + testTask(id = 2, listId = 2, priority = Priority.HIGH), + testTask(id = 3, listId = 1), + ) + private val done = listOf( + testTask(id = 4, listId = 1, tags = listOf("next"), status = TaskStatus.COMPLETED), + testTask(id = 5, listId = 2, status = TaskStatus.COMPLETED), + ) + + @Test + fun `a rules source opens a task screen that matches by the rule, open and done`() { + val filter = BlockSource.Rules(urgent).taskFilter + assertThat(filter).isEqualTo(TaskFilter.Matching(urgent)) + val matched = (open + done).filter { + TaskFiltering.matches(it, filter!!, clock.todayStart, clock.todayEnd, ruleClock = clock) + } + assertThat(matched.map { it.taskId }).containsExactly(1L, 2L, 4L) + // Without a clock to read the rule against, nothing matches, as for a saved filter. + assertThat(open.none { TaskFiltering.matches(it, filter!!, clock.todayStart, clock.todayEnd) }).isTrue() + assertThat(filter!!.groupingKey()).isEqualTo("rules") + assertThat(TaskGroups.offeredFor(filter)).isNotEmpty() + } + + @Test + fun `progress, counts and previews read a rules source through the view's scope unless ignored`() { + val blocks = listOf( + Block(1, BlockType.PROGRESS, BlockSource.Rules(urgent)), + Block(2, BlockType.PROGRESS, BlockSource.Rules(urgent), BlockWidth.HALF, ignoreScope = true), + Block(3, BlockType.COUNT_TILE, BlockSource.Rules(urgent), BlockWidth.HALF), + Block(4, BlockType.PREVIEW, BlockSource.Rules(urgent)), + Block(5, BlockType.TASK_LIST, BlockSource.Rules(urgent)), + ) + val view = View(2, "Work", ViewIcon.WORK, Rule.InList(setOf(1L)), blocks) + val state = homeContent(view, listOf(view), listOf(list(1), list(2)), open, done, emptySet(), emptyList(), clock, null) + assertThat(state.progress.getValue(1L)).isEqualTo(BlockProgress(done = 1, total = 2)) + assertThat(state.progress.getValue(2L)).isEqualTo(BlockProgress(done = 1, total = 3)) + assertThat(state.counts.getValue(3L)).isEqualTo(1) + assertThat(state.previews.getValue(4L).map { it.taskId }).containsExactly(1L) + assertThat(state.goneSources).isEmpty() + } + + @Test + fun `a rule survives the route to its task screen`() { + val rule = Rule.AllOf(listOf(Rule.InList(setOf(3L), setOf("EXTERNAL:7")), Rule.Not(Rule.HasTag("a b&c")))) + assertThat(RuleCodec.decode(RuleCodec.encode(rule, "ROUTE"), "ROUTE")).isEqualTo(rule) + } +}