From e673b3cb4d3a61122630219817ad98f1c64b9157 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Wed, 24 Jul 2024 23:03:11 +0200 Subject: [PATCH] Fix cleanup --- .../src/main/kotlin/ch/dissem/yaep/ui/common/App.kt | 11 ++++++++--- .../main/kotlin/ch/dissem/yaep/ui/common/selector.kt | 2 ++ .../src/main/kotlin/ch/dissem/yaep/domain/GameCell.kt | 4 ++-- .../src/main/kotlin/ch/dissem/yaep/domain/GameRow.kt | 10 ++++++---- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/commonUI/src/main/kotlin/ch/dissem/yaep/ui/common/App.kt b/commonUI/src/main/kotlin/ch/dissem/yaep/ui/common/App.kt index ead7de9..d3ed892 100644 --- a/commonUI/src/main/kotlin/ch/dissem/yaep/ui/common/App.kt +++ b/commonUI/src/main/kotlin/ch/dissem/yaep/ui/common/App.kt @@ -71,9 +71,10 @@ fun PuzzleGrid( val allOptions = row.options for (item in row) { var selection by remember { mutableStateOf(item.selection) } - val options = remember { allOptions.map { Toggleable(it, item.options.contains(it)) } } + val options = + remember { allOptions.map { Toggleable(it, item.options.contains(it)) } } LaunchedEffect(item) { - item.removedListeners.add { removed -> + item.optionsRemovedListeners.add { removed -> options .filter { removed.contains(it.item) } .forEach { it.enabled = false } @@ -88,10 +89,14 @@ fun PuzzleGrid( .padding(8.dp) .weight(1f), options = options, + onOptionRemoved = { + item.options.remove(it) + row.cleanupOptions() + }, selectedItem = selection, onSelectItem = { item.selection = it - grid.cleanupOptions() + row.cleanupOptions() } ) } diff --git a/commonUI/src/main/kotlin/ch/dissem/yaep/ui/common/selector.kt b/commonUI/src/main/kotlin/ch/dissem/yaep/ui/common/selector.kt index 9e0de46..ab80877 100644 --- a/commonUI/src/main/kotlin/ch/dissem/yaep/ui/common/selector.kt +++ b/commonUI/src/main/kotlin/ch/dissem/yaep/ui/common/selector.kt @@ -28,6 +28,7 @@ import kotlin.math.min fun > Selector( modifier: Modifier = Modifier, options: List>>, + onOptionRemoved: (Item?) -> Unit, selectedItem: Item?, onSelectItem: (Item?) -> Unit, ) { @@ -54,6 +55,7 @@ fun > Selector( matcher = PointerMatcher.mouse(PointerButton.Secondary), onClick = { option.enabled = false + onOptionRemoved(option.item) } ) ) diff --git a/domain/src/main/kotlin/ch/dissem/yaep/domain/GameCell.kt b/domain/src/main/kotlin/ch/dissem/yaep/domain/GameCell.kt index 76100c3..a6e73c4 100644 --- a/domain/src/main/kotlin/ch/dissem/yaep/domain/GameCell.kt +++ b/domain/src/main/kotlin/ch/dissem/yaep/domain/GameCell.kt @@ -6,14 +6,14 @@ class GameCell>( options: Collection> ) { val selectionChangedListeners = mutableListOf<(Item?) -> Unit>() - val removedListeners = mutableListOf<(Collection>) -> Unit>() + val optionsRemovedListeners = mutableListOf<(Collection>) -> Unit>() var selection: Item? = selection set(value) { field = value selectionChangedListeners.forEach { listener -> listener(value) } } - val options = ObservableSet(options.toMutableSet()) { removedListeners.forEach { listener -> listener(it) } } + val options = ObservableSet(options.toMutableSet()) { optionsRemovedListeners.forEach { listener -> listener(it) } } } fun > GameCell?.mayBe(item: Item, mayHaveSelection: Boolean = true) = diff --git a/domain/src/main/kotlin/ch/dissem/yaep/domain/GameRow.kt b/domain/src/main/kotlin/ch/dissem/yaep/domain/GameRow.kt index 1bf65b6..074ebaa 100644 --- a/domain/src/main/kotlin/ch/dissem/yaep/domain/GameRow.kt +++ b/domain/src/main/kotlin/ch/dissem/yaep/domain/GameRow.kt @@ -11,12 +11,14 @@ class GameRow>( cells.forEach { cleanupOptions(it) } } - private fun cleanupOptions(cell: GameCell) { - if (cell.options.size == 1 && cell.selection == null) { - cell.selection = cell.options.first() + private fun cleanupOptions(cell: GameCell, firstCall: Boolean = true) { + if ((firstCall && cell.selection != null) || (cell.options.size == 1 && cell.selection == null)) { + if (cell.selection == null) { + cell.selection = cell.options.first() + } filter { otherCell -> otherCell != cell && otherCell.selection == null }.forEach { otherCell -> otherCell.options.remove(cell.selection) - cleanupOptions(otherCell) + cleanupOptions(otherCell, false) } } }