Commit 07a613d3 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Add adapter check before accepting a new suggestion provider

parent c101b39a
...@@ -22,6 +22,7 @@ import android.widget.FrameLayout ...@@ -22,6 +22,7 @@ import android.widget.FrameLayout
import chat.rocket.android.R import chat.rocket.android.R
import chat.rocket.android.widget.autocompletion.model.SuggestionModel import chat.rocket.android.widget.autocompletion.model.SuggestionModel
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicInteger
/** /**
...@@ -31,6 +32,7 @@ private const val NO_STATE_INDEX = 0 ...@@ -31,6 +32,7 @@ private const val NO_STATE_INDEX = 0
class SuggestionsView : FrameLayout, TextWatcher { class SuggestionsView : FrameLayout, TextWatcher {
private val recyclerView: RecyclerView private val recyclerView: RecyclerView
private val registeredTokens = CopyOnWriteArrayList<String>()
// Maps tokens to their respective adapters. // Maps tokens to their respective adapters.
private val adaptersByToken = hashMapOf<String, SuggestionsAdapter<out BaseSuggestionViewHolder>>() private val adaptersByToken = hashMapOf<String, SuggestionsAdapter<out BaseSuggestionViewHolder>>()
private val externalProvidersByToken = hashMapOf<String, ((query: String) -> Unit)>() private val externalProvidersByToken = hashMapOf<String, ((query: String) -> Unit)>()
...@@ -160,6 +162,9 @@ class SuggestionsView : FrameLayout, TextWatcher { ...@@ -160,6 +162,9 @@ class SuggestionsView : FrameLayout, TextWatcher {
} }
fun addSuggestionProviderAction(token: String, provider: (query: String) -> Unit): SuggestionsView { fun addSuggestionProviderAction(token: String, provider: (query: String) -> Unit): SuggestionsView {
if (adaptersByToken[token] == null) {
throw IllegalStateException("token \"$token\" suggestion provider added without adapter")
}
externalProvidersByToken.getOrPut(token, { provider }) externalProvidersByToken.getOrPut(token, { provider })
return this return this
} }
......
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