Fix lots of trouble with the completion popup (resize, change mode, etc.) - next step

This commit is contained in:
Benoit Marty 2019-12-19 20:31:36 +01:00
parent c18be94986
commit 237b22df59
3 changed files with 20 additions and 104 deletions

View File

@ -1,94 +0,0 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.riotx.features.autocomplete
import android.content.Context
import android.database.DataSetObserver
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.airbnb.epoxy.EpoxyController
import com.otaliastudios.autocomplete.AutocompletePresenter
abstract class EpoxyAutocompletePresenter<T>(context: Context) : AutocompletePresenter<T>(context), AutocompleteClickListener<T> {
private var recyclerView: RecyclerView? = null
private var clicks: ClickProvider<T>? = null
private var observer: Observer? = null
override fun registerClickProvider(provider: ClickProvider<T>) {
this.clicks = provider
}
override fun registerDataSetObserver(observer: DataSetObserver) {
this.observer = Observer(observer)
}
override fun getView(): ViewGroup? {
recyclerView = RecyclerView(context).apply {
layoutManager = LinearLayoutManager(context)
setHasFixedSize(false)
adapter = providesController().adapter
observer?.let {
adapter?.registerAdapterDataObserver(it)
}
itemAnimator = null
}
return recyclerView
}
override fun onViewShown() {}
override fun onViewHidden() {
recyclerView?.adapter = null
recyclerView = null
observer = null
}
abstract fun providesController(): EpoxyController
protected fun dispatchLayoutChange() {
observer?.onChanged()
}
override fun onItemClick(t: T) {
clicks?.click(t)
}
private class Observer internal constructor(private val root: DataSetObserver) : RecyclerView.AdapterDataObserver() {
override fun onChanged() {
root.onChanged()
}
override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
root.onChanged()
}
override fun onItemRangeChanged(positionStart: Int, itemCount: Int, payload: Any?) {
root.onChanged()
}
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
root.onChanged()
}
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
root.onChanged()
}
}
}

View File

@ -17,21 +17,26 @@
package im.vector.riotx.features.autocomplete.command package im.vector.riotx.features.autocomplete.command
import android.content.Context import android.content.Context
import com.airbnb.epoxy.EpoxyController import androidx.recyclerview.widget.RecyclerView
import im.vector.riotx.features.autocomplete.EpoxyAutocompletePresenter import com.otaliastudios.autocomplete.RecyclerViewPresenter
import im.vector.riotx.features.autocomplete.AutocompleteClickListener
import im.vector.riotx.features.command.Command import im.vector.riotx.features.command.Command
import javax.inject.Inject import javax.inject.Inject
class AutocompleteCommandPresenter @Inject constructor(context: Context, class AutocompleteCommandPresenter @Inject constructor(context: Context,
private val controller: AutocompleteCommandController) : private val controller: AutocompleteCommandController) :
EpoxyAutocompletePresenter<Command>(context) { RecyclerViewPresenter<Command>(context), AutocompleteClickListener<Command> {
init { init {
controller.listener = this controller.listener = this
} }
override fun providesController(): EpoxyController { override fun instantiateAdapter(): RecyclerView.Adapter<*> {
return controller return controller.adapter
}
override fun onItemClick(t: Command) {
dispatchClick(t)
} }
override fun onQuery(query: CharSequence?) { override fun onQuery(query: CharSequence?) {

View File

@ -17,16 +17,17 @@
package im.vector.riotx.features.autocomplete.user package im.vector.riotx.features.autocomplete.user
import android.content.Context import android.content.Context
import com.airbnb.epoxy.EpoxyController import androidx.recyclerview.widget.RecyclerView
import com.airbnb.mvrx.Async import com.airbnb.mvrx.Async
import com.airbnb.mvrx.Success import com.airbnb.mvrx.Success
import com.otaliastudios.autocomplete.RecyclerViewPresenter
import im.vector.matrix.android.api.session.user.model.User import im.vector.matrix.android.api.session.user.model.User
import im.vector.riotx.features.autocomplete.EpoxyAutocompletePresenter import im.vector.riotx.features.autocomplete.AutocompleteClickListener
import javax.inject.Inject import javax.inject.Inject
class AutocompleteUserPresenter @Inject constructor(context: Context, class AutocompleteUserPresenter @Inject constructor(context: Context,
private val controller: AutocompleteUserController private val controller: AutocompleteUserController
) : EpoxyAutocompletePresenter<User>(context) { ) : RecyclerViewPresenter<User>(context), AutocompleteClickListener<User> {
var callback: Callback? = null var callback: Callback? = null
@ -34,8 +35,12 @@ class AutocompleteUserPresenter @Inject constructor(context: Context,
controller.listener = this controller.listener = this
} }
override fun providesController(): EpoxyController { override fun instantiateAdapter(): RecyclerView.Adapter<*> {
return controller return controller.adapter
}
override fun onItemClick(t: User) {
dispatchClick(t)
} }
override fun onQuery(query: CharSequence?) { override fun onQuery(query: CharSequence?) {