From 56195434e75ce5f86fd2bb328453692f0fb6d846 Mon Sep 17 00:00:00 2001 From: inorichi Date: Fri, 12 Apr 2019 18:40:04 +0200 Subject: [PATCH] Add intent filter for external queries --- app/src/main/AndroidManifest.xml | 4 +++ .../CatalogueSearchController.kt | 10 +++--- .../global_search/CatalogueSearchPresenter.kt | 33 ++++++++++++++++++- .../kanade/tachiyomi/ui/main/MainActivity.kt | 17 ++++++++-- 4 files changed, 57 insertions(+), 7 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3e5f35a9b..7f4746834 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -33,6 +33,10 @@ + + + + diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/global_search/CatalogueSearchController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/global_search/CatalogueSearchController.kt index 77e23a6ba..a3f7f4ca2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/global_search/CatalogueSearchController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/global_search/CatalogueSearchController.kt @@ -18,8 +18,10 @@ import kotlinx.android.synthetic.main.catalogue_global_search_controller.* * This controller should only handle UI actions, IO actions should be done by [CatalogueSearchPresenter] * [CatalogueSearchCardAdapter.OnMangaClickListener] called when manga is clicked in global search */ -open class CatalogueSearchController(protected val initialQuery: String? = null) : - NucleusController(), +open class CatalogueSearchController( + protected val initialQuery: String? = null, + protected val extensionFilter: String? = null +) : NucleusController(), CatalogueSearchCardAdapter.OnMangaClickListener { /** @@ -60,7 +62,7 @@ open class CatalogueSearchController(protected val initialQuery: String? = null) * @return instance of [CatalogueSearchPresenter] */ override fun createPresenter(): CatalogueSearchPresenter { - return CatalogueSearchPresenter(initialQuery) + return CatalogueSearchPresenter(initialQuery, extensionFilter) } /** @@ -185,4 +187,4 @@ open class CatalogueSearchController(protected val initialQuery: String? = null) getHolder(source)?.setImage(manga) } -} \ No newline at end of file +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/global_search/CatalogueSearchPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/global_search/CatalogueSearchPresenter.kt index cba9dc3b2..b0cc97a62 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/global_search/CatalogueSearchPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/global_search/CatalogueSearchPresenter.kt @@ -5,6 +5,7 @@ import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault +import eu.kanade.tachiyomi.extension.ExtensionManager import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.SourceManager @@ -21,6 +22,7 @@ import rx.subjects.PublishSubject import timber.log.Timber import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get +import uy.kohesive.injekt.injectLazy /** * Presenter of [CatalogueSearchController] @@ -32,6 +34,7 @@ import uy.kohesive.injekt.api.get */ open class CatalogueSearchPresenter( val initialQuery: String? = "", + val initialExtensionFilter: String? = null, val sourceManager: SourceManager = Injekt.get(), val db: DatabaseHelper = Injekt.get(), val preferencesHelper: PreferencesHelper = Injekt.get() @@ -40,7 +43,7 @@ open class CatalogueSearchPresenter( /** * Enabled sources. */ - val sources by lazy { getEnabledSources() } + val sources by lazy { getSourcesToQuery() } /** * Query from the view. @@ -63,9 +66,16 @@ open class CatalogueSearchPresenter( */ private var fetchImageSubscription: Subscription? = null + private val extensionManager by injectLazy() + + private var extensionFilter: String? = null + override fun onCreate(savedState: Bundle?) { super.onCreate(savedState) + extensionFilter = savedState?.getString(CatalogueSearchPresenter::extensionFilter.name) ?: + initialExtensionFilter + // Perform a search with previous or initial state search(savedState?.getString(BrowseCataloguePresenter::query.name) ?: initialQuery.orEmpty()) } @@ -78,6 +88,7 @@ open class CatalogueSearchPresenter( override fun onSave(state: Bundle) { state.putString(BrowseCataloguePresenter::query.name, query) + state.putString(CatalogueSearchPresenter::extensionFilter.name, extensionFilter) super.onSave(state) } @@ -97,6 +108,26 @@ open class CatalogueSearchPresenter( .sortedBy { "(${it.lang}) ${it.name}" } } + private fun getSourcesToQuery(): List { + val filter = extensionFilter + val enabledSources = getEnabledSources() + if (filter.isNullOrEmpty()) { + return enabledSources + } + + val filterSources = extensionManager.installedExtensions + .filter { it.pkgName == filter } + .flatMap { it.sources } + .filter { it in enabledSources } + .filterIsInstance() + + if (filterSources.isEmpty()) { + return enabledSources + } + + return filterSources + } + /** * Creates a catalogue search item */ diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index 63d07429b..a1a5c93b6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -164,12 +164,21 @@ class MainActivity : BaseActivity() { //If the intent match the "standard" Android search intent // or the Google-specific search intent (triggered by saying or typing "search *query* on *Tachiyomi*" in Google Search/Google Assistant) - setSelectedDrawerItem(R.id.nav_drawer_catalogues) //Get the search query provided in extras, and if not null, perform a global search with it. - intent.getStringExtra(SearchManager.QUERY)?.also { query -> + val query = intent.getStringExtra(SearchManager.QUERY) + if (query != null && !query.isEmpty()) { + setSelectedDrawerItem(R.id.nav_drawer_catalogues) router.pushController(CatalogueSearchController(query).withFadeTransaction()) } } + INTENT_SEARCH -> { + val query = intent.getStringExtra(INTENT_SEARCH_QUERY) + val filter = intent.getStringExtra(INTENT_SEARCH_FILTER) + if (query != null && !query.isEmpty()) { + setSelectedDrawerItem(R.id.nav_drawer_catalogues) + router.pushController(CatalogueSearchController(query, filter).withFadeTransaction()) + } + } else -> return false } return true @@ -254,6 +263,10 @@ class MainActivity : BaseActivity() { const val SHORTCUT_CATALOGUES = "eu.kanade.tachiyomi.SHOW_CATALOGUES" const val SHORTCUT_DOWNLOADS = "eu.kanade.tachiyomi.SHOW_DOWNLOADS" const val SHORTCUT_MANGA = "eu.kanade.tachiyomi.SHOW_MANGA" + + const val INTENT_SEARCH = "eu.kanade.tachiyomi.SEARCH" + const val INTENT_SEARCH_QUERY = "query" + const val INTENT_SEARCH_FILTER = "filter" } }