Skip to content

Commit

Permalink
feat(search): Prioritize apps starting with query (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
khwolf authored Dec 21, 2023
1 parent 381ac23 commit ad30277
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,27 @@ class AppDrawerAdapter(
this.updateDisplayedApps()
}

private fun onlyFirstStringStartsWith(first: String, second: String, query: String) : Boolean {
return first.startsWith(query, true) and !second.startsWith(query, true);
}

@SuppressLint("NotifyDataSetChanged")
private fun updateDisplayedApps() {
filteredApps = apps.filter { app ->
regex.replace(app.displayName, "").contains(filterQuery, ignoreCase = true)
}.toList()
}.toList().sortedWith(
Comparator<UnlauncherApp>{
a, b -> when {
// if an app's name starts with the query prefer it
onlyFirstStringStartsWith(a.displayName, b.displayName, filterQuery) -> -1
onlyFirstStringStartsWith(b.displayName, a.displayName, filterQuery) -> 1
// if both or none start with the query sort in normal oder
a.displayName > b.displayName -> 1
a.displayName < b.displayName -> -1
else -> 0
}
}
)
notifyDataSetChanged()
}

Expand Down

0 comments on commit ad30277

Please sign in to comment.