Add EndGameDialogViewModel

This commit is contained in:
Lucas Lima 2020-03-07 17:36:55 -03:00
parent 308d34b1b6
commit 76f78db7e5
2 changed files with 61 additions and 38 deletions

View file

@ -10,65 +10,58 @@ import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.ViewModelProviders
import dagger.android.support.DaggerAppCompatDialogFragment
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.ShareManager
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModel
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModelFactory
import dev.lucasnlm.antimine.level.viewmodel.EngGameDialogViewModel
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import javax.inject.Inject
class EndGameDialogFragment : DaggerAppCompatDialogFragment() {
private val gameOverEmojis = listOf(
"\uD83D\uDE10", "\uD83D\uDE44", "\uD83D\uDE25", "\uD83D\uDE13", "\uD83D\uDE31",
"\uD83E\uDD2C", "\uD83E\uDD15", "\uD83D\uDE16", "\uD83D\uDCA3", "\uD83D\uDE05"
)
private val victoryEmojis = listOf(
"\uD83D\uDE00", "\uD83D\uDE0E", "\uD83D\uDE1D", "\uD83E\uDD73", "\uD83D\uDE06"
)
@Inject
lateinit var viewModelFactory: GameViewModelFactory
private lateinit var endGameViewModel: EngGameDialogViewModel
private lateinit var viewModel: GameViewModel
private var isVictory: Boolean = false
private var time: Long = 0L
private var rightMines: Int = 0
private var totalMines: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activity?.let {
viewModel = ViewModelProviders.of(it, viewModelFactory).get(GameViewModel::class.java)
endGameViewModel = ViewModelProviders.of(this).get(EngGameDialogViewModel::class.java)
}
this.isVictory = arguments?.getBoolean(DIALOG_STATE) == true
this.time = arguments?.getLong(DIALOG_TIME) ?: 0L
this.rightMines = arguments?.getInt(DIALOG_RIGHT_MINES) ?: 0
this.totalMines = arguments?.getInt(DIALOG_TOTAL_MINES) ?: 0
}
@SuppressLint("InflateParams")
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val isVictory = arguments?.getBoolean(DIALOG_STATE) == true
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog =
AlertDialog.Builder(context!!, R.style.MyDialog).apply {
val view = LayoutInflater
.from(context)
.inflate(R.layout.dialog_end_game, null, false)
.apply {
val title = if (isVictory)
endGameViewModel.randomVictoryEmoji() else endGameViewModel.randomGameOverEmoji()
val titleRes =
if (isVictory) R.string.you_won else R.string.you_lost
val message = endGameViewModel.messageTo(context, rightMines, totalMines, time, isVictory)
val time: Long = arguments?.getLong(DIALOG_TIME) ?: 0L
val rightMines: Int = arguments?.getInt(DIALOG_RIGHT_MINES) ?: 0
val totalMines: Int = arguments?.getInt(DIALOG_TOTAL_MINES) ?: 0
findViewById<TextView>(R.id.title_emoji).text = title
findViewById<TextView>(R.id.title).text = context.getString(titleRes)
findViewById<TextView>(R.id.subtitle).text = message
}
val message: String = when {
isVictory ->
getString(R.string.game_over_desc_4, time)
rightMines / totalMines > 0.9 ->
getString(R.string.game_over_desc_3)
rightMines < 4 ->
getString(arrayOf(R.string.game_over_desc_0, R.string.game_over_desc_1).random())
else ->
getString(R.string.game_over_desc_2, rightMines, totalMines, time)
}
return AlertDialog.Builder(context!!, R.style.MyDialog).apply {
val view = LayoutInflater.from(context).inflate(R.layout.dialog_end_game, null, false)
val emojis = if (isVictory) victoryEmojis else gameOverEmojis
val titleRes = if (isVictory) R.string.you_won else R.string.you_lost
view.findViewById<TextView>(R.id.title_emoji).text = emojis.random()
view.findViewById<TextView>(R.id.title).text = context.getString(titleRes)
view.findViewById<TextView>(R.id.subtitle).text = message
setView(view)
setPositiveButton(R.string.new_game) { _, _ ->
@ -76,19 +69,18 @@ class EndGameDialogFragment : DaggerAppCompatDialogFragment() {
viewModel.startNewGame()
}
}
setNeutralButton(R.string.share) { _, _ ->
val setup = viewModel.levelSetup.value
val field = viewModel.field.value
if (setup != null && field != null) {
ShareManager(context, setup, field).share(rightMines, time.toInt())
endGameViewModel.share(context, setup, field, rightMines, time)
} else {
Toast.makeText(context, context.getString(R.string.fail_to_share), Toast.LENGTH_SHORT).show()
}
}
}.create()
}
companion object {
fun newInstance(victory: Boolean, rightMines: Int, totalMines: Int, time: Long): EndGameDialogFragment =

View file

@ -0,0 +1,31 @@
package dev.lucasnlm.antimine.level.viewmodel
import android.content.Context
import androidx.lifecycle.ViewModel
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.ShareManager
import dev.lucasnlm.antimine.common.level.data.Area
import dev.lucasnlm.antimine.common.level.data.LevelSetup
class EngGameDialogViewModel : ViewModel() {
fun randomVictoryEmoji() = listOf(
"\uD83D\uDE00", "\uD83D\uDE0E", "\uD83D\uDE1D", "\uD83E\uDD73", "\uD83D\uDE06"
).random()
fun randomGameOverEmoji() = listOf(
"\uD83D\uDE10", "\uD83D\uDE44", "\uD83D\uDE25", "\uD83D\uDE13", "\uD83D\uDE31",
"\uD83E\uDD2C", "\uD83E\uDD15", "\uD83D\uDE16", "\uD83D\uDCA3", "\uD83D\uDE05"
).random()
fun messageTo(context: Context, rightMines: Int, totalMines: Int, time: Long, isVictory: Boolean) =
when {
isVictory -> context.getString(R.string.game_over_desc_4, time)
rightMines / totalMines > 0.9 -> context.getString(R.string.game_over_desc_3)
rightMines < 4 -> context.getString(arrayOf(R.string.game_over_desc_0, R.string.game_over_desc_1).random())
else -> context.getString(R.string.game_over_desc_2, rightMines, totalMines, time)
}
fun share(context: Context, levelSetup: LevelSetup, field: List<Area>, rightMines: Int, time: Long) {
ShareManager(context, levelSetup, field).share(rightMines, time.toInt())
}
}