Use 'commit' method to write shared prefs (#154)

This commit is contained in:
BinTianqi
2025-08-18 13:32:12 +08:00
parent c7396eccf7
commit b3d746083f
3 changed files with 5 additions and 5 deletions

View File

@@ -60,7 +60,7 @@ class ManageSpaceActivity: FragmentActivity() {
dataDir.resolve("shared_prefs").deleteRecursively() dataDir.resolve("shared_prefs").deleteRecursively()
} else { } else {
val sharedPref = applicationContext.getSharedPreferences("data", MODE_PRIVATE) val sharedPref = applicationContext.getSharedPreferences("data", MODE_PRIVATE)
sharedPref.edit { clear() } sharedPref.edit(true) { clear() }
} }
this.showOperationResultToast(true) this.showOperationResultToast(true)
finish() finish()

View File

@@ -347,7 +347,7 @@ fun NotificationsScreen(onNavigateUp: () -> Unit) = MyScaffold(R.string.notifica
NotificationUtils.ID.SYSTEM_UPDATE_PENDING to R.string.system_update_pending NotificationUtils.ID.SYSTEM_UPDATE_PENDING to R.string.system_update_pending
) )
map.forEach { (k, v) -> map.forEach { (k, v) ->
SwitchItem(v, getState = { sp.getBoolean("n_$k", true) }, onCheckedChange = { sp.edit { putBoolean("n_$k", it) } }) SwitchItem(v, getState = { sp.getBoolean("n_$k", true) }, onCheckedChange = { sp.edit(true) { putBoolean("n_$k", it) } })
} }
} }

View File

@@ -31,19 +31,19 @@ private class BooleanSharedPref(val key: String, val defValue: Boolean = false):
override fun getValue(thisRef: SharedPrefs, property: KProperty<*>): Boolean = override fun getValue(thisRef: SharedPrefs, property: KProperty<*>): Boolean =
thisRef.sharedPrefs.getBoolean(key, defValue) thisRef.sharedPrefs.getBoolean(key, defValue)
override fun setValue(thisRef: SharedPrefs, property: KProperty<*>, value: Boolean) = override fun setValue(thisRef: SharedPrefs, property: KProperty<*>, value: Boolean) =
thisRef.sharedPrefs.edit { putBoolean(key, value) } thisRef.sharedPrefs.edit(true) { putBoolean(key, value) }
} }
private class StringSharedPref(val key: String): ReadWriteProperty<SharedPrefs, String?> { private class StringSharedPref(val key: String): ReadWriteProperty<SharedPrefs, String?> {
override fun getValue(thisRef: SharedPrefs, property: KProperty<*>): String? = override fun getValue(thisRef: SharedPrefs, property: KProperty<*>): String? =
thisRef.sharedPrefs.getString(key, null) thisRef.sharedPrefs.getString(key, null)
override fun setValue(thisRef: SharedPrefs, property: KProperty<*>, value: String?) = override fun setValue(thisRef: SharedPrefs, property: KProperty<*>, value: String?) =
thisRef.sharedPrefs.edit { putString(key, value) } thisRef.sharedPrefs.edit(true) { putString(key, value) }
} }
private class IntSharedPref(val key: String, val defValue: Int = 0): ReadWriteProperty<SharedPrefs, Int> { private class IntSharedPref(val key: String, val defValue: Int = 0): ReadWriteProperty<SharedPrefs, Int> {
override fun getValue(thisRef: SharedPrefs, property: KProperty<*>): Int = override fun getValue(thisRef: SharedPrefs, property: KProperty<*>): Int =
thisRef.sharedPrefs.getInt(key, defValue) thisRef.sharedPrefs.getInt(key, defValue)
override fun setValue(thisRef: SharedPrefs, property: KProperty<*>, value: Int) = override fun setValue(thisRef: SharedPrefs, property: KProperty<*>, value: Int) =
thisRef.sharedPrefs.edit { putInt(key, value) } thisRef.sharedPrefs.edit(true) { putInt(key, value) }
} }