Avoid displaying two loaders if there is no elements between them

This commit is contained in:
Benoit Marty 2019-07-15 14:08:51 +02:00
parent 8901a5e09a
commit fc1c0caea3

View File

@ -18,7 +18,6 @@ package im.vector.riotx.features.home.room.detail.timeline


import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.util.LongSparseArray
import android.view.View import android.view.View
import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListUpdateCallback import androidx.recyclerview.widget.ListUpdateCallback
@ -178,17 +177,20 @@ class TimelineEventController @Inject constructor(private val dateFormatter: Tim
} }


override fun buildModels() { override fun buildModels() {
LoadingItem_() val loaderAdded = LoadingItem_()
.id("forward_loading_item") .id("forward_loading_item")
.addWhen(Timeline.Direction.FORWARDS) .addWhen(Timeline.Direction.FORWARDS)


val timelineModels = getModels() val timelineModels = getModels()
add(timelineModels) add(timelineModels)


// Avoid displaying two loaders if there is no elements between them
if (!loaderAdded || timelineModels.isNotEmpty()) {
LoadingItem_() LoadingItem_()
.id("backward_loading_item") .id("backward_loading_item")
.addWhen(Timeline.Direction.BACKWARDS) .addWhen(Timeline.Direction.BACKWARDS)
} }
}


// Timeline.LISTENER *************************************************************************** // 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 val shouldAdd = timeline?.hasMoreToLoad(direction) ?: false
addIf(shouldAdd, this@TimelineEventController) addIf(shouldAdd, this@TimelineEventController)
return shouldAdd
} }


fun searchPositionOfEvent(eventId: String): Int? { fun searchPositionOfEvent(eventId: String): Int? {