Review mines on dismiss

This commit is contained in:
Lucas Lima 2020-12-12 21:37:23 -03:00
parent 50e3dab940
commit ae3df88949

View file

@ -2,6 +2,7 @@ package dev.lucasnlm.antimine.level.view
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Dialog import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View 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.EndGameDialogEvent
import dev.lucasnlm.antimine.level.viewmodel.EndGameDialogViewModel import dev.lucasnlm.antimine.level.viewmodel.EndGameDialogViewModel
import dev.lucasnlm.external.IInstantAppManager import dev.lucasnlm.external.IInstantAppManager
import kotlinx.android.synthetic.main.view_stats.*
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject import org.koin.android.ext.android.inject
@ -29,6 +31,7 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
private val endGameViewModel by viewModel<EndGameDialogViewModel>() private val endGameViewModel by viewModel<EndGameDialogViewModel>()
private val gameViewModel by sharedViewModel<GameViewModel>() private val gameViewModel by sharedViewModel<GameViewModel>()
private val preferencesRepository: IPreferencesRepository by inject() private val preferencesRepository: IPreferencesRepository by inject()
private var revealMinesOnDismiss = true
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -53,6 +56,15 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
fragmentTransaction.commitAllowingStateLoss() fragmentTransaction.commitAllowingStateLoss()
} }
override fun onDismiss(dialog: DialogInterface) {
if (revealMinesOnDismiss) {
gameViewModel.viewModelScope.launch {
gameViewModel.revealMines()
}
}
super.onDismiss(dialog)
}
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog = override fun onCreateDialog(savedInstanceState: Bundle?): Dialog =
AlertDialog.Builder(requireContext()).apply { AlertDialog.Builder(requireContext()).apply {
@ -83,13 +95,11 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
} }
findViewById<View>(R.id.close).setOnClickListener { findViewById<View>(R.id.close).setOnClickListener {
gameViewModel.viewModelScope.launch {
gameViewModel.revealMines()
}
dismissAllowingStateLoss() dismissAllowingStateLoss()
} }
if (state.isVictory == true) { if (state.isVictory == true) {
revealMinesOnDismiss = false
if (!instantAppManager.isEnabled(context)) { if (!instantAppManager.isEnabled(context)) {
setNeutralButton(R.string.share) { _, _ -> setNeutralButton(R.string.share) { _, _ ->
gameViewModel.shareObserver.postValue(Unit) gameViewModel.shareObserver.postValue(Unit)
@ -98,14 +108,17 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
} else { } else {
if (state.showContinueButton) { if (state.showContinueButton) {
setNegativeButton(R.string.retry) { _, _ -> setNegativeButton(R.string.retry) { _, _ ->
revealMinesOnDismiss = false
gameViewModel.retryObserver.postValue(Unit) gameViewModel.retryObserver.postValue(Unit)
} }
setNeutralButton(R.string.continue_game) { _, _ -> setNeutralButton(R.string.continue_game) { _, _ ->
revealMinesOnDismiss = false
gameViewModel.continueObserver.postValue(Unit) gameViewModel.continueObserver.postValue(Unit)
} }
} else { } else {
setNeutralButton(R.string.retry) { _, _ -> setNeutralButton(R.string.retry) { _, _ ->
revealMinesOnDismiss = false
gameViewModel.retryObserver.postValue(Unit) gameViewModel.retryObserver.postValue(Unit)
} }
} }