diff --git a/app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt b/app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt index c48f28e5..1bee8526 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt @@ -750,7 +750,13 @@ class GameActivity : ThematicActivity(R.layout.activity_game), DialogInterface.O keepScreenOn(false) if (!isResuming) { + lifecycleScope.launch { + gameViewModel.saveGame() + gameViewModel.saveStats() + } + cloudSaveManager.uploadSave() + gameViewModel.addNewTip() waitAndShowEndGameAlert( @@ -773,8 +779,10 @@ class GameActivity : ThematicActivity(R.layout.activity_game), DialogInterface.O if (!isResuming) { cloudSaveManager.uploadSave() - GlobalScope.launch(context = Dispatchers.Main) { + lifecycleScope.launch { gameViewModel.gameOver(isResuming) + gameViewModel.saveGame() + gameViewModel.saveStats() waitAndShowEndGameAlert( victory = false, await = true diff --git a/app/src/main/java/dev/lucasnlm/antimine/splash/viewmodel/SplashViewModel.kt b/app/src/main/java/dev/lucasnlm/antimine/splash/viewmodel/SplashViewModel.kt index 78f4ee3e..d51338f6 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/splash/viewmodel/SplashViewModel.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/splash/viewmodel/SplashViewModel.kt @@ -73,6 +73,8 @@ class SplashViewModel( } catch (e: Exception) { null } + }.distinctBy { + it.uid }.forEach { statsRepository.addStats(it) } diff --git a/common/src/main/java/dev/lucasnlm/antimine/common/level/viewmodel/GameViewModel.kt b/common/src/main/java/dev/lucasnlm/antimine/common/level/viewmodel/GameViewModel.kt index 1eacf3af..9f24285d 100644 --- a/common/src/main/java/dev/lucasnlm/antimine/common/level/viewmodel/GameViewModel.kt +++ b/common/src/main/java/dev/lucasnlm/antimine/common/level/viewmodel/GameViewModel.kt @@ -234,7 +234,7 @@ open class GameViewModel( } } - private suspend fun saveStats() { + suspend fun saveStats() { if (initialized) { gameController.let { if (it.hasMines()) { @@ -466,9 +466,6 @@ open class GameViewModel( } viewModelScope.launch { - saveStats() - saveGame() - checkVictoryAchievements() } }