- implement searchable settings

- `SettingsController.setupPreferenceScreen` must return a PreferenceScreen
This commit is contained in:
lmj0011 2020-08-19 09:28:29 -05:00
parent 22518f173f
commit 8c82b766e3
24 changed files with 218 additions and 99 deletions

View file

@ -202,7 +202,6 @@ dependencies {
// Preferences
implementation 'com.github.tfcporciuncula:flow-preferences:1.3.0'
implementation 'com.github.ByteHamster:SearchPreference:v1.0.3'
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
// Model View Presenter
final nucleus_version = '3.0.0'
@ -276,6 +275,7 @@ dependencies {
testImplementation "org.robolectric:shadows-play-services:$robolectric_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$BuildPluginsVersion.KOTLIN"
implementation "org.jetbrains.kotlin:kotlin-reflect:$BuildPluginsVersion.KOTLIN"
final coroutines_version = '1.3.8'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"

View file

@ -13,7 +13,7 @@ import uy.kohesive.injekt.api.get
class ExtensionFilterController : SettingsController() {
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.label_extensions
val activeLangs = preferences.enabledLanguages().get()

View file

@ -24,7 +24,7 @@ class SourceFilterController : SettingsController() {
private val onlineSources by lazy { Injekt.get<SourceManager>().getOnlineSources() }
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.label_sources
// Get the list of active language codes.
@ -46,7 +46,7 @@ class SourceFilterController : SettingsController() {
// Create a preference group and set initial state and change listener
switchPreferenceCategory {
preferenceScreen.addPreference(this)
this@apply.addPreference(this)
title = LocaleHelper.getSourceDisplayName(lang, context)
isPersistent = false
if (lang in activeLangsCodes) {

View file

@ -41,7 +41,7 @@ class AboutController : SettingsController() {
private val isUpdaterEnabled = BuildConfig.INCLUDE_UPDATER
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.pref_category_about
preference {

View file

@ -38,7 +38,7 @@ class MoreController :
private var isDownloading: Boolean = false
private var downloadQueueSize: Int = 0
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.label_more
val tintColor = context.getResourceColor(R.attr.colorAccent)

View file

@ -41,7 +41,7 @@ class SettingsAdvancedController : SettingsController() {
private val db: DatabaseHelper by injectLazy()
@SuppressLint("BatteryLife")
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.pref_category_advanced
switchPreference {

View file

@ -49,7 +49,7 @@ class SettingsBackupController : SettingsController() {
requestPermissionsSafe(arrayOf(WRITE_EXTERNAL_STORAGE), 500)
}
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.backup
preference {

View file

@ -12,7 +12,7 @@ import eu.kanade.tachiyomi.util.preference.titleRes
class SettingsBrowseController : SettingsController() {
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.browse
preferenceCategory {

View file

@ -50,7 +50,7 @@ abstract class SettingsController : PreferenceController() {
setupPreferenceScreen(screen)
}
abstract fun setupPreferenceScreen(screen: PreferenceScreen): Any?
abstract fun setupPreferenceScreen(screen: PreferenceScreen): PreferenceScreen
private fun getThemedContext(): Context {
val tv = TypedValue()

View file

@ -40,7 +40,7 @@ class SettingsDownloadController : SettingsController() {
private val db: DatabaseHelper by injectLazy()
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.pref_category_downloads
preference {

View file

@ -24,7 +24,7 @@ import kotlinx.coroutines.flow.launchIn
class SettingsGeneralController : SettingsController() {
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.pref_category_general
intListPreference {

View file

@ -40,7 +40,7 @@ class SettingsLibraryController : SettingsController() {
private val db: DatabaseHelper = Injekt.get()
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.pref_category_library
val dbCategories = db.getCategories().executeAsBlocking()

View file

@ -7,6 +7,7 @@ import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction
import eu.kanade.tachiyomi.ui.setting.settingssearch.SettingsSearchController
import eu.kanade.tachiyomi.ui.setting.settingssearch.SettingsSearchHelper
import eu.kanade.tachiyomi.util.preference.iconRes
import eu.kanade.tachiyomi.util.preference.iconTint
import eu.kanade.tachiyomi.util.preference.onClick
@ -21,7 +22,9 @@ import reactivecircus.flowbinding.appcompat.queryTextEvents
class SettingsMainController : SettingsController() {
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
SettingsSearchHelper.initPreferenceSearchResultCollection(context, preferenceManager)
titleRes = R.string.label_settings
val tintColor = context.getResourceColor(R.attr.colorAccent)
@ -106,8 +109,10 @@ class SettingsMainController : SettingsController() {
// Create query listener which opens the global search view.
searchView.queryTextEvents()
.filterIsInstance<QueryTextEvent.QuerySubmitted>()
.onEach { performSettingsSearch(it.queryText.toString()) }
.filterIsInstance<QueryTextEvent.QueryChanged>()
.onEach {
performSettingsSearch(it.queryText.toString())
}
.launchIn(scope)
}

View file

@ -13,7 +13,7 @@ import eu.kanade.tachiyomi.util.preference.titleRes
class SettingsParentalControlsController : SettingsController() {
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.pref_category_parental_controls
listPreference {

View file

@ -19,7 +19,7 @@ import kotlinx.coroutines.flow.launchIn
class SettingsReaderController : SettingsController() {
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.pref_category_reader
intListPreference {

View file

@ -14,7 +14,7 @@ import kotlinx.coroutines.flow.launchIn
class SettingsSecurityController : SettingsController() {
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.pref_category_security
if (BiometricManager.from(context).canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) {

View file

@ -31,7 +31,7 @@ class SettingsTrackingController :
private val trackManager: TrackManager by injectLazy()
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.pref_category_tracking
switchPreference {

View file

@ -3,9 +3,9 @@ package eu.kanade.tachiyomi.ui.setting.settingssearch
import android.os.Bundle
import android.os.Parcelable
import android.util.SparseArray
import androidx.preference.Preference
import androidx.recyclerview.widget.RecyclerView
import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.kanade.tachiyomi.ui.setting.SettingsController
/**
* Adapter that holds the search cards.
@ -72,7 +72,7 @@ class SettingsSearchAdapter(val controller: SettingsSearchController) :
}
interface OnTitleClickListener {
fun onTitleClick(pref: Preference)
fun onTitleClick(ctrl: SettingsController)
}
private companion object {

View file

@ -8,18 +8,13 @@ import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.SearchView
import androidx.preference.Preference
import androidx.recyclerview.widget.LinearLayoutManager
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.databinding.SettingsSearchControllerBinding
import eu.kanade.tachiyomi.ui.base.controller.NucleusController
import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction
import eu.kanade.tachiyomi.ui.setting.SettingsController
import eu.kanade.tachiyomi.ui.setting.SettingsControllerFactory
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import reactivecircus.flowbinding.appcompat.QueryTextEvent
import reactivecircus.flowbinding.appcompat.queryTextEvents
/**
* This controller shows and manages the different search result in settings search.
@ -83,10 +78,13 @@ open class SettingsSearchController(
val searchView = searchItem.actionView as SearchView
searchView.maxWidth = Int.MAX_VALUE
searchItem.expandActionView()
setItems(getResultSet())
searchItem.setOnActionExpandListener(object : MenuItem.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
searchView.onActionViewExpanded() // Required to show the query in the view
searchView.setQuery(presenter.query, false)
setItems(getResultSet())
return true
}
@ -95,14 +93,19 @@ open class SettingsSearchController(
}
})
searchView.queryTextEvents()
.filterIsInstance<QueryTextEvent.QuerySubmitted>()
.onEach {
presenter.search(it.queryText.toString())
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
searchItem.collapseActionView()
setTitle() // Update toolbar title
setTitle(query) // Update toolbar title
setItems(getResultSet(query))
return false
}
.launchIn(scope)
override fun onQueryTextChange(newText: String?): Boolean {
setItems(getResultSet(newText))
return false
}
})
}
/**
@ -141,17 +144,44 @@ open class SettingsSearchController(
* @param pref used to find holder containing source
* @return the holder of the preference or null if it's not bound.
*/
private fun getHolder(pref: Preference): SettingsSearchHolder? {
val adapter = adapter ?: return null
// private fun getHolder(pref: Preference): SettingsSearchHolder? {
// val adapter = adapter ?: return null
//
// adapter.allBoundViewHolders.forEach { holder ->
// val item = adapter.getItem(holder.bindingAdapterPosition)
// if (item != null && pref.key == item.pref.key) {
// return holder as SettingsSearchHolder
// }
// }
//
// return null
// }
adapter.allBoundViewHolders.forEach { holder ->
val item = adapter.getItem(holder.bindingAdapterPosition)
if (item != null && pref.key == item.pref.key) {
return holder as SettingsSearchHolder
/**
* returns a list of `SettingsSearchItem` to be shown as search results
*/
fun getResultSet(query: String? = null): List<SettingsSearchItem> {
val list = mutableListOf<SettingsSearchItem>()
if (query.isNullOrBlank()) {
SettingsSearchHelper.prefSearchResultList.forEach {
list.add(SettingsSearchItem(it, null))
}
} else {
SettingsSearchHelper.prefSearchResultList
.filter {
val inTitle = it.title.contains(query, true)
val inSummary = it.summary.contains(query, true)
val inBreadcrumb = it.breadcrumb.contains(query, true)
return@filter inTitle || inSummary || inBreadcrumb
}
.forEach {
list.add(SettingsSearchItem(it, null))
}
}
return null
return list
}
/**
@ -166,12 +196,7 @@ open class SettingsSearchController(
/**
* Opens a catalogue with the given search.
*/
override fun onTitleClick(pref: Preference) {
// TODO - These asserts will be the death of me, fix them.
for (ctrl in this!!.controllers!!) {
if (ctrl.findPreference(pref.key) != null) {
router.pushController(ctrl.withFadeTransaction())
}
}
override fun onTitleClick(ctrl: SettingsController) {
router.replaceTopController(ctrl.withFadeTransaction())
}
}

View file

@ -0,0 +1,106 @@
package eu.kanade.tachiyomi.ui.setting.settingssearch
import android.content.Context
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceGroup
import androidx.preference.PreferenceManager
import eu.kanade.tachiyomi.ui.browse.extension.ExtensionFilterController
import eu.kanade.tachiyomi.ui.browse.source.SourceFilterController
import eu.kanade.tachiyomi.ui.more.AboutController
import eu.kanade.tachiyomi.ui.more.MoreController
import eu.kanade.tachiyomi.ui.setting.SettingsAdvancedController
import eu.kanade.tachiyomi.ui.setting.SettingsBackupController
import eu.kanade.tachiyomi.ui.setting.SettingsBrowseController
import eu.kanade.tachiyomi.ui.setting.SettingsController
import eu.kanade.tachiyomi.ui.setting.SettingsDownloadController
import eu.kanade.tachiyomi.ui.setting.SettingsGeneralController
import eu.kanade.tachiyomi.ui.setting.SettingsLibraryController
import eu.kanade.tachiyomi.ui.setting.SettingsParentalControlsController
import eu.kanade.tachiyomi.ui.setting.SettingsReaderController
import eu.kanade.tachiyomi.ui.setting.SettingsSecurityController
import eu.kanade.tachiyomi.ui.setting.SettingsTrackingController
import kotlin.reflect.KClass
import kotlin.reflect.full.createInstance
object SettingsSearchHelper {
var prefSearchResultList: MutableList<SettingsSearchResult> = mutableListOf()
private set
/**
* All subclasses of `SettingsController` should be listed here, in order to have their preferences searchable.
*/
private val settingControllersList: List<KClass<out SettingsController>> = listOf(
SettingsAdvancedController::class,
SettingsBackupController::class,
SettingsBrowseController::class,
SettingsDownloadController::class,
SettingsGeneralController::class,
SettingsLibraryController::class,
SettingsParentalControlsController::class,
SettingsReaderController::class,
SettingsSecurityController::class,
SettingsTrackingController::class,
ExtensionFilterController::class,
SourceFilterController::class,
AboutController::class,
MoreController::class
)
/**
* Must be called to populate `prefSearchResultList`
*/
fun initPreferenceSearchResultCollection(context: Context, preferenceManager: PreferenceManager) {
prefSearchResultList.clear()
settingControllersList.forEach { kClass ->
val ctrl = kClass.createInstance()
val settingsPrefScreen = ctrl.setupPreferenceScreen(preferenceManager.createPreferenceScreen(context))
val prefCount = settingsPrefScreen.preferenceCount
for (i in 0 until prefCount) {
val rootPref = settingsPrefScreen.getPreference(i)
if (rootPref.title == null) continue // no title, not a preference. (note: only info notes appear to not have titles)
getSettingSearchResult(ctrl, rootPref, "${settingsPrefScreen.title}")
}
}
}
/**
* Extracts the data needed from a `Preference` to create a `SettingsSearchResult`, and then adds it to `prefSearchResultList`
*/
private fun getSettingSearchResult(ctrl: SettingsController, pref: Preference, breadcrumbs: String = "") {
when (pref) {
is PreferenceGroup -> {
val breadcrumbsStr = breadcrumbs + " > ${pref.title}"
for (x in 0 until pref.preferenceCount) {
val subPref = pref.getPreference(x)
getSettingSearchResult(ctrl, subPref, breadcrumbsStr) // recursion
}
}
is PreferenceCategory -> {
val breadcrumbsStr = breadcrumbs + " > ${pref.title}"
for (x in 0 until pref.preferenceCount) {
val subPref = pref.getPreference(x)
getSettingSearchResult(ctrl, subPref, breadcrumbsStr) // recursion
}
}
else -> {
// Is an actual preference
val title = pref.title.toString()
val summary = if (pref.summary != null) pref.summary.toString() else ""
val breadcrumbsStr = breadcrumbs + " > ${pref.title}"
prefSearchResultList.add(SettingsSearchResult(title, summary, breadcrumbsStr, ctrl))
}
}
}
data class SettingsSearchResult(
val title: String,
val summary: String,
val breadcrumb: String,
val searchController: SettingsController
)
}

View file

@ -2,8 +2,8 @@ package eu.kanade.tachiyomi.ui.setting.settingssearch
import android.view.View
import eu.kanade.tachiyomi.ui.base.holder.BaseFlexibleViewHolder
import kotlinx.android.synthetic.main.settings_search_controller_card.setting
import kotlinx.android.synthetic.main.settings_search_controller_card.title_wrapper
import kotlin.reflect.full.createInstance
import kotlinx.android.synthetic.main.settings_search_controller_card.*
/**
* Holder that binds the [SettingsSearchItem] containing catalogue cards.
@ -14,17 +14,12 @@ import kotlinx.android.synthetic.main.settings_search_controller_card.title_wrap
class SettingsSearchHolder(view: View, val adapter: SettingsSearchAdapter) :
BaseFlexibleViewHolder(view, adapter) {
/**
* Adapter containing preference from search results.
*/
private val settingsAdapter = SettingsSearchAdapter(adapter.controller)
private var lastBoundResults: List<SettingsSearchItem>? = null
init {
title_wrapper.setOnClickListener {
adapter.getItem(bindingAdapterPosition)?.let {
adapter.titleClickListener.onTitleClick(it.pref)
val ctrl = it.settingsSearchResult.searchController
// needs to be a new instance to avoid this error https://github.com/bluelinelabs/Conductor/issues/446
adapter.titleClickListener.onTitleClick(ctrl::class.createInstance())
}
}
}
@ -35,17 +30,8 @@ class SettingsSearchHolder(view: View, val adapter: SettingsSearchAdapter) :
* @param item item of card.
*/
fun bind(item: SettingsSearchItem) {
val preference = item.pref
val results = item.results
val titlePrefix = if (item.highlighted) "" else ""
// Set Title with country code if available.
setting.text = titlePrefix + preference.key
if (results !== lastBoundResults) {
settingsAdapter.updateDataSet(results)
lastBoundResults = results
}
search_result_pref_title.text = item.settingsSearchResult.title
search_result_pref_summary.text = item.settingsSearchResult.summary
search_result_pref_breadcrumb.text = item.settingsSearchResult.breadcrumb
}
}

View file

@ -1,7 +1,6 @@
package eu.kanade.tachiyomi.ui.setting.settingssearch
import android.view.View
import androidx.preference.Preference
import androidx.recyclerview.widget.RecyclerView
import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem
@ -13,9 +12,8 @@ import eu.kanade.tachiyomi.R
*
* @param pref the source for the search results.
* @param results the search results.
* @param highlighted whether this search item should be highlighted/marked in the catalogue search view.
*/
class SettingsSearchItem(val pref: Preference, val results: List<SettingsSearchItem>?, val highlighted: Boolean = false) :
class SettingsSearchItem(val settingsSearchResult: SettingsSearchHelper.SettingsSearchResult, val results: List<SettingsSearchItem>?) :
AbstractFlexibleItem<SettingsSearchHolder>() {
/**
@ -55,7 +53,7 @@ class SettingsSearchItem(val pref: Preference, val results: List<SettingsSearchI
*/
override fun equals(other: Any?): Boolean {
if (other is SettingsSearchItem) {
return pref.key == other.pref.key
return settingsSearchResult == settingsSearchResult
}
return false
}
@ -66,6 +64,6 @@ class SettingsSearchItem(val pref: Preference, val results: List<SettingsSearchI
* @return hashcode
*/
override fun hashCode(): Int {
return pref.key.toInt()
return settingsSearchResult.hashCode()
}
}

View file

@ -10,46 +10,41 @@
android:id="@+id/title_wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="@dimen/search_result_wrapper_margin_start"
android:layout_marginTop="@dimen/search_result_wrapper_margin_top"
android:layout_marginBottom="@dimen/search_result_wrapper_margin_bottom"
android:background="?attr/selectableItemBackground">
<TextView
android:id="@+id/setting"
android:id="@+id/search_result_pref_title"
style="@style/TextAppearance.Regular.SubHeading"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="@dimen/material_component_text_fields_padding_above_and_below_label"
app:layout_constraintBottom_toTopOf="@+id/location"
app:layout_constraintEnd_toStartOf="@+id/goto_icon"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:text="Title" />
<TextView
android:id="@+id/location"
android:id="@+id/search_result_pref_summary"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/search_result_pref_title"
tools:text="Summary" />
<TextView
android:id="@+id/search_result_pref_breadcrumb"
style="@style/TextAppearance.Regular.Caption"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:padding="@dimen/material_component_text_fields_padding_above_and_below_label"
app:layout_constraintEnd_toStartOf="@+id/goto_icon"
app:layout_constraintStart_toStartOf="@+id/setting"
app:layout_constraintTop_toBottomOf="@+id/setting"
tools:text="Location" />
<ImageView
android:id="@+id/goto_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/label_more"
android:padding="@dimen/material_component_text_fields_padding_above_and_below_label"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_arrow_forward_24dp"
app:tint="?android:attr/textColorPrimary" />
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/search_result_pref_summary"
tools:text="Location" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -23,4 +23,8 @@
<dimen name="text_small_body">14sp</dimen>
<dimen name="bottom_sheet_width">0dp</dimen>
<dimen name="search_result_wrapper_margin_start">50dp</dimen>
<dimen name="search_result_wrapper_margin_top">20dp</dimen>
<dimen name="search_result_wrapper_margin_bottom">20dp</dimen>
</resources>