From cccf045f8807edf7c3286196587191e157fb0e4e Mon Sep 17 00:00:00 2001 From: septs Date: Sat, 30 Nov 2024 11:05:54 +0800 Subject: [PATCH 1/4] refactor: preference repository --- .../angry/openeuicc/util/PreferenceUtils.kt | 41 +++++++------------ 1 file changed, 15 insertions(+), 26 deletions(-) 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..cd72a19 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,32 @@ 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 notificationDownloadFlow = get(PreferenceKeys.NOTIFICATION_DOWNLOAD, true) - val notificationDeleteFlow: Flow = - dataStore.data.map { it[PreferenceKeys.NOTIFICATION_DELETE] ?: true } + val notificationDeleteFlow = get(PreferenceKeys.NOTIFICATION_DELETE, true) - val notificationSwitchFlow: Flow = - dataStore.data.map { it[PreferenceKeys.NOTIFICATION_SWITCH] ?: false } + val notificationSwitchFlow = get(PreferenceKeys.NOTIFICATION_SWITCH, false) // ---- Advanced ---- - val disableSafeguardFlow: Flow = - dataStore.data.map { it[PreferenceKeys.DISABLE_SAFEGUARD_REMOVABLE_ESIM] ?: false } + val disableSafeguardFlow = get(PreferenceKeys.DISABLE_SAFEGUARD_REMOVABLE_ESIM, false) - val verboseLoggingFlow: Flow = - dataStore.data.map { it[PreferenceKeys.VERBOSE_LOGGING] ?: false } + val verboseLoggingFlow = get(PreferenceKeys.VERBOSE_LOGGING, false) // ---- Developer Options ---- - val developerOptionsEnabledFlow: Flow = - dataStore.data.map { it[PreferenceKeys.DEVELOPER_OPTIONS_ENABLED] ?: false } + val developerOptionsEnabledFlow = get(PreferenceKeys.DEVELOPER_OPTIONS_ENABLED, false) - val experimentalDownloadWizardFlow: Flow = - dataStore.data.map { it[PreferenceKeys.EXPERIMENTAL_DOWNLOAD_WIZARD] ?: false } + val experimentalDownloadWizardFlow = get(PreferenceKeys.EXPERIMENTAL_DOWNLOAD_WIZARD, false) - val unfilteredProfileListFlow: Flow = - dataStore.data.map { it[PreferenceKeys.UNFILTERED_PROFILE_LIST] ?: false } + val unfilteredProfileListFlow = get(PreferenceKeys.UNFILTERED_PROFILE_LIST, false) - val ignoreTLSCertificateFlow: Flow = - dataStore.data.map { it[PreferenceKeys.IGNORE_TLS_CERTIFICATE] ?: false } + val ignoreTLSCertificateFlow = get(PreferenceKeys.IGNORE_TLS_CERTIFICATE, false) - suspend fun updatePreference(key: Preferences.Key, value: T) { - dataStore.edit { - it[key] = value - } - } + private fun get(key: Preferences.Key, defaultValue: T): Flow = + context.dataStore.data.map { it[key] ?: defaultValue } + + suspend fun updatePreference(key: Preferences.Key, value: T) = + context.dataStore.edit { it[key] = value } } \ No newline at end of file -- 2.45.3 From 69162d471224caebb9af32f76ea99efd02e710df Mon Sep 17 00:00:00 2001 From: septs Date: Sat, 30 Nov 2024 12:33:49 +0800 Subject: [PATCH 2/4] chore: compact code --- .../main/java/im/angry/openeuicc/util/PreferenceUtils.kt | 6 ------ 1 file changed, 6 deletions(-) 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 cd72a19..ff469ec 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 @@ -40,23 +40,17 @@ class PreferenceRepository(private val context: Context) { // Expose flows so that we can also handle default values // ---- Profile Notifications ---- val notificationDownloadFlow = get(PreferenceKeys.NOTIFICATION_DOWNLOAD, true) - val notificationDeleteFlow = get(PreferenceKeys.NOTIFICATION_DELETE, true) - val notificationSwitchFlow = get(PreferenceKeys.NOTIFICATION_SWITCH, false) // ---- Advanced ---- val disableSafeguardFlow = get(PreferenceKeys.DISABLE_SAFEGUARD_REMOVABLE_ESIM, false) - val verboseLoggingFlow = get(PreferenceKeys.VERBOSE_LOGGING, false) // ---- Developer Options ---- val developerOptionsEnabledFlow = get(PreferenceKeys.DEVELOPER_OPTIONS_ENABLED, false) - val experimentalDownloadWizardFlow = get(PreferenceKeys.EXPERIMENTAL_DOWNLOAD_WIZARD, false) - val unfilteredProfileListFlow = get(PreferenceKeys.UNFILTERED_PROFILE_LIST, false) - val ignoreTLSCertificateFlow = get(PreferenceKeys.IGNORE_TLS_CERTIFICATE, false) private fun get(key: Preferences.Key, defaultValue: T): Flow = -- 2.45.3 From 37fdb484c53cb9288217c69b3622c2172d0a8a7b Mon Sep 17 00:00:00 2001 From: septs Date: Sat, 30 Nov 2024 13:41:35 +0800 Subject: [PATCH 3/4] chore: rename method --- .../angry/openeuicc/util/PreferenceUtils.kt | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) 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 ff469ec..3ce470e 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 @@ -39,21 +39,21 @@ object PreferenceKeys { class PreferenceRepository(private val context: Context) { // Expose flows so that we can also handle default values // ---- Profile Notifications ---- - val notificationDownloadFlow = get(PreferenceKeys.NOTIFICATION_DOWNLOAD, true) - val notificationDeleteFlow = get(PreferenceKeys.NOTIFICATION_DELETE, true) - val notificationSwitchFlow = get(PreferenceKeys.NOTIFICATION_SWITCH, false) + val notificationDownloadFlow = bind(PreferenceKeys.NOTIFICATION_DOWNLOAD, true) + val notificationDeleteFlow = bind(PreferenceKeys.NOTIFICATION_DELETE, true) + val notificationSwitchFlow = bind(PreferenceKeys.NOTIFICATION_SWITCH, false) // ---- Advanced ---- - val disableSafeguardFlow = get(PreferenceKeys.DISABLE_SAFEGUARD_REMOVABLE_ESIM, false) - val verboseLoggingFlow = get(PreferenceKeys.VERBOSE_LOGGING, false) + val disableSafeguardFlow = bind(PreferenceKeys.DISABLE_SAFEGUARD_REMOVABLE_ESIM, false) + val verboseLoggingFlow = bind(PreferenceKeys.VERBOSE_LOGGING, false) // ---- Developer Options ---- - val developerOptionsEnabledFlow = get(PreferenceKeys.DEVELOPER_OPTIONS_ENABLED, false) - val experimentalDownloadWizardFlow = get(PreferenceKeys.EXPERIMENTAL_DOWNLOAD_WIZARD, false) - val unfilteredProfileListFlow = get(PreferenceKeys.UNFILTERED_PROFILE_LIST, false) - val ignoreTLSCertificateFlow = get(PreferenceKeys.IGNORE_TLS_CERTIFICATE, false) + val developerOptionsEnabledFlow = bind(PreferenceKeys.DEVELOPER_OPTIONS_ENABLED, false) + val experimentalDownloadWizardFlow = bind(PreferenceKeys.EXPERIMENTAL_DOWNLOAD_WIZARD, false) + val unfilteredProfileListFlow = bind(PreferenceKeys.UNFILTERED_PROFILE_LIST, false) + val ignoreTLSCertificateFlow = bind(PreferenceKeys.IGNORE_TLS_CERTIFICATE, false) - private fun get(key: Preferences.Key, defaultValue: T): Flow = + private fun bind(key: Preferences.Key, defaultValue: T): Flow = context.dataStore.data.map { it[key] ?: defaultValue } suspend fun updatePreference(key: Preferences.Key, value: T) = -- 2.45.3 From 18edef0247a2a47ebeaa914cb04bce0f934b58a2 Mon Sep 17 00:00:00 2001 From: septs Date: Sat, 30 Nov 2024 23:03:23 +0800 Subject: [PATCH 4/4] chore: rename method --- .../angry/openeuicc/util/PreferenceUtils.kt | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) 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 3ce470e..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 @@ -39,21 +39,21 @@ object PreferenceKeys { class PreferenceRepository(private val context: Context) { // Expose flows so that we can also handle default values // ---- Profile Notifications ---- - val notificationDownloadFlow = bind(PreferenceKeys.NOTIFICATION_DOWNLOAD, true) - val notificationDeleteFlow = bind(PreferenceKeys.NOTIFICATION_DELETE, true) - val notificationSwitchFlow = bind(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 = bind(PreferenceKeys.DISABLE_SAFEGUARD_REMOVABLE_ESIM, false) - val verboseLoggingFlow = bind(PreferenceKeys.VERBOSE_LOGGING, false) + val disableSafeguardFlow = bindFlow(PreferenceKeys.DISABLE_SAFEGUARD_REMOVABLE_ESIM, false) + val verboseLoggingFlow = bindFlow(PreferenceKeys.VERBOSE_LOGGING, false) // ---- Developer Options ---- - val developerOptionsEnabledFlow = bind(PreferenceKeys.DEVELOPER_OPTIONS_ENABLED, false) - val experimentalDownloadWizardFlow = bind(PreferenceKeys.EXPERIMENTAL_DOWNLOAD_WIZARD, false) - val unfilteredProfileListFlow = bind(PreferenceKeys.UNFILTERED_PROFILE_LIST, false) - val ignoreTLSCertificateFlow = bind(PreferenceKeys.IGNORE_TLS_CERTIFICATE, 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) - private fun bind(key: Preferences.Key, defaultValue: T): Flow = + private fun bindFlow(key: Preferences.Key, defaultValue: T): Flow = context.dataStore.data.map { it[key] ?: defaultValue } suspend fun updatePreference(key: Preferences.Key, value: T) = -- 2.45.3