diff --git a/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt b/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt index 2b6cd47..3642c49 100644 --- a/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt +++ b/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt @@ -15,6 +15,7 @@ import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp +import androidx.compose.ui.window.Dialog import androidx.navigation.NavDestination.Companion.hierarchy import androidx.navigation.NavGraph.Companion.findStartDestination import androidx.navigation.NavHostController @@ -114,7 +115,7 @@ class MainActivity : BaseActivity() { NavigationAction { scope.launch { drawerState.open() } } } composable("history") { - HistoryList(historyViewModel) + HistoryList(historyViewModel) { navController.navigate("counter") } mainViewModel.topBarActions = emptyList() mainViewModel.topBarIcon = Icons.Outlined.ArrowBack mainViewModel.topBarTitle = stringResource(R.string.menu_history) diff --git a/app/src/main/java/me/zobrist/tichucounter/data/DaoBase.kt b/app/src/main/java/me/zobrist/tichucounter/data/DaoBase.kt index 6595ab3..5f4a9c3 100644 --- a/app/src/main/java/me/zobrist/tichucounter/data/DaoBase.kt +++ b/app/src/main/java/me/zobrist/tichucounter/data/DaoBase.kt @@ -16,4 +16,7 @@ interface DaoBase { @Delete fun delete(entity: T) + + @Delete + fun delete(entity: List) } \ No newline at end of file diff --git a/app/src/main/java/me/zobrist/tichucounter/repository/GameRepository.kt b/app/src/main/java/me/zobrist/tichucounter/repository/GameRepository.kt index 4bbac6f..4473bb6 100644 --- a/app/src/main/java/me/zobrist/tichucounter/repository/GameRepository.kt +++ b/app/src/main/java/me/zobrist/tichucounter/repository/GameRepository.kt @@ -2,6 +2,7 @@ package me.zobrist.tichucounter.repository import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.take import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import me.zobrist.tichucounter.data.Game @@ -50,7 +51,7 @@ class GameRepository @Inject constructor( } } - private suspend fun setActive(id: Long) { + suspend fun setActive(id: Long) { withContext(Dispatchers.IO) { gameDao.setActive(id) gameDao.setOthersInactive(id) @@ -85,4 +86,17 @@ class GameRepository @Inject constructor( gameDao.update(active) } } + + suspend fun deleteGame(uid: Long) { + withContext(Dispatchers.IO) { + try { + gameDao.getGameById(uid).take(1).collect() { + gameDao.delete(it) + val rounds = roundDao.getAllForGame(it.uid) + roundDao.delete(rounds) + } + } catch (_: NullPointerException) { + } + } + } } \ 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 f9a698f..9172226 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 @@ -1,77 +1,109 @@ package me.zobrist.tichucounter.ui.history -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items -import androidx.compose.material3.Card -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.Text +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.outlined.Delete +import androidx.compose.material.icons.outlined.DeleteForever +import androidx.compose.material.icons.outlined.OpenInFull +import androidx.compose.material3.* import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp +import me.zobrist.tichucounter.R import me.zobrist.tichucounter.data.GameAndScore import java.text.DateFormat import java.util.* @Composable -fun HistoryList(viewModel: HistoryViewModel) { - HistoryList(viewModel.gameAndHistory) +fun HistoryList(viewModel: HistoryViewModel, navigateToCalculator: () -> Unit) { + HistoryList(viewModel.gameAndHistory, + { + viewModel.activateGame(it) + navigateToCalculator() + }, + {viewModel.deleteGame(it)}) } @Composable -fun HistoryList(games: List) { - LazyColumn { - items(games) { - HistoryListItem(it) +fun HistoryList(games: List, + onOpenClicked: (GameId: Long) -> Unit, + onDeleteClicked: (GameId: Long) -> Unit) { + Row { + LazyColumn { + items(games) { + HistoryListItem(it, onOpenClicked, onDeleteClicked) + } } } } @Composable -fun HistoryListItem(game: GameAndScore) { +fun HistoryListItem( + game: GameAndScore, + onOpenClicked: (GameId: Long) -> Unit, + onDeleteClicked: (GameId: Long) -> Unit) { val format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.getDefault()) + + val cardColor = if(game.active) + { + CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.secondaryContainer) + + } else + { + CardDefaults.cardColors() + } + Card( modifier = Modifier .fillMaxWidth() .padding(all = 4.dp), + colors = cardColor ) { - Column( + Row( Modifier - .padding(all = 12.dp), - ) { - Row { + .padding(all = 12.dp)){ + Column(Modifier.weight(4f)){ Text( - text = game.nameA, + text = game.nameA + " vs " + game.nameB, + maxLines = 1, + overflow = TextOverflow.Ellipsis, style = MaterialTheme.typography.headlineSmall ) Text( - text = game.scoreA.toString(), - style = MaterialTheme.typography.headlineSmall + text = game.scoreA.toString() + " : " + game.scoreB.toString(), + style = MaterialTheme.typography.bodyLarge ) - } - Row { + Spacer(modifier = Modifier.padding(5.dp)) Text( - text = game.nameB, - style = MaterialTheme.typography.headlineSmall - ) - Text( - text = game.scoreB.toString(), - style = MaterialTheme.typography.headlineSmall - ) - } - Row { - Text( - text = format.format(game.modified), + text = stringResource(R.string.created, format.format(game.created)), style = MaterialTheme.typography.labelSmall ) + Text( + text = stringResource(R.string.modified, format.format(game.modified)), + style = MaterialTheme.typography.labelSmall + ) + } + Column( + Modifier + .wrapContentSize() + .width(70.dp)){ + ElevatedButton( + onClick = { onDeleteClicked(game.gameId) }, enabled = !game.active ) { + Icon(Icons.Outlined.Delete, null) + } + + ElevatedButton(onClick = { onOpenClicked(game.gameId) }, enabled = !game.active) { + Icon(Icons.Outlined.OpenInFull, null) + } } } } @@ -83,9 +115,9 @@ private fun HistoryListPreview() { val tempData = listOf( GameAndScore(false, "abc", "def", Date(), Date(), 1, 10, 50), GameAndScore(true, "ADTH", "dogfg", Date(), Date(), 2, 20, 60), - GameAndScore(false, "TeamA3", "TeamB3", Date(), Date(), 3, 30, 70), + GameAndScore(false, "TeamA3 langer Name", "TeamB3", Date(), Date(), 3, 30, 70), GameAndScore(false, "TeamA4", "TeamB4", Date(), Date(), 4, 40, 80), GameAndScore(false, "TeamA5", "TeamB5", Date(), Date(), 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 20a1bdb..bb28bf5 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 @@ -9,12 +9,14 @@ import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.launch import me.zobrist.tichucounter.data.GameAndScore import me.zobrist.tichucounter.data.GameDao +import me.zobrist.tichucounter.repository.GameRepository import javax.inject.Inject @HiltViewModel class HistoryViewModel @Inject constructor( - private val gameDao: GameDao + private val gameDao: GameDao, + private val gameRepository: GameRepository ) : ViewModel() { var gameAndHistory by mutableStateOf(emptyList()) @@ -27,4 +29,19 @@ class HistoryViewModel @Inject constructor( } } } + + fun deleteGame(gameId: Long) + { + viewModelScope.launch { + gameRepository.deleteGame(gameId) + } + } + + fun activateGame(gameId: Long) + { + viewModelScope.launch { + gameRepository.setActive(gameId) + } + + } } \ 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 d0ddb84..d0f4fbd 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -13,5 +13,8 @@ Ein Aus Neues Spiel + "Erstellt: %s " + Bearbeitet: %s + Alle löschen \ 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 16d1602..e092648 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -16,4 +16,7 @@ On Off New Game + Created: %s + Modified: %s + Delete all \ No newline at end of file