Browse Source

refactor: preference repository (#74)

reduce template code

Reviewed-on: https://gitea.angry.im/PeterCxy/OpenEUICC/pulls/74
Co-authored-by: septs <github@septs.pw>
Co-committed-by: septs <github@septs.pw>
septs 1 year ago
parent
commit
50c77ea467
1 changed files with 14 additions and 31 deletions
  1. 14 31
      app-common/src/main/java/im/angry/openeuicc/util/PreferenceUtils.kt

+ 14 - 31
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")
     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
     // Expose flows so that we can also handle default values
     // ---- Profile Notifications ----
     // ---- Profile Notifications ----
-    val notificationDownloadFlow: Flow<Boolean> =
-        dataStore.data.map { it[PreferenceKeys.NOTIFICATION_DOWNLOAD] ?: true }
-
-    val notificationDeleteFlow: Flow<Boolean> =
-        dataStore.data.map { it[PreferenceKeys.NOTIFICATION_DELETE] ?: true }
-
-    val notificationSwitchFlow: Flow<Boolean> =
-        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 ----
     // ---- Advanced ----
-    val disableSafeguardFlow: Flow<Boolean> =
-        dataStore.data.map { it[PreferenceKeys.DISABLE_SAFEGUARD_REMOVABLE_ESIM] ?: false }
-
-    val verboseLoggingFlow: Flow<Boolean> =
-        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 ----
     // ---- Developer Options ----
-    val developerOptionsEnabledFlow: Flow<Boolean> =
-        dataStore.data.map { it[PreferenceKeys.DEVELOPER_OPTIONS_ENABLED] ?: false }
-
-    val experimentalDownloadWizardFlow: Flow<Boolean> =
-        dataStore.data.map { it[PreferenceKeys.EXPERIMENTAL_DOWNLOAD_WIZARD] ?: false }
-
-    val unfilteredProfileListFlow: Flow<Boolean> =
-        dataStore.data.map { it[PreferenceKeys.UNFILTERED_PROFILE_LIST] ?: 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 ignoreTLSCertificateFlow: Flow<Boolean> =
-        dataStore.data.map { it[PreferenceKeys.IGNORE_TLS_CERTIFICATE] ?: false }
+    private fun <T> bindFlow(key: Preferences.Key<T>, defaultValue: T): Flow<T> =
+        context.dataStore.data.map { it[key] ?: defaultValue }
 
 
-    suspend fun <T> updatePreference(key: Preferences.Key<T>, value: T) {
-        dataStore.edit {
-            it[key] = value
-        }
-    }
+    suspend fun <T> updatePreference(key: Preferences.Key<T>, value: T) =
+        context.dataStore.edit { it[key] = value }
 }
 }