Merge pull request #20 from alrajdev/simple_fuzzy_search

add simple fuzzy search
This commit is contained in:
ottop 2024-11-20 00:21:51 +02:00 committed by GitHub
commit 01863dcfd1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 60 additions and 3 deletions

View file

@ -955,6 +955,14 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
updateMenu(updatedApps) updateMenu(updatedApps)
} else { } else {
isJobActive = false isJobActive = false
val fuzzyPattern = if(sharedPreferenceManager.isFuzzySearchEnabled()) {
stringUtils.getFuzzyPattern(cleanQuery)
}
else {
null
}
updatedApps.forEach { updatedApps.forEach {
val cleanItemText = stringUtils.cleanString(sharedPreferenceManager.getAppName( val cleanItemText = stringUtils.cleanString(sharedPreferenceManager.getAppName(
it.first.applicationInfo.packageName, it.first.applicationInfo.packageName,
@ -962,7 +970,10 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
packageManager.getApplicationLabel(it.first.applicationInfo) packageManager.getApplicationLabel(it.first.applicationInfo)
).toString()) ).toString())
if (cleanItemText != null) { if (cleanItemText != null) {
if (cleanItemText.contains(cleanQuery, ignoreCase = true)) { if (
(fuzzyPattern != null && cleanItemText.contains(fuzzyPattern)) ||
(cleanItemText.contains(cleanQuery, ignoreCase = true))
) {
newFilteredApps.add(it) newFilteredApps.add(it)
} }
} }

View file

@ -114,6 +114,12 @@ class GestureAppsFragment(private val direction: String) : Fragment(),
if (cleanQuery.isNullOrEmpty()) { if (cleanQuery.isNullOrEmpty()) {
newFilteredApps.addAll(updatedApps) newFilteredApps.addAll(updatedApps)
} else { } else {
val fuzzyPattern = if(sharedPreferenceManager.isFuzzySearchEnabled()) {
stringUtils.getFuzzyPattern(cleanQuery)
}
else {
null
}
updatedApps.forEach { updatedApps.forEach {
val cleanItemText = stringUtils.cleanString(sharedPreferenceManager.getAppName( val cleanItemText = stringUtils.cleanString(sharedPreferenceManager.getAppName(
it.first.applicationInfo.packageName, it.first.applicationInfo.packageName,
@ -121,7 +127,10 @@ class GestureAppsFragment(private val direction: String) : Fragment(),
requireContext().packageManager.getApplicationLabel(it.first.applicationInfo) requireContext().packageManager.getApplicationLabel(it.first.applicationInfo)
).toString()) ).toString())
if (cleanItemText != null) { if (cleanItemText != null) {
if (cleanItemText.contains(cleanQuery, ignoreCase = true)) { if (
(fuzzyPattern != null && cleanItemText.contains(fuzzyPattern)) ||
(cleanItemText.contains(cleanQuery, ignoreCase = true))
) {
newFilteredApps.add(it) newFilteredApps.add(it)
} }
} }

View file

@ -114,6 +114,12 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener, Ti
if (cleanQuery.isNullOrEmpty()) { if (cleanQuery.isNullOrEmpty()) {
newFilteredApps.addAll(updatedApps) newFilteredApps.addAll(updatedApps)
} else { } else {
val fuzzyPattern = if(sharedPreferenceManager.isFuzzySearchEnabled()) {
stringUtils.getFuzzyPattern(cleanQuery)
}
else {
null
}
updatedApps.forEach { updatedApps.forEach {
val cleanItemText = stringUtils.cleanString(sharedPreferenceManager.getAppName( val cleanItemText = stringUtils.cleanString(sharedPreferenceManager.getAppName(
it.first.applicationInfo.packageName, it.first.applicationInfo.packageName,
@ -121,7 +127,10 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener, Ti
requireContext().packageManager.getApplicationLabel(it.first.applicationInfo) requireContext().packageManager.getApplicationLabel(it.first.applicationInfo)
).toString()) ).toString())
if (cleanItemText != null) { if (cleanItemText != null) {
if (cleanItemText.contains(cleanQuery, ignoreCase = true)) { if (
(fuzzyPattern != null && cleanItemText.contains(fuzzyPattern)) ||
(cleanItemText.contains(cleanQuery, ignoreCase = true))
) {
newFilteredApps.add(it) newFilteredApps.add(it)
} }
} }

View file

@ -211,6 +211,10 @@ class SharedPreferenceManager (private val context: Context) {
return preferences.getString("searchSize", "medium") return preferences.getString("searchSize", "medium")
} }
fun isFuzzySearchEnabled(): Boolean {
return preferences.getBoolean("fuzzySearchEnabled", false)
}
fun getAppSpacing(): Int? { fun getAppSpacing(): Int? {
return preferences.getString("appSpacing", "20")?.toInt() return preferences.getString("appSpacing", "20")?.toInt()
} }

View file

@ -23,4 +23,19 @@ class StringUtils {
view.movementMethod = LinkMovementMethod.getInstance() view.movementMethod = LinkMovementMethod.getInstance()
} }
/** Create a Regex pattern for simple Fuzzy search
*
* Example:
* 'cl' will create 'c.*l' which matches 'Clock', 'Calendar'
* 'msg' will create 'm.*s.*g' which matches 'Messages'
* 'cmr' will create 'c.*m.*r' which matches 'Camera'
*/
fun getFuzzyPattern(query: String): Regex {
val pattern = query
.flatMap { char -> listOf(char.toString(), ".*") }
// remove the last unnecessary .* since the char itself is sufficient
.dropLast(1)
.joinToString(separator = "")
return Regex(pattern, RegexOption.IGNORE_CASE)
}
} }

View file

@ -124,6 +124,7 @@
<string name="enable_search">Näytä Hakupalkki</string> <string name="enable_search">Näytä Hakupalkki</string>
<string name="search_alignment">Hakupalkin Sijainti</string> <string name="search_alignment">Hakupalkin Sijainti</string>
<string name="search_size">Hakupalkin Koko</string> <string name="search_size">Hakupalkin Koko</string>
<string name="enable_fuzzy_search">Ota sumea haku käyttöön</string>
<string name="automatically_open_keyboard">Avaa Näppäimistö Automaattisesti</string> <string name="automatically_open_keyboard">Avaa Näppäimistö Automaattisesti</string>
<string name="automatic_app_opening">Avaa Hakutulos Automaattisesti</string> <string name="automatic_app_opening">Avaa Hakutulos Automaattisesti</string>
<string name="auto_launch_summary">Avaa sovellus automaattisesti kun se on viimeinen hakutulos</string> <string name="auto_launch_summary">Avaa sovellus automaattisesti kun se on viimeinen hakutulos</string>

View file

@ -126,6 +126,7 @@
<string name="enable_search">Enable Search</string> <string name="enable_search">Enable Search</string>
<string name="search_alignment">Search Alignment</string> <string name="search_alignment">Search Alignment</string>
<string name="search_size">Search Size</string> <string name="search_size">Search Size</string>
<string name="enable_fuzzy_search">Enable Fuzzy Search</string>
<string name="automatically_open_keyboard">Automatically Open Keyboard</string> <string name="automatically_open_keyboard">Automatically Open Keyboard</string>
<string name="automatic_app_opening">Automatic App Opening</string> <string name="automatic_app_opening">Automatic App Opening</string>
<string name="auto_launch_summary">Automatically launch an app when it\'s the last search result</string> <string name="auto_launch_summary">Automatically launch an app when it\'s the last search result</string>

View file

@ -70,6 +70,13 @@
app:key="searchSize" app:key="searchSize"
app:title="@string/search_size" app:title="@string/search_size"
app:useSimpleSummaryProvider="true" /> app:useSimpleSummaryProvider="true" />
<SwitchPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:dependency="searchEnabled"
android:defaultValue="false"
android:title="@string/enable_fuzzy_search"
app:key="fuzzySearchEnabled" />
<SwitchPreference <SwitchPreference
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"