PreferenceUtils.kt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package im.angry.openeuicc.util
  2. import android.content.Context
  3. import androidx.datastore.core.DataStore
  4. import androidx.datastore.preferences.core.Preferences
  5. import androidx.datastore.preferences.core.booleanPreferencesKey
  6. import androidx.datastore.preferences.core.edit
  7. import androidx.datastore.preferences.preferencesDataStore
  8. import androidx.fragment.app.Fragment
  9. import im.angry.openeuicc.OpenEuiccApplication
  10. import kotlinx.coroutines.flow.Flow
  11. import kotlinx.coroutines.flow.map
  12. private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "prefs")
  13. val Context.preferenceRepository: PreferenceRepository
  14. get() = (applicationContext as OpenEuiccApplication).appContainer.preferenceRepository
  15. val Fragment.preferenceRepository: PreferenceRepository
  16. get() = requireContext().preferenceRepository
  17. internal object PreferenceKeys {
  18. // ---- Profile Notifications ----
  19. val NOTIFICATION_DOWNLOAD = booleanPreferencesKey("notification_download")
  20. val NOTIFICATION_DELETE = booleanPreferencesKey("notification_delete")
  21. val NOTIFICATION_SWITCH = booleanPreferencesKey("notification_switch")
  22. // ---- Advanced ----
  23. val DISABLE_SAFEGUARD_REMOVABLE_ESIM = booleanPreferencesKey("disable_safeguard_removable_esim")
  24. val VERBOSE_LOGGING = booleanPreferencesKey("verbose_logging")
  25. // ---- Developer Options ----
  26. val DEVELOPER_OPTIONS_ENABLED = booleanPreferencesKey("developer_options_enabled")
  27. val UNFILTERED_PROFILE_LIST = booleanPreferencesKey("unfiltered_profile_list")
  28. val IGNORE_TLS_CERTIFICATE = booleanPreferencesKey("ignore_tls_certificate")
  29. }
  30. class PreferenceRepository(private val context: Context) {
  31. // Expose flows so that we can also handle default values
  32. // ---- Profile Notifications ----
  33. val notificationDownloadFlow = bindFlow(PreferenceKeys.NOTIFICATION_DOWNLOAD, true)
  34. val notificationDeleteFlow = bindFlow(PreferenceKeys.NOTIFICATION_DELETE, true)
  35. val notificationSwitchFlow = bindFlow(PreferenceKeys.NOTIFICATION_SWITCH, false)
  36. // ---- Advanced ----
  37. val disableSafeguardFlow = bindFlow(PreferenceKeys.DISABLE_SAFEGUARD_REMOVABLE_ESIM, false)
  38. val verboseLoggingFlow = bindFlow(PreferenceKeys.VERBOSE_LOGGING, false)
  39. // ---- Developer Options ----
  40. val developerOptionsEnabledFlow = bindFlow(PreferenceKeys.DEVELOPER_OPTIONS_ENABLED, false)
  41. val unfilteredProfileListFlow = bindFlow(PreferenceKeys.UNFILTERED_PROFILE_LIST, false)
  42. val ignoreTLSCertificateFlow = bindFlow(PreferenceKeys.IGNORE_TLS_CERTIFICATE, false)
  43. private fun <T> bindFlow(key: Preferences.Key<T>, defaultValue: T): PreferenceFlowWrapper<T> =
  44. PreferenceFlowWrapper(context, key, defaultValue)
  45. }
  46. class PreferenceFlowWrapper<T> private constructor(
  47. private val context: Context,
  48. private val key: Preferences.Key<T>,
  49. inner: Flow<T>
  50. ) : Flow<T> by inner {
  51. internal constructor(context: Context, key: Preferences.Key<T>, defaultValue: T) : this(
  52. context,
  53. key,
  54. context.dataStore.data.map { it[key] ?: defaultValue }
  55. )
  56. suspend fun updatePreference(value: T) {
  57. context.dataStore.edit { it[key] = value }
  58. }
  59. }