Skip to content

Commit

Permalink
highlight selected preference
Browse files Browse the repository at this point in the history
  • Loading branch information
SanjaySargam authored and david-allison committed May 23, 2024
1 parent 1246253 commit e605223
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<HeaderPreference>(selectedHeaderPreferenceKey))

requirePreference<Preference>(R.string.pref_advanced_screen_key).apply {
if (AdaptionUtil.isXiaomiRestrictedLearningDevice) {
isVisible = false
Expand All @@ -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<HeaderPreference>(selectedHeaderPreferenceKey))
}

override fun onStart() {
super.onStart()
requireActivity().setTitle(R.string.settings)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Preferences :
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback,
SearchPreferenceResultListener {

private fun hasLateralNavigation(): Boolean {
fun hasLateralNavigation(): Boolean {
return findViewById<FragmentContainerView>(R.id.lateral_nav_container) != null
}

Expand Down
16 changes: 16 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/preferences/HeaderPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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) {
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions AnkiDroid/src/main/res/xml/preference_headers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@
android:icon="@drawable/ic_tune_white"
app:summaryEntries="@array/advanced_summary_entries">
</com.ichi2.preferences.HeaderPreference>
<Preference
<com.ichi2.preferences.HeaderPreference
android:key="@string/pref_dev_options_screen_key"
android:title="@string/pref_cat_dev_options"
android:icon="@drawable/ic_code"
android:fragment="com.ichi2.anki.preferences.DevOptionsFragment"
app:isPreferenceVisible="false">
</Preference>
</com.ichi2.preferences.HeaderPreference>
<!-- About -->
<Preference
<com.ichi2.preferences.HeaderPreference
android:key="@string/about_screen_key"
android:icon="@drawable/ic_outline_info_24"
android:title="@string/pref_cat_about_title"
Expand Down

0 comments on commit e605223

Please sign in to comment.