scroll recents carouse to start when item added/moved to start (#7120)

This commit is contained in:
Nikita Fedrunov 2022-09-14 22:55:01 +02:00 committed by GitHub
parent 7631c7ac9f
commit 1d3c191153
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

1
changelog.d/6776.bugfix Normal file
View File

@ -0,0 +1 @@
[App Layout] Recents carousel now scrolled to first position when new item added to or moved to this position

View File

@ -18,6 +18,7 @@ package im.vector.app.features.home.room.list.home.header
import android.content.res.Resources import android.content.res.Resources
import android.util.TypedValue import android.util.TypedValue
import androidx.recyclerview.widget.RecyclerView
import com.airbnb.epoxy.Carousel import com.airbnb.epoxy.Carousel
import com.airbnb.epoxy.CarouselModelBuilder import com.airbnb.epoxy.CarouselModelBuilder
import com.airbnb.epoxy.EpoxyController import com.airbnb.epoxy.EpoxyController
@ -44,6 +45,25 @@ class HomeRoomsHeadersController @Inject constructor(
var recentsRoomListener: RoomListListener? = null var recentsRoomListener: RoomListListener? = null
var invitesClickListener: (() -> Unit)? = null var invitesClickListener: (() -> Unit)? = null
private var carousel: Carousel? = null
private val carouselAdapterObserver = object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
if (toPosition == 0 || fromPosition == 0) {
carousel?.post {
carousel?.layoutManager?.scrollToPosition(0)
}
}
super.onItemRangeMoved(fromPosition, toPosition, itemCount)
}
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
if (positionStart == 0) {
carousel?.layoutManager?.scrollToPosition(0)
}
}
}
private val recentsHPadding = TypedValue.applyDimension( private val recentsHPadding = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, TypedValue.COMPLEX_UNIT_DIP,
4f, 4f,
@ -92,9 +112,28 @@ class HomeRoomsHeadersController @Inject constructor(
) )
) )
onBind { _, view, _ -> onBind { _, view, _ ->
host.carousel = view
val colorSurface = MaterialColors.getColor(view, R.attr.vctr_toolbar_background) val colorSurface = MaterialColors.getColor(view, R.attr.vctr_toolbar_background)
view.setBackgroundColor(colorSurface) view.setBackgroundColor(colorSurface)
try {
view.adapter?.registerAdapterDataObserver(host.carouselAdapterObserver)
} catch (e: IllegalStateException) {
// do nothing
}
} }
onUnbind { _, view ->
host.carousel = null
try {
view.adapter?.unregisterAdapterDataObserver(host.carouselAdapterObserver)
} catch (e: IllegalStateException) {
// do nothing
}
}
withModelsFrom(recents) { roomSummary -> withModelsFrom(recents) { roomSummary ->
val onClick = host.recentsRoomListener?.let { it::onRoomClicked } val onClick = host.recentsRoomListener?.let { it::onRoomClicked }
val onLongClick = host.recentsRoomListener?.let { it::onRoomLongClicked } val onLongClick = host.recentsRoomListener?.let { it::onRoomLongClicked }