From ba9d984a42c43ee387ef3e1a579444a101d075ba Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Thu, 26 Nov 2026 17:00:05 +0100 Subject: [PATCH] Add the view and block model (#59) --- .../de/jeanlucmakiola/agendula/domain/View.kt | 253 ++++++++++++++++++ .../agendula/domain/ViewTest.kt | 90 +++++++ 2 files changed, 343 insertions(+) create mode 100644 app/src/main/java/de/jeanlucmakiola/agendula/domain/View.kt create mode 100644 app/src/test/java/de/jeanlucmakiola/agendula/domain/ViewTest.kt diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/domain/View.kt b/app/src/main/java/de/jeanlucmakiola/agendula/domain/View.kt new file mode 100644 index 0000000..57a7573 --- /dev/null +++ b/app/src/main/java/de/jeanlucmakiola/agendula/domain/View.kt @@ -0,0 +1,253 @@ +package de.jeanlucmakiola.agendula.domain + +/** + * A stored home layout: [blocks] in order, over the tasks [scope] admits (every + * task when `null`). Home is the first view. + */ +data class View( + val id: Long, + val name: String, + val icon: ViewIcon, + val scope: Rule?, + val blocks: List, + val quickAdd: Boolean = true, +) { + /** What is wrong with this view's blocks; empty when nothing is. */ + val problems: List get() = blocks.layoutProblems() + + companion object { + const val HOME_ID = 1L + + /** + * The Home view every install starts with: the blocks of the fixed home + * of 1.4, in its order. The name is empty so the UI shows its own, + * translated one until the user renames it. + */ + fun home(): View = View( + id = HOME_ID, + name = "", + icon = ViewIcon.HOME, + scope = null, + blocks = listOf( + Block(1, BlockType.PROGRESS, BlockSource.Smart(SmartList.TODAY)), + Block(2, BlockType.COUNT_TILE, BlockSource.Smart(SmartList.OVERDUE), BlockWidth.HALF), + Block(3, BlockType.COUNT_TILE, BlockSource.Smart(SmartList.ALL), BlockWidth.HALF), + Block(4, BlockType.PREVIEW, BlockSource.Smart(SmartList.UPCOMING)), + Block(5, BlockType.LISTS), + Block(6, BlockType.TAGS), + Block(7, BlockType.FILTERS), + ), + ) + } +} + +/** One block of a [View]. [source] is set exactly when [type] takes one. */ +data class Block( + val id: Long, + val type: BlockType, + val source: BlockSource? = null, + val width: BlockWidth = BlockWidth.FULL, + val ignoreScope: Boolean = false, + val options: BlockOptions = BlockOptions.defaultFor(type), +) { + init { + require(options.type == type) { "Options ${options.type} on a $type block" } + } +} + +/** How much of a row a block takes: half-width blocks pack two to a row. */ +enum class BlockWidth { HALF, FULL } + +/** The kinds of block a view can hold. */ +enum class BlockType(val widths: Set, val takesSource: Boolean) { + PROGRESS(setOf(BlockWidth.FULL, BlockWidth.HALF), takesSource = true), + COUNT_TILE(setOf(BlockWidth.HALF, BlockWidth.FULL), takesSource = true), + PREVIEW(setOf(BlockWidth.FULL), takesSource = true), + TASK_LIST(setOf(BlockWidth.FULL), takesSource = true), + LISTS(setOf(BlockWidth.FULL), takesSource = false), + TAGS(setOf(BlockWidth.FULL), takesSource = false), + FILTERS(setOf(BlockWidth.FULL), takesSource = false), + SECTION_HEADING(setOf(BlockWidth.FULL), takesSource = false), + WEEK_STRIP(setOf(BlockWidth.FULL), takesSource = false), + + /** A preview of the completed tasks, latest first. */ + RECENTLY_COMPLETED(setOf(BlockWidth.FULL), takesSource = false), +} + +/** The tasks a block shows. */ +sealed interface BlockSource { + data class Smart(val smart: SmartList) : BlockSource + data class List(val listId: Long) : BlockSource + data class Tag(val tag: String) : BlockSource + data class Filter(val filterId: Long) : BlockSource + data class Rules(val rule: Rule) : BlockSource +} + +/** The settings of a block, one shape per [BlockType]. */ +sealed interface BlockOptions { + val type: BlockType + + /** [title] replaces the source's name when set. */ + data class Progress(val wave: Boolean = true, val title: String? = null) : BlockOptions { + override val type get() = BlockType.PROGRESS + } + + /** [color] and [icon] replace the source's own when set. */ + data class CountTile( + val title: String? = null, + val color: TileColor? = null, + val icon: ViewIcon? = null, + ) : BlockOptions { + override val type get() = BlockType.COUNT_TILE + } + + data class Preview( + val count: Int = PREVIEW_COUNTS.first(), + val showAll: Boolean = true, + val inlineDone: Boolean = false, + ) : BlockOptions { + init { + require(count in PREVIEW_COUNTS) { "Preview count $count" } + } + + override val type get() = BlockType.PREVIEW + } + + /** [sort] is the app's sort order when `null`. */ + data class TaskList( + val sort: TaskSortOrder? = null, + val showCompleted: Boolean = true, + val inlineDone: Boolean = true, + ) : BlockOptions { + override val type get() = BlockType.TASK_LIST + } + + /** [accounts] limits the lists to those accounts; every account when `null`. */ + data class Lists(val accounts: Set? = null, val counts: Boolean = true) : BlockOptions { + override val type get() = BlockType.LISTS + } + + data object Tags : BlockOptions { + override val type get() = BlockType.TAGS + } + + data object Filters : BlockOptions { + override val type get() = BlockType.FILTERS + } + + data class SectionHeading(val text: String = "") : BlockOptions { + override val type get() = BlockType.SECTION_HEADING + } + + data object WeekStrip : BlockOptions { + override val type get() = BlockType.WEEK_STRIP + } + + data class RecentlyCompleted(val count: Int = PREVIEW_COUNTS.first()) : BlockOptions { + init { + require(count in PREVIEW_COUNTS) { "Preview count $count" } + } + + override val type get() = BlockType.RECENTLY_COMPLETED + } + + companion object { + /** How many tasks a preview may show. */ + val PREVIEW_COUNTS = listOf(3, 5, 10) + + fun defaultFor(type: BlockType): BlockOptions = when (type) { + BlockType.PROGRESS -> Progress() + BlockType.COUNT_TILE -> CountTile() + BlockType.PREVIEW -> Preview() + BlockType.TASK_LIST -> TaskList() + BlockType.LISTS -> Lists() + BlockType.TAGS -> Tags + BlockType.FILTERS -> Filters + BlockType.SECTION_HEADING -> SectionHeading() + BlockType.WEEK_STRIP -> WeekStrip + BlockType.RECENTLY_COMPLETED -> RecentlyCompleted() + } + } +} + +/** The tonal colour roles a count tile can take. */ +enum class TileColor { PRIMARY, SECONDARY, TERTIARY, ERROR } + +/** The curated icons a view (or a tile) can carry. Stored by name. */ +enum class ViewIcon { HOME, WORK, PERSON, STAR, CALENDAR, LIST, FLAG, BOLT, HEART, SCHOOL, SHOPPING, FITNESS } + +/** Where the app opens. */ +sealed interface StartDestination { + data object LastUsed : StartDestination + data class View(val id: Long) : StartDestination + data class List(val id: Long) : StartDestination + data class Filter(val id: Long) : StartDestination +} + +/** Something [layoutProblems] found wrong with a list of blocks. */ +sealed interface LayoutProblem { + data class DuplicateId(val id: Long) : LayoutProblem + data class WidthNotAllowed(val blockId: Long) : LayoutProblem + data class SourceMismatch(val blockId: Long) : LayoutProblem + data object SeveralTaskLists : LayoutProblem + data object TaskListNotLast : LayoutProblem +} + +/** + * What breaks the layout rules: ids repeat, a width the type doesn't allow, a + * source on a type that takes none or none where one is needed, and more than + * one task list or one that isn't the last block. + */ +fun List.layoutProblems(): List { + val blocks = this + val taskLists = blocks.count { it.type == BlockType.TASK_LIST } + return buildList { + blocks.groupingBy { it.id }.eachCount().filterValues { it > 1 }.keys + .forEach { add(LayoutProblem.DuplicateId(it)) } + blocks.forEach { block -> + if (block.width !in block.type.widths) add(LayoutProblem.WidthNotAllowed(block.id)) + if ((block.source != null) != block.type.takesSource) add(LayoutProblem.SourceMismatch(block.id)) + } + if (taskLists > 1) add(LayoutProblem.SeveralTaskLists) + if (taskLists > 0 && blocks.last().type != BlockType.TASK_LIST) add(LayoutProblem.TaskListNotLast) + } +} + +/** + * These blocks made to follow the layout rules: repeated ids and blocks with a + * wrong source dropped, widths a type doesn't allow made full, and only the + * first task list kept, moved to the end. + */ +fun List.normalized(): List { + val seen = HashSet() + val kept = filter { seen.add(it.id) && (it.source != null) == it.type.takesSource } + .map { if (it.width in it.type.widths) it else it.copy(width = BlockWidth.FULL) } + val taskList = kept.firstOrNull { it.type == BlockType.TASK_LIST } + return kept.filterNot { it.type == BlockType.TASK_LIST } + listOfNotNull(taskList) +} + +/** + * The blocks laid out in rows: a full block alone, half blocks two by two. A half + * block followed by a full one, or by nothing, sits alone in its row. + */ +fun List.rows(): List> { + val rows = mutableListOf>() + var pending: Block? = null + for (block in this) { + val half = pending + when { + block.width == BlockWidth.FULL -> { + if (half != null) rows += listOf(half) + rows += listOf(block) + pending = null + } + half == null -> pending = block + else -> { + rows += listOf(half, block) + pending = null + } + } + } + pending?.let { rows += listOf(it) } + return rows +} diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/domain/ViewTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/domain/ViewTest.kt new file mode 100644 index 0000000..d914e1f --- /dev/null +++ b/app/src/test/java/de/jeanlucmakiola/agendula/domain/ViewTest.kt @@ -0,0 +1,90 @@ +package de.jeanlucmakiola.agendula.domain + +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows + +class ViewTest { + + private fun half(id: Long) = Block(id, BlockType.COUNT_TILE, BlockSource.Smart(SmartList.ALL), BlockWidth.HALF) + private fun full(id: Long) = Block(id, BlockType.TAGS) + private fun taskList(id: Long) = Block(id, BlockType.TASK_LIST, BlockSource.Smart(SmartList.TODAY)) + + @Test + fun `home is the 1_4 home in its order`() { + val home = View.home() + assertThat(home.scope).isNull() + assertThat(home.quickAdd).isTrue() + assertThat(home.problems).isEmpty() + assertThat(home.blocks.rows().map { row -> row.map { it.type to it.source } }).containsExactly( + listOf(BlockType.PROGRESS to BlockSource.Smart(SmartList.TODAY)), + listOf( + BlockType.COUNT_TILE to BlockSource.Smart(SmartList.OVERDUE), + BlockType.COUNT_TILE to BlockSource.Smart(SmartList.ALL), + ), + listOf(BlockType.PREVIEW to BlockSource.Smart(SmartList.UPCOMING)), + listOf(BlockType.LISTS to null), + listOf(BlockType.TAGS to null), + listOf(BlockType.FILTERS to null), + ).inOrder() + assertThat((home.blocks[3].options as BlockOptions.Preview)).isEqualTo(BlockOptions.Preview(count = 3, showAll = true)) + } + + @Test + fun `half blocks pack two to a row, a lone one sits alone`() { + val blocks = listOf(half(1), half(2), half(3), full(4), half(5), half(6), half(7)) + assertThat(blocks.rows().map { row -> row.map { it.id } }).containsExactly( + listOf(1L, 2L), + listOf(3L), + listOf(4L), + listOf(5L, 6L), + listOf(7L), + ).inOrder() + assertThat(emptyList().rows()).isEmpty() + } + + @Test + fun `options must match the type`() { + assertThrows { Block(1, BlockType.TAGS, options = BlockOptions.Filters) } + assertThrows { BlockOptions.Preview(count = 4) } + BlockType.entries.forEach { assertThat(BlockOptions.defaultFor(it).type).isEqualTo(it) } + } + + @Test + fun `layout problems name what breaks the rules`() { + val blocks = listOf( + taskList(1), + Block(2, BlockType.PREVIEW, BlockSource.Smart(SmartList.UPCOMING), BlockWidth.HALF), + Block(2, BlockType.LISTS, BlockSource.Smart(SmartList.ALL)), + Block(4, BlockType.PROGRESS), + taskList(5), + full(6), + ) + assertThat(blocks.layoutProblems()).containsExactly( + LayoutProblem.DuplicateId(2), + LayoutProblem.WidthNotAllowed(2), + LayoutProblem.SourceMismatch(2), + LayoutProblem.SourceMismatch(4), + LayoutProblem.SeveralTaskLists, + LayoutProblem.TaskListNotLast, + ) + assertThat(listOf(half(1), half(2), taskList(3)).layoutProblems()).isEmpty() + } + + @Test + fun `normalising fixes what layout problems find`() { + val blocks = listOf( + taskList(1), + Block(2, BlockType.PREVIEW, BlockSource.Smart(SmartList.UPCOMING), BlockWidth.HALF), + Block(2, BlockType.TAGS), + Block(4, BlockType.PROGRESS), + taskList(5), + full(6), + ) + val normalized = blocks.normalized() + assertThat(normalized.map { it.id }).containsExactly(2L, 6L, 1L).inOrder() + assertThat(normalized[0].width).isEqualTo(BlockWidth.FULL) + assertThat(normalized.layoutProblems()).isEmpty() + assertThat(View.home().blocks.normalized()).isEqualTo(View.home().blocks) + } +}