Make integer settings use IntegerListPreference

Avoids unnecessary type casting of setting values and duplication in resource files.
This commit is contained in:
lynxnb 2022-07-12 11:57:02 +02:00 committed by ◱ Mark
parent cbc896c8f8
commit 365ca66b1b
7 changed files with 31 additions and 54 deletions

View File

@ -54,7 +54,7 @@ class MainActivity : AppCompatActivity() {
private val adapter = GenericAdapter()
private val layoutType get() = LayoutType.values()[settings.layoutType.toInt()]
private val layoutType get() = LayoutType.values()[settings.layoutType]
private val missingIcon by lazy { ContextCompat.getDrawable(this, R.drawable.default_icon)!!.toBitmap(256, 256) }
@ -92,7 +92,7 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState : Bundle?) {
// Need to create new instance of settings, dependency injection happens
AppCompatDelegate.setDefaultNightMode(
when ((Settings(this).appTheme.toInt())) {
when ((Settings(this).appTheme)) {
0 -> AppCompatDelegate.MODE_NIGHT_NO
1 -> AppCompatDelegate.MODE_NIGHT_YES
2 -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
@ -106,8 +106,8 @@ class MainActivity : AppCompatActivity() {
PreferenceManager.setDefaultValues(this, R.xml.preferences, false)
adapter.apply {
setHeaderItems(listOf(HeaderRomFilterItem(formatOrder, if (settings.filter == 0) null else formatOrder[settings.filter - 1]) { romFormat ->
settings.filter = romFormat?.let { formatOrder.indexOf(romFormat) + 1 } ?: 0
setHeaderItems(listOf(HeaderRomFilterItem(formatOrder, if (settings.romFormatFilter == 0) null else formatOrder[settings.romFormatFilter - 1]) { romFormat ->
settings.romFormatFilter = romFormat?.let { formatOrder.indexOf(romFormat) + 1 } ?: 0
formatFilter = romFormat
populateAdapter()
}))

View File

@ -36,6 +36,6 @@ class SkylineApplication : Application() {
val appFilesPath = applicationContext.getPublicFilesDir().canonicalPath
File("$appFilesPath/logs/").mkdirs()
initializeLog("$appFilesPath/", getSettings().logLevel.toInt())
initializeLog("$appFilesPath/", getSettings().logLevel)
}
}

View File

@ -28,7 +28,7 @@ import emu.skyline.R as sR
* @see androidx.preference.ListPreference
*/
@SuppressLint("RestrictedApi")
class IntegerListPreference @JvmOverloads constructor(
open class IntegerListPreference @JvmOverloads constructor(
context : Context,
attrs : AttributeSet? = null,
defStyleAttr : Int = TypedArrayUtils.getAttr(

View File

@ -14,12 +14,12 @@ import androidx.preference.R
/**
* This preference is used to set the theme to Light/Dark mode
*/
class ThemePreference @JvmOverloads constructor(context : Context, attrs : AttributeSet? = null, defStyleAttr : Int = R.attr.dialogPreferenceStyle) : ListPreference(context, attrs, defStyleAttr) {
class ThemePreference @JvmOverloads constructor(context : Context, attrs : AttributeSet? = null, defStyleAttr : Int = R.attr.dialogPreferenceStyle) : IntegerListPreference(context, attrs, defStyleAttr) {
/**
* This changes [AppCompatDelegate.sDefaultNightMode] based on what the user's selection is
*/
override fun callChangeListener(newValue : Any?) : Boolean {
AppCompatDelegate.setDefaultNightMode(when ((newValue as String).toInt()) {
AppCompatDelegate.setDefaultNightMode(when (newValue as Int) {
0 -> AppCompatDelegate.MODE_NIGHT_NO
1 -> AppCompatDelegate.MODE_NIGHT_YES
2 -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM

View File

@ -10,40 +10,37 @@ import android.content.pm.ActivityInfo
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import javax.inject.Singleton
import emu.skyline.R
@Singleton
class Settings @Inject constructor(@ApplicationContext private val context : Context) {
var layoutType by sharedPreferences(context, "1")
// Emulator
var searchLocation by sharedPreferences(context, "")
var refreshRequired by sharedPreferences(context, false)
var appTheme by sharedPreferences(context, "2")
var appTheme by sharedPreferences(context, 2)
var layoutType by sharedPreferences(context, 1)
var selectAction by sharedPreferences(context, false)
var perfStats by sharedPreferences(context, false)
var operationMode by sharedPreferences(context, true)
var onScreenControl by sharedPreferences(context, true)
var onScreenControlRecenterSticks by sharedPreferences(context, true)
var logLevel by sharedPreferences(context, 3)
var logCompact by sharedPreferences(context, false)
var logLevel by sharedPreferences(context, "3")
var filter by sharedPreferences(context, 0)
var maxRefreshRate by sharedPreferences(context, false)
var aspectRatio by sharedPreferences(context, 0)
// System
var operationMode by sharedPreferences(context, true)
var usernameValue by sharedPreferences(context, context.getString(R.string.username_default))
var systemLanguage by sharedPreferences(context, 1)
// Display
var forceTripleBuffering by sharedPreferences(context, true)
var disableFrameThrottling by sharedPreferences(context, false)
var maxRefreshRate by sharedPreferences(context, false)
var aspectRatio by sharedPreferences(context, 0)
var orientation by sharedPreferences(context, ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE)
var respectDisplayCutout by sharedPreferences(context, false)
// Input
var onScreenControl by sharedPreferences(context, true)
var onScreenControlRecenterSticks by sharedPreferences(context, true)
// Other
var romFormatFilter by sharedPreferences(context, 0)
var refreshRequired by sharedPreferences(context, false)
}

View File

@ -7,33 +7,16 @@
<item>Debug</item>
<item>Verbose</item>
</string-array>
<string-array name="log_level_val">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
<string-array name="layout_type">
<item>List</item>
<item>Grid</item>
<item>Grid Compact</item>
</string-array>
<string-array name="layout_type_val">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
<string-array name="app_theme">
<item>Light</item>
<item>Dark</item>
<item>Use System Default</item>
</string-array>
<string-array name="app_theme_val">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
<string-array name="system_languages">
<item>American English</item>
<item>British English</item>

View File

@ -13,14 +13,12 @@
<emu.skyline.preference.ThemePreference
android:defaultValue="2"
android:entries="@array/app_theme"
android:entryValues="@array/app_theme_val"
app:key="app_theme"
app:title="@string/theme"
app:useSimpleSummaryProvider="true" />
<ListPreference
<emu.skyline.preference.IntegerListPreference
android:defaultValue="1"
android:entries="@array/layout_type"
android:entryValues="@array/layout_type_val"
app:key="layout_type"
app:title="@string/layout_type"
app:useSimpleSummaryProvider="true" />
@ -36,10 +34,9 @@
android:summaryOn="@string/perf_stats_desc_on"
app:key="perf_stats"
app:title="@string/perf_stats" />
<ListPreference
<emu.skyline.preference.IntegerListPreference
android:defaultValue="2"
android:entries="@array/log_level"
android:entryValues="@array/log_level_val"
app:key="log_level"
app:title="@string/log_level"
app:useSimpleSummaryProvider="true" />