Update game over dialog

This commit is contained in:
Lucas Lima 2020-12-18 17:40:52 -03:00
parent 808b87145b
commit 2e43f957cd
No known key found for this signature in database
GPG key ID: 049CCC5A365B00D2
9 changed files with 243 additions and 58 deletions

View file

@ -659,7 +659,7 @@ class GameActivity : ThematicActivity(R.layout.activity_game), DialogInterface.O
score?.rightMines ?: 0,
score?.totalMines ?: 0,
currentGameStatus.time,
if (gameResult == GameResult.Victory) 1 else 0
if (gameResult == GameResult.Victory) 2 else 1
).apply {
showAllowingStateLoss(supportFragmentManager, EndGameDialogFragment.TAG)
}

View file

@ -3,13 +3,16 @@ 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.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.appcompat.widget.AppCompatButton
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewModelScope
@ -19,24 +22,36 @@ import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository
import dev.lucasnlm.antimine.gameover.model.GameResult
import dev.lucasnlm.antimine.gameover.viewmodel.EndGameDialogEvent
import dev.lucasnlm.antimine.gameover.viewmodel.EndGameDialogViewModel
import dev.lucasnlm.antimine.preferences.PreferencesActivity
import dev.lucasnlm.external.Ads
import dev.lucasnlm.external.IAdsManager
import dev.lucasnlm.external.IBillingManager
import dev.lucasnlm.external.IInstantAppManager
import kotlinx.android.synthetic.main.view_play_games_button.view.*
import kotlinx.android.synthetic.main.view_stats.*
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.singleOrNull
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.sharedViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel
class EndGameDialogFragment : AppCompatDialogFragment() {
private val adsManager: IAdsManager by inject()
private val instantAppManager: IInstantAppManager by inject()
private val endGameViewModel by viewModel<EndGameDialogViewModel>()
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)
if (!preferencesRepository.isPremiumEnabled()) {
billingManager.start()
}
arguments?.run {
endGameViewModel.sendEvent(
EndGameDialogEvent.BuildCustomEndGame(
@ -61,8 +76,12 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
override fun onDismiss(dialog: DialogInterface) {
if (revealMinesOnDismiss) {
gameViewModel.viewModelScope.launch {
gameViewModel.revealMines()
activity?.let {
if (!it.isFinishing) {
gameViewModel.viewModelScope.launch {
gameViewModel.revealMines()
}
}
}
}
super.onDismiss(dialog)
@ -88,7 +107,83 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
}
}
findViewById<TextView>(R.id.received_message).apply {
val shareButton: AppCompatButton = findViewById(R.id.share)
val newGameButton: AppCompatButton = findViewById(R.id.new_game)
val continueButton: AppCompatButton = findViewById(R.id.continue_game)
val removeAdsButton: AppCompatButton = findViewById(R.id.remove_ads)
val settingsButton: View = findViewById(R.id.settings)
val closeButton: View = findViewById(R.id.close)
val receivedMessage: TextView = findViewById(R.id.received_message)
shareButton.setOnClickListener {
revealMinesOnDismiss = false
gameViewModel.shareObserver.postValue(Unit)
dismissAllowingStateLoss()
}
newGameButton.setOnClickListener {
revealMinesOnDismiss = false
gameViewModel.startNewGame()
dismissAllowingStateLoss()
}
continueButton.setOnClickListener {
revealMinesOnDismiss = false
if (preferencesRepository.isPremiumEnabled()) {
gameViewModel.continueObserver.postValue(Unit)
} else {
showAdsAndContinue()
}
}
settingsButton.setOnClickListener {
showSettings()
}
closeButton.setOnClickListener {
dismissAllowingStateLoss()
}
if (state.gameResult == GameResult.Victory) {
if (!instantAppManager.isEnabled(context)) {
shareButton.visibility = View.GONE
}
shareButton.visibility = View.VISIBLE
continueButton.visibility = View.GONE
} else {
shareButton.visibility = View.GONE
if (state.showContinueButton) {
continueButton.visibility = View.VISIBLE
if (!preferencesRepository.isPremiumEnabled()) {
continueButton.compoundDrawablePadding = 0
continueButton.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.watch_ads_icon, 0, 0, 0
)
}
} else {
continueButton.visibility = View.GONE
}
}
if (!preferencesRepository.isPremiumEnabled()) {
activity?.let { activity ->
removeAdsButton.visibility = View.VISIBLE
val label = context.getString(R.string.remove_ad)
val price = billingManager.getPrice().singleOrNull()
val unlockLabel = price?.let { "$label - $it" } ?: label
removeAdsButton.text = unlockLabel
removeAdsButton.setOnClickListener {
lifecycleScope.launch {
billingManager.charge(activity)
}
dismissAllowingStateLoss()
}
}
}
receivedMessage.apply {
if (state.received > 0 &&
state.gameResult == GameResult.Victory &&
preferencesRepository.useHelp()
@ -99,47 +194,39 @@ class EndGameDialogFragment : AppCompatDialogFragment() {
visibility = View.GONE
}
}
findViewById<View>(R.id.close).setOnClickListener {
dismissAllowingStateLoss()
}
if (state.gameResult == GameResult.Victory) {
revealMinesOnDismiss = false
if (!instantAppManager.isEnabled(context)) {
setNeutralButton(R.string.share) { _, _ ->
gameViewModel.shareObserver.postValue(Unit)
}
}
} else {
if (state.showContinueButton) {
setNegativeButton(R.string.retry) { _, _ ->
revealMinesOnDismiss = false
gameViewModel.retryObserver.postValue(Unit)
}
setNeutralButton(R.string.continue_game) { _, _ ->
revealMinesOnDismiss = false
gameViewModel.continueObserver.postValue(Unit)
}
} else {
setNeutralButton(R.string.retry) { _, _ ->
revealMinesOnDismiss = false
gameViewModel.retryObserver.postValue(Unit)
}
}
}
}
}
}
setView(view)
setPositiveButton(R.string.new_game) { _, _ ->
gameViewModel.startNewGame()
}
}.create()
private fun showSettings() {
startActivity(Intent(requireContext(), PreferencesActivity::class.java))
}
private fun showAdsAndContinue() {
activity?.let {
if (!it.isFinishing) {
adsManager.requestRewarded(
it,
Ads.RewardsAds,
onRewarded = {
gameViewModel.continueObserver.postValue(Unit)
dismissAllowingStateLoss()
},
onFail = {
Toast.makeText(
it.applicationContext,
R.string.unknown_error,
Toast.LENGTH_SHORT
).show()
}
)
}
}
}
companion object {
fun newInstance(
gameResult: GameResult,

View file

@ -2,7 +2,8 @@
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="#FF000000"
android:pathData="M12,5V2.21c0,-0.45 -0.54,-0.67 -0.85,-0.35l-3.8,3.79c-0.2,0.2 -0.2,0.51 0,0.71l3.79,3.79c0.32,0.31 0.86,0.09 0.86,-0.36V7c3.73,0 6.68,3.42 5.86,7.29 -0.47,2.27 -2.31,4.1 -4.57,4.57 -3.57,0.75 -6.75,-1.7 -7.23,-5.01 -0.07,-0.48 -0.49,-0.85 -0.98,-0.85 -0.6,0 -1.08,0.53 -1,1.13 0.62,4.39 4.8,7.64 9.53,6.72 3.12,-0.61 5.63,-3.12 6.24,-6.24C20.84,9.48 16.94,5 12,5z"/>

View file

@ -1,5 +1,10 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12.65,15.67c0.14,-0.36 0.05,-0.77 -0.23,-1.05l-2.09,-2.06 0.03,-0.03c1.74,-1.94 2.98,-4.17 3.71,-6.53h1.94c0.54,0 0.99,-0.45 0.99,-0.99v-0.02c0,-0.54 -0.45,-0.99 -0.99,-0.99L10,4L10,3c0,-0.55 -0.45,-1 -1,-1s-1,0.45 -1,1v1L1.99,4c-0.54,0 -0.99,0.45 -0.99,0.99 0,0.55 0.45,0.99 0.99,0.99h10.18C11.5,7.92 10.44,9.75 9,11.35c-0.81,-0.89 -1.49,-1.86 -2.06,-2.88 -0.16,-0.29 -0.45,-0.47 -0.78,-0.47 -0.69,0 -1.13,0.75 -0.79,1.35 0.63,1.13 1.4,2.21 2.3,3.21L3.3,16.87c-0.4,0.39 -0.4,1.03 0,1.42 0.39,0.39 1.02,0.39 1.42,0L9,14l2.02,2.02c0.51,0.51 1.38,0.32 1.63,-0.35zM17.5,10c-0.6,0 -1.14,0.37 -1.35,0.94l-3.67,9.8c-0.24,0.61 0.22,1.26 0.87,1.26 0.39,0 0.74,-0.24 0.88,-0.61l0.89,-2.39h4.75l0.9,2.39c0.14,0.36 0.49,0.61 0.88,0.61 0.65,0 1.11,-0.65 0.88,-1.26l-3.67,-9.8c-0.22,-0.57 -0.76,-0.94 -1.36,-0.94zM15.88,17l1.62,-4.33L19.12,17h-3.24z"/>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M12.65,15.67c0.14,-0.36 0.05,-0.77 -0.23,-1.05l-2.09,-2.06 0.03,-0.03c1.74,-1.94 2.98,-4.17 3.71,-6.53h1.94c0.54,0 0.99,-0.45 0.99,-0.99v-0.02c0,-0.54 -0.45,-0.99 -0.99,-0.99L10,4L10,3c0,-0.55 -0.45,-1 -1,-1s-1,0.45 -1,1v1L1.99,4c-0.54,0 -0.99,0.45 -0.99,0.99 0,0.55 0.45,0.99 0.99,0.99h10.18C11.5,7.92 10.44,9.75 9,11.35c-0.81,-0.89 -1.49,-1.86 -2.06,-2.88 -0.16,-0.29 -0.45,-0.47 -0.78,-0.47 -0.69,0 -1.13,0.75 -0.79,1.35 0.63,1.13 1.4,2.21 2.3,3.21L3.3,16.87c-0.4,0.39 -0.4,1.03 0,1.42 0.39,0.39 1.02,0.39 1.42,0L9,14l2.02,2.02c0.51,0.51 1.38,0.32 1.63,-0.35zM17.5,10c-0.6,0 -1.14,0.37 -1.35,0.94l-3.67,9.8c-0.24,0.61 0.22,1.26 0.87,1.26 0.39,0 0.74,-0.24 0.88,-0.61l0.89,-2.39h4.75l0.9,2.39c0.14,0.36 0.49,0.61 0.88,0.61 0.65,0 1.11,-0.65 0.88,-1.26l-3.67,-9.8c-0.22,-0.57 -0.76,-0.94 -1.36,-0.94zM15.88,17l1.62,-4.33L19.12,17h-3.24z" />
</vector>

View file

@ -1,5 +1,10 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M5,13.18v2.81c0,0.73 0.4,1.41 1.04,1.76l5,2.73c0.6,0.33 1.32,0.33 1.92,0l5,-2.73c0.64,-0.35 1.04,-1.03 1.04,-1.76v-2.81l-6.04,3.3c-0.6,0.33 -1.32,0.33 -1.92,0L5,13.18zM11.04,3.52l-8.43,4.6c-0.69,0.38 -0.69,1.38 0,1.76l8.43,4.6c0.6,0.33 1.32,0.33 1.92,0L21,10.09L21,16c0,0.55 0.45,1 1,1s1,-0.45 1,-1L23,9.59c0,-0.37 -0.2,-0.7 -0.52,-0.88l-9.52,-5.19c-0.6,-0.32 -1.32,-0.32 -1.92,0z"/>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M5,13.18v2.81c0,0.73 0.4,1.41 1.04,1.76l5,2.73c0.6,0.33 1.32,0.33 1.92,0l5,-2.73c0.64,-0.35 1.04,-1.03 1.04,-1.76v-2.81l-6.04,3.3c-0.6,0.33 -1.32,0.33 -1.92,0L5,13.18zM11.04,3.52l-8.43,4.6c-0.69,0.38 -0.69,1.38 0,1.76l8.43,4.6c0.6,0.33 1.32,0.33 1.92,0L21,10.09L21,16c0,0.55 0.45,1 1,1s1,-0.45 1,-1L23,9.59c0,-0.37 -0.2,-0.7 -0.52,-0.88l-9.52,-5.19c-0.6,-0.32 -1.32,-0.32 -1.92,0z" />
</vector>

View file

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M18,3v2h-2L16,3L8,3v2L6,5L6,3L4,3v18h2v-2h2v2h8v-2h2v2h2L20,3h-2zM8,17L6,17v-2h2v2zM8,13L6,13v-2h2v2zM8,9L6,9L6,7h2v2zM18,17h-2v-2h2v2zM18,13h-2v-2h2v2zM18,9h-2L16,7h2v2z" />
</vector>

View file

@ -31,6 +31,7 @@
android:textAlignment="center"
android:gravity="center_horizontal"
android:textAllCaps="true"
android:textColor="@color/accent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_emoji"
@ -54,14 +55,16 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/you_have_received"
android:layout_marginTop="4dp"
android:layout_marginVertical="4dp"
android:textAlignment="center"
android:gravity="center"
android:visibility="gone"
app:drawableEndCompat="@drawable/tip"
app:drawableTint="@color/accent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/subtitle"/>
app:layout_constraintTop_toBottomOf="@+id/subtitle"
tools:visibility="visible"/>
<ImageView
android:id="@+id/close"
@ -69,9 +72,82 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?selectableItemBackgroundBorderless"
android:contentDescription="@string/cancel"
android:src="@drawable/close"
android:padding="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<ImageView
android:id="@+id/settings"
android:importantForAccessibility="no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?selectableItemBackgroundBorderless"
android:src="@drawable/settings"
android:contentDescription="@string/settings"
android:padding="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<dev.lucasnlm.external.view.AdPlaceHolderView
android:id="@+id/ads"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="8dp"
android:minHeight="66dp"
app:layout_constraintTop_toBottomOf="@+id/received_message"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@+id/actions"/>
<LinearLayout
android:id="@+id/actions"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/ads"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<androidx.appcompat.widget.AppCompatButton
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:id="@+id/new_game"
android:textAllCaps="true"
android:text="@string/new_game"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<requestFocus/>
</androidx.appcompat.widget.AppCompatButton>
<androidx.appcompat.widget.AppCompatButton
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:id="@+id/continue_game"
android:textAllCaps="true"
android:text="@string/continue_game"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.appcompat.widget.AppCompatButton
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:id="@+id/share"
android:textAllCaps="true"
android:text="@string/share"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
tools:visibility="visible"/>
<androidx.appcompat.widget.AppCompatButton
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:id="@+id/remove_ads"
android:textAllCaps="true"
android:text="@string/remove_ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
tools:visibility="visible"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -79,7 +79,7 @@
<string name="open_tile">Open</string>
<string name="flag_tile">Flag</string>
<string name="retry">Retry</string>
<string name="continue_game">Continue</string>
<string name="continue_game">Continue this game</string>
<string name="empty">Empty</string>
<string name="cant_do_it_now">Impossible to do that now</string>
<string name="you_have_received">You have received: %1$d</string>

View file

@ -2,7 +2,6 @@ package dev.lucasnlm.external.view
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
@ -21,17 +20,19 @@ class AdPlaceHolderView : FrameLayout {
init {
addView(adView)
setOnClickListener {
adView.performClick()
this.visibility = View.GONE
}
// setOnClickListener {
// adView.performClick()
// this.visibility = View.GONE
// }
loadAd()
}
fun loadAd() {
adView.loadAd(AdRequest.Builder().build())
if (!adView.isLoading) {
this.visibility = View.GONE
}
// if (!adView.isLoading) {
// this.visibility = View.GONE
// }
}
}