PreferenceUtils.kt 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 REFRESH_AFTER_SWITCH = booleanPreferencesKey("refresh_after_switch")
  28. val UNFILTERED_PROFILE_LIST = booleanPreferencesKey("unfiltered_profile_list")
  29. val IGNORE_TLS_CERTIFICATE = booleanPreferencesKey("ignore_tls_certificate")
  30. val EUICC_MEMORY_RESET = booleanPreferencesKey("euicc_memory_reset")
  31. }
  32. open class PreferenceRepository(private val context: Context) {
  33. // Expose flows so that we can also handle default values
  34. // ---- Profile Notifications ----
  35. val notificationDownloadFlow = bindFlow(PreferenceKeys.NOTIFICATION_DOWNLOAD, true)
  36. val notificationDeleteFlow = bindFlow(PreferenceKeys.NOTIFICATION_DELETE, true)
  37. val notificationSwitchFlow = bindFlow(PreferenceKeys.NOTIFICATION_SWITCH, false)
  38. // ---- Advanced ----
  39. val disableSafeguardFlow = bindFlow(PreferenceKeys.DISABLE_SAFEGUARD_REMOVABLE_ESIM, false)
  40. val verboseLoggingFlow = bindFlow(PreferenceKeys.VERBOSE_LOGGING, false)
  41. // ---- Developer Options ----
  42. val refreshAfterSwitchFlow = bindFlow(PreferenceKeys.REFRESH_AFTER_SWITCH, true)
  43. val developerOptionsEnabledFlow = bindFlow(PreferenceKeys.DEVELOPER_OPTIONS_ENABLED, false)
  44. val unfilteredProfileListFlow = bindFlow(PreferenceKeys.UNFILTERED_PROFILE_LIST, false)
  45. val ignoreTLSCertificateFlow = bindFlow(PreferenceKeys.IGNORE_TLS_CERTIFICATE, false)
  46. val euiccMemoryResetFlow = bindFlow(PreferenceKeys.EUICC_MEMORY_RESET, false)
  47. protected fun <T> bindFlow(key: Preferences.Key<T>, defaultValue: T): PreferenceFlowWrapper<T> =
  48. PreferenceFlowWrapper(context, key, defaultValue)
  49. }
  50. class PreferenceFlowWrapper<T> private constructor(
  51. private val context: Context,
  52. private val key: Preferences.Key<T>,
  53. inner: Flow<T>,
  54. ) : Flow<T> by inner {
  55. internal constructor(context: Context, key: Preferences.Key<T>, defaultValue: T) :
  56. this(context, key, context.dataStore.data.map { it[key] ?: defaultValue })
  57. suspend fun updatePreference(value: T) {
  58. context.dataStore.edit { it[key] = value }
  59. }
  60. }