fix nullable string
This commit is contained in:
parent
c242fedfee
commit
178abf0f83
1 changed files with 20 additions and 32 deletions
|
@ -3,6 +3,7 @@ package ani.dantotsu.settings.saving
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import ani.dantotsu.settings.saving.internal.Location
|
import ani.dantotsu.settings.saving.internal.Location
|
||||||
|
import ani.dantotsu.snackString
|
||||||
|
|
||||||
object PrefWrapper {
|
object PrefWrapper {
|
||||||
|
|
||||||
|
@ -44,7 +45,7 @@ object PrefWrapper {
|
||||||
Int::class -> putInt(prefName.name, value as Int)
|
Int::class -> putInt(prefName.name, value as Int)
|
||||||
Float::class -> putFloat(prefName.name, value as Float)
|
Float::class -> putFloat(prefName.name, value as Float)
|
||||||
Long::class -> putLong(prefName.name, value as Long)
|
Long::class -> putLong(prefName.name, value as Long)
|
||||||
String::class -> putString(prefName.name, value as String)
|
String::class -> putString(prefName.name, value as String?)
|
||||||
Set::class -> putStringSet(prefName.name, value as Set<String>)
|
Set::class -> putStringSet(prefName.name, value as Set<String>)
|
||||||
else -> throw IllegalArgumentException("Type not supported")
|
else -> throw IllegalArgumentException("Type not supported")
|
||||||
}
|
}
|
||||||
|
@ -55,42 +56,25 @@ object PrefWrapper {
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
fun <T> getVal(prefName: PrefName, default: T) : T {
|
fun <T> getVal(prefName: PrefName, default: T) : T {
|
||||||
return try {
|
return try {
|
||||||
val pref = when (prefName.data.prefLocation) {
|
val pref = getPrefLocation(prefName)
|
||||||
Location.General -> generalPreferences
|
|
||||||
Location.Anime -> animePreferences
|
|
||||||
Location.Manga -> mangaPreferences
|
|
||||||
Location.Player -> playerPreferences
|
|
||||||
Location.Reader -> readerPreferences
|
|
||||||
Location.Irrelevant -> irrelevantPreferences
|
|
||||||
Location.AnimeDownloads -> animeDownloadsPreferences
|
|
||||||
Location.Protected -> protectedPreferences
|
|
||||||
}
|
|
||||||
when (prefName.data.type) {
|
when (prefName.data.type) {
|
||||||
Boolean::class -> pref!!.getBoolean(prefName.name, default as Boolean) as T
|
Boolean::class -> pref!!.getBoolean(prefName.name, default as Boolean) as T
|
||||||
Int::class -> pref!!.getInt(prefName.name, default as Int) as T
|
Int::class -> pref!!.getInt(prefName.name, default as Int) as T
|
||||||
Float::class -> pref!!.getFloat(prefName.name, default as Float) as T
|
Float::class -> pref!!.getFloat(prefName.name, default as Float) as T
|
||||||
Long::class -> pref!!.getLong(prefName.name, default as Long) as T
|
Long::class -> pref!!.getLong(prefName.name, default as Long) as T
|
||||||
String::class -> pref!!.getString(prefName.name, default as String) as T
|
String::class -> pref!!.getString(prefName.name, default as String?) as T
|
||||||
Set::class -> pref!!.getStringSet(prefName.name, default as Set<String>) as T
|
Set::class -> pref!!.getStringSet(prefName.name, default as Set<String>) as T
|
||||||
else -> throw IllegalArgumentException("Type not supported")
|
else -> throw IllegalArgumentException("Type not supported")
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
snackString("Error getting preference: ${e.message}")
|
||||||
default
|
default
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
fun <T> getLiveVal(prefName: PrefName, default: T) : SharedPreferenceLiveData<T> {
|
fun <T> getLiveVal(prefName: PrefName, default: T) : SharedPreferenceLiveData<T> {
|
||||||
val pref = when (prefName.data.prefLocation) {
|
val pref = getPrefLocation(prefName)
|
||||||
Location.General -> generalPreferences
|
|
||||||
Location.Anime -> animePreferences
|
|
||||||
Location.Manga -> mangaPreferences
|
|
||||||
Location.Player -> playerPreferences
|
|
||||||
Location.Reader -> readerPreferences
|
|
||||||
Location.Irrelevant -> irrelevantPreferences
|
|
||||||
Location.AnimeDownloads -> animeDownloadsPreferences
|
|
||||||
Location.Protected -> protectedPreferences
|
|
||||||
}
|
|
||||||
return when (prefName.data.type) {
|
return when (prefName.data.type) {
|
||||||
Boolean::class -> SharedPreferenceBooleanLiveData(
|
Boolean::class -> SharedPreferenceBooleanLiveData(
|
||||||
pref!!,
|
pref!!,
|
||||||
|
@ -127,16 +111,7 @@ object PrefWrapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun removeVal(prefName: PrefName) {
|
fun removeVal(prefName: PrefName) {
|
||||||
val pref = when (prefName.data.prefLocation) {
|
val pref = getPrefLocation(prefName)
|
||||||
Location.General -> generalPreferences
|
|
||||||
Location.Anime -> animePreferences
|
|
||||||
Location.Manga -> mangaPreferences
|
|
||||||
Location.Player -> playerPreferences
|
|
||||||
Location.Reader -> readerPreferences
|
|
||||||
Location.Irrelevant -> irrelevantPreferences
|
|
||||||
Location.AnimeDownloads -> animeDownloadsPreferences
|
|
||||||
Location.Protected -> protectedPreferences
|
|
||||||
}
|
|
||||||
with(pref!!.edit()) {
|
with(pref!!.edit()) {
|
||||||
remove(prefName.name)
|
remove(prefName.name)
|
||||||
apply()
|
apply()
|
||||||
|
@ -168,4 +143,17 @@ object PrefWrapper {
|
||||||
?: throw ClassCastException("Cannot cast to SharedPreferenceLiveData<Set<String>>")
|
?: throw ClassCastException("Cannot cast to SharedPreferenceLiveData<Set<String>>")
|
||||||
|
|
||||||
fun getAnimeDownloadPreferences(): SharedPreferences = animeDownloadsPreferences!! //needs to be used externally
|
fun getAnimeDownloadPreferences(): SharedPreferences = animeDownloadsPreferences!! //needs to be used externally
|
||||||
|
|
||||||
|
private fun getPrefLocation(prefName: PrefName): SharedPreferences? {
|
||||||
|
return when (prefName.data.prefLocation) {
|
||||||
|
Location.General -> generalPreferences
|
||||||
|
Location.Anime -> animePreferences
|
||||||
|
Location.Manga -> mangaPreferences
|
||||||
|
Location.Player -> playerPreferences
|
||||||
|
Location.Reader -> readerPreferences
|
||||||
|
Location.Irrelevant -> irrelevantPreferences
|
||||||
|
Location.AnimeDownloads -> animeDownloadsPreferences
|
||||||
|
Location.Protected -> protectedPreferences
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue