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-31 12:03:36 +00:00
|
|
|
import android.content.Context
|
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
|
2018-10-31 12:03:36 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.roomMember
|
2018-10-31 10:22:42 +00:00
|
|
|
import im.vector.matrix.android.api.session.room.model.MessageContent
|
2018-10-31 12:03:36 +00:00
|
|
|
import im.vector.riotredesign.core.extensions.avatarDrawable
|
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 12:03:36 +00:00
|
|
|
class TimelineEventController(private val context: Context) : EpoxyController(
|
2018-10-31 10:22:42 +00:00
|
|
|
EpoxyAsyncUtil.getAsyncBackgroundHandler(),
|
|
|
|
EpoxyAsyncUtil.getAsyncBackgroundHandler()
|
2018-10-19 13:30:40 +00:00
|
|
|
) {
|
|
|
|
|
2018-10-31 12:03:36 +00:00
|
|
|
private val messagesLoadedWithInformation = HashSet<String?>()
|
|
|
|
|
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
|
|
|
|
}
|
2018-10-31 12:03:36 +00:00
|
|
|
for (index in 0 until data.size) {
|
|
|
|
val event = data[index]
|
|
|
|
val nextEvent = if (index + 1 < data.size) data[index + 1] else null
|
|
|
|
|
|
|
|
if (event.root.type == EventType.MESSAGE) {
|
|
|
|
val messageContent = event.root.content<MessageContent>()
|
|
|
|
val roomMember = event.roomMember()
|
|
|
|
if (messageContent == null || roomMember == null) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
val nextRoomMember = nextEvent?.roomMember()
|
|
|
|
if (nextRoomMember != roomMember) {
|
|
|
|
messagesLoadedWithInformation.add(event.root.eventId)
|
|
|
|
}
|
|
|
|
val showInformation = messagesLoadedWithInformation.contains(event.root.eventId)
|
|
|
|
|
|
|
|
val avatarDrawable = context.avatarDrawable(roomMember.displayName ?: "")
|
|
|
|
TimelineMessageItem(
|
|
|
|
message = messageContent.body,
|
|
|
|
showInformation = showInformation,
|
|
|
|
avatarDrawable = avatarDrawable,
|
|
|
|
memberName = roomMember.displayName
|
|
|
|
)
|
|
|
|
.onBind { timeline?.loadAround(index) }
|
|
|
|
.id(event.root.eventId)
|
|
|
|
.addTo(this)
|
2018-10-31 10:22:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 12:03:36 +00:00
|
|
|
//It's a hack at the moment
|
|
|
|
val isLastEvent = data.last().root.type == EventType.STATE_ROOM_CREATE
|
2018-10-31 10:22:42 +00:00
|
|
|
LoadingItemModel_()
|
|
|
|
.id("backward_loading_item")
|
|
|
|
.addIf(!isLastEvent, this)
|
2018-10-19 13:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|