1
1
package com.github.droidworksstudio.launcher.helper
2
2
3
+ import android.annotation.SuppressLint
3
4
import android.content.Context
4
5
import android.content.SharedPreferences
5
6
import android.content.res.Configuration
@@ -10,7 +11,9 @@ import com.google.gson.Gson
10
11
import com.google.gson.GsonBuilder
11
12
import com.google.gson.JsonDeserializer
12
13
import com.google.gson.reflect.TypeToken
14
+ import com.google.gson.stream.JsonReader
13
15
import dagger.hilt.android.qualifiers.ApplicationContext
16
+ import java.io.StringReader
14
17
import javax.inject.Inject
15
18
16
19
class PreferenceHelper @Inject constructor(@ApplicationContext context : Context ) {
@@ -344,6 +347,7 @@ class PreferenceHelper @Inject constructor(@ApplicationContext context: Context)
344
347
return Gson ().toJson(all)
345
348
}
346
349
350
+ @SuppressLint(" CommitPrefEdits" )
347
351
fun loadFromString (json : String ) {
348
352
val editor = prefs.edit()
349
353
// Custom deserializer to handle numbers properly
@@ -391,25 +395,34 @@ class PreferenceHelper @Inject constructor(@ApplicationContext context: Context)
391
395
map
392
396
}).create()
393
397
394
- val all: HashMap <String , Any ?> = gson.fromJson(json, object : TypeToken <HashMap <String , Any ?>>() {}.type)
395
-
396
- for ((key, value) in all) {
397
- when (value) {
398
- is String -> editor.putString(key, value)
399
- is Boolean -> editor.putBoolean(key, value)
400
- is Int -> editor.putInt(key, value)
401
- is Float -> editor.putFloat(key, value)
402
- is MutableSet <* > -> {
403
- val list = value.filterIsInstance<String >().toSet()
404
- editor.putStringSet(key, list)
405
- }
398
+ try {
399
+ // Read JSON string with lenient mode enabled
400
+ val jsonReader = JsonReader (StringReader (json))
401
+ jsonReader.isLenient = true // Enable lenient parsing for malformed JSON
402
+
403
+ // Deserialize the JSON to HashMap
404
+ val all: HashMap <String , Any ?> = gson.fromJson(jsonReader, object : TypeToken <HashMap <String , Any ?>>() {}.type)
405
+
406
+ for ((key, value) in all) {
407
+ when (value) {
408
+ is String -> editor.putString(key, value)
409
+ is Boolean -> editor.putBoolean(key, value)
410
+ is Int -> editor.putInt(key, value)
411
+ is Float -> editor.putFloat(key, value)
412
+ is MutableSet <* > -> {
413
+ val list = value.filterIsInstance<String >().toSet()
414
+ editor.putStringSet(key, list)
415
+ }
406
416
407
- else -> {
408
- Log .d(" backup error" , " $key | $value " )
417
+ else -> {
418
+ Log .d(" backup error" , " $key | $value " )
419
+ }
409
420
}
410
421
}
422
+ editor.apply ()
423
+ } catch (e: Exception ) {
424
+ e.printStackTrace()
411
425
}
412
- editor.apply ()
413
426
}
414
427
415
428
fun clear () {
0 commit comments