diff --git a/app-common/src/main/java/im/angry/openeuicc/util/PreferenceUtils.kt b/app-common/src/main/java/im/angry/openeuicc/util/PreferenceUtils.kt index ae5e3b8..807706e 100644 --- a/app-common/src/main/java/im/angry/openeuicc/util/PreferenceUtils.kt +++ b/app-common/src/main/java/im/angry/openeuicc/util/PreferenceUtils.kt @@ -36,43 +36,26 @@ object PreferenceKeys { val IGNORE_TLS_CERTIFICATE = booleanPreferencesKey("ignore_tls_certificate") } -class PreferenceRepository(context: Context) { - private val dataStore = context.dataStore - +class PreferenceRepository(private val context: Context) { // Expose flows so that we can also handle default values // ---- Profile Notifications ---- - val notificationDownloadFlow: Flow = - dataStore.data.map { it[PreferenceKeys.NOTIFICATION_DOWNLOAD] ?: true } - - val notificationDeleteFlow: Flow = - dataStore.data.map { it[PreferenceKeys.NOTIFICATION_DELETE] ?: true } - - val notificationSwitchFlow: Flow = - dataStore.data.map { it[PreferenceKeys.NOTIFICATION_SWITCH] ?: false } + val notificationDownloadFlow = bindFlow(PreferenceKeys.NOTIFICATION_DOWNLOAD, true) + val notificationDeleteFlow = bindFlow(PreferenceKeys.NOTIFICATION_DELETE, true) + val notificationSwitchFlow = bindFlow(PreferenceKeys.NOTIFICATION_SWITCH, false) // ---- Advanced ---- - val disableSafeguardFlow: Flow = - dataStore.data.map { it[PreferenceKeys.DISABLE_SAFEGUARD_REMOVABLE_ESIM] ?: false } - - val verboseLoggingFlow: Flow = - dataStore.data.map { it[PreferenceKeys.VERBOSE_LOGGING] ?: false } + val disableSafeguardFlow = bindFlow(PreferenceKeys.DISABLE_SAFEGUARD_REMOVABLE_ESIM, false) + val verboseLoggingFlow = bindFlow(PreferenceKeys.VERBOSE_LOGGING, false) // ---- Developer Options ---- - val developerOptionsEnabledFlow: Flow = - dataStore.data.map { it[PreferenceKeys.DEVELOPER_OPTIONS_ENABLED] ?: false } + val developerOptionsEnabledFlow = bindFlow(PreferenceKeys.DEVELOPER_OPTIONS_ENABLED, false) + val experimentalDownloadWizardFlow = bindFlow(PreferenceKeys.EXPERIMENTAL_DOWNLOAD_WIZARD, false) + val unfilteredProfileListFlow = bindFlow(PreferenceKeys.UNFILTERED_PROFILE_LIST, false) + val ignoreTLSCertificateFlow = bindFlow(PreferenceKeys.IGNORE_TLS_CERTIFICATE, false) - val experimentalDownloadWizardFlow: Flow = - dataStore.data.map { it[PreferenceKeys.EXPERIMENTAL_DOWNLOAD_WIZARD] ?: false } + private fun bindFlow(key: Preferences.Key, defaultValue: T): Flow = + context.dataStore.data.map { it[key] ?: defaultValue } - val unfilteredProfileListFlow: Flow = - dataStore.data.map { it[PreferenceKeys.UNFILTERED_PROFILE_LIST] ?: false } - - val ignoreTLSCertificateFlow: Flow = - dataStore.data.map { it[PreferenceKeys.IGNORE_TLS_CERTIFICATE] ?: false } - - suspend fun updatePreference(key: Preferences.Key, value: T) { - dataStore.edit { - it[key] = value - } - } + suspend fun updatePreference(key: Preferences.Key, value: T) = + context.dataStore.edit { it[key] = value } } \ No newline at end of file