Bimba.git

commit 5946e4a5438bbcdf58d8ca981a15414d84f02ec6

Author: Adam Pioterek <adam.pioterek@protonmail.ch>

sorting search results by similarity & departures with onStop

%!v(PANIC=String method: strings: negative Repeat count)


diff --git a/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt
index 4ca8912121a11dbb067ac0aeda831b3a178fe863..db824235e54f7397eb1438d21b5fdd06a252512e 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt
@@ -151,7 +151,7 @@                 if (suggestions.isEmpty()) {
                     suggestionsAdapter.clearSuggestions()
                     suggestionsAdapter.addSuggestion(EmptySuggestion())
                 } else {
-                    suggestionsAdapter.updateSuggestions(suggestions)
+                    suggestionsAdapter.updateSuggestions(suggestions, query)
                 }
                 searchView.showSuggestionsList()
             }




diff --git a/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
index cda5767a48380ef82399f1c84cf44fa818d7ccfa..84301031315bd5576bb5771f81dff3248510976f 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
@@ -172,7 +172,7 @@             adapter.departures = fullDepartures[(dateSpinner.selectedItem as ServiceAdapter.RowItem).service]
         } else {
             val now = Calendar.getInstance()
             val seconds = now.secondsAfterMidnight()
-            adapter.departures = this.departures.flatMap { it.value }.sortedBy { it.timeTill(seconds) }  // todo sorted by also onStop
+            adapter.departures = this.departures.flatMap { it.value }.sortedBy { (if (it.onStop) 0 else 1) * 172800 + it.timeTill(seconds) }  // todo sorted by also onStop
         }
         adapter.notifyDataSetChanged()
     }




diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/adapters/SuggestionsAdapter.kt b/app/src/main/java/ml/adamsprogs/bimba/models/adapters/SuggestionsAdapter.kt
index 67f5dfd4134c978aa78c284f9c0d22d7975294d7..6408a4506cde6c5d2fdf7a84a6024a78c5699881 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/models/adapters/SuggestionsAdapter.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/models/adapters/SuggestionsAdapter.kt
@@ -58,10 +58,17 @@         holder.icon.setImageDrawable(icon)
 
     }
 
-    fun updateSuggestions(newSuggestions: List<GtfsSuggestion>) {
-        suggestions = newSuggestions
+    fun updateSuggestions(newSuggestions: List<GtfsSuggestion>, query: String) {
+        suggestions = sort(newSuggestions, query).take(6)
         suggestions_clone = suggestions
         notifyDataSetChanged()
+    }
+
+    private fun sort(suggestions: List<GtfsSuggestion>, query: String): List<GtfsSuggestion> {
+        val r = Regex(query, RegexOption.IGNORE_CASE)
+        return suggestions.sortedBy {
+            (r.find(it.name)?.range?.start?.toString()?.padStart(128, '0') ?: "")+it.name
+        }
     }
 
     operator fun contains(suggestion: GtfsSuggestion): Boolean {




diff --git a/todo b/todo
index f5c02d57fbfe088bad8d14a191cad8931ff919b9..1e4199ff4600d6e80f16fef0c2d104e03b001001 100644
--- a/todo
+++ b/todo
@@ -11,7 +11,7 @@ - departures retry
 x search results flickering
 - favourite cache
 v in a moment
-t departures sort (timeTill, onStop) : StopActivity
-t sort and limit search results
+v departures sort (timeTill, onStop) : StopActivity
+v sort and limit search results
 - PEKA info
 - check update (only in APK builds)