From 5d1738e1cc9a74b02f1225af6d915d887570d319 Mon Sep 17 00:00:00 2001 From: SanjaySargam Date: Thu, 23 May 2024 12:10:35 +0530 Subject: [PATCH] highlight selected preference --- .../ichi2/anki/preferences/HeaderFragment.kt | 37 +++++++++++++++++++ .../com/ichi2/anki/preferences/Preferences.kt | 2 +- .../com/ichi2/preferences/HeaderPreference.kt | 16 ++++++++ .../src/main/res/xml/preference_headers.xml | 6 +-- 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/preferences/HeaderFragment.kt b/AnkiDroid/src/main/java/com/ichi2/anki/preferences/HeaderFragment.kt index 705bc31840a0..4a0cc2a7288f 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/preferences/HeaderFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/preferences/HeaderFragment.kt @@ -26,12 +26,19 @@ import com.bytehamster.lib.preferencesearch.SearchPreference import com.ichi2.anki.BuildConfig import com.ichi2.anki.R import com.ichi2.compat.CompatHelper +import com.ichi2.preferences.HeaderPreference import com.ichi2.utils.AdaptionUtil class HeaderFragment : PreferenceFragmentCompat() { + private var selectedHeaderPreference: HeaderPreference? = null + private var selectedHeaderPreferenceKey: String = DEFAULT_SELECTED_HEADER override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.preference_headers, rootKey) + selectedHeaderPreferenceKey = savedInstanceState?.getString(KEY_SELECTED_HEADER_PREF) ?: DEFAULT_SELECTED_HEADER + + highlightHeaderPreference(requirePreference(selectedHeaderPreferenceKey)) + requirePreference(R.string.pref_advanced_screen_key).apply { if (AdaptionUtil.isXiaomiRestrictedLearningDevice) { isVisible = false @@ -48,6 +55,33 @@ class HeaderFragment : PreferenceFragmentCompat() { ) } + private fun highlightHeaderPreference(headerPreference: HeaderPreference) { + if (!(activity as Preferences).hasLateralNavigation()) { + return + } + selectedHeaderPreference?.setHighlighted(false) + // highlight the newly selected header + selectedHeaderPreference = headerPreference.apply { + setHighlighted(true) + selectedHeaderPreferenceKey = this.key + } + } + + override fun onPreferenceTreeClick(preference: Preference): Boolean { + highlightHeaderPreference(preference as HeaderPreference) + return super.onPreferenceTreeClick(preference) + } + + override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + outState.putString(KEY_SELECTED_HEADER_PREF, selectedHeaderPreferenceKey) + } + + override fun onViewStateRestored(savedInstanceState: Bundle?) { + super.onViewStateRestored(savedInstanceState) + highlightHeaderPreference(requirePreference(selectedHeaderPreferenceKey)) + } + override fun onStart() { super.onStart() requireActivity().setTitle(R.string.settings) @@ -66,6 +100,9 @@ class HeaderFragment : PreferenceFragmentCompat() { } companion object { + private const val KEY_SELECTED_HEADER_PREF = "selected_header_pref" + private const val DEFAULT_SELECTED_HEADER = "generalScreen" + fun configureSearchBar(activity: AppCompatActivity, searchConfiguration: SearchConfiguration) { with(searchConfiguration) { setActivity(activity) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/preferences/Preferences.kt b/AnkiDroid/src/main/java/com/ichi2/anki/preferences/Preferences.kt index 102b10b54393..fa996d910d84 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/preferences/Preferences.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/preferences/Preferences.kt @@ -54,7 +54,7 @@ class Preferences : PreferenceFragmentCompat.OnPreferenceStartFragmentCallback, SearchPreferenceResultListener { - private fun hasLateralNavigation(): Boolean { + fun hasLateralNavigation(): Boolean { return findViewById(R.id.lateral_nav_container) != null } diff --git a/AnkiDroid/src/main/java/com/ichi2/preferences/HeaderPreference.kt b/AnkiDroid/src/main/java/com/ichi2/preferences/HeaderPreference.kt index 559869c067b6..b997acf748df 100644 --- a/AnkiDroid/src/main/java/com/ichi2/preferences/HeaderPreference.kt +++ b/AnkiDroid/src/main/java/com/ichi2/preferences/HeaderPreference.kt @@ -19,6 +19,8 @@ import android.content.Context import android.util.AttributeSet import androidx.core.content.withStyledAttributes import androidx.preference.Preference +import androidx.preference.PreferenceViewHolder +import com.google.android.material.color.MaterialColors import com.ichi2.anki.LanguageUtils import com.ichi2.anki.R @@ -33,6 +35,8 @@ constructor( defStyleAttr: Int = androidx.preference.R.attr.preferenceStyle, defStyleRes: Int = androidx.preference.R.style.Preference ) : Preference(context, attrs, defStyleAttr, defStyleRes) { + private var isHighlighted = false + private val highlightColor: Int = MaterialColors.getColor(context, R.attr.currentDeckBackgroundColor, 0) init { context.withStyledAttributes(attrs, R.styleable.HeaderPreference) { @@ -43,6 +47,18 @@ constructor( } } + override fun onBindViewHolder(holder: PreferenceViewHolder) { + super.onBindViewHolder(holder) + if (isHighlighted) { + holder.itemView.setBackgroundColor(highlightColor) + } + } + + fun setHighlighted(highlight: Boolean) { + isHighlighted = highlight + notifyChanged() + } + companion object { /** * Join [entries] with ` • ` as separator diff --git a/AnkiDroid/src/main/res/xml/preference_headers.xml b/AnkiDroid/src/main/res/xml/preference_headers.xml index 1693de0b9c02..2faf181b71fd 100644 --- a/AnkiDroid/src/main/res/xml/preference_headers.xml +++ b/AnkiDroid/src/main/res/xml/preference_headers.xml @@ -104,15 +104,15 @@ android:icon="@drawable/ic_tune_white" app:summaryEntries="@array/advanced_summary_entries"> - - + -