From fc1c0caea35f6f3e7bc4c05abcb0c7bdf57ba75e Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 15 Jul 2019 14:08:51 +0200 Subject: [PATCH] Avoid displaying two loaders if there is no elements between them --- .../timeline/TimelineEventController.kt | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/TimelineEventController.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/TimelineEventController.kt index b6cf00a3..0b78f815 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/TimelineEventController.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/TimelineEventController.kt @@ -18,7 +18,6 @@ package im.vector.riotx.features.home.room.detail.timeline import android.os.Handler import android.os.Looper -import android.util.LongSparseArray import android.view.View import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListUpdateCallback @@ -84,7 +83,7 @@ class TimelineEventController @Inject constructor(private val dateFormatter: Tim } private val collapsedEventIds = linkedSetOf() - private val mergeItemCollapseStates = HashMap() + private val mergeItemCollapseStates = HashMap() private val modelCache = arrayListOf() private var currentSnapshot: List = emptyList() @@ -178,16 +177,19 @@ class TimelineEventController @Inject constructor(private val dateFormatter: Tim } override fun buildModels() { - LoadingItem_() + val loaderAdded = LoadingItem_() .id("forward_loading_item") .addWhen(Timeline.Direction.FORWARDS) val timelineModels = getModels() add(timelineModels) - LoadingItem_() - .id("backward_loading_item") - .addWhen(Timeline.Direction.BACKWARDS) + // Avoid displaying two loaders if there is no elements between them + if (!loaderAdded || timelineModels.isNotEmpty()) { + LoadingItem_() + .id("backward_loading_item") + .addWhen(Timeline.Direction.BACKWARDS) + } } // Timeline.LISTENER *************************************************************************** @@ -310,9 +312,13 @@ class TimelineEventController @Inject constructor(private val dateFormatter: Tim } } - private fun LoadingItem_.addWhen(direction: Timeline.Direction) { + /** + * Return true if added + */ + private fun LoadingItem_.addWhen(direction: Timeline.Direction): Boolean { val shouldAdd = timeline?.hasMoreToLoad(direction) ?: false addIf(shouldAdd, this@TimelineEventController) + return shouldAdd } fun searchPositionOfEvent(eventId: String): Int? {