Reduce cyclomatic complexity
This commit is contained in:
@@ -2,6 +2,7 @@ package ch.dissem.yaep.ui.common
|
|||||||
|
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.RowScope
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.layout.wrapContentHeight
|
import androidx.compose.foundation.layout.wrapContentHeight
|
||||||
@@ -61,11 +62,33 @@ private fun PuzzleRow(
|
|||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.wrapContentHeight()
|
.wrapContentHeight()
|
||||||
) {
|
) {
|
||||||
val allOptions = row.options
|
|
||||||
for (cell in row) {
|
for (cell in row) {
|
||||||
|
PuzzleCell(
|
||||||
|
cell,
|
||||||
|
row,
|
||||||
|
onUpdate,
|
||||||
|
onSnapshot,
|
||||||
|
onUndo,
|
||||||
|
spacing,
|
||||||
|
selectionManager
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun RowScope.PuzzleCell(
|
||||||
|
cell: GameCell<ItemClass<*>>,
|
||||||
|
row: GameRow<ItemClass<*>>,
|
||||||
|
onUpdate: () -> Unit,
|
||||||
|
onSnapshot: () -> Unit,
|
||||||
|
onUndo: () -> Boolean,
|
||||||
|
spacing: Dp,
|
||||||
|
selectionManager: GridSelectionManager
|
||||||
|
) {
|
||||||
var selection by remember(cell) { mutableStateOf(cell.selection) }
|
var selection by remember(cell) { mutableStateOf(cell.selection) }
|
||||||
val options = remember(cell) {
|
val options = remember(cell) {
|
||||||
allOptions.map { Toggleable(it, cell.options.contains(it)) }
|
row.options.map { Toggleable(it, cell.options.contains(it)) }
|
||||||
}
|
}
|
||||||
LaunchedEffect(cell) {
|
LaunchedEffect(cell) {
|
||||||
cell.optionsChangedListeners.add { enabled ->
|
cell.optionsChangedListeners.add { enabled ->
|
||||||
@@ -79,31 +102,9 @@ private fun PuzzleRow(
|
|||||||
val focusable = remember(cell, selectionManager) {
|
val focusable = remember(cell, selectionManager) {
|
||||||
selectionManager.add { e ->
|
selectionManager.add { e ->
|
||||||
if (selection != null) {
|
if (selection != null) {
|
||||||
when (e.key) {
|
handleClearSelection(e.key, row, cell, options, onSnapshot, onUndo)
|
||||||
Key.Spacebar, Key.Enter, Key.Delete, Key.Backspace -> {
|
|
||||||
onSelectItem(row, cell, options, null, onSnapshot, onUndo)
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> false
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
val i = getNumber(e)
|
handleSelection(e, options, cell, row, onSnapshot, onUndo)
|
||||||
if (i != null && i in 1..options.size) {
|
|
||||||
val selectedItem = options[i - 1].item
|
|
||||||
if (e.isShiftPressed) {
|
|
||||||
if (cell.options.contains(selectedItem)) {
|
|
||||||
onOptionRemoved(row, cell, selectedItem, onSnapshot)
|
|
||||||
} else {
|
|
||||||
cell.options.add(selectedItem)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
onSelectItem(row, cell, options, selectedItem, onSnapshot, onUndo)
|
|
||||||
}
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,8 +126,48 @@ private fun PuzzleRow(
|
|||||||
onSelectItem(row, cell, options, selectedItem, onSnapshot, onUndo)
|
onSelectItem(row, cell, options, selectedItem, onSnapshot, onUndo)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handleSelection(
|
||||||
|
e: KeyEvent,
|
||||||
|
options: List<Toggleable<Item<ItemClass<*>>>>,
|
||||||
|
cell: GameCell<ItemClass<*>>,
|
||||||
|
row: GameRow<ItemClass<*>>,
|
||||||
|
onSnapshot: () -> Unit,
|
||||||
|
onUndo: () -> Boolean
|
||||||
|
): Boolean {
|
||||||
|
val i = getNumber(e)
|
||||||
|
return if (i != null && i in 1..options.size) {
|
||||||
|
val selectedItem = options[i - 1].item
|
||||||
|
if (e.isShiftPressed) {
|
||||||
|
if (cell.options.contains(selectedItem)) {
|
||||||
|
onOptionRemoved(row, cell, selectedItem, onSnapshot)
|
||||||
|
} else {
|
||||||
|
cell.options.add(selectedItem)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
onSelectItem(row, cell, options, selectedItem, onSnapshot, onUndo)
|
||||||
}
|
}
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handleClearSelection(
|
||||||
|
key: Key,
|
||||||
|
row: GameRow<ItemClass<*>>,
|
||||||
|
cell: GameCell<ItemClass<*>>,
|
||||||
|
options: List<Toggleable<Item<ItemClass<*>>>>,
|
||||||
|
onSnapshot: () -> Unit,
|
||||||
|
onUndo: () -> Boolean
|
||||||
|
): Boolean = when (key) {
|
||||||
|
Key.Spacebar, Key.Enter, Key.Delete, Key.Backspace -> {
|
||||||
|
onSelectItem(row, cell, options, null, onSnapshot, onUndo)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onOptionRemoved(
|
private fun onOptionRemoved(
|
||||||
|
|||||||
Reference in New Issue
Block a user