Commit 906fcb27 authored by Lucio Maciel's avatar Lucio Maciel

Some improvements on autocomplete speed

parent b405a032
......@@ -29,6 +29,9 @@ abstract class SuggestionsAdapter<VH : BaseSuggestionViewHolder>(
notifyDataSetChanged()
}
private var currentCount = 0;
private var currentItems = listOf<SuggestionModel>()
init {
setHasStableIds(true)
}
......@@ -41,10 +44,10 @@ abstract class SuggestionsAdapter<VH : BaseSuggestionViewHolder>(
holder.bind(getItem(position), itemClickListener)
}
override fun getItemCount() = strategy.autocompleteItems(currentTerm).size
override fun getItemCount() = currentCount
private fun getItem(position: Int): SuggestionModel {
return strategy.autocompleteItems(currentTerm)[position]
return currentItems[position]
}
/**
......@@ -58,12 +61,15 @@ abstract class SuggestionsAdapter<VH : BaseSuggestionViewHolder>(
fun autocomplete(newTerm: String) {
this.currentTerm = newTerm.toLowerCase().trim()
currentItems = strategy.autocompleteItems(currentTerm)
currentCount = currentItems.size
}
fun addItems(list: List<SuggestionModel>) {
strategy.addAll(list)
// Since we've just added new items we should check for possible new completion suggestions.
strategy.autocompleteItems(currentTerm)
//strategy.autocompleteItems(currentTerm)
autocomplete(currentTerm)
notifyDataSetChanged()
}
......
......@@ -10,6 +10,7 @@ import android.text.TextWatcher
import android.transition.Slide
import android.transition.TransitionManager
import android.util.AttributeSet
import android.util.Log
import android.view.Gravity
import android.view.View
import android.widget.EditText
......@@ -24,6 +25,7 @@ import chat.rocket.android.suggestions.model.SuggestionModel
import chat.rocket.android.suggestions.ui.SuggestionsAdapter.Companion.CONSTRAINT_BOUND_TO_START
import java.lang.ref.WeakReference
import java.util.concurrent.atomic.AtomicInteger
import kotlin.system.measureTimeMillis
// This is a special index that means we're not at an autocompleting state.
private const val NO_STATE_INDEX = 0
......@@ -101,22 +103,32 @@ class SuggestionsView : FrameLayout, TextWatcher {
return
}
val prefixEndIndex = this.editor?.get()?.selectionStart ?: NO_STATE_INDEX
if (prefixEndIndex == NO_STATE_INDEX || prefixEndIndex < completionOffset.get()) return
val prefix = s.subSequence(completionOffset.get(), this.editor?.get()?.selectionStart
?: completionOffset.get()).toString()
recyclerView.adapter?.let {
it as SuggestionsAdapter
// we need to look up only after the '@'
it.autocomplete(prefix)
val cacheMap = localProvidersByToken[it.token]
if (cacheMap != null && cacheMap[prefix] != null) {
it.addItems(cacheMap[prefix]!!)
} else {
// fetch more suggestions from an external source if any
externalProvidersByToken[it.token]?.invoke(prefix)
}
if (completionOffset.get() == NO_STATE_INDEX) {
return
}
measureTimeMillis {
val prefixEndIndex = this.editor?.get()?.selectionStart ?: NO_STATE_INDEX
if (prefixEndIndex == NO_STATE_INDEX || prefixEndIndex < completionOffset.get()) return
val prefix = s.subSequence(completionOffset.get(), this.editor?.get()?.selectionStart
?: completionOffset.get()).toString()
recyclerView.adapter?.also {
it as SuggestionsAdapter
// we need to look up only after the '@'
measureTimeMillis { it.autocomplete(prefix) }.let { time ->
Log.d("SuggestionsView", "autocomplete($prefix) in $time ms")
}
val cacheMap = localProvidersByToken[it.token]
if (cacheMap != null && cacheMap[prefix] != null) {
if (it.itemCount == 0) {
it.addItems(cacheMap[prefix]!!)
}
} else {
// fetch more suggestions from an external source if any
externalProvidersByToken[it.token]?.invoke(prefix)
}
}
}.let { Log.d("SuggestionsView", "whole prefix in $it ms") }
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment