2023-02-27 14:07:28 +01:00
|
|
|
package com.keylesspalace.tusky.settings
|
|
|
|
|
|
|
|
import androidx.preference.PreferenceDataStore
|
2023-06-11 15:34:58 +02:00
|
|
|
import com.keylesspalace.tusky.appstore.EventHub
|
2023-02-27 14:07:28 +01:00
|
|
|
import com.keylesspalace.tusky.appstore.PreferenceChangedEvent
|
|
|
|
import com.keylesspalace.tusky.db.AccountEntity
|
|
|
|
import com.keylesspalace.tusky.db.AccountManager
|
2023-06-11 15:34:58 +02:00
|
|
|
import com.keylesspalace.tusky.di.ApplicationScope
|
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import javax.inject.Inject
|
2023-02-27 14:07:28 +01:00
|
|
|
|
2023-06-11 15:34:58 +02:00
|
|
|
class AccountPreferenceDataStore @Inject constructor(
|
2023-02-27 14:07:28 +01:00
|
|
|
private val accountManager: AccountManager,
|
2023-06-11 15:34:58 +02:00
|
|
|
private val eventHub: EventHub,
|
|
|
|
@ApplicationScope private val externalScope: CoroutineScope
|
2023-02-27 14:07:28 +01:00
|
|
|
) : PreferenceDataStore() {
|
2023-06-11 15:34:58 +02:00
|
|
|
private val account: AccountEntity = accountManager.activeAccount!!
|
2023-02-27 14:07:28 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-06-11 15:34:58 +02:00
|
|
|
externalScope.launch {
|
|
|
|
eventHub.dispatch(PreferenceChangedEvent(key))
|
|
|
|
}
|
2023-02-27 14:07:28 +01:00
|
|
|
}
|
|
|
|
}
|