Merge pull request #12 from lucasnlm/fix-crash-with-state-loss

Fix instant app bugs
This commit is contained in:
Lucas Nunes 2020-03-10 21:05:49 -03:00 committed by GitHub
commit 018d180568
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 77 additions and 30 deletions

View file

@ -56,6 +56,7 @@
<activity
android:name="dev.lucasnlm.antimine.GameActivity"
android:saveEnabled="false"
android:configChanges="screenSize|orientation"
android:theme="@style/AppTheme.NoActionBar">

View file

@ -378,16 +378,24 @@ class GameActivity : DaggerAppCompatActivity() {
}
}
private fun waitAndShowEndGameDialog(victory: Boolean, await: Long = DateUtils.SECOND_IN_MILLIS) {
if (supportFragmentManager.findFragmentByTag(EndGameDialogFragment.TAG) == null) {
postDelayed(Handler(), {
if (gameStatus is GameStatus.Over && !isFinishing) {
val over = gameStatus as GameStatus.Over
EndGameDialogFragment.newInstance(victory, over.rightMines, over.totalMines, over.time).apply {
show(supportFragmentManager, EndGameDialogFragment.TAG)
}
private fun showEndGameDialog(victory: Boolean) {
if (gameStatus is GameStatus.Over && !isFinishing) {
if (supportFragmentManager.findFragmentByTag(EndGameDialogFragment.TAG) == null) {
val over = gameStatus as GameStatus.Over
EndGameDialogFragment.newInstance(victory, over.rightMines, over.totalMines, over.time).apply {
showAllowingStateLoss(supportFragmentManager, EndGameDialogFragment.TAG)
}
}
}
}
private fun waitAndShowEndGameDialog(victory: Boolean, await: Long = DateUtils.SECOND_IN_MILLIS) {
if (await > 0L) {
postDelayed(Handler(), {
showEndGameDialog(victory)
}, null, await)
} else {
showEndGameDialog(victory)
}
}

View file

@ -6,6 +6,7 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.ViewModelProviders
import dagger.android.support.DaggerAppCompatDialogFragment
import dev.lucasnlm.antimine.R
@ -30,6 +31,7 @@ class EndGameDialogFragment : DaggerAppCompatDialogFragment() {
private lateinit var viewModel: GameViewModel
private lateinit var shareViewModel: ShareViewModel
private var hasValidData = false
private var isVictory: Boolean = false
private var time: Long = 0L
private var rightMines: Int = 0
@ -44,10 +46,19 @@ class EndGameDialogFragment : DaggerAppCompatDialogFragment() {
shareViewModel = ViewModelProviders.of(this).get(ShareViewModel::class.java)
}
isVictory = arguments?.getBoolean(DIALOG_STATE) == true
time = arguments?.getLong(DIALOG_TIME) ?: 0L
rightMines = arguments?.getInt(DIALOG_RIGHT_MINES) ?: 0
totalMines = arguments?.getInt(DIALOG_TOTAL_MINES) ?: 0
arguments?.run {
isVictory = getBoolean(DIALOG_STATE) == true
time = getLong(DIALOG_TIME)
rightMines = getInt(DIALOG_RIGHT_MINES)
totalMines = getInt(DIALOG_TOTAL_MINES)
hasValidData = true
}
}
fun showAllowingStateLoss(manager: FragmentManager, tag: String?) {
val fragmentTransaction = manager.beginTransaction()
fragmentTransaction.add(this, tag)
fragmentTransaction.commitAllowingStateLoss()
}
@SuppressLint("InflateParams")
@ -57,21 +68,35 @@ class EndGameDialogFragment : DaggerAppCompatDialogFragment() {
.from(context)
.inflate(R.layout.dialog_end_game, null, false)
.apply {
val title = when {
isVictory -> endGameViewModel.randomVictoryEmoji()
else -> endGameViewModel.randomGameOverEmoji()
val titleEmoji: String
val title: String
val message: String
when {
!hasValidData -> {
titleEmoji = endGameViewModel.randomNeutralEmoji()
title = context.getString(R.string.new_game)
message = context.getString(R.string.new_game_request)
}
isVictory -> {
titleEmoji = endGameViewModel.randomVictoryEmoji()
title = context.getString(R.string.you_won)
message = endGameViewModel.messageTo(context, rightMines, totalMines, time, isVictory)
}
else -> {
titleEmoji = endGameViewModel.randomGameOverEmoji()
title = context.getString(R.string.you_lost)
message = endGameViewModel.messageTo(context, rightMines, totalMines, time, isVictory)
}
}
val titleRes =
if (isVictory) R.string.you_won else R.string.you_lost
val message = endGameViewModel.messageTo(context, rightMines, totalMines, time, isVictory)
findViewById<TextView>(R.id.title).text = context.getString(titleRes)
findViewById<TextView>(R.id.title).text = title
findViewById<TextView>(R.id.subtitle).text = message
findViewById<TextView>(R.id.title_emoji).apply {
text = title
text = titleEmoji
setOnClickListener {
text = when {
!hasValidData -> endGameViewModel.randomNeutralEmoji(text.toString())
isVictory -> endGameViewModel.randomVictoryEmoji(text.toString())
else -> endGameViewModel.randomGameOverEmoji(text.toString())
}

View file

@ -9,16 +9,29 @@ class EngGameDialogViewModel : ViewModel() {
"\uD83D\uDE00", "\uD83D\uDE0E", "\uD83D\uDE1D", "\uD83E\uDD73", "\uD83D\uDE06"
).filter { it != except }.random()
fun randomNeutralEmoji(except: String? = null) = listOf(
"\uD83D\uDE01", "\uD83E\uDD14", "\uD83D\uDE42", "\uD83D\uDE09"
).filter { it != except }.random()
fun randomGameOverEmoji(except: String? = null) = listOf(
"\uD83D\uDE10", "\uD83D\uDE44", "\uD83D\uDE25", "\uD83D\uDE13", "\uD83D\uDE31",
"\uD83E\uDD2C", "\uD83E\uDD15", "\uD83D\uDE16", "\uD83D\uDCA3", "\uD83D\uDE05"
).filter { it != except }.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 messageTo(context: Context, rightMines: Int, totalMines: Int, time: Long, isVictory: Boolean): String =
if (totalMines != 0 && time != 0L) {
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)
}
} else {
context.getString(R.string.game_over_desc_1)
}
}

View file

@ -22,8 +22,8 @@ class UnlockedHorizontalScrollView : HorizontalScrollView {
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean =
super.onTouchEvent(event) or recyclerView!!.onTouchEvent(event)
super.onTouchEvent(event) or (recyclerView?.onTouchEvent(event) ?: false)
override fun onInterceptTouchEvent(event: MotionEvent): Boolean =
super.onInterceptTouchEvent(event) or recyclerView!!.onInterceptTouchEvent(event)
super.onInterceptTouchEvent(event) or (recyclerView?.onInterceptTouchEvent(event) ?: false)
}

View file

@ -18,7 +18,7 @@
<color name="launcher_background">#FCC216</color>
<color name="accent">#D32F2F</color>
<color name="install_button">#2B8D43</color>
<color name="install_button">#00C853</color>
<color name="text_color">#212121</color>
<color name="highlight">#212121</color>