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
This commit is contained in:
Nik Clayton 2022-12-31 13:02:23 +01:00 committed by GitHub
parent 22834431ca
commit 2e72fa0dfc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 28 deletions

View file

@ -324,6 +324,11 @@ class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable {
} }
} }
override fun onResume() {
super.onResume()
requireActivity().setTitle(R.string.action_view_account_preferences)
}
private fun openNotificationPrefs() { private fun openNotificationPrefs() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val intent = Intent() val intent = Intent()

View file

@ -204,6 +204,11 @@ class NotificationPreferencesFragment : PreferenceFragmentCompat(), Injectable {
} }
} }
override fun onResume() {
super.onResume()
requireActivity().setTitle(R.string.pref_title_edit_notification_settings)
}
companion object { companion object {
fun newInstance(): NotificationPreferencesFragment { fun newInstance(): NotificationPreferencesFragment {
return NotificationPreferencesFragment() return NotificationPreferencesFragment()

View file

@ -23,6 +23,8 @@ import android.util.Log
import androidx.activity.OnBackPressedCallback import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.commit import androidx.fragment.app.commit
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.keylesspalace.tusky.BaseActivity import com.keylesspalace.tusky.BaseActivity
import com.keylesspalace.tusky.MainActivity import com.keylesspalace.tusky.MainActivity
@ -41,6 +43,7 @@ import javax.inject.Inject
class PreferencesActivity : class PreferencesActivity :
BaseActivity(), BaseActivity(),
SharedPreferences.OnSharedPreferenceChangeListener, SharedPreferences.OnSharedPreferenceChangeListener,
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback,
HasAndroidInjector { HasAndroidInjector {
@Inject @Inject
@ -82,8 +85,6 @@ class PreferencesActivity :
GENERAL_PREFERENCES -> PreferencesFragment.newInstance() GENERAL_PREFERENCES -> PreferencesFragment.newInstance()
ACCOUNT_PREFERENCES -> AccountPreferencesFragment.newInstance() ACCOUNT_PREFERENCES -> AccountPreferencesFragment.newInstance()
NOTIFICATION_PREFERENCES -> NotificationPreferencesFragment.newInstance() NOTIFICATION_PREFERENCES -> NotificationPreferencesFragment.newInstance()
TAB_FILTER_PREFERENCES -> TabFilterPreferencesFragment.newInstance()
PROXY_PREFERENCES -> ProxyPreferencesFragment.newInstance()
else -> throw IllegalArgumentException("preferenceType not known") else -> throw IllegalArgumentException("preferenceType not known")
} }
@ -91,18 +92,34 @@ class PreferencesActivity :
replace(R.id.fragment_container, fragment, fragmentTag) 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) onBackPressedDispatcher.addCallback(this, restartActivitiesOnBackPressedCallback)
restartActivitiesOnBackPressedCallback.isEnabled = savedInstanceState?.getBoolean(EXTRA_RESTART_ON_BACK, false) ?: false 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() { override fun onResume() {
super.onResume() super.onResume()
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this) PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this)
@ -159,8 +176,6 @@ class PreferencesActivity :
const val GENERAL_PREFERENCES = 0 const val GENERAL_PREFERENCES = 0
const val ACCOUNT_PREFERENCES = 1 const val ACCOUNT_PREFERENCES = 1
const val NOTIFICATION_PREFERENCES = 2 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_PREFERENCE_TYPE = "EXTRA_PREFERENCE_TYPE"
private const val EXTRA_RESTART_ON_BACK = "restart" private const val EXTRA_RESTART_ON_BACK = "restart"

View file

@ -206,14 +206,7 @@ class PreferencesFragment : PreferenceFragmentCompat(), Injectable {
preferenceCategory(R.string.pref_title_timeline_filters) { preferenceCategory(R.string.pref_title_timeline_filters) {
preference { preference {
setTitle(R.string.pref_title_post_tabs) setTitle(R.string.pref_title_post_tabs)
setOnPreferenceClickListener { fragment = "com.keylesspalace.tusky.components.preference.TabFilterPreferencesFragment"
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
}
} }
} }
@ -259,14 +252,7 @@ class PreferencesFragment : PreferenceFragmentCompat(), Injectable {
preferenceCategory(R.string.pref_title_proxy_settings) { preferenceCategory(R.string.pref_title_proxy_settings) {
httpProxyPref = preference { httpProxyPref = preference {
setTitle(R.string.pref_title_http_proxy_settings) setTitle(R.string.pref_title_http_proxy_settings)
setOnPreferenceClickListener { fragment = "com.keylesspalace.tusky.components.preference.ProxyPreferencesFragment"
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
}
} }
} }
} }
@ -278,6 +264,7 @@ class PreferencesFragment : PreferenceFragmentCompat(), Injectable {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
requireActivity().setTitle(R.string.action_view_preferences)
updateHttpProxySummary() updateHttpProxySummary()
} }

View file

@ -65,6 +65,11 @@ class ProxyPreferencesFragment : PreferenceFragmentCompat() {
} }
} }
override fun onResume() {
super.onResume()
requireActivity().setTitle(R.string.pref_title_http_proxy_settings)
}
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
if (pendingRestart) { if (pendingRestart) {

View file

@ -46,6 +46,11 @@ class TabFilterPreferencesFragment : PreferenceFragmentCompat() {
} }
} }
override fun onResume() {
super.onResume()
requireActivity().setTitle(R.string.pref_title_post_tabs)
}
companion object { companion object {
fun newInstance(): TabFilterPreferencesFragment { fun newInstance(): TabFilterPreferencesFragment {
return TabFilterPreferencesFragment() return TabFilterPreferencesFragment()