Skip to content

Commit

Permalink
highlight selected preference
Browse files Browse the repository at this point in the history
  • Loading branch information
SanjaySargam committed May 16, 2024
1 parent bc58dce commit 5f5c0b4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,29 @@ import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentContainerView
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.bytehamster.lib.preferencesearch.SearchConfiguration
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 = KEY_SELECTED_HEADER_PREF
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preference_headers, rootKey)

if (savedInstanceState != null) {
selectedHeaderPreferenceKey = savedInstanceState.getString(KEY_SELECTED_HEADER_PREF).toString()
}

highlightHeaderPreference(requirePreference<HeaderPreference>(selectedHeaderPreferenceKey))

requirePreference<Preference>(R.string.pref_advanced_screen_key).apply {
if (AdaptionUtil.isXiaomiRestrictedLearningDevice) {
isVisible = false
Expand All @@ -48,6 +58,35 @@ class HeaderFragment : PreferenceFragmentCompat() {
)
}

private fun highlightHeaderPreference(headerPreference: HeaderPreference) {
if (!hasLateralNavigation()) {
return
}
selectedHeaderPreference?.setHighlighted(false)
selectedHeaderPreference = headerPreference
selectedHeaderPreference?.setHighlighted(true)
selectedHeaderPreferenceKey = headerPreference.key
}

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

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 +105,7 @@ class HeaderFragment : PreferenceFragmentCompat() {
}

companion object {
private const val KEY_SELECTED_HEADER_PREF = "generalScreen"
fun configureSearchBar(activity: AppCompatActivity, searchConfiguration: SearchConfiguration) {
with(searchConfiguration) {
setActivity(activity)
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 5f5c0b4

Please sign in to comment.