Przeglądaj źródła

menu categories sorting fix - merge fix

milan jurkulák 2 lat temu
rodzic
commit
ba7b46565d

+ 1 - 0
src/main/kotlin/eu/mjdev/desktop/components/appsmenu/AppsMenu.kt

@@ -66,6 +66,7 @@ fun AppsMenu(
     val appCategories by appCategories.collectAsState(emptyList())
     val items by rememberComputed(searchTextState.value, category) {
         when {
+            // todo fuzzy sort
             searchTextState.trimIsNotEmpty() -> allApps.filter { app ->
                 app.desktopData.contains(searchTextState.value)
             }

+ 6 - 2
src/main/kotlin/eu/mjdev/desktop/data/Category.kt

@@ -1,7 +1,8 @@
 package eu.mjdev.desktop.data
 
 data class Category(
-    val name : String
+    val name: String,
+    val priority: Int = parsePriority(name),
 ) {
     override fun toString(): String {
         return name
@@ -10,6 +11,9 @@ data class Category(
     companion object {
         const val UNCATEGORIZED: String = "Uncategorized"
 
-        val Empty: Category = Category(UNCATEGORIZED)
+        val Empty: Category = Category(UNCATEGORIZED, -1)
+
+        private fun parsePriority(name: String): Int =
+            if (name.contentEquals(UNCATEGORIZED, true)) -1 else 1
     }
 }

+ 11 - 12
src/main/kotlin/eu/mjdev/desktop/provider/AppsProvider.kt

@@ -15,8 +15,6 @@ import eu.mjdev.desktop.extensions.Custom.lines
 import eu.mjdev.desktop.extensions.Custom.sortedByName
 import eu.mjdev.desktop.extensions.Custom.text
 import eu.mjdev.desktop.extensions.Custom.textAsLocale
-import eu.mjdev.desktop.extensions.Custom.trimIsEmpty
-import eu.mjdev.desktop.extensions.Custom.trimStartsWith
 import eu.mjdev.desktop.helpers.system.Shell
 import eu.mjdev.desktop.provider.DesktopProvider.Companion.LocalDesktop
 import kotlinx.coroutines.CoroutineScope
@@ -146,13 +144,13 @@ class AppsProvider(
     }
 
     val appCategories = flowBlock {
-        allApps.flatMap { app ->
-            app.categories.map { c ->
-                provideCategory(c)
-            }.distinct().sorted()
-        }.map { c ->
+        allApps.asSequence().flatMap { app ->
+            app.categories.map { c -> provideCategory(c) }
+        }.distinct().sorted().map { c ->
             Category(c)
-        }
+        }.sortedByDescending { c ->
+            c.priority
+        }.toList()
     }.flowOn(Dispatchers.IO)
 
     val favoriteApps: Flow<List<App>> = flowBlock {
@@ -171,9 +169,10 @@ class AppsProvider(
     }.flowOn(Dispatchers.IO)
 
     // todo languages
-    fun provideCategory(category: String): String {
-        return if (
-            category.trimIsEmpty() || category.trimStartsWith("X-", true)
-        ) "Uncategorized" else category
+    fun provideCategory(name: String): String {
+        return name.trim().let { n ->
+            if (n.isEmpty() || n.startsWith("X-", true)) Category.UNCATEGORIZED
+            else name
+        }
     }
 }