Catch errors for invalid preference values

pull/1280/head
Alex Baker 5 years ago
parent b74b78e3df
commit 9fdd409fc1

@ -184,10 +184,14 @@ class Preferences @JvmOverloads constructor(
setDefaults() setDefaults()
} }
fun getStringValue(key: String?): String? = prefs.getString(key, null) fun getStringValue(keyResource: Int): String? = getStringValue(context.getString(keyResource))
fun getStringValue(keyResource: Int): String? = fun getStringValue(key: String?): String? = try {
prefs.getString(context.getString(keyResource), null) prefs.getString(key, null)
} catch (e: Exception) {
Timber.e(e)
null
}
val defaultReminders: Int val defaultReminders: Int
get() = getIntegerFromString( get() = getIntegerFromString(
@ -199,18 +203,11 @@ class Preferences @JvmOverloads constructor(
val fontSize: Int val fontSize: Int
get() = getInt(R.string.p_fontSize, 16) get() = getInt(R.string.p_fontSize, 16)
fun getIntegerFromString(keyResource: Int, defaultValue: Int): Int { fun getIntegerFromString(keyResource: Int, defaultValue: Int): Int =
return getIntegerFromString(context.getString(keyResource), defaultValue) getIntegerFromString(context.getString(keyResource), defaultValue)
}
fun getIntegerFromString(keyResource: String?, defaultValue: Int): Int { fun getIntegerFromString(keyResource: String?, defaultValue: Int): Int =
return try { getStringValue(keyResource)?.toIntOrNull() ?: defaultValue
prefs.getString(keyResource, null)?.toInt() ?: return defaultValue
} catch (e: Exception) {
Timber.e(e)
defaultValue
}
}
private fun getUri(key: Int): Uri? { private fun getUri(key: Int): Uri? {
val uri = getStringValue(key) val uri = getStringValue(key)

Loading…
Cancel
Save