Created
July 28, 2021 07:44
-
-
Save zsoltk/a94c4dcd1335b329cd96fba180e134aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun Moves( | |
gameState: GameState, | |
onClickMove: (Int) -> Unit, | |
) { | |
val listState = rememberLazyListState() | |
val coroutineScope = rememberCoroutineScope() | |
LazyRow( | |
state = listState, | |
modifier = Modifier.padding(start = 16.dp, end = 16.dp), | |
verticalAlignment = Alignment.CenterVertically | |
) { | |
val moves = gameState.moves() | |
val selectedItemIndex = gameState.currentIndex - 1 | |
moves.forEachIndexed { index, move -> | |
val selected = index == selectedItemIndex | |
// Move number vs ply (half-move) number | |
if (index % 2 == 0) { | |
item { | |
StepNumber(index / 2 + 1) | |
Spacer(modifier = Modifier.width(2.dp)) | |
} | |
} | |
// Passing a key to item | |
item("$index$selected") { | |
if (selected) { | |
Pill { | |
Move(move) { onClickMove(index) } | |
} | |
} else { | |
Move(move) { onClickMove(index) } | |
} | |
} | |
// Some trailing space after every second move | |
if (index % 2 == 1) { | |
item { | |
Spacer(modifier = Modifier.width(10.dp)) | |
} | |
} | |
} | |
// Scroll to the currently selected one | |
if (moves.isNotEmpty()) { | |
coroutineScope.launch { | |
listState.animateScrollToItem(selectedItemIndex * 2) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment