diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/domain/ViewLayout.kt b/app/src/main/java/de/jeanlucmakiola/agendula/domain/ViewLayout.kt index 023d76c..4c46585 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/domain/ViewLayout.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/domain/ViewLayout.kt @@ -14,16 +14,18 @@ import java.time.temporal.TemporalAdjusters /** Whether a block of [type] can be added to these blocks: a second task list can't. */ fun List.canAdd(type: BlockType): Boolean = type != BlockType.TASK_LIST || none { it.type == BlockType.TASK_LIST } -/** An id no block here has. */ -fun List.nextBlockId(): Long = (maxOfOrNull { it.id } ?: 0L) + 1 +/** An id no block here has, nor any of [reserved] (a removed block its undo may bring back). */ +fun List.nextBlockId(reserved: Set = emptySet()): Long = + (maxOf(maxOfOrNull { it.id } ?: 0L, reserved.maxOrNull() ?: 0L)) + 1 /** * These blocks with a new [type] block reading [source], at the end but before * a task list; unchanged when [canAdd] says no or [source] doesn't suit the type. + * Its id is none of [reserved] either. */ -fun List.withBlock(type: BlockType, source: BlockSource? = null): List { +fun List.withBlock(type: BlockType, source: BlockSource? = null, reserved: Set = emptySet()): List { if (!canAdd(type) || (source != null) != type.takesSource) return this - val block = Block(nextBlockId(), type, source) + val block = Block(nextBlockId(reserved), type, source) val taskList = indexOfFirst { it.type == BlockType.TASK_LIST } return if (taskList < 0) this + block else toMutableList().apply { add(taskList, block) } } @@ -33,11 +35,13 @@ fun List.without(id: Long): List = filterNot { it.id == id } /** * [block] put back at [index], e.g. undoing its removal; a task list still goes - * last. Unchanged when a block with its id is here already. + * last. Unchanged when it is here already, or is a task list and one is; given a + * new id when another block took its own meanwhile. */ fun List.restored(block: Block, index: Int): List { - if (any { it.id == block.id }) return this - return toMutableList().apply { add(index.coerceIn(0, size), block) }.normalized() + if (any { it == block } || !canAdd(block.type)) return this + val back = if (any { it.id == block.id }) block.copy(id = nextBlockId()) else block + return toMutableList().apply { add(index.coerceIn(0, size), back) }.normalized() } /** These blocks in the order of [ids]; blocks [ids] leaves out keep their place after them. A task list stays last. */ diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/domain/ViewLayoutTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/domain/ViewLayoutTest.kt index 98c7435..322fab6 100644 --- a/app/src/test/java/de/jeanlucmakiola/agendula/domain/ViewLayoutTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/agendula/domain/ViewLayoutTest.kt @@ -139,4 +139,26 @@ class ViewLayoutTest { assertThat(recentlyCompleted(tasks, 3).map { it.taskId }).containsExactly(2L, 5L, 1L).inOrder() assertThat(recentlyCompleted(tasks, 10).map { it.taskId }).containsExactly(2L, 5L, 1L, 3L).inOrder() } + + @Test + fun `undo still brings a block back when a new one took its id meanwhile`() { + val last = home.last() + val removed = home.without(last.id) + // A block added while the undo is offered keeps clear of the removed block's id. + val added = removed.withBlock(BlockType.WEEK_STRIP, reserved = setOf(last.id)) + assertThat(added.last().id).isEqualTo(last.id + 1) + // And one that took it anyway doesn't swallow the undo. + val clash = removed + Block(last.id, BlockType.WEEK_STRIP) + val restored = clash.restored(last, 6) + assertThat(restored).hasSize(8) + assertThat(restored[6].type).isEqualTo(last.type) + assertThat(restored.layoutProblems()).isEmpty() + } + + @Test + fun `a removed task list doesn't come back next to a new one`() { + val taskList = withTaskList.last() + val other = withTaskList.without(taskList.id).withBlock(BlockType.TASK_LIST, BlockSource.Smart(SmartList.TODAY)) + assertThat(other.restored(taskList, 7)).isEqualTo(other) + } }