Bottom space bar: always try to center the selected space

Change-Id: Ia9820869d27b409da50900ab9819cec9e5e4691b
This commit is contained in:
SpiritCroc 2022-10-08 19:07:34 +02:00
parent a53c183847
commit 731dc8987d
2 changed files with 14 additions and 7 deletions

View file

@ -171,10 +171,10 @@ class HomeDetailFragment :
checkNotificationTabStatus()
views.spaceBarRecyclerView.layoutManager = LinearLayoutManager(context)
val spaceBarAdapter = spaceBarController.also { controller ->
controller.spaceRoomListener = spaceBarListener
}.adapter
views.spaceBarRecyclerView.layoutManager = LinearLayoutManager(context)
views.spaceBarRecyclerView.adapter = spaceBarAdapter
// Reduce sensitivity of viewpager to avoid scrolling horizontally by accident too easily

View file

@ -69,8 +69,10 @@ class SpaceBarController @Inject constructor(
)
onBind { _, view, _ ->
host.carousel = view
view.post {
host.scrollToSpace(selectedSpace?.roomId)
}
}
onUnbind { _, _ ->
host.carousel = null
@ -137,12 +139,17 @@ class SpaceBarController @Inject constructor(
var effectivePosition = position
val lm = safeCarousel.layoutManager as? LinearLayoutManager
if (lm != null) {
// Look-ahead of 1
if (lm.findFirstCompletelyVisibleItemPosition() >= position) {
effectivePosition--
} else if (lm.findLastVisibleItemPosition() <= position) {
effectivePosition++
// Scroll to an element such that the new selection is roughly in the middle
val firstVisible = lm.findFirstCompletelyVisibleItemPosition()
val visibleRange = lm.findLastCompletelyVisibleItemPosition() - firstVisible + 1
val overshoot = visibleRange/2
val currentMiddle = firstVisible + overshoot
if (currentMiddle < position) {
effectivePosition = position + overshoot
} else if (currentMiddle > position) {
effectivePosition = position - overshoot
}
// List limits
effectivePosition = max(0, min(effectivePosition, lm.itemCount-1))
}
safeCarousel.smoothScrollToPosition(effectivePosition)