2018-11-02 09:30:50 +00:00
|
|
|
package im.vector.riotredesign.features.home.room.detail.timeline
|
2018-10-19 13:30:40 +00:00
|
|
|
|
|
|
|
import com.airbnb.epoxy.EpoxyAsyncUtil
|
2019-01-10 10:37:14 +00:00
|
|
|
import com.airbnb.epoxy.EpoxyModel
|
2018-10-31 10:22:42 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.EventType
|
2019-01-15 14:47:03 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.TimelineEvent
|
2019-01-07 18:38:36 +00:00
|
|
|
import im.vector.matrix.android.api.session.room.timeline.TimelineData
|
2018-10-31 15:11:59 +00:00
|
|
|
import im.vector.riotredesign.core.extensions.localDateTime
|
2018-10-28 18:18:14 +00:00
|
|
|
import im.vector.riotredesign.features.home.LoadingItemModel_
|
2019-01-10 10:37:14 +00:00
|
|
|
import im.vector.riotredesign.features.home.room.detail.timeline.paging.PagedListEpoxyController
|
2018-10-19 13:30:40 +00:00
|
|
|
|
2018-11-05 13:25:37 +00:00
|
|
|
class TimelineEventController(private val roomId: String,
|
2019-01-15 14:47:03 +00:00
|
|
|
private val dateFormatter: TimelineDateFormatter,
|
|
|
|
private val timelineItemFactory: TimelineItemFactory
|
2019-01-14 15:18:39 +00:00
|
|
|
) : PagedListEpoxyController<TimelineEvent>(
|
2018-10-31 10:22:42 +00:00
|
|
|
EpoxyAsyncUtil.getAsyncBackgroundHandler(),
|
|
|
|
EpoxyAsyncUtil.getAsyncBackgroundHandler()
|
2018-10-19 13:30:40 +00:00
|
|
|
) {
|
2018-11-27 21:42:46 +00:00
|
|
|
init {
|
|
|
|
setFilterDuplicates(true)
|
|
|
|
}
|
|
|
|
|
2019-01-10 10:37:14 +00:00
|
|
|
private var isLoadingForward: Boolean = false
|
|
|
|
private var isLoadingBackward: Boolean = false
|
2019-01-11 14:17:15 +00:00
|
|
|
private var hasReachedEnd: Boolean = false
|
2018-10-31 10:22:42 +00:00
|
|
|
|
2018-12-19 10:50:44 +00:00
|
|
|
var callback: Callback? = null
|
|
|
|
|
2019-01-07 18:38:36 +00:00
|
|
|
fun update(timelineData: TimelineData?) {
|
2019-01-11 14:17:15 +00:00
|
|
|
timelineData?.let {
|
|
|
|
isLoadingForward = it.isLoadingForward
|
|
|
|
isLoadingBackward = it.isLoadingBackward
|
|
|
|
hasReachedEnd = it.events.lastOrNull()?.root?.type == EventType.STATE_ROOM_CREATE
|
|
|
|
submitList(it.events)
|
|
|
|
requestModelBuild()
|
|
|
|
}
|
2019-01-07 18:38:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 13:30:40 +00:00
|
|
|
|
2019-01-14 15:18:39 +00:00
|
|
|
override fun buildItemModels(currentPosition: Int, items: List<TimelineEvent?>): List<EpoxyModel<*>> {
|
2019-01-10 10:37:14 +00:00
|
|
|
if (items.isNullOrEmpty()) {
|
|
|
|
return emptyList()
|
2018-10-31 10:22:42 +00:00
|
|
|
}
|
2019-01-10 10:37:14 +00:00
|
|
|
val epoxyModels = ArrayList<EpoxyModel<*>>()
|
|
|
|
val event = items[currentPosition] ?: return emptyList()
|
|
|
|
val nextEvent = if (currentPosition + 1 < items.size) items[currentPosition + 1] else null
|
|
|
|
|
|
|
|
val date = event.root.localDateTime()
|
|
|
|
val nextDate = nextEvent?.root?.localDateTime()
|
|
|
|
val addDaySeparator = date.toLocalDate() != nextDate?.toLocalDate()
|
|
|
|
|
2019-01-16 09:41:32 +00:00
|
|
|
timelineItemFactory.create(event, nextEvent, callback)?.also {
|
2019-01-10 10:37:14 +00:00
|
|
|
it.id(event.localId)
|
|
|
|
epoxyModels.add(it)
|
|
|
|
}
|
|
|
|
if (addDaySeparator) {
|
|
|
|
val formattedDay = dateFormatter.formatMessageDay(date)
|
|
|
|
val daySeparatorItem = DaySeparatorItem(formattedDay).id(roomId + formattedDay)
|
|
|
|
epoxyModels.add(daySeparatorItem)
|
|
|
|
}
|
|
|
|
return epoxyModels
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun addModels(models: List<EpoxyModel<*>>) {
|
2019-01-07 18:38:36 +00:00
|
|
|
LoadingItemModel_()
|
|
|
|
.id(roomId + "forward_loading_item")
|
|
|
|
.addIf(isLoadingForward, this)
|
|
|
|
|
2019-01-10 10:37:14 +00:00
|
|
|
super.add(models)
|
2018-10-31 10:22:42 +00:00
|
|
|
|
|
|
|
LoadingItemModel_()
|
2018-11-05 13:25:37 +00:00
|
|
|
.id(roomId + "backward_loading_item")
|
2019-01-11 14:17:15 +00:00
|
|
|
.addIf(!hasReachedEnd, this)
|
2018-10-19 13:30:40 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 13:25:37 +00:00
|
|
|
|
2018-12-19 10:50:44 +00:00
|
|
|
interface Callback {
|
|
|
|
fun onUrlClicked(url: String)
|
|
|
|
}
|
|
|
|
|
2018-10-19 13:30:40 +00:00
|
|
|
}
|