Bimba.git

commit af2053702af6d7725095d2e63a480512c714e91a

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

less search results flickering

%!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 7391042e6767d1b2b6820d472a9b7f3048a5582b..4ca8912121a11dbb067ac0aeda831b3a178fe863 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.kt
@@ -73,8 +73,6 @@         providerProxy = ProviderProxy(this)
         timetable = Timetable.getTimetable()
         NetworkStateReceiver.init(this)
 
-        getSuggestions()
-
         prepareFavourites()
 
         prepareListeners()
@@ -109,18 +107,7 @@
         searchView.addTextChangeListener(object : TextWatcher {
             override fun afterTextChanged(s: Editable?) {
                 if (searchView.isSearchEnabled) {
-                    searchView.clearSuggestions()
-                    searchView.updateLastSuggestions(listOf<GtfsSuggestion>())
-                    providerProxy.getSuggestions(s.toString()) { suggestions ->
-                        if(suggestions.isEmpty())
-                            suggestionsAdapter.addSuggestion(EmptySuggestion())
-                        suggestions.forEach {
-                            if (it.name.contains(s as CharSequence, true)) {
-                                suggestionsAdapter.addSuggestion(it)
-                            }
-                        }
-                        searchView.showSuggestionsList()
-                    }
+                    getSuggestions(s.toString())
                 }
             }
 
@@ -151,13 +138,26 @@             override fun onSearchStateChanged(enabled: Boolean) {
             }
 
             override fun onSearchConfirmed(text: CharSequence?) {
-                // todo re-search
-                println("OnSearchConfirmed")
+                getSuggestions(text as String)
             }
 
         })
     }
 
+    private fun getSuggestions(query: String = "") {
+        providerProxy.getSuggestions(query) { suggestions ->
+            if (!suggestionsAdapter.equals(suggestions)) {
+                if (suggestions.isEmpty()) {
+                    suggestionsAdapter.clearSuggestions()
+                    suggestionsAdapter.addSuggestion(EmptySuggestion())
+                } else {
+                    suggestionsAdapter.updateSuggestions(suggestions)
+                }
+                searchView.showSuggestionsList()
+            }
+        }
+    }
+
     override fun onSuggestionClickListener(suggestion: GtfsSuggestion) {
         val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
         var view = (context as DashActivity).currentFocus
@@ -283,14 +283,6 @@         favouritesList.adapter!!.notifyDataSetChanged()
         showError(drawer_layout, code, this)
     }
 
-    private fun getSuggestions() {
-        providerProxy.getSuggestions { suggestions ->
-            suggestions.forEach {
-                suggestionsAdapter.addSuggestion(it)
-            }
-        }
-    }
-
     private fun prepareListeners() {
         val filter = IntentFilter(TimetableDownloader.ACTION_DOWNLOADED)
         filter.addAction(VmService.ACTION_READY)
@@ -338,7 +330,7 @@         }
         Snackbar.make(findViewById(R.id.drawer_layout), message, Snackbar.LENGTH_LONG).show()
         if (result == TimetableDownloader.RESULT_FINISHED) {
             timetable = Timetable.getTimetable(this, true)
-            getSuggestions()
+            getSuggestions(searchView.text)
             showValidityInDrawer()
         }
     }




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 4183ab57cfb227b61c4c0875ac5c32131c617d38..a9fb15deb860b64e735f75e4bf3d3f67740e98fb 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
@@ -12,6 +12,7 @@ import android.widget.TextView
 import androidx.core.content.ContextCompat.getColor
 import ml.adamsprogs.bimba.R
 import ml.adamsprogs.bimba.getDrawable
+import ml.adamsprogs.bimba.models.suggestions.EmptySuggestion
 import ml.adamsprogs.bimba.models.suggestions.GtfsSuggestion
 import ml.adamsprogs.bimba.models.suggestions.LineSuggestion
 import ml.adamsprogs.bimba.models.suggestions.StopSuggestion
@@ -59,8 +60,14 @@         holder.icon.setImageDrawable(icon)
 
     }
 
+    fun updateSuggestions(newSuggestions: List<GtfsSuggestion>) {
+        suggestions = newSuggestions
+        suggestions_clone = suggestions
+        notifyDataSetChanged()
+    }
+
     operator fun contains(suggestion: GtfsSuggestion): Boolean {
-        return suggestion in suggestions || suggestion in suggestions_clone
+        return suggestion in suggestions //|| suggestion in suggestions_clone
     }
 
     inner class ViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {
@@ -71,5 +78,14 @@     }
 
     interface OnSuggestionClickListener {
         fun onSuggestionClickListener(suggestion: GtfsSuggestion)
+    }
+
+    fun equals(other: List<GtfsSuggestion>): Boolean {
+        if ((suggestions.containsAll(other) and other.containsAll(suggestions)))
+            return true
+        if (other.isEmpty())
+            if ((suggestions.isEmpty()) or (suggestions[0] is EmptySuggestion))
+                return true
+        return false
     }
 }