2018-10-29 16:48:31 +00:00
|
|
|
package im.vector.riotredesign.features.home.room.detail
|
2018-10-19 13:30:40 +00:00
|
|
|
|
2018-10-31 10:22:42 +00:00
|
|
|
import android.arch.paging.PagedList
|
2018-10-19 13:30:40 +00:00
|
|
|
import com.airbnb.epoxy.EpoxyAsyncUtil
|
2018-10-31 10:22:42 +00:00
|
|
|
import com.airbnb.epoxy.EpoxyController
|
|
|
|
import im.vector.matrix.android.api.session.events.model.EnrichedEvent
|
2018-10-19 13:30:40 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.Event
|
2018-10-31 10:22:42 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.EventType
|
|
|
|
import im.vector.matrix.android.api.session.room.model.MessageContent
|
|
|
|
import im.vector.matrix.android.api.session.room.model.RoomMember
|
2018-10-28 18:18:14 +00:00
|
|
|
import im.vector.riotredesign.features.home.LoadingItemModel_
|
2018-10-19 13:30:40 +00:00
|
|
|
|
2018-10-31 10:22:42 +00:00
|
|
|
private const val PREFETCH_DISTANCE = 5
|
|
|
|
|
|
|
|
class TimelineEventController : EpoxyController(
|
|
|
|
EpoxyAsyncUtil.getAsyncBackgroundHandler(),
|
|
|
|
EpoxyAsyncUtil.getAsyncBackgroundHandler()
|
2018-10-19 13:30:40 +00:00
|
|
|
) {
|
|
|
|
|
2018-10-31 10:22:42 +00:00
|
|
|
private val pagedListCallback = object : PagedList.Callback() {
|
|
|
|
override fun onChanged(position: Int, count: Int) {
|
|
|
|
requestModelBuild()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onInserted(position: Int, count: Int) {
|
|
|
|
requestModelBuild()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onRemoved(position: Int, count: Int) {
|
|
|
|
requestModelBuild()
|
2018-10-19 13:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 10:22:42 +00:00
|
|
|
var timeline: PagedList<EnrichedEvent>? = null
|
|
|
|
set(value) {
|
|
|
|
field?.removeWeakCallback(pagedListCallback)
|
|
|
|
field = value
|
|
|
|
field?.addWeakCallback(null, pagedListCallback)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun buildModels() {
|
|
|
|
buildModels(timeline)
|
2018-10-19 13:30:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 10:22:42 +00:00
|
|
|
private fun buildModels(data: List<EnrichedEvent>?) {
|
|
|
|
if (data.isNullOrEmpty()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data.forEachIndexed { index, enrichedEvent ->
|
|
|
|
val item = if (enrichedEvent.root.type == EventType.MESSAGE) {
|
|
|
|
val messageContent = enrichedEvent.root.content<MessageContent>()
|
|
|
|
val roomMember = enrichedEvent.getMetadata<Event>(EventType.STATE_ROOM_MEMBER)?.content<RoomMember>()
|
|
|
|
val title = "${roomMember?.displayName} : ${messageContent?.body}"
|
|
|
|
TimelineEventItem(title = title)
|
|
|
|
} else {
|
|
|
|
TimelineEventItem(title = enrichedEvent.toString())
|
|
|
|
}
|
|
|
|
item
|
|
|
|
.onBind { timeline?.loadAround(index) }
|
|
|
|
.id(enrichedEvent.root.eventId)
|
|
|
|
.addTo(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
val isLastEvent = data.last().getMetadata<Boolean>(EnrichedEvent.IS_LAST_EVENT) ?: false
|
|
|
|
LoadingItemModel_()
|
|
|
|
.id("backward_loading_item")
|
|
|
|
.addIf(!isLastEvent, this)
|
2018-10-19 13:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|