mirror of
https://github.com/skyline-emu/skyline.git
synced 2024-12-23 19:21:55 +01:00
Remove settings from SharedPreference if they are of the wrong type
This commit is contained in:
parent
2840a126dd
commit
bb4937121f
@ -6,6 +6,7 @@
|
|||||||
package emu.skyline.utils
|
package emu.skyline.utils
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.util.Log
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import kotlin.properties.ReadWriteProperty
|
import kotlin.properties.ReadWriteProperty
|
||||||
import kotlin.reflect.KProperty
|
import kotlin.reflect.KProperty
|
||||||
@ -30,13 +31,19 @@ class SharedPreferencesDelegate<T>(context : Context, private val clazz : Class<
|
|||||||
|
|
||||||
override fun getValue(thisRef : Any, property : KProperty<*>) : T = (prefix + camelToSnakeCase(property.name)).let { keyName ->
|
override fun getValue(thisRef : Any, property : KProperty<*>) : T = (prefix + camelToSnakeCase(property.name)).let { keyName ->
|
||||||
prefs.let {
|
prefs.let {
|
||||||
@Suppress("IMPLICIT_CAST_TO_ANY")
|
try {
|
||||||
when (clazz) {
|
@Suppress("IMPLICIT_CAST_TO_ANY")
|
||||||
Float::class.java, java.lang.Float::class.java -> it.getFloat(keyName, default as Float)
|
when (clazz) {
|
||||||
Boolean::class.java, java.lang.Boolean::class.java -> it.getBoolean(keyName, default as Boolean)
|
Float::class.java, java.lang.Float::class.java -> it.getFloat(keyName, default as Float)
|
||||||
String::class.java, java.lang.String::class.java -> it.getString(keyName, default as String)
|
Boolean::class.java, java.lang.Boolean::class.java -> it.getBoolean(keyName, default as Boolean)
|
||||||
Int::class.java, java.lang.Integer::class.java -> it.getInt(keyName, default as Int)
|
String::class.java, java.lang.String::class.java -> it.getString(keyName, default as String)
|
||||||
else -> error("Unsupported type $clazz")
|
Int::class.java, java.lang.Integer::class.java -> it.getInt(keyName, default as Int)
|
||||||
|
else -> error("Unsupported type $clazz")
|
||||||
|
}
|
||||||
|
} catch (e : ClassCastException) {
|
||||||
|
Log.w(TAG, "Found preference '$keyName' of unexpected type, removing it")
|
||||||
|
it.edit().remove(keyName).apply()
|
||||||
|
default
|
||||||
}
|
}
|
||||||
} as T
|
} as T
|
||||||
}
|
}
|
||||||
@ -45,6 +52,10 @@ class SharedPreferencesDelegate<T>(context : Context, private val clazz : Class<
|
|||||||
text.forEachIndexed { index, c ->
|
text.forEachIndexed { index, c ->
|
||||||
if (index != 0 && c.isUpperCase()) append('_')
|
if (index != 0 && c.isUpperCase()) append('_')
|
||||||
append(c.lowercase())
|
append(c.lowercase())
|
||||||
}.toString()
|
}
|
||||||
|
}.toString()
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val TAG = SharedPreferencesDelegate::class.java.simpleName
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user