From 4c16bf3f973ae72814924289e116c7e20e6148a2 Mon Sep 17 00:00:00 2001 From: Tiger Oakes Date: Mon, 15 Jun 2020 11:04:36 -0700 Subject: [PATCH] [fenix] Replace requireNotNull(findPreference()) --- .../fenix/settings/CustomizationFragment.kt | 19 ++--- .../settings/TrackingProtectionFragment.kt | 71 +++++++------------ .../logins/SavedLoginsSettingFragment.kt | 15 ++-- ...itePermissionsDetailsExceptionsFragment.kt | 5 +- 4 files changed, 40 insertions(+), 70 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/settings/CustomizationFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/CustomizationFragment.kt index edd608d282..c5826f3550 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/CustomizationFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/CustomizationFragment.kt @@ -13,7 +13,6 @@ import androidx.preference.PreferenceFragmentCompat import org.mozilla.fenix.R import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.ext.components -import org.mozilla.fenix.ext.getPreferenceKey import org.mozilla.fenix.ext.requireComponents import org.mozilla.fenix.ext.settings import org.mozilla.fenix.ext.showToolbar @@ -69,8 +68,7 @@ class CustomizationFragment : PreferenceFragmentCompat() { } private fun bindLightTheme() { - val keyLightTheme = getPreferenceKey(R.string.pref_key_light_theme) - radioLightTheme = requireNotNull(findPreference(keyLightTheme)) + radioLightTheme = requirePreference(R.string.pref_key_light_theme) radioLightTheme.onClickListener { setNewTheme(AppCompatDelegate.MODE_NIGHT_NO) } @@ -79,16 +77,14 @@ class CustomizationFragment : PreferenceFragmentCompat() { @SuppressLint("WrongConstant") // Suppressing erroneous lint warning about using MODE_NIGHT_AUTO_BATTERY, a likely library bug private fun bindAutoBatteryTheme() { - val keyBatteryTheme = getPreferenceKey(R.string.pref_key_auto_battery_theme) - radioAutoBatteryTheme = requireNotNull(findPreference(keyBatteryTheme)) + radioAutoBatteryTheme = requirePreference(R.string.pref_key_auto_battery_theme) radioAutoBatteryTheme.onClickListener { setNewTheme(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY) } } private fun bindDarkTheme() { - val keyDarkTheme = getPreferenceKey(R.string.pref_key_dark_theme) - radioDarkTheme = requireNotNull(findPreference(keyDarkTheme)) + radioDarkTheme = requirePreference(R.string.pref_key_dark_theme) radioDarkTheme.onClickListener { requireContext().components.analytics.metrics.track( Event.DarkThemeSelected( @@ -100,8 +96,7 @@ class CustomizationFragment : PreferenceFragmentCompat() { } private fun bindFollowDeviceTheme() { - val keyDeviceTheme = getPreferenceKey(R.string.pref_key_follow_device_theme) - radioFollowDeviceTheme = requireNotNull(findPreference(keyDeviceTheme)) + radioFollowDeviceTheme = requirePreference(R.string.pref_key_follow_device_theme) if (SDK_INT >= Build.VERSION_CODES.P) { radioFollowDeviceTheme.onClickListener { setNewTheme(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) @@ -120,16 +115,14 @@ class CustomizationFragment : PreferenceFragmentCompat() { } private fun setupToolbarCategory() { - val keyToolbarTop = getPreferenceKey(R.string.pref_key_toolbar_top) - val topPreference = requireNotNull(findPreference(keyToolbarTop)) + val topPreference = requirePreference(R.string.pref_key_toolbar_top) topPreference.onClickListener { requireContext().components.analytics.metrics.track(Event.ToolbarPositionChanged( Event.ToolbarPositionChanged.Position.TOP )) } - val keyToolbarBottom = getPreferenceKey(R.string.pref_key_toolbar_bottom) - val bottomPreference = requireNotNull(findPreference(keyToolbarBottom)) + val bottomPreference = requirePreference(R.string.pref_key_toolbar_top) bottomPreference.onClickListener { requireContext().components.analytics.metrics.track(Event.ToolbarPositionChanged( Event.ToolbarPositionChanged.Position.BOTTOM diff --git a/app/src/main/java/org/mozilla/fenix/settings/TrackingProtectionFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/TrackingProtectionFragment.kt index 4846cd1ab6..d2acd7737d 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/TrackingProtectionFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/TrackingProtectionFragment.kt @@ -16,7 +16,6 @@ import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.R import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.ext.components -import org.mozilla.fenix.ext.getPreferenceKey import org.mozilla.fenix.ext.nav import org.mozilla.fenix.ext.requireComponents import org.mozilla.fenix.ext.settings @@ -56,11 +55,10 @@ class TrackingProtectionFragment : PreferenceFragmentCompat() { showToolbar(getString(R.string.preference_enhanced_tracking_protection)) // Tracking Protection Switch - val trackingProtectionKey = getPreferenceKey(R.string.pref_key_tracking_protection) - val preferenceTP = findPreference(trackingProtectionKey) + val preferenceTP = requirePreference(R.string.pref_key_tracking_protection) - preferenceTP?.isChecked = requireContext().settings().shouldUseTrackingProtection - preferenceTP?.setOnPreferenceChangeListener { preference, trackingProtectionOn -> + preferenceTP.isChecked = requireContext().settings().shouldUseTrackingProtection + preferenceTP.setOnPreferenceChangeListener { preference, trackingProtectionOn -> preference.context.settings().shouldUseTrackingProtection = trackingProtectionOn with(preference.context.components) { @@ -72,10 +70,8 @@ class TrackingProtectionFragment : PreferenceFragmentCompat() { true } - val trackingProtectionLearnMore = - requireContext().getPreferenceKey(R.string.pref_key_etp_learn_more) - val learnMorePreference = findPreference(trackingProtectionLearnMore) - learnMorePreference?.setOnPreferenceClickListener { + val learnMorePreference = requirePreference(R.string.pref_key_etp_learn_more) + learnMorePreference.setOnPreferenceClickListener { (activity as HomeActivity).openToBrowserAndLoad( searchTermOrURL = SupportUtils.getGenericSumoURLForTopic (SupportUtils.SumoTopic.TRACKING_PROTECTION), @@ -84,22 +80,19 @@ class TrackingProtectionFragment : PreferenceFragmentCompat() { ) true } - learnMorePreference?.summary = getString( + learnMorePreference.summary = getString( R.string.preference_enhanced_tracking_protection_explanation, getString(R.string.app_name) ) - val exceptions = getPreferenceKey(R.string.pref_key_tracking_protection_exceptions) - val preferenceExceptions = findPreference(exceptions) - preferenceExceptions?.onPreferenceClickListener = exceptionsClickListener + val preferenceExceptions = requirePreference(R.string.pref_key_tracking_protection_exceptions) + preferenceExceptions.onPreferenceClickListener = exceptionsClickListener } private fun bindTrackingProtectionRadio( mode: TrackingProtectionMode ): RadioButtonInfoPreference { - val radio = requireNotNull(findPreference( - getPreferenceKey(mode.preferenceKey) - )) + val radio = requirePreference(mode.preferenceKey) radio.contentDescription = getString(mode.contentDescriptionRes) val metrics = requireComponents.analytics.metrics @@ -132,37 +125,23 @@ class TrackingProtectionFragment : PreferenceFragmentCompat() { private fun bindCustom(): RadioButtonInfoPreference { val radio = bindTrackingProtectionRadio(TrackingProtectionMode.CUSTOM) - customCookies = requireNotNull( - findPreference( - getString(R.string.pref_key_tracking_protection_custom_cookies) - ) - ) + customCookies = + requirePreference(R.string.pref_key_tracking_protection_custom_cookies) - customCookiesSelect = requireNotNull( - findPreference( - getString(R.string.pref_key_tracking_protection_custom_cookies_select) - ) - ) - customTracking = requireNotNull( - findPreference( - getString(R.string.pref_key_tracking_protection_custom_tracking_content) - ) - ) - customTrackingSelect = requireNotNull( - findPreference( - getString(R.string.pref_key_tracking_protection_custom_tracking_content_select) - ) - ) - customCryptominers = requireNotNull( - findPreference( - getString(R.string.pref_key_tracking_protection_custom_cryptominers) - ) - ) - customFingerprinters = requireNotNull( - findPreference( - getString(R.string.pref_key_tracking_protection_custom_fingerprinters) - ) - ) + customCookiesSelect = + requirePreference(R.string.pref_key_tracking_protection_custom_cookies_select) + + customTracking = + requirePreference(R.string.pref_key_tracking_protection_custom_tracking_content) + + customTrackingSelect = + requirePreference(R.string.pref_key_tracking_protection_custom_tracking_content_select) + + customCryptominers = + requirePreference(R.string.pref_key_tracking_protection_custom_cryptominers) + + customFingerprinters = + requirePreference(R.string.pref_key_tracking_protection_custom_fingerprinters) customCookies.onPreferenceChangeListener = object : SharedPreferenceUpdater() { override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean { diff --git a/app/src/main/java/org/mozilla/fenix/settings/logins/SavedLoginsSettingFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/logins/SavedLoginsSettingFragment.kt index 593c21fa9b..7d4ed8043b 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/logins/SavedLoginsSettingFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/logins/SavedLoginsSettingFragment.kt @@ -14,6 +14,7 @@ import org.mozilla.fenix.ext.metrics import org.mozilla.fenix.ext.showToolbar import org.mozilla.fenix.settings.RadioButtonPreference import org.mozilla.fenix.settings.SharedPreferenceUpdater +import org.mozilla.fenix.settings.requirePreference class SavedLoginsSettingFragment : PreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { @@ -29,9 +30,8 @@ class SavedLoginsSettingFragment : PreferenceFragmentCompat() { } private fun bindSave(): RadioButtonPreference { - val keySave = getString(R.string.pref_key_save_logins) - val preferenceSave = findPreference(keySave) - preferenceSave?.onPreferenceChangeListener = object : SharedPreferenceUpdater() { + val preferenceSave = requirePreference(R.string.pref_key_save_logins) + preferenceSave.onPreferenceChangeListener = object : SharedPreferenceUpdater() { override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean { if (newValue == true) { context?.metrics?.track( @@ -45,13 +45,12 @@ class SavedLoginsSettingFragment : PreferenceFragmentCompat() { return super.onPreferenceChange(preference, newValue) } } - return requireNotNull(preferenceSave) + return preferenceSave } private fun bindNeverSave(): RadioButtonPreference { - val keyNeverSave = getString(R.string.pref_key_never_save_logins) - val preferenceNeverSave = findPreference(keyNeverSave) - preferenceNeverSave?.onPreferenceChangeListener = object : SharedPreferenceUpdater() { + val preferenceNeverSave = requirePreference(R.string.pref_key_never_save_logins) + preferenceNeverSave.onPreferenceChangeListener = object : SharedPreferenceUpdater() { override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean { if (newValue == true) { context?.metrics?.track( @@ -65,7 +64,7 @@ class SavedLoginsSettingFragment : PreferenceFragmentCompat() { return super.onPreferenceChange(preference, newValue) } } - return requireNotNull(preferenceNeverSave) + return preferenceNeverSave } private fun setupRadioGroups( diff --git a/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsDetailsExceptionsFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsDetailsExceptionsFragment.kt index b87fc747d6..096ec8d9a6 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsDetailsExceptionsFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsDetailsExceptionsFragment.kt @@ -18,13 +18,13 @@ import kotlinx.coroutines.withContext import mozilla.components.feature.sitepermissions.SitePermissions import org.mozilla.fenix.R import org.mozilla.fenix.ext.components -import org.mozilla.fenix.ext.getPreferenceKey import org.mozilla.fenix.ext.showToolbar import org.mozilla.fenix.settings.PhoneFeature import org.mozilla.fenix.settings.PhoneFeature.CAMERA import org.mozilla.fenix.settings.PhoneFeature.LOCATION import org.mozilla.fenix.settings.PhoneFeature.MICROPHONE import org.mozilla.fenix.settings.PhoneFeature.NOTIFICATION +import org.mozilla.fenix.settings.requirePreference class SitePermissionsDetailsExceptionsFragment : PreferenceFragmentCompat() { private lateinit var sitePermissions: SitePermissions @@ -75,8 +75,7 @@ class SitePermissionsDetailsExceptionsFragment : PreferenceFragmentCompat() { } private fun bindClearPermissionsButton() { - val keyPreference = getPreferenceKey(R.string.pref_key_exceptions_clear_site_permissions) - val button: Preference = requireNotNull(findPreference(keyPreference)) + val button: Preference = requirePreference(R.string.pref_key_exceptions_clear_site_permissions) button.onPreferenceClickListener = Preference.OnPreferenceClickListener { AlertDialog.Builder(requireContext()).apply {