@@ -12,6 +12,7 @@ import com.lagradost.cloudstream3.AcraApplication.Companion.setKeyClass
1212import com.lagradost.cloudstream3.mvvm.logError
1313import kotlin.reflect.KClass
1414import kotlin.reflect.KProperty
15+ import androidx.core.content.edit
1516
1617const val DOWNLOAD_HEADER_CACHE = " download_header_cache"
1718
@@ -28,6 +29,7 @@ class PreferenceDelegate<T : Any>(
2829 val key : String , val default : T // , private val klass: KClass<T>
2930) {
3031 private val klass: KClass <out T > = default::class
32+
3133 // simple cache to make it not get the key every time it is accessed, however this requires
3234 // that ONLY this changes the key
3335 private var cache: T ? = null
@@ -51,10 +53,10 @@ class PreferenceDelegate<T : Any>(
5153
5254/* * When inserting many keys use this function, this is because apply for every key is very expensive on memory */
5355data class Editor (
54- val editor : SharedPreferences .Editor
56+ val editor : SharedPreferences .Editor
5557) {
5658 /* * Always remember to call apply after */
57- fun <T > setKeyRaw (path : String , value : T ) {
59+ fun <T > setKeyRaw (path : String , value : T ) {
5860 @Suppress(" UNCHECKED_CAST" )
5961 if (isStringSet(value)) {
6062 editor.putStringSet(path, value as Set <String >)
@@ -69,7 +71,7 @@ data class Editor(
6971 }
7072 }
7173
72- private fun isStringSet (value : Any? ) : Boolean {
74+ private fun isStringSet (value : Any? ): Boolean {
7375 if (value is Set <* >) {
7476 return value.filterIsInstance<String >().size == value.size
7577 }
@@ -99,9 +101,10 @@ object DataStore {
99101 return " ${folder} /${path} "
100102 }
101103
102- fun editor (context : Context , isEditingAppSettings : Boolean = false) : Editor {
104+ fun editor (context : Context , isEditingAppSettings : Boolean = false): Editor {
103105 val editor: SharedPreferences .Editor =
104- if (isEditingAppSettings) context.getDefaultSharedPrefs().edit() else context.getSharedPrefs().edit()
106+ if (isEditingAppSettings) context.getDefaultSharedPrefs()
107+ .edit() else context.getSharedPrefs().edit()
105108 return Editor (editor)
106109 }
107110
@@ -130,9 +133,9 @@ object DataStore {
130133 try {
131134 val prefs = getSharedPrefs()
132135 if (prefs.contains(path)) {
133- val editor : SharedPreferences . Editor = prefs.edit()
134- editor. remove(path)
135- editor. apply ()
136+ prefs.edit {
137+ remove(path)
138+ }
136139 }
137140 } catch (e: Exception ) {
138141 logError(e)
@@ -141,17 +144,24 @@ object DataStore {
141144
142145 fun Context.removeKeys (folder : String ): Int {
143146 val keys = getKeys(" $folder /" )
144- keys.forEach { value ->
145- removeKey(value)
147+ try {
148+ getSharedPrefs().edit {
149+ keys.forEach { value ->
150+ remove(value)
151+ }
152+ }
153+ return keys.size
154+ } catch (e: Exception ) {
155+ logError(e)
156+ return 0
146157 }
147- return keys.size
148158 }
149159
150160 fun <T > Context.setKey (path : String , value : T ) {
151161 try {
152- val editor : SharedPreferences . Editor = getSharedPrefs().edit()
153- editor. putString(path, mapper.writeValueAsString(value))
154- editor. apply ()
162+ getSharedPrefs().edit {
163+ putString(path, mapper.writeValueAsString(value))
164+ }
155165 } catch (e: Exception ) {
156166 logError(e)
157167 }
0 commit comments