Skip to content

Commit

Permalink
Merge pull request #144 from lucasnlm/add-themes-feature
Browse files Browse the repository at this point in the history
Add themes feature
  • Loading branch information
lucasnlm authored Aug 10, 2020
2 parents 60e5518 + 8a79e10 commit 935b1c1
Show file tree
Hide file tree
Showing 59 changed files with 973 additions and 437 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {

defaultConfig {
// versionCode and versionName must be hardcoded to support F-droid
versionCode 703031
versionName '7.3.3'
versionCode 800001
versionName '8.0.0'
minSdkVersion 16
targetSdkVersion 30
multiDexEnabled true
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@
android:value="dev.lucasnlm.antimine.GameActivity" />
</activity>

<activity
android:label="@string/themes"
android:name="dev.lucasnlm.antimine.theme.ThemeActivity"
android:theme="@style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="dev.lucasnlm.antimine.GameActivity" />
</activity>

<activity
android:name="dev.lucasnlm.antimine.stats.StatsActivity"
android:label="@string/events"
Expand Down
105 changes: 60 additions & 45 deletions app/src/main/java/dev/lucasnlm/antimine/GameActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.text.format.DateUtils
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.WindowManager
import androidx.activity.viewModels
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.TooltipCompat
import androidx.core.os.HandlerCompat.postDelayed
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
Expand All @@ -35,15 +33,19 @@ import dev.lucasnlm.antimine.control.ControlDialogFragment
import dev.lucasnlm.antimine.core.analytics.IAnalyticsManager
import dev.lucasnlm.antimine.core.analytics.models.Analytics
import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository
import dev.lucasnlm.antimine.custom.CustomLevelDialogFragment
import dev.lucasnlm.antimine.history.HistoryActivity
import dev.lucasnlm.antimine.instant.InstantAppManager
import dev.lucasnlm.antimine.custom.CustomLevelDialogFragment
import dev.lucasnlm.antimine.level.view.EndGameDialogFragment
import dev.lucasnlm.antimine.level.view.LevelFragment
import dev.lucasnlm.antimine.preferences.PreferencesActivity
import dev.lucasnlm.antimine.share.viewmodel.ShareViewModel
import dev.lucasnlm.antimine.stats.StatsActivity
import dev.lucasnlm.antimine.theme.ThemeActivity
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_tv_game.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
Expand All @@ -52,7 +54,7 @@ import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class GameActivity : AppCompatActivity(R.layout.activity_game), DialogInterface.OnDismissListener {
class GameActivity : ThematicActivity(R.layout.activity_game), DialogInterface.OnDismissListener {
@Inject
lateinit var preferencesRepository: IPreferencesRepository

Expand All @@ -68,6 +70,8 @@ class GameActivity : AppCompatActivity(R.layout.activity_game), DialogInterface.
val viewModel: GameViewModel by viewModels()
private val shareViewModel: ShareViewModel by viewModels()

override val noActionBar: Boolean = true

private var status: Status = Status.PreGame
private val areaSizeMultiplier by lazy { preferencesRepository.areaSizeMultiplier() }
private var totalMines: Int = 0
Expand Down Expand Up @@ -215,46 +219,49 @@ class GameActivity : AppCompatActivity(R.layout.activity_game), DialogInterface.
analyticsManager.sentEvent(Analytics.Quit)
}

override fun onCreateOptionsMenu(menu: Menu): Boolean =
when (status) {
is Status.Over, is Status.Running -> {
menuInflater.inflate(R.menu.top_menu_over, menu)
true
@ExperimentalCoroutinesApi
@FlowPreview
private fun bindToolbar() {
menu.apply {
TooltipCompat.setTooltipText(this, getString(R.string.open_menu))
setColorFilter(minesCount.currentTextColor)
setOnClickListener {
drawer.openDrawer(GravityCompat.START)
}
else -> true
}
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return if (item.itemId == R.id.reset) {

val confirmResign = status == Status.Running
analyticsManager.sentEvent(Analytics.TapGameReset(confirmResign))

if (confirmResign) {
newGameConfirmation {
GlobalScope.launch {
viewModel.startNewGame()
private fun refreshNewGameButton() {
newGame.apply {
TooltipCompat.setTooltipText(this, getString(R.string.new_game))
setColorFilter(minesCount.currentTextColor)
setOnClickListener {
lifecycleScope.launch {
val confirmResign = status == Status.Running
analyticsManager.sentEvent(Analytics.TapGameReset(confirmResign))

if (confirmResign) {
newGameConfirmation {
GlobalScope.launch {
viewModel.startNewGame()
}
}
} else {
GlobalScope.launch {
viewModel.startNewGame()
}
}
}
} else {
GlobalScope.launch {
viewModel.startNewGame()
}
}
true
} else {
super.onOptionsItemSelected(item)
}
}

private fun bindToolbar() {
setSupportActionBar(toolbar)
toolbar.title = ""

supportActionBar?.apply {
title = ""
setDisplayHomeAsUpEnabled(true)
setHomeButtonEnabled(true)
visibility = when (status) {
is Status.Over, is Status.Running -> {
View.VISIBLE
}
else -> {
View.GONE
}
}
}
}

Expand Down Expand Up @@ -316,6 +323,7 @@ class GameActivity : AppCompatActivity(R.layout.activity_game), DialogInterface.
R.id.about -> showAbout()
R.id.settings -> showSettings()
R.id.rate -> openRateUsLink("Drawer")
R.id.themes -> openThemes()
R.id.share_now -> shareCurrentGame()
R.id.previous_games -> openSaveHistory()
R.id.stats -> openStats()
Expand Down Expand Up @@ -397,7 +405,7 @@ class GameActivity : AppCompatActivity(R.layout.activity_game), DialogInterface.
}

private fun newGameConfirmation(action: () -> Unit) {
AlertDialog.Builder(this, R.style.MyDialog).apply {
AlertDialog.Builder(this).apply {
setTitle(R.string.new_game)
setMessage(R.string.retry_sure)
setPositiveButton(R.string.resume) { _, _ -> action() }
Expand All @@ -407,7 +415,7 @@ class GameActivity : AppCompatActivity(R.layout.activity_game), DialogInterface.
}

private fun showQuitConfirmation(action: () -> Unit) {
AlertDialog.Builder(this, R.style.MyDialog)
AlertDialog.Builder(this)
.setTitle(R.string.are_you_sure)
.setMessage(R.string.quit_confirm)
.setPositiveButton(R.string.quit) { _, _ -> action() }
Expand Down Expand Up @@ -440,6 +448,13 @@ class GameActivity : AppCompatActivity(R.layout.activity_game), DialogInterface.
}
}

private fun openThemes() {
analyticsManager.sentEvent(Analytics.OpenThemes)
Intent(this, ThemeActivity::class.java).apply {
startActivity(this)
}
}

private fun openSaveHistory() {
analyticsManager.sentEvent(Analytics.OpenSaveHistory)
Intent(this, HistoryActivity::class.java).apply {
Expand Down Expand Up @@ -510,16 +525,16 @@ class GameActivity : AppCompatActivity(R.layout.activity_game), DialogInterface.
when (event) {
Event.ResumeGame -> {
status = Status.Running
invalidateOptionsMenu()
refreshNewGameButton()
}
Event.StartNewGame -> {
status = Status.PreGame
invalidateOptionsMenu()
refreshNewGameButton()
}
Event.Resume, Event.Running -> {
status = Status.Running
viewModel.runClock()
invalidateOptionsMenu()
refreshNewGameButton()
keepScreenOn(true)
}
Event.Victory -> {
Expand All @@ -532,7 +547,7 @@ class GameActivity : AppCompatActivity(R.layout.activity_game), DialogInterface.
viewModel.stopClock()
viewModel.revealAllEmptyAreas()
viewModel.victory()
invalidateOptionsMenu()
refreshNewGameButton()
keepScreenOn(false)
waitAndShowEndGameDialog(
victory = true,
Expand All @@ -547,7 +562,7 @@ class GameActivity : AppCompatActivity(R.layout.activity_game), DialogInterface.
totalArea
)
status = Status.Over(currentTime, score)
invalidateOptionsMenu()
refreshNewGameButton()
keepScreenOn(false)
viewModel.stopClock()

Expand Down
43 changes: 43 additions & 0 deletions app/src/main/java/dev/lucasnlm/antimine/ThematicActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dev.lucasnlm.antimine

import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import dev.lucasnlm.antimine.core.themes.model.AppTheme
import dev.lucasnlm.antimine.core.themes.repository.IThemeRepository
import javax.inject.Inject

open class ThematicActivity : AppCompatActivity {
constructor() : super()
constructor(@LayoutRes contentLayoutId: Int) : super(contentLayoutId)

@Inject
lateinit var themeRepository: IThemeRepository

protected open val noActionBar: Boolean = false

protected val usingTheme: AppTheme by lazy {
currentTheme()
}

private fun currentTheme() = themeRepository.getTheme()

override fun onCreate(savedInstanceState: Bundle?) {
themeRepository.getCustomTheme()?.let {
if (noActionBar) {
setTheme(it.themeNoActionBar)
} else {
setTheme(it.theme)
}
}
super.onCreate(savedInstanceState)
}

override fun onResume() {
super.onResume()

if (usingTheme.id != currentTheme().id) {
recreate()
}
}
}
40 changes: 5 additions & 35 deletions app/src/main/java/dev/lucasnlm/antimine/TvGameActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.text.format.DateUtils
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
Expand Down Expand Up @@ -113,34 +111,6 @@ class TvGameActivity : AppCompatActivity() {
}
}

override fun onCreateOptionsMenu(menu: Menu): Boolean =
when (status) {
is Status.Over, is Status.Running -> {
menuInflater.inflate(R.menu.top_menu_over, menu)
true
}
else -> true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return if (item.itemId == R.id.reset) {
if (status == Status.Running) {
newGameConfirmation {
GlobalScope.launch {
viewModel.startNewGame()
}
}
} else {
GlobalScope.launch {
viewModel.startNewGame()
}
}
true
} else {
super.onOptionsItemSelected(item)
}
}

private fun loadGameFragment() {
val fragmentManager = supportFragmentManager

Expand All @@ -161,7 +131,7 @@ class TvGameActivity : AppCompatActivity() {
}

private fun newGameConfirmation(action: () -> Unit) {
AlertDialog.Builder(this, R.style.MyDialog).apply {
AlertDialog.Builder(this).apply {
setTitle(R.string.new_game)
setMessage(R.string.retry_sure)
setPositiveButton(R.string.resume) { _, _ -> action() }
Expand All @@ -171,7 +141,7 @@ class TvGameActivity : AppCompatActivity() {
}

private fun showQuitConfirmation(action: () -> Unit) {
AlertDialog.Builder(this, R.style.MyDialog)
AlertDialog.Builder(this)
.setTitle(R.string.are_you_sure)
.setMessage(R.string.quit_confirm)
.setPositiveButton(R.string.quit) { _, _ -> action() }
Expand All @@ -198,7 +168,7 @@ class TvGameActivity : AppCompatActivity() {
}

private fun showVictory() {
AlertDialog.Builder(this, R.style.MyDialog).apply {
AlertDialog.Builder(this).apply {
setTitle(R.string.you_won)
setMessage(R.string.all_mines_disabled)
setCancelable(false)
Expand All @@ -218,7 +188,7 @@ class TvGameActivity : AppCompatActivity() {
Handler(),
{
if (status is Status.Over && !isFinishing) {
AlertDialog.Builder(this, R.style.MyDialog).apply {
AlertDialog.Builder(this).apply {
setTitle(R.string.new_game)
setMessage(R.string.new_game_request)
setPositiveButton(R.string.yes) { _, _ ->
Expand All @@ -242,7 +212,7 @@ class TvGameActivity : AppCompatActivity() {
Handler(),
{
if (status is Status.Over && !isFinishing) {
AlertDialog.Builder(this, R.style.MyDialog).apply {
AlertDialog.Builder(this).apply {
setTitle(R.string.you_lost)
setMessage(R.string.new_game_request)
setPositiveButton(R.string.yes) { _, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.Observer
import dagger.hilt.android.AndroidEntryPoint
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.ThematicActivity
import dev.lucasnlm.antimine.about.models.AboutEvent
import dev.lucasnlm.antimine.about.viewmodel.AboutViewModel
import dev.lucasnlm.antimine.about.views.AboutInfoFragment
import dev.lucasnlm.antimine.about.views.thirds.ThirdPartyFragment
import dev.lucasnlm.antimine.about.views.translators.TranslatorsFragment

class AboutActivity : AppCompatActivity(R.layout.activity_empty) {
@AndroidEntryPoint
class AboutActivity : ThematicActivity(R.layout.activity_empty) {
private val aboutViewModel: AboutViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Loading

0 comments on commit 935b1c1

Please sign in to comment.