3204: Add an account based preference store (#3205)

* 3204: Add an account based preference store

* 3204: (related) reformat a bit, add todo

* 3204: Use the preference data store for all three account settings

* 3204: Move event handling to account settings handler

* 3204: Correct includes

* 3204: Appease linter

* 3204: Appease linter again

* 3204: Add an account based preference store

* 3204: Use the preference data store for all three account settings

* 3204: Move event handling to account settings handler

* 3204: Correct includes

* 3204: Add general "preference upgrade loop stepper"; use it for removing obsolete account settings (in shared)

* 3204: Add missing spaces

* 3204: Key is non-nullable

* 3204: Upgrade to new settings migration code

* 3204: Remove (commented) DI code
This commit is contained in:
UlrichKu 2023-02-27 14:07:28 +01:00 committed by GitHub
commit b4d10c9613
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 45 deletions

View file

@ -0,0 +1,35 @@
package com.keylesspalace.tusky.settings
import androidx.preference.PreferenceDataStore
import com.keylesspalace.tusky.appstore.EventHub
import com.keylesspalace.tusky.appstore.PreferenceChangedEvent
import com.keylesspalace.tusky.db.AccountEntity
import com.keylesspalace.tusky.db.AccountManager
class AccountPreferenceHandler(
private val account: AccountEntity,
private val accountManager: AccountManager,
private val eventHub: EventHub,
) : PreferenceDataStore() {
override fun getBoolean(key: String, defValue: Boolean): Boolean {
return when (key) {
PrefKeys.ALWAYS_SHOW_SENSITIVE_MEDIA -> account.alwaysShowSensitiveMedia
PrefKeys.ALWAYS_OPEN_SPOILER -> account.alwaysOpenSpoiler
PrefKeys.MEDIA_PREVIEW_ENABLED -> account.mediaPreviewEnabled
else -> defValue
}
}
override fun putBoolean(key: String, value: Boolean) {
when (key) {
PrefKeys.ALWAYS_SHOW_SENSITIVE_MEDIA -> account.alwaysShowSensitiveMedia = value
PrefKeys.ALWAYS_OPEN_SPOILER -> account.alwaysOpenSpoiler = value
PrefKeys.MEDIA_PREVIEW_ENABLED -> account.mediaPreviewEnabled = value
}
accountManager.saveAccount(account)
eventHub.dispatch(PreferenceChangedEvent(key))
}
}

View file

@ -41,7 +41,7 @@ enum class AppTheme(val value: String) {
*
* - Adding a new preference that does not change the interpretation of an existing preference
*/
const val SCHEMA_VERSION = 2023021501
const val SCHEMA_VERSION = 2023022701
object PrefKeys {
// Note: not all of these keys are actually used as SharedPreferences keys but we must give