mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
Fixed OOM when using restore data
This commit is contained in:
parent
adc653943b
commit
527046766a
2 changed files with 33 additions and 20 deletions
|
@ -32,7 +32,6 @@ import com.lagradost.cloudstream3.utils.Coroutines.main
|
||||||
import com.lagradost.cloudstream3.utils.DataStore.getDefaultSharedPrefs
|
import com.lagradost.cloudstream3.utils.DataStore.getDefaultSharedPrefs
|
||||||
import com.lagradost.cloudstream3.utils.DataStore.getSharedPrefs
|
import com.lagradost.cloudstream3.utils.DataStore.getSharedPrefs
|
||||||
import com.lagradost.cloudstream3.utils.DataStore.mapper
|
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.checkWrite
|
||||||
import com.lagradost.cloudstream3.utils.UIHelper.requestRW
|
import com.lagradost.cloudstream3.utils.UIHelper.requestRW
|
||||||
import com.lagradost.cloudstream3.utils.VideoDownloadManager.setupStream
|
import com.lagradost.cloudstream3.utils.VideoDownloadManager.setupStream
|
||||||
|
@ -256,8 +255,12 @@ object BackupUtils {
|
||||||
map: Map<String, T>?,
|
map: Map<String, T>?,
|
||||||
isEditingAppSettings: Boolean = false
|
isEditingAppSettings: Boolean = false
|
||||||
) {
|
) {
|
||||||
map?.filter { it.key.isTransferable() }?.forEach {
|
val editor = DataStore.editor(this, isEditingAppSettings)
|
||||||
setKeyRaw(it.key, it.value, isEditingAppSettings)
|
map?.forEach {
|
||||||
|
if (it.key.isTransferable()) {
|
||||||
|
editor.setKeyRaw(it.key, it.value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
editor.apply()
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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 {
|
object DataStore {
|
||||||
val mapper: JsonMapper = JsonMapper.builder().addModule(kotlinModule())
|
val mapper: JsonMapper = JsonMapper.builder().addModule(kotlinModule())
|
||||||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).build()
|
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).build()
|
||||||
|
@ -66,22 +88,10 @@ object DataStore {
|
||||||
return "${folder}/${path}"
|
return "${folder}/${path}"
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> Context.setKeyRaw(path: String, value: T, isEditingAppSettings: Boolean = false) {
|
fun editor(context : Context, isEditingAppSettings: Boolean = false) : Editor {
|
||||||
try {
|
|
||||||
val editor: SharedPreferences.Editor =
|
val editor: SharedPreferences.Editor =
|
||||||
if (isEditingAppSettings) getDefaultSharedPrefs().edit() else getSharedPrefs().edit()
|
if (isEditingAppSettings) context.getDefaultSharedPrefs().edit() else context.getSharedPrefs().edit()
|
||||||
when (value) {
|
return Editor(editor)
|
||||||
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 Context.getDefaultSharedPrefs(): SharedPreferences {
|
fun Context.getDefaultSharedPrefs(): SharedPreferences {
|
||||||
|
|
Loading…
Reference in a new issue