Timeline : use snapshot from pagedlist to build list

This commit is contained in:
ganfra 2018-11-05 14:25:37 +01:00
parent b3fe742ec4
commit c6bcd1c022
3 changed files with 24 additions and 15 deletions

View file

@ -28,8 +28,9 @@ class HomeModule(private val homeActivity: HomeActivity) : Module {
TextItemFactory()
}
single {
TimelineEventController(get(), get(), get())
factory {
val roomId = it.get(0) as String
TimelineEventController(roomId, get(), get(), get())
}
}.invoke()

View file

@ -19,6 +19,7 @@ import im.vector.riotredesign.features.home.AvatarRenderer
import im.vector.riotredesign.features.home.room.detail.timeline.TimelineEventController
import kotlinx.android.synthetic.main.fragment_room_detail.*
import org.koin.android.ext.android.inject
import org.koin.core.parameter.ParameterList
class RoomDetailFragment : RiotFragment() {
@ -34,7 +35,7 @@ class RoomDetailFragment : RiotFragment() {
private val matrix by inject<Matrix>()
private val currentSession = matrix.currentSession
private var roomId by FragmentArgumentDelegate<String>()
private val timelineEventController by inject<TimelineEventController>()
private val timelineEventController by inject<TimelineEventController>(parameters = { ParameterList(roomId) })
private lateinit var room: Room
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@ -81,7 +82,6 @@ class RoomDetailFragment : RiotFragment() {
private fun renderEvents(events: PagedList<EnrichedEvent>?) {
timelineEventController.timeline = events
timelineEventController.requestModelBuild()
}
}

View file

@ -8,7 +8,8 @@ import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.riotredesign.core.extensions.localDateTime
import im.vector.riotredesign.features.home.LoadingItemModel_
class TimelineEventController(private val messageItemFactory: MessageItemFactory,
class TimelineEventController(private val roomId: String,
private val messageItemFactory: MessageItemFactory,
private val textItemFactory: TextItemFactory,
private val dateFormatter: TimelineDateFormatter
) : EpoxyController(
@ -18,35 +19,37 @@ class TimelineEventController(private val messageItemFactory: MessageItemFactory
private val pagedListCallback = object : PagedList.Callback() {
override fun onChanged(position: Int, count: Int) {
requestModelBuild()
buildSnapshotList()
}
override fun onInserted(position: Int, count: Int) {
requestModelBuild()
buildSnapshotList()
}
override fun onRemoved(position: Int, count: Int) {
requestModelBuild()
buildSnapshotList()
}
}
var snapshotList: List<EnrichedEvent>? = emptyList()
var timeline: PagedList<EnrichedEvent>? = null
set(value) {
field?.removeWeakCallback(pagedListCallback)
field = value
field?.addWeakCallback(null, pagedListCallback)
buildSnapshotList()
}
override fun buildModels() {
buildModels(timeline)
buildModels(snapshotList)
}
private fun buildModels(data: List<EnrichedEvent>?) {
private fun buildModels(data: List<EnrichedEvent?>?) {
if (data.isNullOrEmpty()) {
return
}
for (index in 0 until data.size) {
val event = data[index]
val event = data[index] ?: continue
val nextEvent = if (index + 1 < data.size) data[index + 1] else null
val date = event.root.localDateTime()
@ -55,7 +58,7 @@ class TimelineEventController(private val messageItemFactory: MessageItemFactory
val item = when (event.root.type) {
EventType.MESSAGE -> messageItemFactory.create(event, nextEvent, addDaySeparator, date)
else -> textItemFactory.create(event)
else -> textItemFactory.create(event)
}
item
?.onBind { timeline?.loadAround(index) }
@ -64,15 +67,20 @@ class TimelineEventController(private val messageItemFactory: MessageItemFactory
if (addDaySeparator) {
val formattedDay = dateFormatter.formatMessageDay(date)
DaySeparatorItem(formattedDay).id(formattedDay).addTo(this)
DaySeparatorItem(formattedDay).id(roomId + formattedDay).addTo(this)
}
}
//It's a hack at the moment
val isLastEvent = data.last().root.type == EventType.STATE_ROOM_CREATE
val isLastEvent = data.last()?.root?.type == EventType.STATE_ROOM_CREATE
LoadingItemModel_()
.id("backward_loading_item")
.id(roomId + "backward_loading_item")
.addIf(!isLastEvent, this)
}
private fun buildSnapshotList() {
snapshotList = timeline?.snapshot() ?: emptyList()
requestModelBuild()
}
}