Fix bugs on game over dialog

This commit is contained in:
Lucas Lima 2020-12-31 12:54:47 -03:00 committed by Lucas Nunes
parent 22b1ffc7c8
commit ad18107a52
3 changed files with 53 additions and 41 deletions

View file

@ -2,9 +2,9 @@ package dev.lucasnlm.antimine.gameover
import android.annotation.SuppressLint
import android.app.Dialog
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
@ -44,7 +44,6 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
private val gameViewModel by sharedViewModel<GameViewModel>()
private val preferencesRepository: IPreferencesRepository by inject()
private val billingManager: IBillingManager by inject()
private var revealMinesOnDismiss = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -75,19 +74,6 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
fragmentTransaction.commitAllowingStateLoss()
}
override fun onDismiss(dialog: DialogInterface) {
if (revealMinesOnDismiss) {
activity?.let {
if (!it.isFinishing) {
gameViewModel.viewModelScope.launch {
gameViewModel.revealMines()
}
}
}
}
super.onDismiss(dialog)
}
@SuppressLint("InflateParams")
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog =
AlertDialog.Builder(requireContext()).apply {
@ -97,17 +83,6 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
.apply {
lifecycleScope.launchWhenCreated {
endGameViewModel.observeState().collect { state ->
findViewById<TextView>(R.id.title).text = state.title
findViewById<TextView>(R.id.subtitle).text = state.message
findViewById<ImageView>(R.id.title_emoji).apply {
setImageResource(state.titleEmoji)
setOnClickListener {
endGameViewModel.sendEvent(
EndGameDialogEvent.ChangeEmoji(state.gameResult, state.titleEmoji)
)
}
}
val adsView: AdPlaceHolderView = findViewById(R.id.ads)
val shareButton: AppCompatButton = findViewById(R.id.share)
val newGameButton: AppCompatButton = findViewById(R.id.new_game)
@ -116,21 +91,32 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
val settingsButton: View = findViewById(R.id.settings)
val closeButton: View = findViewById(R.id.close)
val receivedMessage: TextView = findViewById(R.id.received_message)
val title: TextView = findViewById(R.id.title)
val subtitle: TextView = findViewById(R.id.subtitle)
val emoji: ImageView = findViewById(R.id.title_emoji)
title.text = state.title
subtitle.text = state.message
emoji.apply {
setImageResource(state.titleEmoji)
setOnClickListener {
endGameViewModel.sendEvent(
EndGameDialogEvent.ChangeEmoji(state.gameResult, state.titleEmoji)
)
}
}
shareButton.setOnClickListener {
revealMinesOnDismiss = false
gameViewModel.shareObserver.postValue(Unit)
}
newGameButton.setOnClickListener {
revealMinesOnDismiss = false
gameViewModel.startNewGame()
dismissAllowingStateLoss()
}
continueButton.setOnClickListener {
revealMinesOnDismiss = false
if (preferencesRepository.isPremiumEnabled()) {
gameViewModel.continueObserver.postValue(Unit)
dismissAllowingStateLoss()
@ -144,6 +130,14 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
}
closeButton.setOnClickListener {
activity?.let {
if (!it.isFinishing) {
gameViewModel.viewModelScope.launch {
gameViewModel.revealMines()
}
}
}
dismissAllowingStateLoss()
}
@ -203,8 +197,26 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
}
}
setOnKeyListener { _, _, keyEvent ->
if (keyEvent.keyCode == KeyEvent.KEYCODE_BACK) {
activity?.let {
if (!it.isFinishing) {
gameViewModel.viewModelScope.launch {
gameViewModel.revealMines()
}
}
}
dismissAllowingStateLoss()
true
} else {
false
}
}
setView(view)
}.create()
}.create().apply {
setCanceledOnTouchOutside(false)
}
private fun showSettings() {
startActivity(Intent(requireContext(), PreferencesActivity::class.java))

View file

@ -1,6 +1,7 @@
package dev.lucasnlm.antimine.gameover.viewmodel
import android.content.Context
import androidx.annotation.DrawableRes
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.core.viewmodel.IntentViewModel
import dev.lucasnlm.antimine.gameover.model.GameResult
@ -11,13 +12,13 @@ class EndGameDialogViewModel(
) : IntentViewModel<EndGameDialogEvent, EndGameDialogState>() {
private fun List<Int>.safeRandomEmoji(
except: Int? = null,
@DrawableRes except: Int,
fallback: Int = R.drawable.emoji_smiling_face_with_sunglasses
) = this.filter { it != except }
) = filter { it != except }
.ifEmpty { listOf(fallback) }
.random()
private fun randomVictoryEmoji(except: Int? = null) = listOf(
private fun randomVictoryEmoji(except: Int) = listOf(
R.drawable.emoji_beaming_face_with_smiling_eyes,
R.drawable.emoji_astonished_face,
R.drawable.emoji_cowboy_hat_face,
@ -33,13 +34,13 @@ class EndGameDialogViewModel(
R.drawable.emoji_triangular_flag,
).safeRandomEmoji(except)
private fun randomNeutralEmoji(except: Int? = null) = listOf(
private fun randomNeutralEmoji(@DrawableRes except: Int) = listOf(
R.drawable.emoji_grimacing_face,
R.drawable.emoji_smiling_face_with_sunglasses,
R.drawable.emoji_triangular_flag,
).safeRandomEmoji(except)
private fun randomGameOverEmoji(except: Int? = null) = listOf(
private fun randomGameOverEmoji(@DrawableRes except: Int) = listOf(
R.drawable.emoji_anguished_face,
R.drawable.emoji_astonished_face,
R.drawable.emoji_bomb,
@ -53,7 +54,6 @@ class EndGameDialogViewModel(
R.drawable.emoji_face_with_head_bandage,
R.drawable.emoji_collision,
R.drawable.emoji_sad_but_relieved_face,
).safeRandomEmoji(except)
private fun messageTo(time: Long, gameResult: GameResult): String =
@ -81,7 +81,7 @@ class EndGameDialogViewModel(
val state = when (event.gameResult) {
GameResult.Victory -> {
EndGameDialogState(
titleEmoji = randomVictoryEmoji(),
titleEmoji = randomVictoryEmoji(0),
title = context.getString(R.string.you_won),
message = messageTo(event.time, event.gameResult),
gameResult = event.gameResult,
@ -91,7 +91,7 @@ class EndGameDialogViewModel(
}
GameResult.GameOver -> {
EndGameDialogState(
titleEmoji = randomGameOverEmoji(),
titleEmoji = randomGameOverEmoji(0),
title = context.getString(R.string.you_lost),
message = messageTo(event.time, event.gameResult),
gameResult = event.gameResult,
@ -101,7 +101,7 @@ class EndGameDialogViewModel(
}
GameResult.Completed -> {
EndGameDialogState(
titleEmoji = randomNeutralEmoji(),
titleEmoji = randomNeutralEmoji(0),
title = context.getString(R.string.you_finished),
message = context.getString(R.string.new_game_request),
gameResult = event.gameResult,

View file

@ -14,7 +14,7 @@ class MinefieldConverter {
@TypeConverter
fun toMinefield(jsonInput: String): Minefield =
jsonAdapter.fromJson(jsonInput) ?: Minefield(9, 9, 9,)
jsonAdapter.fromJson(jsonInput) ?: Minefield(9, 9, 9)
@TypeConverter
fun toJsonString(field: Minefield): String = jsonAdapter.toJson(field)