From 9596b8632c141c17a82aeefa704268277f41fc8c Mon Sep 17 00:00:00 2001 From: septs Date: Wed, 18 Dec 2024 01:26:06 +0100 Subject: [PATCH] refactor: strong preference key constraint (#128) Reviewed-on: https://gitea.angry.im/PeterCxy/OpenEUICC/pulls/128 Co-authored-by: septs Co-committed-by: septs --- .../im/angry/openeuicc/ui/SettingsFragment.kt | 43 ++++++++++--------- .../ui/UnprivilegedSettingsFragment.kt | 2 +- 2 files changed, 24 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 ef4ef8bb..fab680f2 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 @@ -29,7 +29,7 @@ open class SettingsFragment: PreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.pref_settings, rootKey) - developerPref = findPreference("pref_developer")!! + developerPref = requirePreference("pref_developer") // Show / hide developer preference based on whether it is enabled lifecycleScope.launch { @@ -38,14 +38,14 @@ open class SettingsFragment: PreferenceFragmentCompat() { .collect() } - findPreference("pref_info_app_version")?.apply { + requirePreference("pref_info_app_version").apply { summary = requireContext().selfAppVersion // Enable developer options when this is clicked for 7 times setOnPreferenceClickListener(::onAppVersionClicked) } - findPreference("pref_advanced_language")?.apply { + requirePreference("pref_advanced_language").apply { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return@apply isVisible = true intent = Intent(Settings.ACTION_APP_LOCALE_SETTINGS).apply { @@ -53,32 +53,35 @@ open class SettingsFragment: PreferenceFragmentCompat() { } } - findPreference("pref_advanced_logs")?.apply { + requirePreference("pref_advanced_logs").apply { intent = Intent(requireContext(), LogsActivity::class.java) } - findPreference("pref_notifications_download") - ?.bindBooleanFlow(preferenceRepository.notificationDownloadFlow) + requirePreference("pref_notifications_download") + .bindBooleanFlow(preferenceRepository.notificationDownloadFlow) - findPreference("pref_notifications_delete") - ?.bindBooleanFlow(preferenceRepository.notificationDeleteFlow) + requirePreference("pref_notifications_delete") + .bindBooleanFlow(preferenceRepository.notificationDeleteFlow) - findPreference("pref_notifications_switch") - ?.bindBooleanFlow(preferenceRepository.notificationSwitchFlow) + requirePreference("pref_notifications_switch") + .bindBooleanFlow(preferenceRepository.notificationSwitchFlow) - findPreference("pref_advanced_disable_safeguard_removable_esim") - ?.bindBooleanFlow(preferenceRepository.disableSafeguardFlow) + requirePreference("pref_advanced_disable_safeguard_removable_esim") + .bindBooleanFlow(preferenceRepository.disableSafeguardFlow) - findPreference("pref_advanced_verbose_logging") - ?.bindBooleanFlow(preferenceRepository.verboseLoggingFlow) + requirePreference("pref_advanced_verbose_logging") + .bindBooleanFlow(preferenceRepository.verboseLoggingFlow) - findPreference("pref_developer_unfiltered_profile_list") - ?.bindBooleanFlow(preferenceRepository.unfilteredProfileListFlow) + requirePreference("pref_developer_unfiltered_profile_list") + .bindBooleanFlow(preferenceRepository.unfilteredProfileListFlow) - findPreference("pref_ignore_tls_certificate") - ?.bindBooleanFlow(preferenceRepository.ignoreTLSCertificateFlow) + requirePreference("pref_developer_ignore_tls_certificate") + .bindBooleanFlow(preferenceRepository.ignoreTLSCertificateFlow) } + protected fun requirePreference(key: CharSequence) = + findPreference(key)!! + override fun onStart() { super.onStart() setupRootViewInsets(requireView().requireViewById(R.id.recycler_view)) @@ -133,8 +136,8 @@ open class SettingsFragment: PreferenceFragmentCompat() { } protected fun mergePreferenceOverlay(overlayKey: String, targetKey: String) { - val overlayCat = findPreference(overlayKey)!! - val targetCat = findPreference(targetKey)!! + val overlayCat = requirePreference(overlayKey) + val targetCat = requirePreference(targetKey) val prefs = buildList { for (i in 0..("pref_info_ara_m")?.apply { + requirePreference("pref_info_ara_m").apply { summary = firstSigner.encodeHex() setOnPreferenceClickListener { requireContext().getSystemService(ClipboardManager::class.java)!!