Skip to content
Snippets Groups Projects
Commit acde6228 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Improved autocomplete

parent 931fa0fb
Branches
Tags
No related merge requests found
......@@ -25,7 +25,7 @@ import de.kuschku.quasseldroid_ng.util.helper.visibleIf
class AutoCompleteAdapter(
lifecycleOwner: LifecycleOwner,
liveData: LiveData<List<AutoCompleteItem>?>,
liveData: LiveData<Pair<String, List<AutoCompleteItem>>?>,
runInBackground: (() -> Unit) -> Any,
runOnUiThread: (Runnable) -> Any,
private val clickListener: ((String) -> Unit)? = null
......@@ -34,10 +34,11 @@ class AutoCompleteAdapter(
init {
liveData.observe(
lifecycleOwner, Observer { it: List<AutoCompleteItem>? ->
lifecycleOwner, Observer { it: Pair<String, List<AutoCompleteItem>>? ->
runInBackground {
val list = it ?: emptyList()
val old: List<AutoCompleteItem> = data
val word = it?.first ?: ""
val list = it?.second ?: emptyList()
val old: List<AutoCompleteItem> = if (word.length >= 3) list else emptyList()
val new: List<AutoCompleteItem> = list
val result = DiffUtil.calculateDiff(
object : DiffUtil.Callback() {
......@@ -50,16 +51,13 @@ class AutoCompleteAdapter(
old[oldItemPosition] == new[newItemPosition]
}, true
)
runOnUiThread(
Runnable {
runOnUiThread(Runnable {
data.clear()
data.addAll(new)
result.dispatchUpdatesTo(this@AutoCompleteAdapter)
})
}
)
}
}
)
})
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = when (viewType) {
......
......@@ -304,7 +304,7 @@ class ChatActivity : ServiceBoundActivity(), SharedPreferences.OnSharedPreferenc
val previous = autocompletionState
if (!originalWord.second.isEmpty()) {
val autoCompletedWords = viewModel.autoCompleteData.value.orEmpty()
val autoCompletedWords = viewModel.autoCompleteData.value?.second.orEmpty()
if (previous != null && lastWord.value.first == previous.originalWord && lastWord.value.second.start == previous.range.start) {
val previousIndex = autoCompletedWords.indexOf(previous.completion)
val autoCompletedWord = if (previousIndex != -1) {
......
......@@ -197,16 +197,16 @@ class QuasselViewModel : ViewModel() {
val lastWord = MutableLiveData<Observable<Pair<String, IntRange>>>()
val autoCompleteData: LiveData<List<AutoCompleteAdapter.AutoCompleteItem>?> = session.zip(
val autoCompleteData: LiveData<Pair<String, List<AutoCompleteAdapter.AutoCompleteItem>>?> = session.zip(
buffer, lastWord
).switchMapRx { (session, id, lastWordWrapper) ->
lastWordWrapper
.distinctUntilChanged()
.debounce(16, TimeUnit.MILLISECONDS)
.debounce(300, TimeUnit.MILLISECONDS)
.switchMap { lastWord ->
val bufferSyncer = session?.bufferSyncer
val bufferInfo = bufferSyncer?.bufferInfo(id)
if (bufferSyncer != null && lastWord.second.length >= 3) {
if (bufferSyncer != null) {
bufferSyncer.liveBufferInfos().switchMap { infos ->
if (bufferInfo?.type?.hasFlag(
Buffer_Type.ChannelBuffer
......@@ -276,30 +276,32 @@ class QuasselViewModel : ViewModel() {
val ignoredStartingCharacters = charArrayOf(
'-', '_', '[', ']', '{', '}', '|', '`', '^', '.', '\\'
)
list
.filter {
Pair(
lastWord.first,
list.filter {
it.name.trimStart(*ignoredStartingCharacters)
.startsWith(
lastWord.first.trimStart(*ignoredStartingCharacters),
ignoreCase = true
)
}.sorted()
)
}
}
} else {
Observable.just(
emptyList()
Pair(lastWord.first, emptyList())
)
}
} else {
Observable.just(
emptyList()
Pair(lastWord.first, emptyList())
)
}
}
} else {
Observable.just(
emptyList()
Pair(lastWord.first, emptyList())
)
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment