Show all games in list but mark active game with an badge.
All checks were successful
Build Android / build (push) Successful in 7m50s
Build Android / build (pull_request) Successful in 7m27s

This commit is contained in:
2023-08-25 15:59:58 +02:00
parent 7108af4cf4
commit 8521247c58

View File

@@ -10,9 +10,12 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredHeight
import androidx.compose.foundation.layout.requiredWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.items
@@ -22,9 +25,9 @@ import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material.icons.outlined.DeleteForever
import androidx.compose.material.icons.outlined.RestartAlt
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Badge
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.DismissDirection
import androidx.compose.material3.DismissValue
import androidx.compose.material3.ExperimentalMaterial3Api
@@ -45,6 +48,7 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Alignment.Companion.CenterVertically
import androidx.compose.ui.Alignment.Companion.TopEnd
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Color
@@ -161,33 +165,23 @@ fun HistoryList(
) {
Row {
LazyColumn(state = lazyListState) {
item {
Text(
modifier = Modifier.padding(start = 10.dp, end = 10.dp),
text = stringResource(R.string.active),
style = MaterialTheme.typography.headlineSmall
items(
items = games,
key = { it.hashCode() }) {
if (it.game.active) {
HistoryListItem(
it,
Modifier.animateItemPlacement()
)
}
items(games.filter { it.game.active }) {
HistoryListItem(it, Modifier.animateItemPlacement())
}
if (games.count() > 1) {
item {
Text(
modifier = Modifier.padding(start = 10.dp, end = 10.dp, top = 10.dp),
text = stringResource(R.string.inactive),
style = MaterialTheme.typography.headlineSmall
)
}
items(items = games.filter { !it.game.active }, key = {
it.hashCode()
}) {
} else {
DismissibleHistoryListItem(
it, onOpenClicked, onDeleteClicked, Modifier.animateItemPlacement()
it,
Modifier.animateItemPlacement(),
onOpenClicked,
onDeleteClicked
)
}
}
item {
Button(enabled = games.count() > 1,
@@ -203,16 +197,17 @@ fun HistoryList(
}
}
}
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DismissibleHistoryListItem(
game: GameWithScores,
modifier: Modifier = Modifier,
onOpenClicked: (gameId: Long) -> Unit,
onDeleteClicked: (gameId: Long) -> Unit,
modifier: Modifier = Modifier,
) {
val density = LocalDensity.current
@@ -228,7 +223,18 @@ fun DismissibleHistoryListItem(
true
})
SwipeToDismiss(modifier = modifier, state = dismissState, background = {
val directions = if (game.game.active) {
setOf()
} else {
setOf(DismissDirection.EndToStart, DismissDirection.StartToEnd)
}
SwipeToDismiss(
modifier = modifier,
state = dismissState,
directions = directions,
background = {
val direction = dismissState.dismissDirection ?: return@SwipeToDismiss
val color by animateColorAsState(
when (dismissState.targetValue) {
@@ -275,6 +281,7 @@ fun DismissibleHistoryListItem(
})
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HistoryListItem(
game: GameWithScores, modifier: Modifier = Modifier
@@ -282,25 +289,18 @@ fun HistoryListItem(
val format =
DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.getDefault())
val cardColor = if (game.game.active) {
CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.secondaryContainer)
} else {
CardDefaults.cardColors()
}
val totalScores = game.getTotalPoints()
Card(
modifier = modifier
.fillMaxWidth()
.padding(all = 4.dp), colors = cardColor
.padding(all = 4.dp)
) {
Row(
Modifier.padding(all = 12.dp)
) {
Column(Modifier.weight(4f)) {
Box( modifier = modifier.fillMaxSize()) {
Column {
Text(
text = game.game.nameA + " vs " + game.game.nameB,
maxLines = 1,
@@ -317,6 +317,18 @@ fun HistoryListItem(
style = MaterialTheme.typography.labelSmall
)
}
if (game.game.active) {
Badge(
modifier = Modifier.align(TopEnd),
contentColor = MaterialTheme.colorScheme.onPrimary,
containerColor = MaterialTheme.colorScheme.primary) {
Text(
text = stringResource(id = R.string.active),
style = MaterialTheme.typography.labelSmall
)
}
}
}
}
}
}
@@ -326,7 +338,7 @@ fun HistoryListItem(
private fun HistoryListPreview() {
val tempData = listOf(
GameWithScores(
Game(true, "abc", "def", Date(), Date()), listOf(Round(1, 550, 500))
Game(true, "abcsdf sdaf asdf sdf ", "defsadf asdf sadf ", Date(), Date()), listOf(Round(1, 550, 500))
), GameWithScores(
Game(false, "ADTH", "dogfg", Date(), Date()), listOf(Round(2, 20, 60))
), GameWithScores(