Skip to content

Commit

Permalink
Merge pull request #256 from lucasnlm/gdx
Browse files Browse the repository at this point in the history
Gdx
  • Loading branch information
lucasnlm authored Apr 4, 2021
2 parents 7f568c7 + ec260c1 commit accc2ab
Show file tree
Hide file tree
Showing 53 changed files with 559 additions and 698 deletions.
32 changes: 18 additions & 14 deletions app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import dev.lucasnlm.antimine.gameover.model.GameResult
import dev.lucasnlm.antimine.common.level.view.GdxLevelFragment
import dev.lucasnlm.antimine.main.MainActivity
import dev.lucasnlm.antimine.preferences.IPreferencesRepository
import dev.lucasnlm.antimine.share.ShareManager
import dev.lucasnlm.antimine.splash.SplashActivity
import dev.lucasnlm.antimine.ui.ThematicActivity
import dev.lucasnlm.antimine.ui.ext.toAndroidColor
Expand Down Expand Up @@ -58,14 +57,17 @@ class GameActivity :
private val analyticsManager: IAnalyticsManager by inject()
private val instantAppManager: IInstantAppManager by inject()
private val savesRepository: ISavesRepository by inject()
private val shareViewModel: ShareManager by inject()
private val playGamesManager: IPlayGamesManager by inject()
private val adsManager: IAdsManager by inject()
private val reviewWrapper: ReviewWrapper by inject()
private val featureFlagManager: IFeatureFlagManager by inject()
private val cloudSaveManager by inject<CloudSaveManager>()
private var gameToast: Toast? = null

private val renderSquareRadius = preferencesRepository.squareRadius()
private val renderSquareDivider = preferencesRepository.squareDivider()
private val renderSquareSize = preferencesRepository.squareSize()

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
intent?.run(::handleIntent)
Expand Down Expand Up @@ -164,7 +166,7 @@ class GameActivity :
controlsToast.apply {
visibility = View.VISIBLE
backgroundTintList = tint
this.text = text
this.text = controlText
}
} else {
controlsToast.visibility = View.GONE
Expand Down Expand Up @@ -210,9 +212,6 @@ class GameActivity :
lifecycleScope.launchWhenCreated {
gameViewModel.observeSideEffects().collect {
when (it) {
is GameEvent.ShareGame -> {
shareCurrentGame()
}
is GameEvent.ShowNewGameDialog -> {
lifecycleScope.launch {
GameOverDialogFragment.newInstance(
Expand All @@ -222,6 +221,7 @@ class GameActivity :
totalMines = 0,
time = singleState().duration,
received = 0,
turn = -1,
).run {
showAllowingStateLoss(supportFragmentManager, WinGameDialogFragment.TAG)
}
Expand Down Expand Up @@ -257,6 +257,7 @@ class GameActivity :
totalMines = it.totalMines,
time = it.timestamp,
received = it.receivedTips,
turn = it.turn,
).run {
showAllowingStateLoss(supportFragmentManager, WinGameDialogFragment.TAG)
}
Expand All @@ -276,6 +277,7 @@ class GameActivity :
totalMines = it.totalMines,
time = it.timestamp,
received = it.receivedTips,
turn = it.turn,
).run {
showAllowingStateLoss(supportFragmentManager, WinGameDialogFragment.TAG)
}
Expand All @@ -294,6 +296,16 @@ class GameActivity :

override fun onResume() {
super.onResume()
if (renderSquareRadius != preferencesRepository.squareRadius() ||
renderSquareDivider != preferencesRepository.squareDivider() ||
renderSquareSize != preferencesRepository.squareSize()
) {
// If used changed any currently rendered settings, we
// must recreate the activity to force all sprites are updated.
recreate()
return
}

analyticsManager.sentEvent(Analytics.Resume)
keepScreenOn(true)
gameViewModel.resumeGame()
Expand Down Expand Up @@ -493,14 +505,6 @@ class GameActivity :
}
}

private fun shareCurrentGame() {
val levelSetup = gameViewModel.singleState().minefield
val field = gameViewModel.singleState().field
lifecycleScope.launch {
shareViewModel.shareField(levelSetup, field)
}
}

private fun keepScreenOn(enabled: Boolean) {
if (enabled) {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
Expand Down
42 changes: 29 additions & 13 deletions app/src/main/java/dev/lucasnlm/antimine/TvGameActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.text.format.DateUtils
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.badlogic.gdx.backends.android.AndroidFragmentApplication
import dev.lucasnlm.antimine.common.level.models.Status
import dev.lucasnlm.antimine.common.level.repository.ISavesRepository
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModel
import dev.lucasnlm.antimine.core.models.Analytics
Expand All @@ -23,9 +22,11 @@ import dev.lucasnlm.external.IAnalyticsManager
import dev.lucasnlm.external.IFeatureFlagManager
import dev.lucasnlm.external.IInstantAppManager
import dev.lucasnlm.external.ReviewWrapper
import kotlinx.android.synthetic.main.activity_game.*
import kotlinx.android.synthetic.main.activity_game.minesCount
import kotlinx.android.synthetic.main.activity_game.timer
import kotlinx.android.synthetic.main.activity_game_tv.*
import kotlinx.android.synthetic.main.activity_game_tv.controlsToast
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import org.koin.android.ext.android.inject
Expand All @@ -44,8 +45,6 @@ class TvGameActivity :
private val reviewWrapper: ReviewWrapper by inject()
private val featureFlagManager: IFeatureFlagManager by inject()

private var status: Status = Status.PreGame

private val areaSizeMultiplier by lazy { preferencesRepository.squareSize() }
private val currentRadius by lazy { preferencesRepository.squareRadius() }
private val useHelp by lazy { preferencesRepository.useHelp() }
Expand Down Expand Up @@ -83,6 +82,24 @@ class TvGameActivity :
visibility = View.VISIBLE
text = it.mineCount.toString()
}

if (it.turn < 3 && it.saveId == 0L) {
val color = usingTheme.palette.background.toAndroidColor(168)
val tint = ColorStateList.valueOf(color)
val controlText = gameViewModel.getControlDescription(applicationContext)

if (controlText != null && controlText.isNotBlank()) {
controlsToast.apply {
visibility = View.VISIBLE
backgroundTintList = tint
this.text = controlText
}
} else {
controlsToast.visibility = View.GONE
}
} else {
controlsToast.visibility = View.GONE
}
}
}

Expand All @@ -98,6 +115,7 @@ class TvGameActivity :
totalMines = 0,
time = singleState().duration,
received = 0,
turn = -1,
).run {
showAllowingStateLoss(supportFragmentManager, WinGameDialogFragment.TAG)
}
Expand All @@ -124,6 +142,7 @@ class TvGameActivity :
totalMines = it.totalMines,
time = it.timestamp,
received = it.receivedTips,
turn = -1,
).run {
showAllowingStateLoss(supportFragmentManager, WinGameDialogFragment.TAG)
}
Expand All @@ -137,6 +156,7 @@ class TvGameActivity :
totalMines = it.totalMines,
time = it.timestamp,
received = it.receivedTips,
turn = -1,
).run {
showAllowingStateLoss(supportFragmentManager, WinGameDialogFragment.TAG)
}
Expand All @@ -155,23 +175,19 @@ class TvGameActivity :
val willReset = restartIfNeed()

