From c6bcd1c02257f6a0289910bc14b791af3f8c07ed Mon Sep 17 00:00:00 2001 From: ganfra Date: Mon, 5 Nov 2018 14:25:37 +0100 Subject: [PATCH] Timeline : use snapshot from pagedlist to build list --- .../riotredesign/features/home/HomeModule.kt | 5 ++-- .../home/room/detail/RoomDetailFragment.kt | 4 +-- .../timeline/TimelineEventController.kt | 30 ++++++++++++------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt b/app/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt index f8c27d1323..a74a67c61e 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt @@ -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() diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt index 9a8c06b777..bb4d617ffb 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt @@ -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() private val currentSession = matrix.currentSession private var roomId by FragmentArgumentDelegate() - private val timelineEventController by inject() + private val timelineEventController by inject(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?) { timelineEventController.timeline = events - timelineEventController.requestModelBuild() } } diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/TimelineEventController.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/TimelineEventController.kt index c8e95fa13f..0a0e021115 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/TimelineEventController.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/TimelineEventController.kt @@ -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? = emptyList() var timeline: PagedList? = 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?) { + private fun buildModels(data: List?) { 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() + } + } \ No newline at end of file