Timeline : try using initialLoadKey to open PagedList around an event. Still need to scroll to the item.

This commit is contained in:
ganfra 2019-01-11 16:35:49 +01:00
parent 02254d5d31
commit d7de986365
2 changed files with 31 additions and 22 deletions

View file

@ -52,13 +52,7 @@ class RoomDetailFragment : RiotFragment(), TimelineEventController.Callback {
super.onActivityCreated(savedInstanceState)
setupRecyclerView()
setupToolbar()
sendButton.setOnClickListener {
val textMessage = composerEditText.text.toString()
if (textMessage.isNotBlank()) {
composerEditText.text = null
roomDetailViewModel.accept(RoomDetailActions.SendMessage(textMessage))
}
}
setupSendButton()
roomDetailViewModel.subscribe { renderState(it) }
}
@ -84,6 +78,16 @@ class RoomDetailFragment : RiotFragment(), TimelineEventController.Callback {
timelineEventController.callback = this
}
private fun setupSendButton() {
sendButton.setOnClickListener {
val textMessage = composerEditText.text.toString()
if (textMessage.isNotBlank()) {
composerEditText.text = null
roomDetailViewModel.accept(RoomDetailActions.SendMessage(textMessage))
}
}
}
private fun renderState(state: RoomDetailViewState) {
renderRoomSummary(state)
renderTimeline(state)

View file

@ -22,8 +22,9 @@ import im.vector.matrix.android.internal.util.tryTransactionAsync
import io.realm.Realm
import io.realm.RealmQuery
private const val PAGE_SIZE = 50
private const val PREFETCH_DISTANCE = 20
private const val PAGE_SIZE = 120
private const val PREFETCH_DISTANCE = 40
private const val EVENT_NOT_FOUND_INDEX = -1
internal class DefaultTimelineService(private val roomId: String,
private val monarchy: Monarchy,
@ -37,8 +38,15 @@ internal class DefaultTimelineService(private val roomId: String,
override fun timeline(eventId: String?): LiveData<TimelineData> {
clearUnlinkedEvents()
var initialLoadKey = 0
if (eventId != null) {
fetchEventIfNeeded(eventId)
val indexOfEvent = indexOfEvent(eventId)
if (indexOfEvent == EVENT_NOT_FOUND_INDEX) {
val params = GetContextOfEventTask.Params(roomId, eventId)
contextOfEventTask.configureWith(params).executeBy(taskExecutor)
} else {
initialLoadKey = indexOfEvent
}
}
val realmDataSourceFactory = monarchy.createDataSourceFactory {
buildDataSourceFactoryQuery(it, eventId)
@ -52,10 +60,14 @@ internal class DefaultTimelineService(private val roomId: String,
val pagedListConfig = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPageSize(PAGE_SIZE)
.setInitialLoadSizeHint(PAGE_SIZE)
.setPrefetchDistance(PREFETCH_DISTANCE)
.build()
val livePagedListBuilder = LivePagedListBuilder(domainSourceFactory, pagedListConfig).setBoundaryCallback(boundaryCallback)
val livePagedListBuilder = LivePagedListBuilder(domainSourceFactory, pagedListConfig)
.setBoundaryCallback(boundaryCallback)
.setInitialLoadKey(initialLoadKey)
val eventsLiveData = monarchy.findAllPagedWithChanges(realmDataSourceFactory, livePagedListBuilder)
return LiveDataUtils.combine(eventsLiveData, boundaryCallback.status) { events, status ->
@ -76,19 +88,12 @@ internal class DefaultTimelineService(private val roomId: String,
}
}
private fun fetchEventIfNeeded(eventId: String) {
if (!isEventPersisted(eventId)) {
val params = GetContextOfEventTask.Params(roomId, eventId)
contextOfEventTask.configureWith(params).executeBy(taskExecutor)
}
}
private fun isEventPersisted(eventId: String): Boolean {
var isEventPersisted = false
private fun indexOfEvent(eventId: String): Int {
var displayIndex = EVENT_NOT_FOUND_INDEX
monarchy.doWithRealm {
isEventPersisted = EventEntity.where(it, eventId = eventId).findFirst() != null
displayIndex = EventEntity.where(it, eventId = eventId).findFirst()?.displayIndex ?: EVENT_NOT_FOUND_INDEX
}
return isEventPersisted
return displayIndex
}
private fun buildDataSourceFactoryQuery(realm: Realm, eventId: String?): RealmQuery<EventEntity> {