if (!willReset) {
if (status == Status.Running) {
gameViewModel.run {
refreshUserPreferences()
resumeGame()
}

analyticsManager.sentEvent(Analytics.Resume)
gameViewModel.run {
refreshUserPreferences()
resumeGame()
}

analyticsManager.sentEvent(Analytics.Resume)
}
}

override fun onPause() {
super.onPause()

if (status == Status.Running) {
gameViewModel.pauseGame()
}
gameViewModel.pauseGame()

if (isFinishing) {
analyticsManager.sentEvent(Analytics.Quit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class CloudSaveManagerImpl(
openDirectly = preferencesRepository.openGameDirectly().toInt(),
unlockedThemes = preferencesRepository.getUnlockedThemes().joinToString(" "),
squareDivider = preferencesRepository.squareDivider(),
doubleClickTimeout = preferencesRepository.getDoubleClickTimeout().toInt(),
)
}
} catch (e: Exception) {
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/dev/lucasnlm/antimine/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import dev.lucasnlm.antimine.common.BuildConfig
import dev.lucasnlm.antimine.core.analytics.DebugAnalyticsManager
import dev.lucasnlm.antimine.core.analytics.ProdAnalyticsManager
import dev.lucasnlm.antimine.core.cloud.CloudSaveManager
import dev.lucasnlm.antimine.share.ShareManager
import dev.lucasnlm.antimine.support.IapHandler
import dev.lucasnlm.external.ExternalAnalyticsWrapper
import dev.lucasnlm.external.IAnalyticsManager
import org.koin.dsl.bind
import org.koin.dsl.module

val AppModule = module {
single { ShareManager(get(), get()) }

single { IapHandler(get(), get(), get()) }

single { CloudSaveManagerImpl(get(), get(), get(), get()) } bind CloudSaveManager::class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ val ViewModelModule = module {
viewModel { MainViewModel(get(), get(), get(), get()) }
viewModel {
GameViewModel(
get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()
get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import dev.lucasnlm.antimine.core.isAndroidTv
import dev.lucasnlm.antimine.level.view.NewGameFragment
import dev.lucasnlm.antimine.preferences.IPreferencesRepository
import dev.lucasnlm.antimine.preferences.PreferencesActivity
import dev.lucasnlm.antimine.tutorial.TutorialActivity
import dev.lucasnlm.external.IAdsManager
import dev.lucasnlm.external.IAnalyticsManager
import dev.lucasnlm.external.IBillingManager
Expand Down Expand Up @@ -64,7 +65,8 @@ class GameOverDialogFragment : AppCompatDialogFragment() {
time = getLong(DIALOG_TIME, 0L),
rightMines = getInt(DIALOG_RIGHT_MINES, 0),
totalMines = getInt(DIALOG_TOTAL_MINES, 0),
received = getInt(DIALOG_RECEIVED, -1)
received = getInt(DIALOG_RECEIVED, -1),
turn = getInt(DIALOG_TURN, 0),
)
)
}
Expand All @@ -88,6 +90,7 @@ class GameOverDialogFragment : AppCompatDialogFragment() {
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 tutorialButton: AppCompatButton = findViewById(R.id.tutorial)
val settingsButton: View = findViewById(R.id.settings)
val closeButton: View = findViewById(R.id.close)
val title: TextView = findViewById(R.id.title)
Expand Down Expand Up @@ -145,7 +148,11 @@ class GameOverDialogFragment : AppCompatDialogFragment() {
dismissAllowingStateLoss()
}

if (state.showContinueButton && featureFlagManager.isContinueGameEnabled) {
if (
!state.showTutorial &&
state.showContinueButton &&
featureFlagManager.isContinueGameEnabled
) {
continueButton.visibility = View.VISIBLE
if (!preferencesRepository.isPremiumEnabled() &&
featureFlagManager.isAdsOnContinueEnabled
Expand All @@ -159,7 +166,14 @@ class GameOverDialogFragment : AppCompatDialogFragment() {
continueButton.visibility = View.GONE
}

if (!preferencesRepository.isPremiumEnabled() &&
if (state.showTutorial) {
tutorialButton.visibility = View.VISIBLE
tutorialButton.setOnClickListener {
val intent = Intent(context, TutorialActivity::class.java)
context.startActivity(intent)
}
} else if (
!preferencesRepository.isPremiumEnabled() &&
!instantAppManager.isEnabled(context) &&
featureFlagManager.isGameOverAdEnabled
) {
Expand Down Expand Up @@ -238,6 +252,7 @@ class GameOverDialogFragment : AppCompatDialogFragment() {
totalMines: Int,
time: Long,
received: Int,
turn: Int,
) = GameOverDialogFragment().apply {
arguments = Bundle().apply {
putInt(DIALOG_GAME_RESULT, gameResult.ordinal)
Expand All @@ -246,6 +261,7 @@ class GameOverDialogFragment : AppCompatDialogFragment() {
putInt(DIALOG_TOTAL_MINES, totalMines)
putInt(DIALOG_RECEIVED, received)
putLong(DIALOG_TIME, time)
putInt(DIALOG_TURN, turn)
}
}

Expand All @@ -255,6 +271,7 @@ class GameOverDialogFragment : AppCompatDialogFragment() {
private const val DIALOG_RIGHT_MINES = "dialog_right_mines"
private const val DIALOG_TOTAL_MINES = "dialog_total_mines"
private const val DIALOG_RECEIVED = "dialog_received"
private const val DIALOG_TURN = "dialog_turn"

val TAG = GameOverDialogFragment::class.simpleName!!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewModelScope
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.common.level.viewmodel.GameEvent
import dev.lucasnlm.antimine.common.level.viewmodel.GameViewModel
import dev.lucasnlm.antimine.core.models.Analytics
import dev.lucasnlm.antimine.gameover.model.GameResult
Expand Down Expand Up @@ -65,7 +64,8 @@ class WinGameDialogFragment : AppCompatDialogFragment() {
time = getLong(DIALOG_TIME, 0L),
rightMines = getInt(DIALOG_RIGHT_MINES, 0),
totalMines = getInt(DIALOG_TOTAL_MINES, 0),
received = getInt(DIALOG_RECEIVED, -1)
received = getInt(DIALOG_RECEIVED, -1),
turn = -1,
)
)
}
Expand All @@ -86,7 +86,7 @@ class WinGameDialogFragment : AppCompatDialogFragment() {
.apply {
lifecycleScope.launchWhenCreated {
endGameViewModel.observeState().collect { state ->
val shareButton: ImageView = findViewById(R.id.share)
val close: View = findViewById(R.id.close)
val statsButton: AppCompatButton = findViewById(R.id.stats)
val newGameButton: AppCompatButton = findViewById(R.id.new_game)
val removeAdsButton: AppCompatButton = findViewById(R.id.remove_ads)
Expand Down Expand Up @@ -146,22 +146,9 @@ class WinGameDialogFragment : AppCompatDialogFragment() {
}

if (state.gameResult == GameResult.Victory || state.gameResult == GameResult.Completed) {
if (instantAppManager.isEnabled(context) || context.isAndroidTv()) {
shareButton.apply {
contentDescription = getString(R.string.cancel)
setImageResource(R.drawable.close)
setOnClickListener {
dismissAllowingStateLoss()
}
}
} else {
shareButton.apply {
setOnClickListener {
gameViewModel.sendEvent(GameEvent.ShareGame)
}
}
close.setOnClickListener {
dismissAllowingStateLoss()
}

statsButton.visibility = View.VISIBLE
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sealed class EndGameDialogEvent {
val rightMines: Int,
val totalMines: Int,
val received: Int,
val turn: Int,
) : EndGameDialogEvent()

data class ChangeEmoji(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ data class EndGameDialogState(
val message: String,
val gameResult: GameResult,
val showContinueButton: Boolean,
val received: Int
val received: Int,
val showTutorial: Boolean,
)
Loading

0 comments on commit accc2ab

Please sign in to comment.