refactor: strong preference key constraint #128

Merged
PeterCxy merged 2 commits from septs/OpenEUICC:strong-preference-key into master 2024-12-18 01:26:12 +01:00
2 changed files with 24 additions and 21 deletions

View file

@ -29,7 +29,7 @@ open class SettingsFragment: PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.pref_settings, rootKey) setPreferencesFromResource(R.xml.pref_settings, rootKey)
developerPref = findPreference("pref_developer")!! developerPref = requirePreference("pref_developer")
// Show / hide developer preference based on whether it is enabled // Show / hide developer preference based on whether it is enabled
lifecycleScope.launch { lifecycleScope.launch {
@ -38,14 +38,14 @@ open class SettingsFragment: PreferenceFragmentCompat() {
.collect() .collect()
} }
findPreference<Preference>("pref_info_app_version")?.apply { requirePreference<Preference>("pref_info_app_version").apply {
summary = requireContext().selfAppVersion summary = requireContext().selfAppVersion
// Enable developer options when this is clicked for 7 times // Enable developer options when this is clicked for 7 times
setOnPreferenceClickListener(::onAppVersionClicked) setOnPreferenceClickListener(::onAppVersionClicked)
} }
findPreference<Preference>("pref_advanced_language")?.apply { requirePreference<Preference>("pref_advanced_language").apply {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return@apply if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return@apply
isVisible = true isVisible = true
intent = Intent(Settings.ACTION_APP_LOCALE_SETTINGS).apply { intent = Intent(Settings.ACTION_APP_LOCALE_SETTINGS).apply {
@ -53,32 +53,35 @@ open class SettingsFragment: PreferenceFragmentCompat() {
} }
} }
findPreference<Preference>("pref_advanced_logs")?.apply { requirePreference<Preference>("pref_advanced_logs").apply {
intent = Intent(requireContext(), LogsActivity::class.java) intent = Intent(requireContext(), LogsActivity::class.java)
} }
findPreference<CheckBoxPreference>("pref_notifications_download") requirePreference<CheckBoxPreference>("pref_notifications_download")
?.bindBooleanFlow(preferenceRepository.notificationDownloadFlow) .bindBooleanFlow(preferenceRepository.notificationDownloadFlow)
findPreference<CheckBoxPreference>("pref_notifications_delete") requirePreference<CheckBoxPreference>("pref_notifications_delete")
?.bindBooleanFlow(preferenceRepository.notificationDeleteFlow) .bindBooleanFlow(preferenceRepository.notificationDeleteFlow)
findPreference<CheckBoxPreference>("pref_notifications_switch") requirePreference<CheckBoxPreference>("pref_notifications_switch")
?.bindBooleanFlow(preferenceRepository.notificationSwitchFlow) .bindBooleanFlow(preferenceRepository.notificationSwitchFlow)
findPreference<CheckBoxPreference>("pref_advanced_disable_safeguard_removable_esim") requirePreference<CheckBoxPreference>("pref_advanced_disable_safeguard_removable_esim")
?.bindBooleanFlow(preferenceRepository.disableSafeguardFlow) .bindBooleanFlow(preferenceRepository.disableSafeguardFlow)
findPreference<CheckBoxPreference>("pref_advanced_verbose_logging") requirePreference<CheckBoxPreference>("pref_advanced_verbose_logging")
?.bindBooleanFlow(preferenceRepository.verboseLoggingFlow) .bindBooleanFlow(preferenceRepository.verboseLoggingFlow)
findPreference<CheckBoxPreference>("pref_developer_unfiltered_profile_list") requirePreference<CheckBoxPreference>("pref_developer_unfiltered_profile_list")
?.bindBooleanFlow(preferenceRepository.unfilteredProfileListFlow) .bindBooleanFlow(preferenceRepository.unfilteredProfileListFlow)
findPreference<CheckBoxPreference>("pref_ignore_tls_certificate") requirePreference<CheckBoxPreference>("pref_developer_ignore_tls_certificate")
?.bindBooleanFlow(preferenceRepository.ignoreTLSCertificateFlow) .bindBooleanFlow(preferenceRepository.ignoreTLSCertificateFlow)
} }
protected fun <T : Preference> requirePreference(key: CharSequence) =
findPreference<T>(key)!!
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
setupRootViewInsets(requireView().requireViewById(R.id.recycler_view)) setupRootViewInsets(requireView().requireViewById(R.id.recycler_view))
@ -133,8 +136,8 @@ open class SettingsFragment: PreferenceFragmentCompat() {
} }
protected fun mergePreferenceOverlay(overlayKey: String, targetKey: String) { protected fun mergePreferenceOverlay(overlayKey: String, targetKey: String) {
val overlayCat = findPreference<PreferenceCategory>(overlayKey)!! val overlayCat = requirePreference<PreferenceCategory>(overlayKey)
val targetCat = findPreference<PreferenceCategory>(targetKey)!! val targetCat = requirePreference<PreferenceCategory>(targetKey)
val prefs = buildList { val prefs = buildList {
for (i in 0..<overlayCat.preferenceCount) { for (i in 0..<overlayCat.preferenceCount) {

View file

@ -31,7 +31,7 @@ class UnprivilegedSettingsFragment : SettingsFragment() {
addPreferencesFromResource(R.xml.pref_unprivileged_settings) addPreferencesFromResource(R.xml.pref_unprivileged_settings)
mergePreferenceOverlay("pref_info_overlay", "pref_info") mergePreferenceOverlay("pref_info_overlay", "pref_info")
findPreference<Preference>("pref_info_ara_m")?.apply { requirePreference<Preference>("pref_info_ara_m").apply {
summary = firstSigner.encodeHex() summary = firstSigner.encodeHex()
setOnPreferenceClickListener { setOnPreferenceClickListener {
requireContext().getSystemService(ClipboardManager::class.java)!! requireContext().getSystemService(ClipboardManager::class.java)!!