Improve delete multiple games in a row.
All checks were successful
Build Android / build (push) Successful in 4m33s
All checks were successful
Build Android / build (push) Successful in 4m33s
closes #53
This commit is contained in:
@@ -27,6 +27,8 @@ class GameRepository @Inject constructor(
|
||||
|
||||
private val newGameFlow = MutableStateFlow(Game())
|
||||
|
||||
private var deletedGame: GameWithScores? = null
|
||||
|
||||
init {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
gameDao.getActiveAsFlow().collect {
|
||||
@@ -104,14 +106,33 @@ class GameRepository @Inject constructor(
|
||||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val game = gameDao.getGameById(uid)
|
||||
gameDao.delete(game)
|
||||
val rounds = roundDao.getAllForGame(game.uid)
|
||||
|
||||
deletedGame = GameWithScores(game, rounds)
|
||||
|
||||
gameDao.delete(game)
|
||||
roundDao.delete(rounds)
|
||||
} catch (_: NullPointerException) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun restoreLastDeletedGame() {
|
||||
if (deletedGame == null) {
|
||||
return
|
||||
}
|
||||
val revert = deletedGame!!
|
||||
deletedGame = null
|
||||
|
||||
withContext(Dispatchers.IO) {
|
||||
gameDao.insert(revert.game)
|
||||
|
||||
revert.rounds.forEach {
|
||||
roundDao.insert(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun deleteAllInactive() {
|
||||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
|
||||
@@ -95,6 +95,7 @@ fun HistoryList(
|
||||
viewModel.activateGame(it)
|
||||
lazyListState.animateScrollToItem(0)
|
||||
|
||||
snackbarHostState.currentSnackbarData?.dismiss()
|
||||
val result = snackbarHostState.showSnackbar(
|
||||
message = activatedMessage,
|
||||
actionLabel = activatedActionLabel,
|
||||
@@ -108,7 +109,9 @@ fun HistoryList(
|
||||
},
|
||||
onDeleteClicked = {
|
||||
scope.launch {
|
||||
viewModel.markToDelete(it)
|
||||
viewModel.deleteGame(it)
|
||||
|
||||
snackbarHostState.currentSnackbarData?.dismiss()
|
||||
val result = snackbarHostState.showSnackbar(
|
||||
message = deletedMessage,
|
||||
actionLabel = deletedActionLabel,
|
||||
@@ -118,7 +121,7 @@ fun HistoryList(
|
||||
if (result == SnackbarResult.Dismissed) {
|
||||
viewModel.deleteGame(it)
|
||||
} else {
|
||||
viewModel.unmarkToDelete(it)
|
||||
viewModel.restoreLastDeletedGame()
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -33,20 +33,18 @@ class HistoryViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun markToDelete(gameId: Long) {
|
||||
gameAndHistory = fullList.filter { it.game.uid != gameId }
|
||||
}
|
||||
|
||||
fun unmarkToDelete(gameId: Long) {
|
||||
gameAndHistory = fullList
|
||||
}
|
||||
|
||||
fun deleteGame(gameId: Long) {
|
||||
viewModelScope.launch {
|
||||
gameRepository.deleteGame(gameId)
|
||||
}
|
||||
}
|
||||
|
||||
fun restoreLastDeletedGame() {
|
||||
viewModelScope.launch {
|
||||
gameRepository.restoreLastDeletedGame()
|
||||
}
|
||||
}
|
||||
|
||||
fun activateGame(gameId: Long) {
|
||||
viewModelScope.launch {
|
||||
gameRepository.setActive(gameId)
|
||||
|
||||
Reference in New Issue
Block a user