Commit 731f286b authored by Leonardo Aramaki's avatar Leonardo Aramaki

Make EmojiSuggestionsAdapter use TrieCompletionStrategy with a result threshold of 5 items

parent 6557738b
package chat.rocket.android.chatroom.adapter
import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
......@@ -25,6 +26,7 @@ class EmojiSuggestionsAdapter : SuggestionsAdapter<EmojiSuggestionViewHolder>(
class EmojiSuggestionViewHolder(view: View) : BaseSuggestionViewHolder(view) {
@SuppressLint("SetTextI18n")
override fun bind(item: SuggestionModel, itemClickListener: SuggestionsAdapter.ItemClickListener?) {
item as EmojiSuggestionUiModel
with(itemView) {
......
......@@ -18,7 +18,9 @@ class TrieCompletionStrategy : CompletionStrategy {
return item
}
override fun autocompleteItems(prefix: String) = trie.autocompleteItems(prefix)
override fun autocompleteItems(prefix: String): List<SuggestionModel> {
return trie.autocompleteItems(prefix)
}
override fun addAll(list: List<SuggestionModel>) {
items.addAll(list)
......
......@@ -34,10 +34,7 @@ internal class Trie {
val sanitizedWord = word.trim().toLowerCase()
var current = root
sanitizedWord.forEach { ch ->
val child = current.getChild(ch)
if (child == null) {
return false
}
val child = current.getChild(ch) ?: return false
current = child
}
if (current.isLeaf) {
......@@ -63,7 +60,7 @@ internal class Trie {
lastNode = lastNode?.getChild(ch)
if (lastNode == null) return emptyList()
}
return lastNode!!.getItems()
return lastNode!!.getItems().take(5).toList()
}
fun getCount() = count
......
package chat.rocket.android.suggestions.strategy.trie.data
import chat.rocket.android.suggestions.model.SuggestionModel
import kotlin.coroutines.experimental.buildSequence
internal class TrieNode(
internal var data: Char,
internal var parent: TrieNode? = null,
internal var isLeaf: Boolean = false,
internal var item: SuggestionModel? = null
) {
internal class TrieNode(internal var data: Char,
internal var parent: TrieNode? = null,
internal var isLeaf: Boolean = false,
internal var item: SuggestionModel? = null) {
val children = hashMapOf<Char, TrieNode>()
fun getChild(c: Char): TrieNode? {
......@@ -28,19 +32,17 @@ internal class TrieNode(internal var data: Char,
return list
}
class X : SuggestionModel("")
fun getItems(): Sequence<SuggestionModel> = buildSequence {
fun getItems(): List<SuggestionModel> {
val list = arrayListOf<SuggestionModel>()
if (isLeaf) {
list.add(item!!)
yield(item!!)
}
children.forEach { node ->
node.value.let {
list.addAll(it.getItems())
yieldAll(it.getItems())
}
}
return list
}
override fun toString(): String = if (parent == null) "" else "${parent.toString()}$data"
......
......@@ -11,7 +11,8 @@ abstract class SuggestionsAdapter<VH : BaseSuggestionViewHolder>(
val token: String,
val constraint: Int = CONSTRAINT_UNBOUND,
completionStrategy: CompletionStrategy? = null,
threshold: Int = MAX_RESULT_COUNT) : RecyclerView.Adapter<VH>() {
threshold: Int = MAX_RESULT_COUNT
) : RecyclerView.Adapter<VH>() {
private var itemType: Type? = null
private var itemClickListener: ItemClickListener? = null
......
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