From 17ed7d18f65142390a1202912ce3ce4a3d99a5f3 Mon Sep 17 00:00:00 2001 From: Fabian Zobrist Date: Tue, 27 Dec 2022 19:00:59 +0100 Subject: [PATCH] Implement undo last round. --- .../me/zobrist/tichucounter/MainActivity.kt | 22 ++++++++++--------- .../fragments/HistoryListViewModel.kt | 10 +++++---- .../tichucounter/repository/GameRepository.kt | 15 ++++++++++++- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt b/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt index e64f60a..711e4ec 100644 --- a/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt +++ b/app/src/main/java/me/zobrist/tichucounter/MainActivity.kt @@ -148,7 +148,10 @@ class MainActivity : AppCompatActivity() { builder.setMessage(getString(R.string.confirmClear)).setTitle(R.string.clear) .setCancelable(false).setPositiveButton(getString(R.string.yes)) { dialog, _ -> dialog.dismiss() - clearAll() + GlobalScope.launch(Dispatchers.IO) { + gameRepository.newGame() + historyListViewModel.updateAll() + } }.setNegativeButton(getString(R.string.no)) { dialog, _ -> dialog.cancel() } @@ -157,7 +160,14 @@ class MainActivity : AppCompatActivity() { true } R.id.action_undo -> { - //historyListViewModel.revertLastRound() + GlobalScope.launch(Dispatchers.IO) { + val history = gameRepository.getActiveRoundHistory() + if(history.isNotEmpty()) + { + gameRepository.removeRoundFromHistory(history.last()) + historyListViewModel.updateAll() + } + } true } R.id.action_theme -> { @@ -177,14 +187,6 @@ class MainActivity : AppCompatActivity() { } } - private fun clearAll() { - GlobalScope.launch(Dispatchers.IO) { - gameRepository.newGame() - historyListViewModel.updateAll() - } - } - - private fun chooseThemeDialog() { val builder = AlertDialog.Builder(this) diff --git a/app/src/main/java/me/zobrist/tichucounter/fragments/HistoryListViewModel.kt b/app/src/main/java/me/zobrist/tichucounter/fragments/HistoryListViewModel.kt index b03667f..339e3a4 100644 --- a/app/src/main/java/me/zobrist/tichucounter/fragments/HistoryListViewModel.kt +++ b/app/src/main/java/me/zobrist/tichucounter/fragments/HistoryListViewModel.kt @@ -81,10 +81,12 @@ class HistoryListViewModel @Inject constructor(val gameRepository: GameRepositor fun updateAll() { viewModelScope.launch { val scores = gameRepository.getActiveRoundHistory() - getHistoryA(scores) - getHistoryB(scores) - getScoreA(scores) - getScoreB(scores) + if (scores != null) { + getHistoryA(scores) + getHistoryB(scores) + getScoreA(scores) + getScoreB(scores) + } scrollDown() } 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 2ff62b2..fe780e2 100644 --- a/app/src/main/java/me/zobrist/tichucounter/repository/GameRepository.kt +++ b/app/src/main/java/me/zobrist/tichucounter/repository/GameRepository.kt @@ -46,7 +46,20 @@ class GameRepository @Inject constructor(private val gameDao: GameDao, private { return withContext(Dispatchers.IO) { val active = getActiveGame() - return@withContext roundDao.getAllForGame(active?.uid) + if(active?.uid != null) + { + return@withContext roundDao.getAllForGame(active.uid) + } + else { + return@withContext listOf() + } + } + } + + suspend fun removeRoundFromHistory(round: Round) + { + withContext(Dispatchers.IO) { + roundDao.delete(round) } }