Skip to content

Commit

Permalink
fix: remove some overhead when storing sets
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelonion committed Feb 7, 2024
1 parent 8741d82 commit 95409f7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ class AnilistQueries {
}


suspend fun getGenresAndTags(activity: Activity): Boolean {
suspend fun getGenresAndTags(): Boolean {
var genres: ArrayList<String>? = PrefManager.getVal<Set<String>>(PrefName.GenresList)
.toMutableList() as ArrayList<String>?
val adultTags = PrefManager.getVal<Set<String>>(PrefName.TagsListIsAdult).toMutableList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class AnilistHomeViewModel : ViewModel() {
if (!BuildConfig.FLAVOR.contains("fdroid")) {
if (PrefManager.getVal(PrefName.CheckUpdate)) AppUpdater.check(context)
}
genres.postValue(Anilist.query.getGenresAndTags(context))
genres.postValue(Anilist.query.getGenresAndTags())
}

val empty = MutableLiveData<Boolean>(null)
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/ani/dantotsu/media/anime/ExoplayerView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1244,10 +1244,10 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL
media.anime!!.selectedEpisode!!
)

val set = PrefManager.getVal<Set<Int>>(PrefName.ContinueAnime).toMutableSet()
if (set.contains(media.id)) set.remove(media.id)
set.add(media.id)
PrefManager.setVal(PrefName.ContinueAnime, set.toSet())
val list = PrefManager.getVal<Set<Int>>(PrefName.ContinuedAnime).toMutableList()
if (list.contains(media.id)) list.remove(media.id)
list.add(media.id)
PrefManager.setVal(PrefName.ContinuedAnime, list.toList())

lifecycleScope.launch(Dispatchers.IO) {
extractor?.onVideoStopped(video)
Expand Down
69 changes: 13 additions & 56 deletions app/src/main/java/ani/dantotsu/settings/saving/PrefManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ object PrefManager {
Compat.importOldPrefs(context)
}

@Suppress("UNCHECKED_CAST")
fun <T> setVal(prefName: PrefName, value: T?) {
val pref = getPrefLocation(prefName.data.prefLocation)
with(pref.edit()) {
Expand All @@ -46,7 +47,7 @@ object PrefManager {
is Float -> putFloat(prefName.name, value)
is Long -> putLong(prefName.name, value)
is String -> putString(prefName.name, value)
is Set<*> -> convertAndPutStringSet(prefName.name, value)
is Set<*> -> putStringSet(prefName.name, value as Set<String>)
null -> remove(prefName.name)
else -> serializeClass(prefName.name, value, prefName.data.prefLocation)
}
Expand All @@ -64,10 +65,7 @@ object PrefManager {
Float::class -> pref.getFloat(prefName.name, default as Float) as T
Long::class -> pref.getLong(prefName.name, default as Long) as T
String::class -> pref.getString(prefName.name, default as String?) as T
Set::class -> convertFromStringSet(
pref.getStringSet(prefName.name, null),
default
) as T
Set::class -> pref.getStringSet(prefName.name, default as Set<String>) as T

List::class -> deserializeClass(
prefName.name,
Expand Down Expand Up @@ -100,9 +98,9 @@ object PrefManager {
prefName.data.default as String?
) as T

Set::class -> convertFromStringSet(
pref.getStringSet(prefName.name, null),
prefName.data.default
Set::class -> pref.getStringSet(
prefName.name,
prefName.data.default as Set<String>
) as T

List::class -> deserializeClass(
Expand Down Expand Up @@ -131,10 +129,7 @@ object PrefManager {
Float::class -> pref.getFloat(prefName.name, default as Float) as T?
Long::class -> pref.getLong(prefName.name, default as Long) as T?
String::class -> pref.getString(prefName.name, default as String?) as T?
Set::class -> convertFromStringSet(
pref.getStringSet(prefName.name, null),
default
) as T?
Set::class -> pref.getStringSet(prefName.name, default as Set<String>) as T?

else -> deserializeClass(prefName.name, default, prefName.data.prefLocation)
}
Expand All @@ -152,11 +147,7 @@ object PrefManager {
is Float -> irrelevantPreferences!!.getFloat(key, default) as T
is Long -> irrelevantPreferences!!.getLong(key, default) as T
is String -> irrelevantPreferences!!.getString(key, default) as T
is Set<*> -> convertFromStringSet(
irrelevantPreferences!!.getStringSet(key, null),
default
) as T

is Set<*> -> irrelevantPreferences!!.getStringSet(key, default as Set<String>) as T
else -> throw IllegalArgumentException("Type not supported")
}
} catch (e: Exception) {
Expand Down Expand Up @@ -193,9 +184,9 @@ object PrefManager {
default as? String
) as T?

clazz.isAssignableFrom(Set::class.java) -> convertFromStringSet(
irrelevantPreferences!!.getStringSet(key, null),
default
clazz.isAssignableFrom(Set::class.java) -> irrelevantPreferences!!.getStringSet(
key,
default as? Set<String> ?: setOf()
) as T?

else -> deserializeClass(key, default, Location.Irrelevant)
Expand All @@ -214,6 +205,7 @@ object PrefManager {
}
}

@Suppress("UNCHECKED_CAST")
fun <T> setCustomVal(key: String, value: T?) {
//for custom force irrelevant
with(irrelevantPreferences!!.edit()) {
Expand All @@ -223,7 +215,7 @@ object PrefManager {
is Float -> putFloat(key, value as Float)
is Long -> putLong(key, value as Long)
is String -> putString(key, value as String)
is Set<*> -> convertAndPutStringSet(key, value)
is Set<*> -> putStringSet(key, value as Set<String>)
null -> remove(key)
else -> serializeClass(key, value, Location.Irrelevant)
}
Expand Down Expand Up @@ -370,41 +362,6 @@ object PrefManager {
}!!
}

private fun <T> convertFromStringSet(stringSet: Set<String>?, default: T): Set<*> {
if (stringSet.isNullOrEmpty()) return default as Set<*>

return try {
val typeIdentifier = stringSet.first()
val convertedSet = stringSet.drop(1) // Remove the type identifier
when (typeIdentifier) {
"Int" -> convertedSet.mapNotNull { it.toIntOrNull() }.toSet()
"Boolean" -> convertedSet.mapNotNull { it.toBooleanStrictOrNull() }.toSet()
"Float" -> convertedSet.mapNotNull { it.toFloatOrNull() }.toSet()
"Long" -> convertedSet.mapNotNull { it.toLongOrNull() }.toSet()
"String" -> convertedSet.toSet()
else -> stringSet
}
} catch (e: Exception) {
snackString("Error converting preference: ${e.message}")
default as Set<*>
}
}

private fun SharedPreferences.Editor.convertAndPutStringSet(key: String, value: Set<*>) {
val typeIdentifier = when (value.firstOrNull()) {
is Int -> "Int"
is Boolean -> "Boolean"
is Float -> "Float"
is Long -> "Long"
is String -> "String"
null -> return
else -> throw IllegalArgumentException("Type not supported")
}
val stringSet = setOf(typeIdentifier) + value.map { it.toString() }
putStringSet(key, stringSet)
}


private fun <T> serializeClass(key: String, value: T, location: Location) {
val pref = getPrefLocation(location)
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ enum class PrefName(val data: Pref) { //TODO: Split this into multiple files
Cast(Pref(Location.Player, Boolean::class, true)),
Pip(Pref(Location.Player, Boolean::class, true)),
RotationPlayer(Pref(Location.Player, Boolean::class, true)),
ContinueAnime(Pref(Location.Player, Set::class, setOf<Int>())),
ContinuedAnime(Pref(Location.Player, List::class, listOf<String>())),

//Reader
ShowSource(Pref(Location.Reader, Boolean::class, true)),
Expand Down

0 comments on commit 95409f7

Please sign in to comment.