From 2e72fa0dfcfeef009b76fc1f638f2d11b68984c3 Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Sat, 31 Dec 2022 13:02:23 +0100 Subject: [PATCH] Handle preference fragments using the framework (#3090) * Handle preference fragments using the framework The previous code started new preference "screens" as activities, even though each one hosted a single fragment. Modify this to use the framework's support for swapping in/out different preference fragments. PreferencesActivity: - Remove the code for launching tab and proxy preferences - Remove the code for setting titles, each fragment is responsible for that - Implement OnPreferenceStartFragmentCallback to swap fragments in/out with the correct animation PreferencesFragment: - Use `fragment` property instead of `setOnPreferenceClickListener` - Set the activity title when resuming Everything else: - Set the activity title when resuming * Lint * Use the commit extension function --- .../preference/AccountPreferencesFragment.kt | 5 +++ .../NotificationPreferencesFragment.kt | 5 +++ .../preference/PreferencesActivity.kt | 39 +++++++++++++------ .../preference/PreferencesFragment.kt | 19 ++------- .../preference/ProxyPreferencesFragment.kt | 5 +++ .../TabFilterPreferencesFragment.kt | 5 +++ 6 files changed, 50 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/preference/AccountPreferencesFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/preference/AccountPreferencesFragment.kt index d0e13719..3c919327 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/preference/AccountPreferencesFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/preference/AccountPreferencesFragment.kt @@ -324,6 +324,11 @@ class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable { } } + override fun onResume() { + super.onResume() + requireActivity().setTitle(R.string.action_view_account_preferences) + } + private fun openNotificationPrefs() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val intent = Intent() diff --git a/app/src/main/java/com/keylesspalace/tusky/components/preference/NotificationPreferencesFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/preference/NotificationPreferencesFragment.kt index b6eb4389..83a96ed5 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/preference/NotificationPreferencesFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/preference/NotificationPreferencesFragment.kt @@ -204,6 +204,11 @@ class NotificationPreferencesFragment : PreferenceFragmentCompat(), Injectable { } } + override fun onResume() { + super.onResume() + requireActivity().setTitle(R.string.pref_title_edit_notification_settings) + } + companion object { fun newInstance(): NotificationPreferencesFragment { return NotificationPreferencesFragment() diff --git a/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesActivity.kt index 44d35b66..1fdc7a65 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesActivity.kt @@ -23,6 +23,8 @@ import android.util.Log import androidx.activity.OnBackPressedCallback import androidx.fragment.app.Fragment import androidx.fragment.app.commit +import androidx.preference.Preference +import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceManager import com.keylesspalace.tusky.BaseActivity import com.keylesspalace.tusky.MainActivity @@ -41,6 +43,7 @@ import javax.inject.Inject class PreferencesActivity : BaseActivity(), SharedPreferences.OnSharedPreferenceChangeListener, + PreferenceFragmentCompat.OnPreferenceStartFragmentCallback, HasAndroidInjector { @Inject @@ -82,8 +85,6 @@ class PreferencesActivity : GENERAL_PREFERENCES -> PreferencesFragment.newInstance() ACCOUNT_PREFERENCES -> AccountPreferencesFragment.newInstance() NOTIFICATION_PREFERENCES -> NotificationPreferencesFragment.newInstance() - TAB_FILTER_PREFERENCES -> TabFilterPreferencesFragment.newInstance() - PROXY_PREFERENCES -> ProxyPreferencesFragment.newInstance() else -> throw IllegalArgumentException("preferenceType not known") } @@ -91,18 +92,34 @@ class PreferencesActivity : replace(R.id.fragment_container, fragment, fragmentTag) } - when (preferenceType) { - GENERAL_PREFERENCES -> setTitle(R.string.action_view_preferences) - ACCOUNT_PREFERENCES -> setTitle(R.string.action_view_account_preferences) - NOTIFICATION_PREFERENCES -> setTitle(R.string.pref_title_edit_notification_settings) - TAB_FILTER_PREFERENCES -> setTitle(R.string.pref_title_post_tabs) - PROXY_PREFERENCES -> setTitle(R.string.pref_title_http_proxy_settings) - } - onBackPressedDispatcher.addCallback(this, restartActivitiesOnBackPressedCallback) restartActivitiesOnBackPressedCallback.isEnabled = savedInstanceState?.getBoolean(EXTRA_RESTART_ON_BACK, false) ?: false } + override fun onPreferenceStartFragment( + caller: PreferenceFragmentCompat, + pref: Preference + ): Boolean { + val args = pref.extras + val fragment = supportFragmentManager.fragmentFactory.instantiate( + classLoader, + pref.fragment!! + ) + fragment.arguments = args + fragment.setTargetFragment(caller, 0) + supportFragmentManager.commit { + setCustomAnimations( + R.anim.slide_from_right, + R.anim.slide_to_left, + R.anim.slide_from_left, + R.anim.slide_to_right + ) + replace(R.id.fragment_container, fragment) + addToBackStack(null) + } + return true + } + override fun onResume() { super.onResume() PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this) @@ -159,8 +176,6 @@ class PreferencesActivity : const val GENERAL_PREFERENCES = 0 const val ACCOUNT_PREFERENCES = 1 const val NOTIFICATION_PREFERENCES = 2 - const val TAB_FILTER_PREFERENCES = 3 - const val PROXY_PREFERENCES = 4 private const val EXTRA_PREFERENCE_TYPE = "EXTRA_PREFERENCE_TYPE" private const val EXTRA_RESTART_ON_BACK = "restart" diff --git a/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt index fd79c338..ddc8d372 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt @@ -206,14 +206,7 @@ class PreferencesFragment : PreferenceFragmentCompat(), Injectable { preferenceCategory(R.string.pref_title_timeline_filters) { preference { setTitle(R.string.pref_title_post_tabs) - setOnPreferenceClickListener { - activity?.let { activity -> - val intent = PreferencesActivity.newIntent(activity, PreferencesActivity.TAB_FILTER_PREFERENCES) - activity.startActivity(intent) - activity.overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left) - } - true - } + fragment = "com.keylesspalace.tusky.components.preference.TabFilterPreferencesFragment" } } @@ -259,14 +252,7 @@ class PreferencesFragment : PreferenceFragmentCompat(), Injectable { preferenceCategory(R.string.pref_title_proxy_settings) { httpProxyPref = preference { setTitle(R.string.pref_title_http_proxy_settings) - setOnPreferenceClickListener { - activity?.let { activity -> - val intent = PreferencesActivity.newIntent(activity, PreferencesActivity.PROXY_PREFERENCES) - activity.startActivity(intent) - activity.overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left) - } - true - } + fragment = "com.keylesspalace.tusky.components.preference.ProxyPreferencesFragment" } } } @@ -278,6 +264,7 @@ class PreferencesFragment : PreferenceFragmentCompat(), Injectable { override fun onResume() { super.onResume() + requireActivity().setTitle(R.string.action_view_preferences) updateHttpProxySummary() } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/preference/ProxyPreferencesFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/preference/ProxyPreferencesFragment.kt index 3e11738c..12a22271 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/preference/ProxyPreferencesFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/preference/ProxyPreferencesFragment.kt @@ -65,6 +65,11 @@ class ProxyPreferencesFragment : PreferenceFragmentCompat() { } } + override fun onResume() { + super.onResume() + requireActivity().setTitle(R.string.pref_title_http_proxy_settings) + } + override fun onPause() { super.onPause() if (pendingRestart) { diff --git a/app/src/main/java/com/keylesspalace/tusky/components/preference/TabFilterPreferencesFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/preference/TabFilterPreferencesFragment.kt index c0a76329..02390529 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/preference/TabFilterPreferencesFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/preference/TabFilterPreferencesFragment.kt @@ -46,6 +46,11 @@ class TabFilterPreferencesFragment : PreferenceFragmentCompat() { } } + override fun onResume() { + super.onResume() + requireActivity().setTitle(R.string.pref_title_post_tabs) + } + companion object { fun newInstance(): TabFilterPreferencesFragment { return TabFilterPreferencesFragment()