From 9ae0890f71eed5f60f246d4f493def601fdacf4b Mon Sep 17 00:00:00 2001 From: Fabian Zobrist Date: Fri, 27 Jan 2023 20:52:17 +0100 Subject: [PATCH] Restyle history page --- .../me/zobrist/tichucounter/MainActivity.kt | 17 +---- .../zobrist/tichucounter/ui/MainViewModel.kt | 6 -- .../tichucounter/ui/history/HistoryView.kt | 62 ++++++++++++++++--- .../ui/history/HistoryViewModel.kt | 5 ++ app/src/main/res/values-de/strings.xml | 3 + app/src/main/res/values/strings.xml | 3 + 6 files changed, 67 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt b/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt index 9b1b0e3..b64fddc 100644 --- a/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt +++ b/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt @@ -162,22 +162,9 @@ class MainActivity : AppCompatActivity(), ISettingsChangeListener { } composable("history") { - var openDialog by remember { mutableStateOf(false) } + HistoryList(historyViewModel) { navController.navigate("counter") } - HistoryList(historyViewModel, openDialog, { deleteAll -> - if (deleteAll) { - mainViewModel.deleteAllInactiveGames() - } - openDialog = false - }) { navController.navigate("counter") } - topBarActions = listOf( - TopBarAction( - Icons.Outlined.DeleteForever, - true - ) { - openDialog = true - } - ) + topBarActions = emptyList() topBarIcon = Icons.Outlined.Menu topBarTitle = stringResource(R.string.menu_history) topBarNavigationAction = diff --git a/app/src/main/java/me/zobrist/tichucounter/ui/MainViewModel.kt b/app/src/main/java/me/zobrist/tichucounter/ui/MainViewModel.kt index 14a5001..f8a9b91 100644 --- a/app/src/main/java/me/zobrist/tichucounter/ui/MainViewModel.kt +++ b/app/src/main/java/me/zobrist/tichucounter/ui/MainViewModel.kt @@ -72,10 +72,4 @@ class MainViewModel @Inject constructor( gameRepository.newGame() } } - - fun deleteAllInactiveGames() { - viewModelScope.launch { - gameRepository.deleteAllInactive() - } - } } \ No newline at end of file diff --git a/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryView.kt b/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryView.kt index b110bf5..2dfc42e 100644 --- a/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryView.kt +++ b/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryView.kt @@ -6,9 +6,11 @@ import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.outlined.DeleteForever import androidx.compose.material.icons.outlined.MoreVert import androidx.compose.material3.* import androidx.compose.runtime.* +import androidx.compose.ui.Alignment.Companion.CenterVertically import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextOverflow @@ -27,19 +29,27 @@ import java.util.* @Composable fun HistoryList( viewModel: HistoryViewModel, - showDeleteDialog: Boolean, - onDialogExecuted: (Boolean) -> Unit, navigateToCalculator: () -> Unit ) { - DeleteConfirmDialog(showDeleteDialog, onDialogExecuted) + var showDeleteDialog by remember { mutableStateOf(false) } - HistoryList(viewModel.gameAndHistory, + DeleteConfirmDialog(showDeleteDialog) { + showDeleteDialog = false + if (it) { + viewModel.deleteAllInactiveGames() + } + } + + HistoryList( + viewModel.gameAndHistory, { viewModel.activateGame(it) navigateToCalculator() }, - { viewModel.deleteGame(it) }) + { viewModel.deleteGame(it) }, + { showDeleteDialog = true }, + ) } @Preview @@ -71,13 +81,49 @@ fun DeleteConfirmDialog(show: Boolean = true, onExecuted: (Boolean) -> Unit = {} fun HistoryList( games: List, onOpenClicked: (GameId: Long) -> Unit, - onDeleteClicked: (GameId: Long) -> Unit + onDeleteClicked: (GameId: Long) -> Unit, + onDeleteAllClicked: () -> Unit + ) { Row { LazyColumn { - items(games) { + item { + Text( + modifier = Modifier.padding(start = 10.dp, end = 10.dp), + text = stringResource(R.string.active), + style = MaterialTheme.typography.headlineSmall + ) + } + items(games.filter { it.game.active }) { HistoryListItem(it, onOpenClicked, onDeleteClicked) } + + 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(games.filter { !it.game.active }) { + HistoryListItem(it, onOpenClicked, onDeleteClicked) + } + + item { + Button( + enabled = games.count() > 1, + modifier = Modifier + .padding(start = 4.dp, end = 4.dp, top = 10.dp) + .align(CenterVertically) + .fillMaxWidth(), + onClick = { onDeleteAllClicked() }) { + Icon(imageVector = Icons.Outlined.DeleteForever, contentDescription = null) + Text(text = stringResource(id = R.string.deleteAll)) + } + } + } } } } @@ -190,5 +236,5 @@ private fun HistoryListPreview() { listOf(Round(5, 50, 90)) ) ) - HistoryList(tempData, {}) {} + HistoryList(tempData, {}, {}) {} } diff --git a/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryViewModel.kt b/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryViewModel.kt index 5f57b21..10da62c 100644 --- a/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryViewModel.kt +++ b/app/src/main/java/me/zobrist/tichucounter/ui/history/HistoryViewModel.kt @@ -42,4 +42,9 @@ class HistoryViewModel @Inject constructor( } } + fun deleteAllInactiveGames() { + viewModelScope.launch { + gameRepository.deleteAllInactive() + } + } } \ No newline at end of file diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 5a3e327..daf97c4 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -18,5 +18,8 @@ Abbrechen Ok Löschen + Alle löschen + Aktives Spiel + Vergangene Spiele \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4e7cd38..2dc7822 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -21,4 +21,7 @@ Cancel Ok Delete + Delete all + Current Game + Past Games \ No newline at end of file