From ae3df88949032d1e6f721d249ac7521f351da3f4 Mon Sep 17 00:00:00 2001 From: Lucas Lima Date: Sat, 12 Dec 2020 21:37:23 -0300 Subject: [PATCH] Review mines on dismiss --- .../level/view/EndGameDialogFragment.kt | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/dev/lucasnlm/antimine/level/view/EndGameDialogFragment.kt b/app/src/main/java/dev/lucasnlm/antimine/level/view/EndGameDialogFragment.kt index e4293740..a0058037 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/level/view/EndGameDialogFragment.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/level/view/EndGameDialogFragment.kt @@ -2,6 +2,7 @@ package dev.lucasnlm.antimine.level.view import android.annotation.SuppressLint import android.app.Dialog +import android.content.DialogInterface import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -18,6 +19,7 @@ import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository import dev.lucasnlm.antimine.level.viewmodel.EndGameDialogEvent import dev.lucasnlm.antimine.level.viewmodel.EndGameDialogViewModel import dev.lucasnlm.external.IInstantAppManager +import kotlinx.android.synthetic.main.view_stats.* import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch import org.koin.android.ext.android.inject @@ -29,6 +31,7 @@ class EndGameDialogFragment : AppCompatDialogFragment() { private val endGameViewModel by viewModel() private val gameViewModel by sharedViewModel() private val preferencesRepository: IPreferencesRepository by inject() + private var revealMinesOnDismiss = true override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -53,6 +56,15 @@ class EndGameDialogFragment : AppCompatDialogFragment() { fragmentTransaction.commitAllowingStateLoss() } + override fun onDismiss(dialog: DialogInterface) { + if (revealMinesOnDismiss) { + gameViewModel.viewModelScope.launch { + gameViewModel.revealMines() + } + } + super.onDismiss(dialog) + } + @SuppressLint("InflateParams") override fun onCreateDialog(savedInstanceState: Bundle?): Dialog = AlertDialog.Builder(requireContext()).apply { @@ -83,13 +95,11 @@ class EndGameDialogFragment : AppCompatDialogFragment() { } findViewById(R.id.close).setOnClickListener { - gameViewModel.viewModelScope.launch { - gameViewModel.revealMines() - } dismissAllowingStateLoss() } if (state.isVictory == true) { + revealMinesOnDismiss = false if (!instantAppManager.isEnabled(context)) { setNeutralButton(R.string.share) { _, _ -> gameViewModel.shareObserver.postValue(Unit) @@ -98,14 +108,17 @@ class EndGameDialogFragment : AppCompatDialogFragment() { } else { if (state.showContinueButton) { setNegativeButton(R.string.retry) { _, _ -> + revealMinesOnDismiss = false gameViewModel.retryObserver.postValue(Unit) } setNeutralButton(R.string.continue_game) { _, _ -> + revealMinesOnDismiss = false gameViewModel.continueObserver.postValue(Unit) } } else { setNeutralButton(R.string.retry) { _, _ -> + revealMinesOnDismiss = false gameViewModel.retryObserver.postValue(Unit) } }