- implement prefernce highlighting after settings search

This commit is contained in:
lmj0011 2020-08-28 12:52:30 -05:00
parent 437a34b5dc
commit 78072ad285
3 changed files with 49 additions and 4 deletions

View file

@ -1,6 +1,9 @@
package eu.kanade.tachiyomi.ui.setting package eu.kanade.tachiyomi.ui.setting
import android.animation.ArgbEvaluator
import android.animation.ValueAnimator
import android.content.Context import android.content.Context
import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import android.util.TypedValue import android.util.TypedValue
import android.view.ContextThemeWrapper import android.view.ContextThemeWrapper
@ -9,6 +12,7 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceController import androidx.preference.PreferenceController
import androidx.preference.PreferenceGroup
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import com.bluelinelabs.conductor.ControllerChangeHandler import com.bluelinelabs.conductor.ControllerChangeHandler
import com.bluelinelabs.conductor.ControllerChangeType import com.bluelinelabs.conductor.ControllerChangeType
@ -26,6 +30,7 @@ import uy.kohesive.injekt.api.get
abstract class SettingsController : PreferenceController() { abstract class SettingsController : PreferenceController() {
var preferenceKey: String? = null
val preferences: PreferencesHelper = Injekt.get() val preferences: PreferencesHelper = Injekt.get()
val scope = CoroutineScope(Job() + Dispatchers.Main) val scope = CoroutineScope(Job() + Dispatchers.Main)
@ -39,6 +44,24 @@ abstract class SettingsController : PreferenceController() {
return super.onCreateView(inflater, container, savedInstanceState) return super.onCreateView(inflater, container, savedInstanceState)
} }
override fun onAttach(view: View) {
super.onAttach(view)
preferenceKey?.let { prefKey ->
val adapter = listView.adapter
scrollToPreference(prefKey)
listView.post {
if (adapter is PreferenceGroup.PreferencePositionCallback) {
val pos = adapter.getPreferenceAdapterPosition(prefKey)
listView.findViewHolderForAdapterPosition(pos)?.let {
animatePreferenceHighlight(it.itemView)
}
}
}
}
}
override fun onDestroyView(view: View) { override fun onDestroyView(view: View) {
super.onDestroyView(view) super.onDestroyView(view)
untilDestroySubscriptions.unsubscribe() untilDestroySubscriptions.unsubscribe()
@ -58,6 +81,17 @@ abstract class SettingsController : PreferenceController() {
return ContextThemeWrapper(activity, tv.resourceId) return ContextThemeWrapper(activity, tv.resourceId)
} }
private fun animatePreferenceHighlight(view: View) {
val duration = 500L
val repeat = 2
val colorAnimation = ValueAnimator.ofObject(ArgbEvaluator(), Color.TRANSPARENT, Color.WHITE)
colorAnimation.duration = duration
colorAnimation.repeatCount = repeat
colorAnimation.addUpdateListener { animator -> view.setBackgroundColor(animator.animatedValue as Int) }
colorAnimation.reverse()
}
open fun getTitle(): String? { open fun getTitle(): String? {
return preferenceScreen?.title?.toString() return preferenceScreen?.title?.toString()
} }

View file

@ -94,12 +94,21 @@ object SettingsSearchHelper {
val summary = if (pref.summary != null) pref.summary.toString() else "" val summary = if (pref.summary != null) pref.summary.toString() else ""
val breadcrumbsStr = breadcrumbs + " > ${pref.title}" val breadcrumbsStr = breadcrumbs + " > ${pref.title}"
prefSearchResultList.add(SettingsSearchResult(title, summary, breadcrumbsStr, ctrl)) prefSearchResultList.add(
SettingsSearchResult(
key = pref.key,
title = title,
summary = summary,
breadcrumb = breadcrumbsStr,
searchController = ctrl
)
)
} }
} }
} }
data class SettingsSearchResult( data class SettingsSearchResult(
val key: String?,
val title: String, val title: String,
val summary: String, val summary: String,
val breadcrumb: String, val breadcrumb: String,

View file

@ -17,9 +17,11 @@ class SettingsSearchHolder(view: View, val adapter: SettingsSearchAdapter) :
init { init {
title_wrapper.setOnClickListener { title_wrapper.setOnClickListener {
adapter.getItem(bindingAdapterPosition)?.let { adapter.getItem(bindingAdapterPosition)?.let {
val ctrl = it.settingsSearchResult.searchController val ctrl = it.settingsSearchResult.searchController::class.createInstance()
// needs to be a new instance to avoid this error https://github.com/bluelinelabs/Conductor/issues/446 ctrl.preferenceKey = it.settingsSearchResult.key
adapter.titleClickListener.onTitleClick(ctrl::class.createInstance())
// must pass a new Controller instance to avoid this error https://github.com/bluelinelabs/Conductor/issues/446
adapter.titleClickListener.onTitleClick(ctrl)
} }
} }
} }