Эх сурвалжийг харах

feat(search): Prioritize apps starting with query (#165)

Klaus-Hendrik Wolf 2 жил өмнө
parent
commit
ad30277441

+ 17 - 1
app/src/main/java/com/sduduzog/slimlauncher/adapters/AppDrawerAdapter.kt

@@ -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()
     }