diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/SettingsPrefs.kt b/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/SettingsPrefs.kt index eaf4a13..b01363d 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/SettingsPrefs.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/SettingsPrefs.kt @@ -407,6 +407,7 @@ class SettingsPrefs @Inject constructor( drop(GROUPINGS) { it.substringAfter(GROUPING_SEPARATOR) in keys } drop(COLLAPSED_SECTIONS) { it in keys } forgetInFilters(p, store, mode) { it in listIds } + forgetInViews(p, store, mode) { it in listIds } } /** @@ -427,6 +428,7 @@ class SettingsPrefs @Inject constructor( prune(GROUPINGS, ListEntry.GROUPING) prune(COLLAPSED_SECTIONS, ListEntry.COLLAPSED) forgetInFilters(p, store, mode) { it !in present } + forgetInViews(p, store, mode) { it !in present } } /** @@ -441,6 +443,14 @@ class SettingsPrefs @Inject constructor( if (kept != raw) p[SAVED_FILTERS] = kept } + /** Drops the lists [gone] names from the views, under the same rule as [forgetInFilters]. */ + private fun forgetInViews(p: MutablePreferences, store: StorageMode, mode: String, gone: (Long) -> Boolean) { + if (mode != store.name) return + val raw = p[VIEWS] ?: return + val kept = ViewCodec.withoutLists(raw, store.name, gone) + if (kept != null && kept != raw) p[VIEWS] = kept + } + suspend fun setSyncIntervalMinutes(minutes: Int) = dataStore.edit { it[SYNC_INTERVAL] = minutes } suspend fun setPushEnabled(enabled: Boolean) = dataStore.edit { it[PUSH_ENABLED] = enabled } @@ -617,17 +627,18 @@ class SettingsPrefs @Inject constructor( p[SAVED_FILTERS] = SavedFilterCodec.remove(p[SAVED_FILTERS], id) val key = TaskFilter.Saved(id).groupingKey() p[GROUPINGS] = p[GROUPINGS].orEmpty().filterTo(HashSet()) { it.substringAfter(GROUPING_SEPARATOR) != key } + p[VIEWS]?.let { raw -> ViewCodec.withoutFilter(raw, modeOf(p).name, id)?.let { p[VIEWS] = it } } } /** - * The views in order, with the list ids of the current store. Home stands in - * for a stored layout that doesn't read, so there is always one. + * The views in order, with the list ids of the current store. Home is always + * first: the default one stands in when the stored Home doesn't read. */ val views: Flow> = dataStore.data.map { p -> p[VIEWS] to p[STORAGE_MODE] } .distinctUntilChanged() .map { (raw, mode) -> ViewCodec.decode(raw, (storedMode(mode) ?: resolver.autoMode()).name) } - .map { it.ifEmpty { listOf(View.home()) } } + .map { views -> listOf(views.firstOrNull { it.id == View.HOME_ID } ?: View.home()) + views.filter { it.id != View.HOME_ID } } .distinctUntilChanged() suspend fun setDefaultEditFields(fields: Set) = dataStore.edit { diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/ViewCodec.kt b/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/ViewCodec.kt index 3faacdc..a02f681 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/ViewCodec.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/ViewCodec.kt @@ -13,6 +13,8 @@ import de.jeanlucmakiola.agendula.domain.TileColor import de.jeanlucmakiola.agendula.domain.View import de.jeanlucmakiola.agendula.domain.ViewIcon import de.jeanlucmakiola.agendula.domain.normalized +import de.jeanlucmakiola.agendula.domain.withoutFilter +import de.jeanlucmakiola.agendula.domain.withoutLists import java.net.URLDecoder import java.net.URLEncoder @@ -42,7 +44,6 @@ internal object ViewCodec { val views = mutableListOf() // A block line belongs to the view above it; those under an unreadable view go with it. var current: View? = null - var readable = false fun close() { current?.let { views += it.copy(blocks = it.blocks.normalized()) } current = null @@ -53,12 +54,9 @@ internal object ViewCodec { "view" -> { close() current = runCatching { decodeView(f, mode) }.getOrNull() - readable = current != null } - "block" -> if (readable) { - runCatching { decodeBlock(f, mode) }.getOrNull()?.let { block -> - current = current?.let { it.copy(blocks = it.blocks + block) } - } + "block" -> current?.let { view -> + runCatching { decodeBlock(f, mode) }.getOrNull()?.let { current = view.copy(blocks = view.blocks + it) } } } } @@ -95,6 +93,23 @@ internal object ViewCodec { ) } + /** + * [raw] without the lists of [mode]'s store [gone] names (see [View.withoutLists]); + * as it was when nothing changes or it doesn't read. + */ + fun withoutLists(raw: String?, mode: String, gone: (Long) -> Boolean): String? = + rewrite(raw, mode) { it.withoutLists(gone) } + + /** [raw] without the blocks showing the saved filter [filterId]. */ + fun withoutFilter(raw: String?, mode: String, filterId: Long): String? = + rewrite(raw, mode) { it.withoutFilter(filterId) } + + private fun rewrite(raw: String?, mode: String, change: (View) -> View): String? { + val views = decode(raw, mode) + val changed = views.map(change) + return if (changed == views) raw else encode(changed, mode) + } + private fun encodeBlock(block: Block, mode: String): String = listOf( "block", block.id.toString(), @@ -113,7 +128,7 @@ internal object ViewCodec { type = type, width = enumOr(f[3], type.widths.first()), ignoreScope = f[4] == "1", - source = f[5].takeIf { it.isNotEmpty() }?.let { decodeSource(it, mode) ?: return null }, + source = f[5].takeIf { it.isNotEmpty() }?.let { decodeSource(it, mode) }, options = decodeOptions(type, pairs(f[6])), ) } @@ -121,19 +136,22 @@ internal object ViewCodec { private fun encodeSource(source: BlockSource, mode: String): String = when (source) { is BlockSource.Smart -> "smart:${source.smart.name}" is BlockSource.List -> "list:${encode("$mode:${source.listId}")}" + is BlockSource.ListElsewhere -> "list:${encode(source.ref)}" is BlockSource.Tag -> "tag:${encode(source.tag)}" is BlockSource.Filter -> "filter:${source.filterId}" is BlockSource.Rules -> "rules:${encodeRule(source.rule, mode)}" } - /** The source [text] names; `null` for a list of another store, which no block here can show. */ - private fun decodeSource(text: String, mode: String): BlockSource? { + /** The source [text] names; a list of another store stays as its stored ref. */ + private fun decodeSource(text: String, mode: String): BlockSource { val kind = text.substringBefore(':') val value = text.substringAfter(':') return when (kind) { "smart" -> BlockSource.Smart(SmartList.valueOf(value)) - "list" -> decode(value).let { ref -> ref.removePrefix("$mode:").takeIf { it != ref } } - ?.toLong()?.let(BlockSource::List) + "list" -> decode(value).let { ref -> + ref.removePrefix("$mode:").takeIf { it != ref }?.toLong()?.let(BlockSource::List) + ?: BlockSource.ListElsewhere(ref) + } "tag" -> BlockSource.Tag(decode(value)) "filter" -> BlockSource.Filter(value.toLong()) "rules" -> BlockSource.Rules(decodeRule(value, mode)) @@ -171,10 +189,10 @@ internal object ViewCodec { private fun decodeOptions(type: BlockType, p: Map): BlockOptions = when (type) { BlockType.PROGRESS -> BlockOptions.Progress( wave = p["wave"] != "0", - title = p["title"]?.let(::decode), + title = text(p["title"]), ) BlockType.COUNT_TILE -> BlockOptions.CountTile( - title = p["title"]?.let(::decode), + title = text(p["title"]), color = p["color"]?.let { enumOrNull(it) }, icon = p["icon"]?.let { enumOrNull(it) }, ) @@ -199,13 +217,17 @@ internal object ViewCodec { BlockType.RECENTLY_COMPLETED -> BlockOptions.RecentlyCompleted(previewCount(p["count"])) } + /** A stored title; empty reads as none, so the source's own name shows. */ + private fun text(value: String?): String? = value?.let(::decode)?.takeIf { it.isNotBlank() } + private fun previewCount(value: String?): Int = value?.toIntOrNull()?.takeIf { it in BlockOptions.PREVIEW_COUNTS } ?: BlockOptions.PREVIEW_COUNTS.first() private fun pairs(text: String): Map = text.split(';').filter { '=' in it } .associate { it.substringBefore('=') to it.substringAfter('=') } - // The rule grammar is the saved filters': written and read through their codec, as the rule of a line. + // The rule grammar is the saved filters': written and read through their codec, as the rule of a + // line. Reading one applies that codec's top-level list pruning, the same one withoutLists applies. private fun encodeRule(rule: Rule, mode: String): String = SavedFilterCodec.upsert(null, SavedFilter(0, "", rule), mode).split('|', limit = 4)[3] diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/domain/View.kt b/app/src/main/java/de/jeanlucmakiola/agendula/domain/View.kt index 57a7573..9309d53 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/domain/View.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/domain/View.kt @@ -78,6 +78,9 @@ enum class BlockType(val widths: Set, val takesSource: Boolean) { sealed interface BlockSource { data class Smart(val smart: SmartList) : BlockSource data class List(val listId: Long) : BlockSource + + /** A list of another task store, kept as stored ([ref], e.g. `OWN:3`) so it survives a switch; it shows nothing here. */ + data class ListElsewhere(val ref: String) : BlockSource data class Tag(val tag: String) : BlockSource data class Filter(val filterId: Long) : BlockSource data class Rules(val rule: Rule) : BlockSource @@ -214,13 +217,13 @@ fun List.layoutProblems(): List { } /** - * 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 + * These blocks made to follow the layout rules: blocks with a wrong source and + * later blocks repeating an id 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 } + val kept = filter { (it.source != null) == it.type.takesSource && seen.add(it.id) } .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) @@ -251,3 +254,22 @@ fun List.rows(): List> { pending?.let { rows += listOf(it) } return rows } + +/** + * This view without the lists [gone] names: the scope and rule sources lose + * them (see [Rule.withoutLists]) and blocks showing one of them drop out. + */ +fun View.withoutLists(gone: (Long) -> Boolean): View = copy( + scope = scope?.withoutLists(gone), + blocks = blocks.mapNotNull { block -> + when (val source = block.source) { + is BlockSource.List -> block.takeUnless { gone(source.listId) } + is BlockSource.Rules -> block.copy(source = BlockSource.Rules(source.rule.withoutLists(gone))) + else -> block + } + }, +) + +/** This view without the blocks showing the saved filter [filterId]. */ +fun View.withoutFilter(filterId: Long): View = + copy(blocks = blocks.filterNot { it.source == BlockSource.Filter(filterId) }) 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 e75fc67..8f48c32 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 @@ -199,17 +199,20 @@ fun ListsScreen( ListsUiState.Failure -> CenteredMessage(stringResource(R.string.lists_failure), PaddingValues(0.dp)) is ListsUiState.Content -> { - ListsContent( - view = activeView, - state = s, - onOpenFilter = onOpenFilter, - onOpenTask = onOpenTask, - onNewList = { newList = true }, - onNewFilter = onNewFilter, - onReorder = viewModel::reorderLists, - topPadding = 0.dp, - bottomPadding = inner.calculateBottomPadding() + 96.dp, - ) + // The state already waits for the view, so this is only a guard. + activeView?.let { view -> + ListsContent( + view = view, + state = s, + onOpenFilter = onOpenFilter, + onOpenTask = onOpenTask, + onNewList = { newList = true }, + onNewFilter = onNewFilter, + onReorder = viewModel::reorderLists, + topPadding = 0.dp, + bottomPadding = inner.calculateBottomPadding() + 96.dp, + ) + } // While searching with a non-blank query, results cover the home // content; an empty query leaves the home content visible behind // the open field. @@ -287,7 +290,8 @@ private fun ListsContent( bottomPadding: androidx.compose.ui.unit.Dp, ) { // Lists get drag handles only while reordering, so the home screen stays calm. - var reordering by rememberSaveable { mutableStateOf(false) } + // Holds the id of the lists block being reordered. + var reordering by rememberSaveable { mutableStateOf(null) } val rows = remember(view.blocks) { view.blocks.rows() } LazyColumn( modifier = Modifier.fillMaxSize(), @@ -304,16 +308,16 @@ private fun ListsContent( return@forEach } when (block.type) { - BlockType.PROGRESS -> progressFor(block, state)?.let { progress -> - item { TodayHero(progress, onClick = { onOpenFilter(progress.filter) }) } + BlockType.PROGRESS -> if (progressFor(block, state) != null) { + item { Tile(block, state, onOpenFilter) } } BlockType.COUNT_TILE -> item { HalfRow(row, state, onOpenFilter) } BlockType.PREVIEW -> previewSection(block, state, onOpenFilter, onOpenTask) BlockType.LISTS -> listsSection( key = "${block.id}", groups = state.groups, - reordering = reordering, - onToggleReorder = { reordering = !reordering }, + reordering = reordering == block.id, + onToggleReorder = { reordering = if (reordering == block.id) null else block.id }, onOpenFilter = onOpenFilter, onNewList = onNewList, onReorder = onReorder, @@ -339,7 +343,7 @@ private data class ProgressState(val done: Int, val total: Int, val filter: Task private fun progressFor(block: Block, state: ListsUiState.Content): ProgressState? { val source = block.source as? BlockSource.Smart ?: return null if (source.smart != SmartList.TODAY) return null - val title = (block.options as BlockOptions.Progress).title + val title = (block.options as BlockOptions.Progress).title?.takeIf { it.isNotBlank() } return ProgressState(state.todayDone, state.todayTotal, TaskFilter.Smart(SmartList.TODAY), title) } @@ -356,26 +360,33 @@ private fun HalfRow(row: List, state: ListsUiState.Content, onOpenFilter: modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp).padding(top = 8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp), ) { - row.forEach { block -> - val cell = Modifier.weight(1f) - when (block.type) { - BlockType.COUNT_TILE -> countFor(block, state)?.let { count -> - SmartCard( - count = count, - modifier = cell, - onClick = { onOpenFilter(TaskFilter.Smart(count.smart)) }, - ) - } ?: Spacer(cell) - BlockType.PROGRESS -> progressFor(block, state)?.let { progress -> - TodayHero(progress, onClick = { onOpenFilter(progress.filter) }, modifier = cell) - } ?: Spacer(cell) - else -> Spacer(cell) - } - } + row.forEach { Tile(it, state, onOpenFilter, Modifier.weight(1f)) } if (row.size == 1 && row.single().width == BlockWidth.HALF) Spacer(Modifier.weight(1f)) } } +/** + * A progress or count tile; an empty space for a source it can't show yet. A full + * progress tile spans the content width on its own, as the Today hero always has. + */ +@Composable +private fun Tile( + block: Block, + state: ListsUiState.Content, + onOpenFilter: (TaskFilter) -> Unit, + modifier: Modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp), +) { + when (block.type) { + BlockType.PROGRESS -> progressFor(block, state)?.let { progress -> + TodayHero(progress, onClick = { onOpenFilter(progress.filter) }, modifier = modifier) + } + BlockType.COUNT_TILE -> countFor(block, state)?.let { count -> + SmartCard(count = count, modifier = modifier, onClick = { onOpenFilter(TaskFilter.Smart(count.smart)) }) + } + else -> null + } ?: Spacer(modifier) +} + /** A preview of the source's next tasks, under its heading; nothing while there are none. */ private fun LazyListScope.previewSection( block: Block, @@ -731,7 +742,7 @@ private fun SearchResults( private fun TodayHero( progress: ProgressState, onClick: () -> Unit, - modifier: Modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp), + modifier: Modifier, ) { val (done, total) = progress val interaction = remember { MutableInteractionSource() } 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 7ab513a..4487a56 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 @@ -10,6 +10,8 @@ import de.jeanlucmakiola.agendula.data.tasks.TasksRepository import de.jeanlucmakiola.agendula.data.tasks.recoveringFromProviderFailure import de.jeanlucmakiola.floret.time.DayWindow import de.jeanlucmakiola.agendula.domain.BlockOptions +import de.jeanlucmakiola.agendula.domain.BlockSource +import de.jeanlucmakiola.agendula.domain.BlockType import de.jeanlucmakiola.agendula.domain.RuleClock import de.jeanlucmakiola.agendula.domain.SavedFilter import de.jeanlucmakiola.agendula.domain.SmartList @@ -97,7 +99,7 @@ sealed interface ListsUiState { /** Completed vs. total tasks *due today* — drives the progress ring. */ val todayDone: Int, val todayTotal: Int, - /** The next few open tasks (due tomorrow onward) for the inline preview, as many as the longest one shows. */ + /** The next few open tasks (due tomorrow onward), as many as the view's longest Upcoming preview shows. */ val upcoming: List, /** Every task (open and completed) — the corpus the home search filters. */ val allTasks: List, @@ -108,8 +110,11 @@ sealed interface ListsUiState { ) : ListsUiState } -/** As many upcoming tasks as the longest preview shows; each preview block takes its own count. */ -private val UPCOMING_PREVIEW = BlockOptions.PREVIEW_COUNTS.max() + +/** How many upcoming tasks this view's previews of Upcoming show at most. */ +internal fun View.upcomingPreviewCount(): Int = blocks + .filter { it.type == BlockType.PREVIEW && it.source == BlockSource.Smart(SmartList.UPCOMING) } + .maxOfOrNull { (it.options as BlockOptions.Preview).count } ?: 0 /** The home overview: smart lists with live counts, then user lists by account. */ @HiltViewModel @@ -127,9 +132,10 @@ class ListsViewModel @Inject constructor( fun syncAll() = manualSync.syncAll() - /** The view Home renders; for now always the first one, Home. */ - val activeView: StateFlow = settingsPrefs.views.map { it.first() }.distinctUntilChanged() - .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), View.home()) + private val homeView = settingsPrefs.views.map { views -> views.first { it.id == View.HOME_ID } }.distinctUntilChanged() + + /** The view Home renders, for now always Home; `null` until it has been read. */ + val activeView: StateFlow = homeView.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), null) /** * Where a new list may go: the device, then every account whose server said @@ -158,10 +164,12 @@ class ListsViewModel @Inject constructor( combine( settingsPrefs.settings.map { it.hiddenFromSmartLists to it.upcomingRange }.distinctUntilChanged(), settingsPrefs.firstDayOfWeek, - ) { (hidden, range), weekStart -> Triple(hidden, range, weekStart) }, + homeView.map { it.upcomingPreviewCount() }.distinctUntilChanged(), + ) { (hidden, range), weekStart, previewCount -> Triple(hidden, range, weekStart) to previewCount }, settingsPrefs.savedFilters, - ) { lists, openTasks, completedTasks, (hidden, upcomingRange, weekStart), filters -> - buildContent(lists, openTasks, completedTasks, hidden, upcomingRange, filters, weekStart) as ListsUiState + ) { lists, openTasks, completedTasks, (settings, previewCount), filters -> + val (hidden, upcomingRange, weekStart) = settings + buildContent(lists, openTasks, completedTasks, hidden, upcomingRange, filters, weekStart, previewCount) as ListsUiState }.recoveringFromProviderFailure { ListsUiState.Failure } .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), ListsUiState.Loading) @@ -173,6 +181,7 @@ class ListsViewModel @Inject constructor( upcomingRange: UpcomingRange, savedFilters: List, weekStart: DayOfWeek, + previewCount: Int, ): ListsUiState.Content { val now = Clock.System.now() val zone = ZoneId.systemDefault() @@ -206,7 +215,7 @@ class ListsViewModel @Inject constructor( // already sorted by the repository's default ordering. val upcoming = smartTopLevel .filter { inSmart(it, SmartList.UPCOMING) } - .take(UPCOMING_PREVIEW) + .take(previewCount) val openByList = topLevel.groupingBy { it.listId }.eachCount() val groups = lists .groupBy { it.accountName } diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/ViewCodecTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/ViewCodecTest.kt index ea8b82b..126e45f 100644 --- a/app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/ViewCodecTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/ViewCodecTest.kt @@ -67,13 +67,36 @@ class ViewCodecTest { } @Test - fun `another store's lists read as theirs only`() { + fun `another store's lists read as theirs only, and survive a rewrite there`() { val raw = ViewCodec.encode(views, "OWN") - val external = ViewCodec.decode(raw, "EXTERNAL")[1] - // The list source of another store can't show here, so its block drops out. - assertThat(external.blocks.map { it.id }).doesNotContain(2L) - val scope = external.scope as Rule.All + val external = ViewCodec.decode(raw, "EXTERNAL") + assertThat(external[1].blocks.single { it.id == 2L }.source).isEqualTo(BlockSource.ListElsewhere("OWN:9")) + val scope = external[1].scope as Rule.All assertThat((scope.children[0] as Rule.Any).children[0]).isEqualTo(Rule.InList(setOf(7L), setOf("OWN:2"))) + assertThat(ViewCodec.decode(ViewCodec.encode(external, "EXTERNAL"), "OWN")).isEqualTo(views) + } + + @Test + fun `an empty title reads as none`() { + val raw = "v1\nview|1|x|HOME|1|\nblock|1|PROGRESS|FULL|0|smart:TODAY|wave=1;title=\nblock|2|COUNT_TILE|HALF|0|smart:ALL|title=+" + val blocks = ViewCodec.decode(raw, "OWN").single().blocks + assertThat(blocks.map { it.options }).containsExactly(BlockOptions.Progress(), BlockOptions.CountTile()).inOrder() + } + + @Test + fun `deleted lists and filters leave the views`() { + val raw = ViewCodec.encode(views, "OWN") + val pruned = ViewCodec.decode(ViewCodec.withoutLists(raw, "OWN") { it == 9L || it == 2L }, "OWN")[1] + assertThat(pruned.blocks.map { it.id }).doesNotContain(2L) + val scope = pruned.scope as Rule.All + // A nested list condition left without this store's lists still names the other store's. + assertThat((scope.children[0] as Rule.Any).children[0]).isEqualTo(Rule.InList(emptySet(), setOf("EXTERNAL:7"))) + assertThat(ViewCodec.withoutLists(raw, "OWN") { it == 99L }).isSameInstanceAs(raw) + + val withoutFilter = ViewCodec.decode(ViewCodec.withoutFilter(raw, "OWN", 12), "OWN")[1] + assertThat(withoutFilter.blocks.map { it.id }).doesNotContain(4L) + assertThat(ViewCodec.withoutFilter(raw, "OWN", 13)).isSameInstanceAs(raw) + assertThat(ViewCodec.withoutFilter("v9\nwhatever", "OWN", 12)).isEqualTo("v9\nwhatever") } @Test diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/domain/ViewTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/domain/ViewTest.kt index d914e1f..e8b8aa0 100644 --- a/app/src/test/java/de/jeanlucmakiola/agendula/domain/ViewTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/agendula/domain/ViewTest.kt @@ -86,5 +86,8 @@ class ViewTest { assertThat(normalized[0].width).isEqualTo(BlockWidth.FULL) assertThat(normalized.layoutProblems()).isEmpty() assertThat(View.home().blocks.normalized()).isEqualTo(View.home().blocks) + // A block dropped for its source doesn't take its id with it. + assertThat(listOf(Block(5, BlockType.LISTS, BlockSource.Smart(SmartList.ALL)), full(5)).normalized()) + .containsExactly(full(5)) } } diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/UpcomingPreviewCountTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/UpcomingPreviewCountTest.kt new file mode 100644 index 0000000..890615b --- /dev/null +++ b/app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/UpcomingPreviewCountTest.kt @@ -0,0 +1,30 @@ +package de.jeanlucmakiola.agendula.ui.lists + +import com.google.common.truth.Truth.assertThat +import de.jeanlucmakiola.agendula.domain.Block +import de.jeanlucmakiola.agendula.domain.BlockOptions +import de.jeanlucmakiola.agendula.domain.BlockSource +import de.jeanlucmakiola.agendula.domain.BlockType +import de.jeanlucmakiola.agendula.domain.SmartList +import de.jeanlucmakiola.agendula.domain.View +import org.junit.jupiter.api.Test + +class UpcomingPreviewCountTest { + + @Test + fun `home previews three upcoming tasks, as 1_4 did`() { + assertThat(View.home().upcomingPreviewCount()).isEqualTo(3) + } + + @Test + fun `the longest Upcoming preview sets the count, other previews don't`() { + val view = View.home().copy( + blocks = listOf( + Block(1, BlockType.PREVIEW, BlockSource.Smart(SmartList.UPCOMING), options = BlockOptions.Preview(count = 5)), + Block(2, BlockType.PREVIEW, BlockSource.Smart(SmartList.TODAY), options = BlockOptions.Preview(count = 10)), + ), + ) + assertThat(view.upcomingPreviewCount()).isEqualTo(5) + assertThat(view.copy(blocks = emptyList()).upcomingPreviewCount()).isEqualTo(0) + } +}