more cleanup

This commit is contained in:
Finnley Somdahl 2024-01-30 20:23:39 -06:00
parent 178abf0f83
commit 883c14bf0d

View file

@ -1,5 +1,6 @@
package ani.dantotsu.settings.saving package ani.dantotsu.settings.saving
import android.annotation.SuppressLint
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
@ -16,7 +17,7 @@ object PrefWrapper {
private var animeDownloadsPreferences: SharedPreferences? = null private var animeDownloadsPreferences: SharedPreferences? = null
private var protectedPreferences: SharedPreferences? = null private var protectedPreferences: SharedPreferences? = null
fun init(context: Context) { fun init(context: Context) { //must be called in Application class or will crash
generalPreferences = context.getSharedPreferences(Location.General.location, Context.MODE_PRIVATE) generalPreferences = context.getSharedPreferences(Location.General.location, Context.MODE_PRIVATE)
animePreferences = context.getSharedPreferences(Location.Anime.location, Context.MODE_PRIVATE) animePreferences = context.getSharedPreferences(Location.Anime.location, Context.MODE_PRIVATE)
mangaPreferences = context.getSharedPreferences(Location.Manga.location, Context.MODE_PRIVATE) mangaPreferences = context.getSharedPreferences(Location.Manga.location, Context.MODE_PRIVATE)
@ -29,17 +30,8 @@ object PrefWrapper {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
fun <T> setVal(prefName: PrefName, value: T) { fun <T> setVal(prefName: PrefName, value: T) {
val pref = when (prefName.data.prefLocation) { val pref = getPrefLocation(prefName)
Location.General -> generalPreferences with(pref.edit()) {
Location.Anime -> animePreferences
Location.Manga -> mangaPreferences
Location.Player -> playerPreferences
Location.Reader -> readerPreferences
Location.Irrelevant -> irrelevantPreferences
Location.AnimeDownloads -> animeDownloadsPreferences
Location.Protected -> protectedPreferences
}
with(pref!!.edit()) {
when (prefName.data.type) { when (prefName.data.type) {
Boolean::class -> putBoolean(prefName.name, value as Boolean) Boolean::class -> putBoolean(prefName.name, value as Boolean)
Int::class -> putInt(prefName.name, value as Int) Int::class -> putInt(prefName.name, value as Int)
@ -58,12 +50,12 @@ object PrefWrapper {
return try { return try {
val pref = getPrefLocation(prefName) val pref = getPrefLocation(prefName)
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) {
@ -77,32 +69,32 @@ object PrefWrapper {
val pref = getPrefLocation(prefName) val pref = getPrefLocation(prefName)
return when (prefName.data.type) { return when (prefName.data.type) {
Boolean::class -> SharedPreferenceBooleanLiveData( Boolean::class -> SharedPreferenceBooleanLiveData(
pref!!, pref,
prefName.name, prefName.name,
default as Boolean default as Boolean
) as SharedPreferenceLiveData<T> ) as SharedPreferenceLiveData<T>
Int::class -> SharedPreferenceIntLiveData( Int::class -> SharedPreferenceIntLiveData(
pref!!, pref,
prefName.name, prefName.name,
default as Int default as Int
) as SharedPreferenceLiveData<T> ) as SharedPreferenceLiveData<T>
Float::class -> SharedPreferenceFloatLiveData( Float::class -> SharedPreferenceFloatLiveData(
pref!!, pref,
prefName.name, prefName.name,
default as Float default as Float
) as SharedPreferenceLiveData<T> ) as SharedPreferenceLiveData<T>
Long::class -> SharedPreferenceLongLiveData( Long::class -> SharedPreferenceLongLiveData(
pref!!, pref,
prefName.name, prefName.name,
default as Long default as Long
) as SharedPreferenceLiveData<T> ) as SharedPreferenceLiveData<T>
String::class -> SharedPreferenceStringLiveData( String::class -> SharedPreferenceStringLiveData(
pref!!, pref,
prefName.name, prefName.name,
default as String default as String
) as SharedPreferenceLiveData<T> ) as SharedPreferenceLiveData<T>
Set::class -> SharedPreferenceStringSetLiveData( Set::class -> SharedPreferenceStringSetLiveData(
pref!!, pref,
prefName.name, prefName.name,
default as Set<String> default as Set<String>
) as SharedPreferenceLiveData<T> ) as SharedPreferenceLiveData<T>
@ -112,7 +104,7 @@ object PrefWrapper {
fun removeVal(prefName: PrefName) { fun removeVal(prefName: PrefName) {
val pref = getPrefLocation(prefName) val pref = getPrefLocation(prefName)
with(pref!!.edit()) { with(pref.edit()) {
remove(prefName.name) remove(prefName.name)
apply() apply()
} }
@ -144,7 +136,7 @@ object PrefWrapper {
fun getAnimeDownloadPreferences(): SharedPreferences = animeDownloadsPreferences!! //needs to be used externally fun getAnimeDownloadPreferences(): SharedPreferences = animeDownloadsPreferences!! //needs to be used externally
private fun getPrefLocation(prefName: PrefName): SharedPreferences? { private fun getPrefLocation(prefName: PrefName): SharedPreferences {
return when (prefName.data.prefLocation) { return when (prefName.data.prefLocation) {
Location.General -> generalPreferences Location.General -> generalPreferences
Location.Anime -> animePreferences Location.Anime -> animePreferences
@ -154,6 +146,6 @@ object PrefWrapper {
Location.Irrelevant -> irrelevantPreferences Location.Irrelevant -> irrelevantPreferences
Location.AnimeDownloads -> animeDownloadsPreferences Location.AnimeDownloads -> animeDownloadsPreferences
Location.Protected -> protectedPreferences Location.Protected -> protectedPreferences
} }!!
} }
} }