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
|
|
|
|
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
|
|
|
|
import im.vector.matrix.android.api.session.events.model.EventType
|
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_
|
2018-10-19 13:30:40 +00:00
|
|
|
|
2018-11-05 13:25:37 +00:00
|
|
|
class TimelineEventController(private val roomId: String,
|
|
|
|
private val messageItemFactory: MessageItemFactory,
|
2018-11-02 16:37:37 +00:00
|
|
|
private val textItemFactory: TextItemFactory,
|
|
|
|
private val dateFormatter: TimelineDateFormatter
|
|
|
|
) : EpoxyController(
|
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)
|
|
|
|
}
|
|
|
|
|
2018-10-31 10:22:42 +00:00
|
|
|
private val pagedListCallback = object : PagedList.Callback() {
|
|
|
|
override fun onChanged(position: Int, count: Int) {
|
2018-11-05 13:25:37 +00:00
|
|
|
buildSnapshotList()
|
2018-10-31 10:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onInserted(position: Int, count: Int) {
|
2018-11-05 13:25:37 +00:00
|
|
|
buildSnapshotList()
|
2018-10-31 10:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onRemoved(position: Int, count: Int) {
|
2018-11-05 13:25:37 +00:00
|
|
|
buildSnapshotList()
|
2018-10-19 13:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 18:38:36 +00:00
|
|
|
private var snapshotList: List<EnrichedEvent> = emptyList()
|
|
|
|
private var timelineData: TimelineData? = null
|
2018-12-19 10:50:44 +00:00
|
|
|
var callback: Callback? = null
|
|
|
|
|
2019-01-07 18:38:36 +00:00
|
|
|
fun update(timelineData: TimelineData?) {
|
|
|
|
timelineData?.events?.removeWeakCallback(pagedListCallback)
|
|
|
|
this.timelineData = timelineData
|
|
|
|
timelineData?.events?.addWeakCallback(null, pagedListCallback)
|
|
|
|
buildSnapshotList()
|
|
|
|
}
|
|
|
|
|
2018-10-31 10:22:42 +00:00
|
|
|
override fun buildModels() {
|
2019-01-07 18:38:36 +00:00
|
|
|
buildModelsWith(
|
|
|
|
snapshotList,
|
|
|
|
timelineData?.isLoadingForward ?: false,
|
|
|
|
timelineData?.isLoadingBackward ?: false
|
|
|
|
)
|
2018-10-19 13:30:40 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 18:38:36 +00:00
|
|
|
private fun buildModelsWith(events: List<EnrichedEvent?>,
|
|
|
|
isLoadingForward: Boolean,
|
|
|
|
isLoadingBackward: Boolean) {
|
|
|
|
if (events.isEmpty()) {
|
2018-10-31 10:22:42 +00:00
|
|
|
return
|
|
|
|
}
|
2019-01-07 18:38:36 +00:00
|
|
|
LoadingItemModel_()
|
|
|
|
.id(roomId + "forward_loading_item")
|
|
|
|
.addIf(isLoadingForward, this)
|
|
|
|
|
|
|
|
for (index in 0 until events.size) {
|
|
|
|
val event = events[index] ?: continue
|
|
|
|
val nextEvent = if (index + 1 < events.size) events[index + 1] else null
|
2018-10-31 12:03:36 +00:00
|
|
|
|
2018-11-02 08:52:10 +00:00
|
|
|
val date = event.root.localDateTime()
|
|
|
|
val nextDate = nextEvent?.root?.localDateTime()
|
|
|
|
val addDaySeparator = date.toLocalDate() != nextDate?.toLocalDate()
|
|
|
|
|
2018-11-02 16:37:37 +00:00
|
|
|
val item = when (event.root.type) {
|
2018-12-19 10:50:44 +00:00
|
|
|
EventType.MESSAGE -> messageItemFactory.create(event, nextEvent, addDaySeparator, date, callback)
|
2019-01-07 18:38:36 +00:00
|
|
|
else -> textItemFactory.create(event)
|
2018-11-02 08:52:10 +00:00
|
|
|
}
|
2019-01-07 18:38:36 +00:00
|
|
|
|
2018-11-02 16:37:37 +00:00
|
|
|
item
|
2019-01-07 18:38:36 +00:00
|
|
|
?.onBind {
|
|
|
|
timelineData?.events?.loadAround(index)
|
|
|
|
}
|
2018-12-18 11:13:46 +00:00
|
|
|
?.id(event.localId)
|
2018-11-02 16:37:37 +00:00
|
|
|
?.addTo(this)
|
|
|
|
|
2018-11-02 08:52:10 +00:00
|
|
|
if (addDaySeparator) {
|
2018-11-02 16:37:37 +00:00
|
|
|
val formattedDay = dateFormatter.formatMessageDay(date)
|
2018-11-05 13:25:37 +00:00
|
|
|
DaySeparatorItem(formattedDay).id(roomId + formattedDay).addTo(this)
|
2018-10-31 10:22:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadingItemModel_()
|
2018-11-05 13:25:37 +00:00
|
|
|
.id(roomId + "backward_loading_item")
|
2019-01-07 18:38:36 +00:00
|
|
|
.addIf(isLoadingBackward, this)
|
|
|
|
|
2018-10-19 13:30:40 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 13:25:37 +00:00
|
|
|
private fun buildSnapshotList() {
|
2019-01-07 18:38:36 +00:00
|
|
|
snapshotList = timelineData?.events?.snapshot() ?: emptyList()
|
2018-11-05 13:25:37 +00:00
|
|
|
requestModelBuild()
|
|
|
|
}
|
|
|
|
|
2018-12-19 10:50:44 +00:00
|
|
|
interface Callback {
|
|
|
|
fun onUrlClicked(url: String)
|
|
|
|
}
|
|
|
|
|
2018-10-19 13:30:40 +00:00
|
|
|
}
|