From 662c016418919a6a3f223f93f830d24bccca1f84 Mon Sep 17 00:00:00 2001 From: septs Date: Sat, 14 Dec 2024 15:32:40 +0800 Subject: [PATCH 1/4] chore: improve preference bounds --- .../im/angry/openeuicc/ui/SettingsFragment.kt | 27 +++++++------------ .../angry/openeuicc/util/PreferenceUtils.kt | 19 ++++++++++--- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/app-common/src/main/java/im/angry/openeuicc/ui/SettingsFragment.kt b/app-common/src/main/java/im/angry/openeuicc/ui/SettingsFragment.kt index c2cbee3..65e6fde 100644 --- a/app-common/src/main/java/im/angry/openeuicc/ui/SettingsFragment.kt +++ b/app-common/src/main/java/im/angry/openeuicc/ui/SettingsFragment.kt @@ -6,7 +6,6 @@ import android.os.Build import android.os.Bundle import android.provider.Settings import android.widget.Toast -import androidx.datastore.preferences.core.Preferences import androidx.lifecycle.lifecycleScope import androidx.preference.CheckBoxPreference import androidx.preference.Preference @@ -14,7 +13,6 @@ import androidx.preference.PreferenceCategory import androidx.preference.PreferenceFragmentCompat import im.angry.openeuicc.common.R import im.angry.openeuicc.util.* -import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch @@ -60,25 +58,25 @@ open class SettingsFragment: PreferenceFragmentCompat() { } findPreference("pref_notifications_download") - ?.bindBooleanFlow(preferenceRepository.notificationDownloadFlow, PreferenceKeys.NOTIFICATION_DOWNLOAD) + ?.bindBooleanFlow(preferenceRepository.notificationDownloadFlow) findPreference("pref_notifications_delete") - ?.bindBooleanFlow(preferenceRepository.notificationDeleteFlow, PreferenceKeys.NOTIFICATION_DELETE) + ?.bindBooleanFlow(preferenceRepository.notificationDeleteFlow) findPreference("pref_notifications_switch") - ?.bindBooleanFlow(preferenceRepository.notificationSwitchFlow, PreferenceKeys.NOTIFICATION_SWITCH) + ?.bindBooleanFlow(preferenceRepository.notificationSwitchFlow) findPreference("pref_advanced_disable_safeguard_removable_esim") - ?.bindBooleanFlow(preferenceRepository.disableSafeguardFlow, PreferenceKeys.DISABLE_SAFEGUARD_REMOVABLE_ESIM) + ?.bindBooleanFlow(preferenceRepository.disableSafeguardFlow) findPreference("pref_advanced_verbose_logging") - ?.bindBooleanFlow(preferenceRepository.verboseLoggingFlow, PreferenceKeys.VERBOSE_LOGGING) + ?.bindBooleanFlow(preferenceRepository.verboseLoggingFlow) findPreference("pref_developer_unfiltered_profile_list") - ?.bindBooleanFlow(preferenceRepository.unfilteredProfileListFlow, PreferenceKeys.UNFILTERED_PROFILE_LIST) + ?.bindBooleanFlow(preferenceRepository.unfilteredProfileListFlow) findPreference("pref_ignore_tls_certificate") - ?.bindBooleanFlow(preferenceRepository.ignoreTLSCertificateFlow, PreferenceKeys.IGNORE_TLS_CERTIFICATE) + ?.bindBooleanFlow(preferenceRepository.ignoreTLSCertificateFlow) } override fun onStart() { @@ -99,10 +97,7 @@ open class SettingsFragment: PreferenceFragmentCompat() { if (numClicks == 7) { lifecycleScope.launch { - preferenceRepository.updatePreference( - PreferenceKeys.DEVELOPER_OPTIONS_ENABLED, - true - ) + preferenceRepository.developerOptionsEnabledFlow.emit(true) lastToast?.cancel() Toast.makeText( @@ -124,15 +119,13 @@ open class SettingsFragment: PreferenceFragmentCompat() { return true } - private fun CheckBoxPreference.bindBooleanFlow(flow: Flow, key: Preferences.Key) { + private fun CheckBoxPreference.bindBooleanFlow(flow: BoundPreference) { lifecycleScope.launch { flow.collect { isChecked = it } } setOnPreferenceChangeListener { _, newValue -> - runBlocking { - preferenceRepository.updatePreference(key, newValue as Boolean) - } + runBlocking { flow.emit(newValue as Boolean) } true } } 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 e768fa9..9c00005 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 @@ -9,7 +9,9 @@ import androidx.datastore.preferences.preferencesDataStore import androidx.fragment.app.Fragment import im.angry.openeuicc.OpenEuiccApplication import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.FlowCollector import kotlinx.coroutines.flow.map +import kotlinx.coroutines.runBlocking private val Context.dataStore: DataStore by preferencesDataStore(name = "prefs") @@ -51,9 +53,18 @@ class PreferenceRepository(private val context: Context) { val unfilteredProfileListFlow = bindFlow(PreferenceKeys.UNFILTERED_PROFILE_LIST, false) val ignoreTLSCertificateFlow = bindFlow(PreferenceKeys.IGNORE_TLS_CERTIFICATE, false) - private fun bindFlow(key: Preferences.Key, defaultValue: T): Flow = - context.dataStore.data.map { it[key] ?: defaultValue } + private fun bindFlow(key: Preferences.Key, defaultValue: T) = + BoundPreference(context.dataStore, key, defaultValue) +} - suspend fun updatePreference(key: Preferences.Key, value: T) = - context.dataStore.edit { it[key] = value } +class BoundPreference( + private val store: DataStore, + private val key: Preferences.Key, + defaultValue: T +) : Flow { + private val flow = store.data.map { it[key] ?: defaultValue } + + suspend fun emit(value: T) = store.edit { it[key] = value } + + override suspend fun collect(collector: FlowCollector) = flow.collect(collector) } \ No newline at end of file -- 2.45.3 From a7c38f8c3e77347c8067bd39b06d527a228532e9 Mon Sep 17 00:00:00 2001 From: septs Date: Sat, 14 Dec 2024 15:59:47 +0800 Subject: [PATCH 2/4] chore: improve preference bounds --- .../angry/openeuicc/util/PreferenceUtils.kt | 35 ++++++------------- 1 file changed, 11 insertions(+), 24 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 9c00005..255d02d 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 @@ -21,37 +21,24 @@ val Context.preferenceRepository: PreferenceRepository val Fragment.preferenceRepository: PreferenceRepository get() = requireContext().preferenceRepository -object PreferenceKeys { - // ---- Profile Notifications ---- - val NOTIFICATION_DOWNLOAD = booleanPreferencesKey("notification_download") - val NOTIFICATION_DELETE = booleanPreferencesKey("notification_delete") - val NOTIFICATION_SWITCH = booleanPreferencesKey("notification_switch") - - // ---- Advanced ---- - val DISABLE_SAFEGUARD_REMOVABLE_ESIM = booleanPreferencesKey("disable_safeguard_removable_esim") - val VERBOSE_LOGGING = booleanPreferencesKey("verbose_logging") - - // ---- Developer Options ---- - val DEVELOPER_OPTIONS_ENABLED = booleanPreferencesKey("developer_options_enabled") - val UNFILTERED_PROFILE_LIST = booleanPreferencesKey("unfiltered_profile_list") - val IGNORE_TLS_CERTIFICATE = booleanPreferencesKey("ignore_tls_certificate") -} - class PreferenceRepository(private val context: Context) { // Expose flows so that we can also handle default values // ---- Profile Notifications ---- - val notificationDownloadFlow = bindFlow(PreferenceKeys.NOTIFICATION_DOWNLOAD, true) - val notificationDeleteFlow = bindFlow(PreferenceKeys.NOTIFICATION_DELETE, true) - val notificationSwitchFlow = bindFlow(PreferenceKeys.NOTIFICATION_SWITCH, false) + val notificationDownloadFlow = bindBooleanFlow("notification_download", true) + val notificationDeleteFlow = bindBooleanFlow("notification_delete", true) + val notificationSwitchFlow = bindBooleanFlow("notification_switch", false) // ---- Advanced ---- - val disableSafeguardFlow = bindFlow(PreferenceKeys.DISABLE_SAFEGUARD_REMOVABLE_ESIM, false) - val verboseLoggingFlow = bindFlow(PreferenceKeys.VERBOSE_LOGGING, false) + val disableSafeguardFlow = bindBooleanFlow("disable_safeguard_removable_esim", false) + val verboseLoggingFlow = bindBooleanFlow("verbose_logging", false) // ---- Developer Options ---- - val developerOptionsEnabledFlow = bindFlow(PreferenceKeys.DEVELOPER_OPTIONS_ENABLED, false) - val unfilteredProfileListFlow = bindFlow(PreferenceKeys.UNFILTERED_PROFILE_LIST, false) - val ignoreTLSCertificateFlow = bindFlow(PreferenceKeys.IGNORE_TLS_CERTIFICATE, false) + val developerOptionsEnabledFlow = bindBooleanFlow("developer_options_enabled", false) + val unfilteredProfileListFlow = bindBooleanFlow("unfiltered_profile_list", false) + val ignoreTLSCertificateFlow = bindBooleanFlow("ignore_tls_certificate", false) + + private fun bindBooleanFlow(name: String, defaultValue: Boolean) = + bindFlow(booleanPreferencesKey(name), defaultValue) private fun bindFlow(key: Preferences.Key, defaultValue: T) = BoundPreference(context.dataStore, key, defaultValue) -- 2.45.3 From 6c5e51fa4dac18f802cecb0fb0691a42738e2183 Mon Sep 17 00:00:00 2001 From: septs Date: Sat, 14 Dec 2024 16:09:30 +0800 Subject: [PATCH 3/4] chore: improve preference bounds --- .../im/angry/openeuicc/util/PreferenceUtils.kt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 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 255d02d..3c901ec 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 @@ -24,20 +24,20 @@ val Fragment.preferenceRepository: PreferenceRepository class PreferenceRepository(private val context: Context) { // Expose flows so that we can also handle default values // ---- Profile Notifications ---- - val notificationDownloadFlow = bindBooleanFlow("notification_download", true) - val notificationDeleteFlow = bindBooleanFlow("notification_delete", true) - val notificationSwitchFlow = bindBooleanFlow("notification_switch", false) + val notificationDownloadFlow = bindFlow("notification_download", true) + val notificationDeleteFlow = bindFlow("notification_delete", true) + val notificationSwitchFlow = bindFlow("notification_switch", false) // ---- Advanced ---- - val disableSafeguardFlow = bindBooleanFlow("disable_safeguard_removable_esim", false) - val verboseLoggingFlow = bindBooleanFlow("verbose_logging", false) + val disableSafeguardFlow = bindFlow("disable_safeguard_removable_esim", false) + val verboseLoggingFlow = bindFlow("verbose_logging", false) // ---- Developer Options ---- - val developerOptionsEnabledFlow = bindBooleanFlow("developer_options_enabled", false) - val unfilteredProfileListFlow = bindBooleanFlow("unfiltered_profile_list", false) - val ignoreTLSCertificateFlow = bindBooleanFlow("ignore_tls_certificate", false) + val developerOptionsEnabledFlow = bindFlow("developer_options_enabled", false) + val unfilteredProfileListFlow = bindFlow("unfiltered_profile_list", false) + val ignoreTLSCertificateFlow = bindFlow("ignore_tls_certificate", false) - private fun bindBooleanFlow(name: String, defaultValue: Boolean) = + private fun bindFlow(name: String, defaultValue: Boolean) = bindFlow(booleanPreferencesKey(name), defaultValue) private fun bindFlow(key: Preferences.Key, defaultValue: T) = -- 2.45.3 From 50e891178679d011df2b4a4b5ef00cd8075211d6 Mon Sep 17 00:00:00 2001 From: septs Date: Sat, 14 Dec 2024 16:16:51 +0800 Subject: [PATCH 4/4] chore: improve preference bounds --- .../src/main/java/im/angry/openeuicc/util/PreferenceUtils.kt | 1 - 1 file changed, 1 deletion(-) 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 3c901ec..3f5525b 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 @@ -11,7 +11,6 @@ import im.angry.openeuicc.OpenEuiccApplication import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.FlowCollector import kotlinx.coroutines.flow.map -import kotlinx.coroutines.runBlocking private val Context.dataStore: DataStore by preferencesDataStore(name = "prefs") -- 2.45.3