Skip to content

Commit

Permalink
Merge pull request #12 from lucasnlm/fix-crash-with-state-loss
Browse files Browse the repository at this point in the history
Fix instant app bugs
  • Loading branch information
lucasnlm committed Mar 11, 2020
2 parents 6446527 + 89ff27a commit 018d180
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 29 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

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

Expand Down
22 changes: 15 additions & 7 deletions app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -378,16 +378,24 @@ class GameActivity : DaggerAppCompatActivity() {
}
}

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 (supportFragmentManager.findFragmentByTag(EndGameDialogFragment.TAG) == null) {
if (await > 0L) {
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)
}
}
showEndGameDialog(victory)
}, null, await)
} else {
showEndGameDialog(victory)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion common/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down

0 comments on commit 018d180

Please sign in to comment.