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 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-31 15:11:59 +00:00
|
|
|
import org.threeten.bp.format.DateTimeFormatter
|
|
|
|
import org.threeten.bp.format.FormatStyle
|
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 15:11:59 +00:00
|
|
|
private val messagesDisplayedWithInformation = HashSet<String?>()
|
2018-10-31 12:03:36 +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
|
|
|
|
}
|
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
|
|
|
|
|
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-10-31 12:03:36 +00:00
|
|
|
if (event.root.type == EventType.MESSAGE) {
|
|
|
|
val messageContent = event.root.content<MessageContent>()
|
|
|
|
val roomMember = event.roomMember()
|
|
|
|
if (messageContent == null || roomMember == null) {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-31 15:11:59 +00:00
|
|
|
|
2018-10-31 12:03:36 +00:00
|
|
|
val nextRoomMember = nextEvent?.roomMember()
|
2018-10-31 15:11:59 +00:00
|
|
|
if (addDaySeparator || nextRoomMember != roomMember) {
|
|
|
|
messagesDisplayedWithInformation.add(event.root.eventId)
|
2018-10-31 12:03:36 +00:00
|
|
|
}
|
2018-10-31 15:11:59 +00:00
|
|
|
val showInformation = messagesDisplayedWithInformation.contains(event.root.eventId)
|
2018-10-31 12:03:36 +00:00
|
|
|
|
|
|
|
TimelineMessageItem(
|
|
|
|
message = messageContent.body,
|
2018-11-02 08:12:26 +00:00
|
|
|
avatarUrl = roomMember.avatarUrl,
|
2018-10-31 12:03:36 +00:00
|
|
|
showInformation = showInformation,
|
2018-10-31 15:11:59 +00:00
|
|
|
time = date.toLocalTime().format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)),
|
2018-10-31 12:03:36 +00:00
|
|
|
memberName = roomMember.displayName
|
|
|
|
)
|
|
|
|
.onBind { timeline?.loadAround(index) }
|
|
|
|
.id(event.root.eventId)
|
|
|
|
.addTo(this)
|
2018-11-02 08:52:10 +00:00
|
|
|
} else {
|
|
|
|
BlankItemModel_()
|
|
|
|
.id(event.root.eventId)
|
|
|
|
.onBind { _, _, _ -> timeline?.loadAround(index) }
|
|
|
|
.addTo(this)
|
|
|
|
}
|
|
|
|
if (addDaySeparator) {
|
|
|
|
val formattedDay = date.toLocalDate().format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM))
|
|
|
|
TimelineDaySeparatorItem(formattedDay).id(formattedDay).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
|
|
|
}
|
|
|
|
|
|
|
|
}
|