From 2e43f957cdd7cbb4b716e6136fba793a9b020fe2 Mon Sep 17 00:00:00 2001 From: Lucas Lima Date: Fri, 18 Dec 2020 17:40:52 -0300 Subject: [PATCH] Update game over dialog --- .../dev/lucasnlm/antimine/GameActivity.kt | 2 +- .../gameover/EndGameDialogFragment.kt | 161 ++++++++++++++---- app/src/main/res/drawable/retry.xml | 3 +- app/src/main/res/drawable/translate.xml | 13 +- app/src/main/res/drawable/tutorial.xml | 13 +- app/src/main/res/drawable/watch_ads_icon.xml | 10 ++ app/src/main/res/layout/dialog_end_game.xml | 80 ++++++++- common/src/main/res/values-en-rUS/strings.xml | 2 +- .../external/view/AdPlaceHolderView.kt | 17 +- 9 files changed, 243 insertions(+), 58 deletions(-) create mode 100644 app/src/main/res/drawable/watch_ads_icon.xml diff --git a/app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt b/app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt index ce8840c3..dfc65552 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt @@ -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) } diff --git a/app/src/main/java/dev/lucasnlm/antimine/gameover/EndGameDialogFragment.kt b/app/src/main/java/dev/lucasnlm/antimine/gameover/EndGameDialogFragment.kt index cc026549..574afe54 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/gameover/EndGameDialogFragment.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/gameover/EndGameDialogFragment.kt @@ -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() private val gameViewModel by sharedViewModel() 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(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(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, diff --git a/app/src/main/res/drawable/retry.xml b/app/src/main/res/drawable/retry.xml index c9701566..bbec7a73 100644 --- a/app/src/main/res/drawable/retry.xml +++ b/app/src/main/res/drawable/retry.xml @@ -2,7 +2,8 @@ android:width="24dp" android:height="24dp" android:viewportWidth="24" - android:viewportHeight="24"> + android:viewportHeight="24" + android:tint="?attr/colorControlNormal"> diff --git a/app/src/main/res/drawable/translate.xml b/app/src/main/res/drawable/translate.xml index 475bb6e2..3b75529f 100644 --- a/app/src/main/res/drawable/translate.xml +++ b/app/src/main/res/drawable/translate.xml @@ -1,5 +1,10 @@ - - + + diff --git a/app/src/main/res/drawable/tutorial.xml b/app/src/main/res/drawable/tutorial.xml index 20d5e297..9b09c549 100644 --- a/app/src/main/res/drawable/tutorial.xml +++ b/app/src/main/res/drawable/tutorial.xml @@ -1,5 +1,10 @@ - - + + diff --git a/app/src/main/res/drawable/watch_ads_icon.xml b/app/src/main/res/drawable/watch_ads_icon.xml new file mode 100644 index 00000000..d4d954df --- /dev/null +++ b/app/src/main/res/drawable/watch_ads_icon.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/dialog_end_game.xml b/app/src/main/res/layout/dialog_end_game.xml index cafda748..ae958ef2 100644 --- a/app/src/main/res/layout/dialog_end_game.xml +++ b/app/src/main/res/layout/dialog_end_game.xml @@ -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"/> + + + + + + + + + + + + + + + + + diff --git a/common/src/main/res/values-en-rUS/strings.xml b/common/src/main/res/values-en-rUS/strings.xml index 8b1c5ec5..9916cfb6 100644 --- a/common/src/main/res/values-en-rUS/strings.xml +++ b/common/src/main/res/values-en-rUS/strings.xml @@ -79,7 +79,7 @@ Open Flag Retry - Continue + Continue this game Empty Impossible to do that now You have received: %1$d diff --git a/proprietary/src/main/java/dev/lucasnlm/external/view/AdPlaceHolderView.kt b/proprietary/src/main/java/dev/lucasnlm/external/view/AdPlaceHolderView.kt index 9e27b155..db8d6273 100644 --- a/proprietary/src/main/java/dev/lucasnlm/external/view/AdPlaceHolderView.kt +++ b/proprietary/src/main/java/dev/lucasnlm/external/view/AdPlaceHolderView.kt @@ -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 +// } } }