Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
weblate committed Mar 13, 2024
2 parents c9596e0 + 5270467 commit 621079a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.DataStore.getDefaultSharedPrefs
import com.lagradost.cloudstream3.utils.DataStore.getSharedPrefs
import com.lagradost.cloudstream3.utils.DataStore.mapper
import com.lagradost.cloudstream3.utils.DataStore.setKeyRaw
import com.lagradost.cloudstream3.utils.UIHelper.checkWrite
import com.lagradost.cloudstream3.utils.UIHelper.requestRW
import com.lagradost.cloudstream3.utils.VideoDownloadManager.setupStream
Expand Down Expand Up @@ -256,8 +255,12 @@ object BackupUtils {
map: Map<String, T>?,
isEditingAppSettings: Boolean = false
) {
map?.filter { it.key.isTransferable() }?.forEach {
setKeyRaw(it.key, it.value, isEditingAppSettings)
val editor = DataStore.editor(this, isEditingAppSettings)
map?.forEach {
if (it.key.isTransferable()) {
editor.setKeyRaw(it.key, it.value)
}
}
editor.apply()
}
}
}
42 changes: 26 additions & 16 deletions app/src/main/java/com/lagradost/cloudstream3/utils/DataStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@ class PreferenceDelegate<T : Any>(
}
}

/** When inserting many keys use this function, this is because apply for every key is very expensive on memory */
data class Editor(
val editor : SharedPreferences.Editor
) {
/** Always remember to call apply after */
fun<T> setKeyRaw(path: String, value: T) {
when (value) {
is Boolean -> editor.putBoolean(path, value)
is Int -> editor.putInt(path, value)
is String -> editor.putString(path, value)
is Float -> editor.putFloat(path, value)
is Long -> editor.putLong(path, value)
(value as? Set<String> != null) -> editor.putStringSet(path, value as Set<String>)
}
}

fun apply() {
editor.apply()
System.gc()
}
}

object DataStore {
val mapper: JsonMapper = JsonMapper.builder().addModule(kotlinModule())
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).build()
Expand All @@ -66,22 +88,10 @@ object DataStore {
return "${folder}/${path}"
}

fun <T> Context.setKeyRaw(path: String, value: T, isEditingAppSettings: Boolean = false) {
try {
val editor: SharedPreferences.Editor =
if (isEditingAppSettings) getDefaultSharedPrefs().edit() else getSharedPrefs().edit()
when (value) {
is Boolean -> editor.putBoolean(path, value)
is Int -> editor.putInt(path, value)
is String -> editor.putString(path, value)
is Float -> editor.putFloat(path, value)
is Long -> editor.putLong(path, value)
(value as? Set<String> != null) -> editor.putStringSet(path, value as Set<String>)
}
editor.apply()
} catch (e: Exception) {
logError(e)
}
fun editor(context : Context, isEditingAppSettings: Boolean = false) : Editor {
val editor: SharedPreferences.Editor =
if (isEditingAppSettings) context.getDefaultSharedPrefs().edit() else context.getSharedPrefs().edit()
return Editor(editor)
}

fun Context.getDefaultSharedPrefs(): SharedPreferences {
Expand Down

0 comments on commit 621079a

Please sign in to comment.