From d53719b79e97b908fbdc2fcf1923d896c0cce858 Mon Sep 17 00:00:00 2001 From: mpm11011 Date: Sun, 30 Aug 2020 16:35:15 -0400 Subject: [PATCH] Limiting search to settings menu only, localized breadcrumb string, and code cleanup after code review. --- app/build.gradle | 2 - .../tachiyomi/ui/more/MoreController.kt | 36 ------------- .../ui/setting/SettingsMainController.kt | 2 +- .../settingssearch/SettingsSearchAdapter.kt | 5 +- .../SettingsSearchController.kt | 52 +++---------------- .../settingssearch/SettingsSearchHelper.kt | 32 +++++++++--- .../settingssearch/SettingsSearchHolder.kt | 5 +- .../settingssearch/SettingsSearchItem.kt | 18 ------- .../settingssearch/SettingsSearchPresenter.kt | 45 +++------------- 9 files changed, 46 insertions(+), 151 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index ac92590e6..0223a0054 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -273,11 +273,9 @@ dependencies { testImplementation "org.robolectric:shadows-multidex:$robolectric_version" 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.9' - implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version" diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/more/MoreController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/more/MoreController.kt index f45b6ae7c..7f357e727 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/more/MoreController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/more/MoreController.kt @@ -2,9 +2,6 @@ package eu.kanade.tachiyomi.ui.more import android.content.Context import android.util.AttributeSet -import android.view.Menu -import android.view.MenuInflater -import androidx.appcompat.widget.SearchView import androidx.preference.Preference import androidx.preference.PreferenceScreen import eu.kanade.tachiyomi.R @@ -18,7 +15,6 @@ import eu.kanade.tachiyomi.ui.category.CategoryController import eu.kanade.tachiyomi.ui.download.DownloadController import eu.kanade.tachiyomi.ui.setting.SettingsController import eu.kanade.tachiyomi.ui.setting.SettingsMainController -import eu.kanade.tachiyomi.ui.setting.settingssearch.SettingsSearchController import eu.kanade.tachiyomi.util.preference.add import eu.kanade.tachiyomi.util.preference.iconRes import eu.kanade.tachiyomi.util.preference.iconTint @@ -30,11 +26,6 @@ import eu.kanade.tachiyomi.util.preference.switchPreference import eu.kanade.tachiyomi.util.preference.titleRes import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.openInBrowser -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 import rx.android.schedulers.AndroidSchedulers import uy.kohesive.injekt.injectLazy @@ -149,33 +140,6 @@ class MoreController : } } - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { - // Inflate menu - inflater.inflate(R.menu.settings_main, menu) - - // Initialize search option. - val searchItem = menu.findItem(R.id.action_search) - val searchView = searchItem.actionView as SearchView - searchView.maxWidth = Int.MAX_VALUE - - // Change hint to show global search. - searchView.queryHint = applicationContext?.getString(R.string.action_search_settings) - - // Create query listener which opens the global search view. - searchView.queryTextEvents() - .filterIsInstance() - .onEach { - performSettingsSearch(it.queryText.toString()) - } - .launchIn(scope) - } - - private fun performSettingsSearch(query: String) { - router.pushController( - SettingsSearchController(query).withFadeTransaction() - ) - } - companion object { private const val URL_HELP = "https://tachiyomi.org/help/" } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsMainController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsMainController.kt index 71cd5537d..4aebe5a92 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsMainController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsMainController.kt @@ -115,7 +115,7 @@ class SettingsMainController : SettingsController() { private fun performSettingsSearch(query: String) { router.pushController( - SettingsSearchController(query).withFadeTransaction() + SettingsSearchController().withFadeTransaction() ) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchAdapter.kt index 75cfae28c..a0a58e620 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchAdapter.kt @@ -64,9 +64,8 @@ class SettingsSearchAdapter(val controller: SettingsSearchController) : */ private fun restoreHolderState(holder: RecyclerView.ViewHolder) { val key = "holder_${holder.bindingAdapterPosition}" - val holderState = bundle.getSparseParcelableArray(key) - if (holderState != null) { - holder.itemView.restoreHierarchyState(holderState) + bundle.getSparseParcelableArray(key)?.let { + holder.itemView.restoreHierarchyState(it) bundle.remove(key) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchController.kt index 44b06597e..6648e340e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchController.kt @@ -17,13 +17,10 @@ import eu.kanade.tachiyomi.ui.setting.SettingsController /** * This controller shows and manages the different search result in settings search. - * This controller should only handle UI actions, IO actions should be done by [SettingsSearchPresenter] - * [SettingsSearchAdapter.WhatListener] called when preference is clicked in settings search + * [SettingsSearchAdapter.OnTitleClickListener] called when preference is clicked in settings search */ -open class SettingsSearchController( - protected val initialQuery: String? = null, - protected val extensionFilter: String? = null -) : NucleusController(), +class SettingsSearchController : + NucleusController(), SettingsSearchAdapter.OnTitleClickListener { /** @@ -57,7 +54,7 @@ open class SettingsSearchController( * @return instance of [SettingsSearchPresenter] */ override fun createPresenter(): SettingsSearchPresenter { - return SettingsSearchPresenter(initialQuery, extensionFilter) + return SettingsSearchPresenter() } /** @@ -108,11 +105,6 @@ open class SettingsSearchController( }) } - /** - * Called when the view is created - * - * @param view view of controller - */ override fun onViewCreated(view: View) { super.onViewCreated(view) @@ -138,44 +130,14 @@ open class SettingsSearchController( adapter?.onRestoreInstanceState(savedViewState) } - /** - * Returns the view holder for the given preference. - * - * @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 -// -// adapter.allBoundViewHolders.forEach { holder -> -// val item = adapter.getItem(holder.bindingAdapterPosition) -// if (item != null && pref.key == item.pref.key) { -// return holder as SettingsSearchHolder -// } -// } -// -// return null -// } - /** * returns a list of `SettingsSearchItem` to be shown as search results */ fun getResultSet(query: String? = null): List { val list = mutableListOf() - 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 - } + if (!query.isNullOrBlank()) { + SettingsSearchHelper.getFilteredResults(query) .forEach { list.add(SettingsSearchItem(it, null)) } @@ -197,6 +159,6 @@ open class SettingsSearchController( * Opens a catalogue with the given search. */ override fun onTitleClick(ctrl: SettingsController) { - router.replaceTopController(ctrl.withFadeTransaction()) + router.pushController(ctrl.withFadeTransaction()) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchHelper.kt index 527e1dc78..6331bdcd6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchHelper.kt @@ -2,11 +2,11 @@ package eu.kanade.tachiyomi.ui.setting.settingssearch import android.annotation.SuppressLint import android.content.Context +import android.content.res.Resources import androidx.preference.Preference import androidx.preference.PreferenceCategory import androidx.preference.PreferenceGroup import androidx.preference.PreferenceManager -import eu.kanade.tachiyomi.ui.more.AboutController import eu.kanade.tachiyomi.ui.setting.SettingsAdvancedController import eu.kanade.tachiyomi.ui.setting.SettingsBackupController import eu.kanade.tachiyomi.ui.setting.SettingsBrowseController @@ -18,6 +18,7 @@ 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 eu.kanade.tachiyomi.util.system.isLTR import kotlin.reflect.KClass import kotlin.reflect.full.createInstance @@ -38,8 +39,7 @@ object SettingsSearchHelper { SettingsParentalControlsController::class, SettingsReaderController::class, SettingsSecurityController::class, - SettingsTrackingController::class, - AboutController::class + SettingsTrackingController::class ) /** @@ -63,13 +63,23 @@ object SettingsSearchHelper { } } + fun getFilteredResults(query: String): List { + return 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 + } + } + /** * 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}" + val breadcrumbsStr = addLocalizedBreadcrumb(breadcrumbs, "${pref.title}") for (x in 0 until pref.preferenceCount) { val subPref = pref.getPreference(x) @@ -77,7 +87,7 @@ object SettingsSearchHelper { } } is PreferenceCategory -> { - val breadcrumbsStr = breadcrumbs + " > ${pref.title}" + val breadcrumbsStr = addLocalizedBreadcrumb(breadcrumbs, "${pref.title}") for (x in 0 until pref.preferenceCount) { val subPref = pref.getPreference(x) @@ -88,7 +98,7 @@ object SettingsSearchHelper { // Is an actual preference val title = pref.title.toString() val summary = if (pref.summary != null) pref.summary.toString() else "" - val breadcrumbsStr = breadcrumbs + " > ${pref.title}" + val breadcrumbsStr = addLocalizedBreadcrumb(breadcrumbs, "${pref.title}") prefSearchResultList.add( SettingsSearchResult( @@ -103,6 +113,16 @@ object SettingsSearchHelper { } } + private fun addLocalizedBreadcrumb(path: String, node: String): String { + return if (Resources.getSystem().isLTR) { + // This locale reads left to right. + "$path > $node" + } else { + // This locale reads right to left. + "$node < $path" + } + } + data class SettingsSearchResult( val key: String?, val title: String, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchHolder.kt index 7313fc9fe..469310eca 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchHolder.kt @@ -3,7 +3,10 @@ package eu.kanade.tachiyomi.ui.setting.settingssearch import android.view.View import eu.kanade.tachiyomi.ui.base.holder.BaseFlexibleViewHolder import kotlin.reflect.full.createInstance -import kotlinx.android.synthetic.main.settings_search_controller_card.* +import kotlinx.android.synthetic.main.settings_search_controller_card.search_result_pref_breadcrumb +import kotlinx.android.synthetic.main.settings_search_controller_card.search_result_pref_summary +import kotlinx.android.synthetic.main.settings_search_controller_card.search_result_pref_title +import kotlinx.android.synthetic.main.settings_search_controller_card.title_wrapper /** * Holder that binds the [SettingsSearchItem] containing catalogue cards. diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchItem.kt index f555abf9f..cb6417472 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchItem.kt @@ -16,11 +16,6 @@ import eu.kanade.tachiyomi.R class SettingsSearchItem(val settingsSearchResult: SettingsSearchHelper.SettingsSearchResult, val results: List?) : AbstractFlexibleItem() { - /** - * Set view. - * - * @return id of view - */ override fun getLayoutRes(): Int { return R.layout.settings_search_controller_card } @@ -34,9 +29,6 @@ class SettingsSearchItem(val settingsSearchResult: SettingsSearchHelper.Settings return SettingsSearchHolder(view, adapter as SettingsSearchAdapter) } - /** - * Bind item to view. - */ override fun bindViewHolder( adapter: FlexibleAdapter>, holder: SettingsSearchHolder, @@ -46,11 +38,6 @@ class SettingsSearchItem(val settingsSearchResult: SettingsSearchHelper.Settings holder.bind(this) } - /** - * Used to check if two items are equal. - * - * @return items are equal? - */ override fun equals(other: Any?): Boolean { if (other is SettingsSearchItem) { return settingsSearchResult == settingsSearchResult @@ -58,11 +45,6 @@ class SettingsSearchItem(val settingsSearchResult: SettingsSearchHelper.Settings return false } - /** - * Return hash code of item. - * - * @return hashcode - */ override fun hashCode(): Int { return settingsSearchResult.hashCode() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchPresenter.kt index 8be02aea1..e3946eed9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/settingssearch/SettingsSearchPresenter.kt @@ -3,30 +3,16 @@ package eu.kanade.tachiyomi.ui.setting.settingssearch import android.os.Bundle import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper -import eu.kanade.tachiyomi.extension.ExtensionManager import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter -import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourcePresenter -import rx.Subscription import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get -import uy.kohesive.injekt.injectLazy /** * Presenter of [SettingsSearchController] * Function calls should be done from here. UI calls should be done from the controller. - * - * @param sourceManager manages the different sources. - * @param db manages the database calls. - * @param preferences manages the preference calls. */ -open class SettingsSearchPresenter( - val initialQuery: String? = "", - val initialExtensionFilter: String? = null, - val sourceManager: SourceManager = Injekt.get(), - val db: DatabaseHelper = Injekt.get(), - val preferences: PreferencesHelper = Injekt.get() -) : BasePresenter() { +open class SettingsSearchPresenter : BasePresenter() { /** * Query from the view. @@ -34,36 +20,17 @@ open class SettingsSearchPresenter( var query = "" private set - /** - * Fetches the different sources by user settings. - */ - private var fetchSourcesSubscription: Subscription? = null - - /** - * Subscription for fetching images of manga. - */ - private var fetchImageSubscription: Subscription? = null - - private val extensionManager by injectLazy() - - private var extensionFilter: String? = null + val sourceManager: SourceManager = Injekt.get() + val db: DatabaseHelper = Injekt.get() + val preferences: PreferencesHelper = Injekt.get() override fun onCreate(savedState: Bundle?) { super.onCreate(savedState) - - extensionFilter = savedState?.getString(SettingsSearchPresenter::extensionFilter.name) - ?: initialExtensionFilter - } - - override fun onDestroy() { - fetchSourcesSubscription?.unsubscribe() - fetchImageSubscription?.unsubscribe() - super.onDestroy() + query = savedState?.getString(SettingsSearchPresenter::query.name) ?: "" // TODO - Some way to restore previous query? } override fun onSave(state: Bundle) { - state.putString(BrowseSourcePresenter::query.name, query) - state.putString(SettingsSearchPresenter::extensionFilter.name, extensionFilter) + state.putString(SettingsSearchPresenter::query.name, query) super.onSave(state) } }