From 784db659648aa7a418d28e95b07e2da4f930112e Mon Sep 17 00:00:00 2001 From: Lucas Lima Date: Mon, 24 Aug 2020 23:45:38 -0300 Subject: [PATCH] Add reset button --- .../preferences/PreferencesActivity.kt | 49 +++++++++++++++++-- .../lucasnlm/antimine/stats/StatsActivity.kt | 2 +- .../lucasnlm/antimine/theme/ThemeActivity.kt | 18 +++++++ .../antimine/theme/viewmodel/ThemeEvent.kt | 2 + .../theme/viewmodel/ThemeViewModel.kt | 5 +- .../{stats_menu.xml => delete_icon_menu.xml} | 0 .../core/preferences/PreferencesRepository.kt | 12 +++++ .../core/themes/repository/ThemeRepository.kt | 6 +++ 8 files changed, 87 insertions(+), 7 deletions(-) rename app/src/main/res/menu/{stats_menu.xml => delete_icon_menu.xml} (100%) diff --git a/app/src/main/java/dev/lucasnlm/antimine/preferences/PreferencesActivity.kt b/app/src/main/java/dev/lucasnlm/antimine/preferences/PreferencesActivity.kt index 780ef19e..46632849 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/preferences/PreferencesActivity.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/preferences/PreferencesActivity.kt @@ -1,21 +1,56 @@ package dev.lucasnlm.antimine.preferences import android.os.Bundle +import android.view.Menu +import android.view.MenuItem import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceManager import dev.lucasnlm.antimine.R import dev.lucasnlm.antimine.ThematicActivity +import dev.lucasnlm.antimine.core.preferences.IPreferencesRepository +import org.koin.android.ext.android.inject class PreferencesActivity : ThematicActivity(R.layout.activity_empty) { + private val preferenceRepository: IPreferencesRepository by inject() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) PreferenceManager.setDefaultValues(this, R.xml.preferences, false) - // Load the preferences from an XML resource - supportFragmentManager - .beginTransaction() - .replace(R.id.content, PrefsFragment()) - .commitAllowingStateLoss() + placePreferenceFragment() + } + + private fun placePreferenceFragment() { + supportFragmentManager.apply { + popBackStack() + + findFragmentByTag(PrefsFragment.TAG)?.let { it -> + beginTransaction().apply { + remove(it) + commitAllowingStateLoss() + } + } + + beginTransaction().apply { + replace(R.id.content, PrefsFragment(), PrefsFragment.TAG) + commitAllowingStateLoss() + } + } + } + + override fun onCreateOptionsMenu(menu: Menu): Boolean { + menuInflater.inflate(R.menu.delete_icon_menu, menu) + return true + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + return if (item.itemId == R.id.delete) { + preferenceRepository.reset() + placePreferenceFragment() + true + } else { + super.onOptionsItemSelected(item) + } } class PrefsFragment : PreferenceFragmentCompat() { @@ -23,5 +58,9 @@ class PreferencesActivity : ThematicActivity(R.layout.activity_empty) { // Load the preferences from an XML resource addPreferencesFromResource(R.xml.preferences) } + + companion object { + val TAG = PrefsFragment::class.simpleName + } } } diff --git a/app/src/main/java/dev/lucasnlm/antimine/stats/StatsActivity.kt b/app/src/main/java/dev/lucasnlm/antimine/stats/StatsActivity.kt index c8298220..ca30adc5 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/stats/StatsActivity.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/stats/StatsActivity.kt @@ -61,7 +61,7 @@ class StatsActivity : ThematicActivity(R.layout.activity_stats) { override fun onCreateOptionsMenu(menu: Menu): Boolean { statsViewModel.singleState().let { if (it.totalGames > 0) { - menuInflater.inflate(R.menu.stats_menu, menu) + menuInflater.inflate(R.menu.delete_icon_menu, menu) } } return true diff --git a/app/src/main/java/dev/lucasnlm/antimine/theme/ThemeActivity.kt b/app/src/main/java/dev/lucasnlm/antimine/theme/ThemeActivity.kt index 1c0a029f..d8d542a7 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/theme/ThemeActivity.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/theme/ThemeActivity.kt @@ -1,6 +1,8 @@ package dev.lucasnlm.antimine.theme import android.os.Bundle +import android.view.Menu +import android.view.MenuItem import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.GridLayoutManager import dev.lucasnlm.antimine.R @@ -53,6 +55,22 @@ class ThemeActivity : ThematicActivity(R.layout.activity_theme) { } } + override fun onCreateOptionsMenu(menu: Menu): Boolean { + if (themeViewModel.singleState().current.id != 0L) { + menuInflater.inflate(R.menu.delete_icon_menu, menu) + } + return true + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + return if (item.itemId == R.id.delete) { + themeViewModel.sendEvent(ThemeEvent.ResetTheme) + true + } else { + super.onOptionsItemSelected(item) + } + } + private fun showUnlockDialog() { if (supportFragmentManager.findFragmentByTag(SupportAppDialogFragment.TAG) == null) { SupportAppDialogFragment.newInstance(true).apply { diff --git a/app/src/main/java/dev/lucasnlm/antimine/theme/viewmodel/ThemeEvent.kt b/app/src/main/java/dev/lucasnlm/antimine/theme/viewmodel/ThemeEvent.kt index b540bc22..c86a55e4 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/theme/viewmodel/ThemeEvent.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/theme/viewmodel/ThemeEvent.kt @@ -8,4 +8,6 @@ sealed class ThemeEvent { data class ChangeTheme( val newTheme: AppTheme ) : ThemeEvent() + + object ResetTheme : ThemeEvent() } diff --git a/app/src/main/java/dev/lucasnlm/antimine/theme/viewmodel/ThemeViewModel.kt b/app/src/main/java/dev/lucasnlm/antimine/theme/viewmodel/ThemeViewModel.kt index 48aa7669..406d59f7 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/theme/viewmodel/ThemeViewModel.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/theme/viewmodel/ThemeViewModel.kt @@ -40,7 +40,10 @@ class ThemeViewModel( } override suspend fun mapEventToState(event: ThemeEvent) = flow { - if (event is ThemeEvent.ChangeTheme) { + if (event is ThemeEvent.ResetTheme) { + val defaultTheme = themeRepository.reset() + emit(state.copy(current = defaultTheme)) + } else if (event is ThemeEvent.ChangeTheme) { setTheme(event.newTheme) emit(state.copy(current = event.newTheme)) } diff --git a/app/src/main/res/menu/stats_menu.xml b/app/src/main/res/menu/delete_icon_menu.xml similarity index 100% rename from app/src/main/res/menu/stats_menu.xml rename to app/src/main/res/menu/delete_icon_menu.xml diff --git a/common/src/main/java/dev/lucasnlm/antimine/core/preferences/PreferencesRepository.kt b/common/src/main/java/dev/lucasnlm/antimine/core/preferences/PreferencesRepository.kt index 7d53c8d7..3b1398fc 100644 --- a/common/src/main/java/dev/lucasnlm/antimine/core/preferences/PreferencesRepository.kt +++ b/common/src/main/java/dev/lucasnlm/antimine/core/preferences/PreferencesRepository.kt @@ -5,6 +5,8 @@ import dev.lucasnlm.antimine.common.level.models.Minefield import dev.lucasnlm.antimine.core.control.ControlStyle interface IPreferencesRepository { + fun reset() + fun customGameMode(): Minefield fun updateCustomGameMode(minefield: Minefield) @@ -52,6 +54,16 @@ class PreferencesRepository( migrateOldPreferences() } + override fun reset() { + preferencesManager.putBoolean(PREFERENCE_ASSISTANT, true) + preferencesManager.putBoolean(PREFERENCE_VIBRATION, true) + preferencesManager.putBoolean(PREFERENCE_ANIMATION, true) + preferencesManager.putBoolean(PREFERENCE_QUESTION_MARK, false) + preferencesManager.putBoolean(PREFERENCE_SOUND_EFFECTS, false) + preferencesManager.putInt(PREFERENCE_AREA_SIZE, 50) + preferencesManager.putInt(PREFERENCE_LONG_PRESS_TIMEOUT, ViewConfiguration.getLongPressTimeout()) + } + override fun customGameMode(): Minefield = Minefield( preferencesManager.getInt(PREFERENCE_CUSTOM_GAME_WIDTH, 9), preferencesManager.getInt(PREFERENCE_CUSTOM_GAME_HEIGHT, 9), diff --git a/common/src/main/java/dev/lucasnlm/antimine/core/themes/repository/ThemeRepository.kt b/common/src/main/java/dev/lucasnlm/antimine/core/themes/repository/ThemeRepository.kt index 6f6c7344..2c2e50ed 100644 --- a/common/src/main/java/dev/lucasnlm/antimine/core/themes/repository/ThemeRepository.kt +++ b/common/src/main/java/dev/lucasnlm/antimine/core/themes/repository/ThemeRepository.kt @@ -13,6 +13,7 @@ interface IThemeRepository { fun getTheme(): AppTheme fun getAllThemes(): List fun setTheme(theme: AppTheme) + fun reset(): AppTheme } class ThemeRepository( @@ -34,6 +35,11 @@ class ThemeRepository( preferenceRepository.useTheme(theme.id) } + override fun reset(): AppTheme { + preferenceRepository.useTheme(0L) + return buildSystemTheme() + } + private fun buildSystemTheme(): AppTheme { return AppTheme( id = 0L,