mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-25 10:55:55 +03:00
Timeline : try using initialLoadKey to open PagedList around an event. Still need to scroll to the item.
This commit is contained in:
parent
02254d5d31
commit
d7de986365
2 changed files with 31 additions and 22 deletions
|
@ -52,13 +52,7 @@ class RoomDetailFragment : RiotFragment(), TimelineEventController.Callback {
|
||||||
super.onActivityCreated(savedInstanceState)
|
super.onActivityCreated(savedInstanceState)
|
||||||
setupRecyclerView()
|
setupRecyclerView()
|
||||||
setupToolbar()
|
setupToolbar()
|
||||||
sendButton.setOnClickListener {
|
setupSendButton()
|
||||||
val textMessage = composerEditText.text.toString()
|
|
||||||
if (textMessage.isNotBlank()) {
|
|
||||||
composerEditText.text = null
|
|
||||||
roomDetailViewModel.accept(RoomDetailActions.SendMessage(textMessage))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
roomDetailViewModel.subscribe { renderState(it) }
|
roomDetailViewModel.subscribe { renderState(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,6 +78,16 @@ class RoomDetailFragment : RiotFragment(), TimelineEventController.Callback {
|
||||||
timelineEventController.callback = this
|
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) {
|
private fun renderState(state: RoomDetailViewState) {
|
||||||
renderRoomSummary(state)
|
renderRoomSummary(state)
|
||||||
renderTimeline(state)
|
renderTimeline(state)
|
||||||
|
|
|
@ -22,8 +22,9 @@ import im.vector.matrix.android.internal.util.tryTransactionAsync
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import io.realm.RealmQuery
|
import io.realm.RealmQuery
|
||||||
|
|
||||||
private const val PAGE_SIZE = 50
|
private const val PAGE_SIZE = 120
|
||||||
private const val PREFETCH_DISTANCE = 20
|
private const val PREFETCH_DISTANCE = 40
|
||||||
|
private const val EVENT_NOT_FOUND_INDEX = -1
|
||||||
|
|
||||||
internal class DefaultTimelineService(private val roomId: String,
|
internal class DefaultTimelineService(private val roomId: String,
|
||||||
private val monarchy: Monarchy,
|
private val monarchy: Monarchy,
|
||||||
|
@ -37,8 +38,15 @@ internal class DefaultTimelineService(private val roomId: String,
|
||||||
|
|
||||||
override fun timeline(eventId: String?): LiveData<TimelineData> {
|
override fun timeline(eventId: String?): LiveData<TimelineData> {
|
||||||
clearUnlinkedEvents()
|
clearUnlinkedEvents()
|
||||||
|
var initialLoadKey = 0
|
||||||
if (eventId != null) {
|
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 {
|
val realmDataSourceFactory = monarchy.createDataSourceFactory {
|
||||||
buildDataSourceFactoryQuery(it, eventId)
|
buildDataSourceFactoryQuery(it, eventId)
|
||||||
|
@ -52,10 +60,14 @@ internal class DefaultTimelineService(private val roomId: String,
|
||||||
val pagedListConfig = PagedList.Config.Builder()
|
val pagedListConfig = PagedList.Config.Builder()
|
||||||
.setEnablePlaceholders(false)
|
.setEnablePlaceholders(false)
|
||||||
.setPageSize(PAGE_SIZE)
|
.setPageSize(PAGE_SIZE)
|
||||||
|
.setInitialLoadSizeHint(PAGE_SIZE)
|
||||||
.setPrefetchDistance(PREFETCH_DISTANCE)
|
.setPrefetchDistance(PREFETCH_DISTANCE)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
val livePagedListBuilder = LivePagedListBuilder(domainSourceFactory, pagedListConfig).setBoundaryCallback(boundaryCallback)
|
val livePagedListBuilder = LivePagedListBuilder(domainSourceFactory, pagedListConfig)
|
||||||
|
.setBoundaryCallback(boundaryCallback)
|
||||||
|
.setInitialLoadKey(initialLoadKey)
|
||||||
|
|
||||||
val eventsLiveData = monarchy.findAllPagedWithChanges(realmDataSourceFactory, livePagedListBuilder)
|
val eventsLiveData = monarchy.findAllPagedWithChanges(realmDataSourceFactory, livePagedListBuilder)
|
||||||
|
|
||||||
return LiveDataUtils.combine(eventsLiveData, boundaryCallback.status) { events, status ->
|
return LiveDataUtils.combine(eventsLiveData, boundaryCallback.status) { events, status ->
|
||||||
|
@ -76,19 +88,12 @@ internal class DefaultTimelineService(private val roomId: String,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fetchEventIfNeeded(eventId: String) {
|
private fun indexOfEvent(eventId: String): Int {
|
||||||
if (!isEventPersisted(eventId)) {
|
var displayIndex = EVENT_NOT_FOUND_INDEX
|
||||||
val params = GetContextOfEventTask.Params(roomId, eventId)
|
|
||||||
contextOfEventTask.configureWith(params).executeBy(taskExecutor)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun isEventPersisted(eventId: String): Boolean {
|
|
||||||
var isEventPersisted = false
|
|
||||||
monarchy.doWithRealm {
|
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> {
|
private fun buildDataSourceFactoryQuery(realm: Realm, eventId: String?): RealmQuery<EventEntity> {
|
||||||
|
|
Loading…
Reference in a new issue