From f2d83e5ae6f01a2ec77ae82da4d45eb1b6d75cf9 Mon Sep 17 00:00:00 2001 From: Suphon Thanakornpakapong Date: Wed, 27 Oct 2021 18:48:30 +0700 Subject: [PATCH] Show as row when there's only one search result --- .../search/LawnchairAppSearchAlgorithm.kt | 25 +++++++++++-------- .../app/lawnchair/search/SearchAdapterItem.kt | 5 ++-- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lawnchair/src/app/lawnchair/search/LawnchairAppSearchAlgorithm.kt b/lawnchair/src/app/lawnchair/search/LawnchairAppSearchAlgorithm.kt index deb9b4c7d6..d5e2b3ca26 100644 --- a/lawnchair/src/app/lawnchair/search/LawnchairAppSearchAlgorithm.kt +++ b/lawnchair/src/app/lawnchair/search/LawnchairAppSearchAlgorithm.kt @@ -40,18 +40,27 @@ class LawnchairAppSearchAlgorithm(private val context: Context) : apps: MutableList, query: String ): ArrayList { - val results = if (useFuzzySearch) { + val appResults = if (useFuzzySearch) { fuzzySearch(apps, query) } else { normalSearch(apps, query) } + val results = mutableListOf() + if (appResults.size == 1) { + val app = appResults.first() + results.add(SearchAdapterItem.fromApp(0, app, normalBackground, true)) + } else { + results.addAll(appResults.mapIndexed { index, info -> + SearchAdapterItem.fromApp(index, info, iconBackground) + }) + } if (results.isEmpty()) { results.add(getEmptySearchItem(query)) } return ArrayList(LawnchairSearchAdapterProvider.decorateSearchResults(results)) } - private fun normalSearch(apps: List, query: String): MutableList { + private fun normalSearch(apps: List, query: String): List { // Do an intersection of the words in the query and each title, and filter out all the // apps that don't match all of the words in the query. val queryTextLower = query.lowercase(Locale.getDefault()) @@ -60,23 +69,17 @@ class LawnchairAppSearchAlgorithm(private val context: Context) : return apps.asSequence() .filter { StringMatcherUtility.matches(queryTextLower, it.title.toString(), matcher) } .take(MAX_RESULTS_COUNT) - .mapIndexed { index, info -> - SearchAdapterItem.fromApp(index, info, iconBackground) - } - .toCollection(ArrayList()) + .toList() } - private fun fuzzySearch(apps: List, query: String): MutableList { + private fun fuzzySearch(apps: List, query: String): List { val matches = FuzzySearch.extractSorted( query.lowercase(Locale.getDefault()), apps, { it.title.toString() }, WeightedRatio(), 65 ) return matches.take(MAX_RESULTS_COUNT) - .mapIndexed { index, match -> - SearchAdapterItem.fromApp(index, match.referent, iconBackground) - } - .toCollection(ArrayList()) + .map { it.referent } } private fun resolveMarketSearchActivity(): ComponentKey? { diff --git a/lawnchair/src/app/lawnchair/search/SearchAdapterItem.kt b/lawnchair/src/app/lawnchair/search/SearchAdapterItem.kt index 4be6868640..9602ba4233 100644 --- a/lawnchair/src/app/lawnchair/search/SearchAdapterItem.kt +++ b/lawnchair/src/app/lawnchair/search/SearchAdapterItem.kt @@ -31,13 +31,14 @@ data class SearchAdapterItem( fun fromApp( pos: Int, appInfo: AppInfo, - background: SearchItemBackground + background: SearchItemBackground, + asRow: Boolean = false ): SearchAdapterItem { val componentName = appInfo.componentName val user = appInfo.user val target = SearchTargetCompat.Builder( SearchTargetCompat.RESULT_TYPE_APPLICATION, - LayoutType.ICON_SINGLE_VERTICAL_TEXT, + if (asRow) LayoutType.ICON_HORIZONTAL_TEXT else LayoutType.ICON_SINGLE_VERTICAL_TEXT , ComponentKey(componentName, user).toString() ) .setPackageName(componentName.packageName)