Add reset button

This commit is contained in:
Lucas Lima 2020-08-24 23:45:38 -03:00
parent 461c005406
commit 784db65964
No known key found for this signature in database
GPG key ID: C5EEF4C30BFBF8D7
8 changed files with 87 additions and 7 deletions

View file

@ -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
}
}
}

View file

@ -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

View file

@ -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 {

View file

@ -8,4 +8,6 @@ sealed class ThemeEvent {
data class ChangeTheme(
val newTheme: AppTheme
) : ThemeEvent()
object ResetTheme : ThemeEvent()
}

View file

@ -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))
}

View file

@ -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),

View file

@ -13,6 +13,7 @@ interface IThemeRepository {
fun getTheme(): AppTheme
fun getAllThemes(): List<AppTheme>
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,