Skip to content

Commit

Permalink
refactor: set Preferences toolbar title in parent layout
Browse files Browse the repository at this point in the history
Changing the title from child fragments isn't appropriate
  • Loading branch information
BrayanDSO committed Nov 23, 2024
1 parent 595afb1 commit 0e1c197
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ import java.util.Date
import java.util.Locale
import net.ankiweb.rsdroid.BuildConfig as BackendBuildConfig

class AboutFragment : Fragment(R.layout.about_layout) {
class AboutFragment : Fragment(R.layout.about_layout), TitleProvider {
override val title: CharSequence
get() = getString(R.string.pref_cat_about_title)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// Version date
Expand Down Expand Up @@ -113,11 +115,6 @@ class AboutFragment : Fragment(R.layout.about_layout) {
}
}

override fun onStart() {
super.onStart()
requireActivity().setTitle(R.string.pref_cat_about_title)
}

/**
* Copies debug info (from [DebugInfoService.getDebugInfo]) to the clipboard
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ import com.ichi2.compat.CompatHelper
import com.ichi2.preferences.HeaderPreference
import com.ichi2.utils.AdaptionUtil

class HeaderFragment : PreferenceFragmentCompat() {
class HeaderFragment : PreferenceFragmentCompat(), TitleProvider {
private var selectedHeaderPreference: HeaderPreference? = null
private var selectedHeaderPreferenceKey: String = DEFAULT_SELECTED_HEADER

override val title: CharSequence
get() = getString(R.string.settings)

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preference_headers, rootKey)

Expand Down Expand Up @@ -88,11 +92,6 @@ class HeaderFragment : PreferenceFragmentCompat() {
highlightHeaderPreference(requirePreference<HeaderPreference>(selectedHeaderPreferenceKey))
}

override fun onStart() {
super.onStart()
requireActivity().setTitle(R.string.settings)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// use the same fragment container to search in case there is a navigation container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ class Preferences :
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback,
SearchPreferenceResultListener {

override fun onTitleChanged(title: CharSequence?, color: Int) {
super.onTitleChanged(title, color)
findViewById<CollapsingToolbarLayout>(R.id.collapsingToolbarLayout)?.title = title
supportActionBar?.title = title
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.preferences)
Expand All @@ -69,6 +63,10 @@ class Preferences :
val isAtTop = viewHolder != null && viewHolder.itemView.top >= 0
findViewById<AppBarLayout>(R.id.appbar).setExpanded(isAtTop, false)
}

val title = if (fragment is TitleProvider) fragment.title else ""
findViewById<CollapsingToolbarLayout>(R.id.collapsingToolbarLayout)?.title = title
supportActionBar?.title = title
}
}

Expand Down Expand Up @@ -150,6 +148,10 @@ class Preferences :
}
}

interface TitleProvider {
val title: CharSequence
}

/* Only enable AnkiDroid notifications unrelated to due reminders */
const val PENDING_NOTIFICATIONS_ONLY = 1000000

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ import kotlin.reflect.jvm.jvmName
abstract class SettingsFragment :
PreferenceFragmentCompat(),
OnPreferenceTreeClickListener,
SharedPreferences.OnSharedPreferenceChangeListener {
SharedPreferences.OnSharedPreferenceChangeListener,
TitleProvider {
/** @return The XML file which defines the preferences displayed by this PreferenceFragment
*/
@get:XmlRes
abstract val preferenceResource: Int

override val title: CharSequence
get() = preferenceManager?.preferenceScreen?.title ?: ""

/**
* Refreshes all values on the screen
* Call if a large number of values are changed from one preference.
Expand Down Expand Up @@ -102,7 +106,6 @@ abstract class SettingsFragment :

override fun onStart() {
super.onStart()
requireActivity().title = preferenceScreen.title
PreferenceManager.getDefaultSharedPreferences(requireContext())
.registerOnSharedPreferenceChangeListener(this)
}
Expand Down

0 comments on commit 0e1c197

Please sign in to comment.