forked from GitHub-Mirror/riotX-android
Makes back pagination work. Still need to refine but ok for proto.
This commit is contained in:
@ -7,7 +7,6 @@ import io.realm.annotations.LinkingObjects
|
||||
|
||||
open class ChunkEntity(var prevToken: String? = null,
|
||||
var nextToken: String? = null,
|
||||
var isLimited: Boolean = true,
|
||||
var events: RealmList<EventEntity> = RealmList()
|
||||
) : RealmObject() {
|
||||
|
||||
|
@ -33,7 +33,7 @@ fun ChunkEntity.Companion.findWithNextToken(realm: Realm, roomId: String, nextTo
|
||||
.findFirst()
|
||||
}
|
||||
|
||||
fun ChunkEntity.Companion.findLastFromRoom(realm: Realm, roomId: String): ChunkEntity? {
|
||||
fun ChunkEntity.Companion.findLastLiveChunkFromRoom(realm: Realm, roomId: String): ChunkEntity? {
|
||||
return where(realm, roomId)
|
||||
.and()
|
||||
.isNull("nextToken")
|
||||
|
@ -5,6 +5,7 @@ import im.vector.matrix.android.internal.database.model.EventEntity
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmQuery
|
||||
import io.realm.RealmResults
|
||||
import io.realm.Sort
|
||||
|
||||
fun EventEntity.Companion.where(realm: Realm, roomId: String): RealmQuery<EventEntity> {
|
||||
return realm.where(EventEntity::class.java)
|
||||
@ -23,9 +24,9 @@ fun EventEntity.Companion.where(realm: Realm, chunk: ChunkEntity?): RealmQuery<E
|
||||
}
|
||||
|
||||
fun RealmResults<EventEntity>.getLast(type: String? = null): EventEntity? {
|
||||
var query = this.where()
|
||||
var query = this.where().sort("originServerTs", Sort.DESCENDING)
|
||||
if (type != null) {
|
||||
query = query.equalTo("type", type)
|
||||
}
|
||||
return query.findAll().last(null)
|
||||
return query.findFirst()
|
||||
}
|
@ -11,6 +11,7 @@ import im.vector.matrix.android.internal.database.model.ChunkEntity
|
||||
import im.vector.matrix.android.internal.database.query.where
|
||||
import im.vector.matrix.android.internal.session.room.timeline.PaginationRequest
|
||||
import im.vector.matrix.android.internal.session.room.timeline.TimelineBoundaryCallback
|
||||
import io.realm.Sort
|
||||
import org.koin.standalone.KoinComponent
|
||||
import org.koin.standalone.inject
|
||||
import java.util.concurrent.Executors
|
||||
@ -28,17 +29,16 @@ data class DefaultRoom(
|
||||
ChunkEntity.where(realm, roomId)
|
||||
.findAll()
|
||||
.last(null)
|
||||
?.let { it.events }
|
||||
?.where()
|
||||
?.sort("originServerTs")
|
||||
?.let {
|
||||
it.events.where().sort("originServerTs", Sort.DESCENDING)
|
||||
}
|
||||
}
|
||||
val domainSourceFactory = realmDataSourceFactory.map { EventMapper.map(it) }
|
||||
|
||||
val pagedListConfig = PagedList.Config.Builder()
|
||||
.setEnablePlaceholders(false)
|
||||
.setPageSize(10)
|
||||
.setInitialLoadSizeHint(30)
|
||||
.setPrefetchDistance(5)
|
||||
.setPrefetchDistance(10)
|
||||
.build()
|
||||
|
||||
val livePagedListBuilder = LivePagedListBuilder(domainSourceFactory, pagedListConfig).setBoundaryCallback(boundaryCallback)
|
||||
|
@ -7,6 +7,7 @@ import com.zhuinden.monarchy.Monarchy
|
||||
import im.vector.matrix.android.api.MatrixCallback
|
||||
import im.vector.matrix.android.api.failure.Failure
|
||||
import im.vector.matrix.android.api.util.Cancelable
|
||||
import im.vector.matrix.android.internal.database.DBConstants
|
||||
import im.vector.matrix.android.internal.database.mapper.asEntity
|
||||
import im.vector.matrix.android.internal.database.model.ChunkEntity
|
||||
import im.vector.matrix.android.internal.database.model.RoomEntity
|
||||
@ -14,6 +15,7 @@ import im.vector.matrix.android.internal.database.query.findAllIncludingEvents
|
||||
import im.vector.matrix.android.internal.database.query.findWithNextToken
|
||||
import im.vector.matrix.android.internal.database.query.findWithPrevToken
|
||||
import im.vector.matrix.android.internal.database.query.where
|
||||
import im.vector.matrix.android.internal.legacy.util.FilterUtil
|
||||
import im.vector.matrix.android.internal.network.executeRequest
|
||||
import im.vector.matrix.android.internal.session.room.RoomAPI
|
||||
import im.vector.matrix.android.internal.session.room.model.PaginationDirection
|
||||
@ -32,10 +34,10 @@ class PaginationRequest(private val roomAPI: RoomAPI,
|
||||
from: String?,
|
||||
direction: PaginationDirection,
|
||||
limit: Int = 10,
|
||||
filter: String? = null,
|
||||
callback: MatrixCallback<TokenChunkEvent>
|
||||
): Cancelable {
|
||||
val job = GlobalScope.launch(coroutineDispatchers.main) {
|
||||
val filter = FilterUtil.createRoomEventFilter(true)?.toJSONString()
|
||||
val chunkOrFailure = execute(roomId, from, direction, limit, filter)
|
||||
chunkOrFailure.bimap({ callback.onFailure(it) }, { callback.onSuccess(it) })
|
||||
}
|
||||
@ -70,14 +72,13 @@ class PaginationRequest(private val roomAPI: RoomAPI,
|
||||
private fun insertInDb(chunkEvent: TokenChunkEvent, roomId: String) {
|
||||
monarchy.runTransactionSync { realm ->
|
||||
val roomEntity = RoomEntity.where(realm, roomId).findFirst()
|
||||
?: return@runTransactionSync
|
||||
?: return@runTransactionSync
|
||||
|
||||
val nextChunk = ChunkEntity.findWithPrevToken(realm, roomId, chunkEvent.nextToken)
|
||||
val prevChunk = ChunkEntity.findWithNextToken(realm, roomId, chunkEvent.prevToken)
|
||||
|
||||
val mergedEvents = chunkEvent.chunk + chunkEvent.stateEvents
|
||||
val mergedEventIds = mergedEvents.filter { it.eventId != null }.map { it.eventId!! }
|
||||
val chunksOverlapped = ChunkEntity.findAllIncludingEvents(realm, mergedEventIds)
|
||||
val eventIds = chunkEvent.chunk.filter { it.eventId != null }.map { it.eventId!! }
|
||||
val chunksOverlapped = ChunkEntity.findAllIncludingEvents(realm, eventIds)
|
||||
val hasOverlapped = chunksOverlapped.isNotEmpty()
|
||||
|
||||
val currentChunk = if (nextChunk != null) {
|
||||
@ -85,9 +86,25 @@ class PaginationRequest(private val roomAPI: RoomAPI,
|
||||
} else {
|
||||
ChunkEntity()
|
||||
}
|
||||
|
||||
currentChunk.prevToken = chunkEvent.prevToken
|
||||
mergedEvents.forEach { event ->
|
||||
|
||||
|
||||
val stateEventsChunk = ChunkEntity.findWithNextToken(realm, roomId, DBConstants.STATE_EVENTS_CHUNK_TOKEN)
|
||||
?: ChunkEntity().apply {
|
||||
this.prevToken = DBConstants.STATE_EVENTS_CHUNK_TOKEN
|
||||
this.nextToken = DBConstants.STATE_EVENTS_CHUNK_TOKEN
|
||||
}
|
||||
|
||||
chunkEvent.stateEvents.forEach { stateEvent ->
|
||||
val eventEntity = stateEvent.asEntity().let {
|
||||
realm.copyToRealmOrUpdate(it)
|
||||
}
|
||||
if (!stateEventsChunk.events.contains(eventEntity)) {
|
||||
stateEventsChunk.events.add(0, eventEntity)
|
||||
}
|
||||
}
|
||||
|
||||
chunkEvent.chunk.forEach { event ->
|
||||
val eventEntity = event.asEntity().let {
|
||||
realm.copyToRealmOrUpdate(it)
|
||||
}
|
||||
@ -101,13 +118,14 @@ class PaginationRequest(private val roomAPI: RoomAPI,
|
||||
roomEntity.chunks.remove(prevChunk)
|
||||
|
||||
} else if (hasOverlapped) {
|
||||
chunksOverlapped.forEach { chunk ->
|
||||
chunk.events.forEach { event ->
|
||||
chunksOverlapped.forEach { overlapped ->
|
||||
overlapped.events.forEach { event ->
|
||||
if (!currentChunk.events.contains(event)) {
|
||||
currentChunk.events.add(0, event)
|
||||
}
|
||||
}
|
||||
roomEntity.chunks.remove(chunk)
|
||||
currentChunk.prevToken = overlapped.prevToken
|
||||
roomEntity.chunks.remove(overlapped)
|
||||
}
|
||||
}
|
||||
if (!roomEntity.chunks.contains(currentChunk)) {
|
||||
|
@ -32,7 +32,7 @@ class TimelineBoundaryCallback(private val paginationRequest: PaginationRequest,
|
||||
return@doWithRealm
|
||||
}
|
||||
val chunkEntity = ChunkEntity.findAllIncludingEvents(realm, Collections.singletonList(itemAtEnd.eventId)).firstOrNull()
|
||||
paginationRequest.execute(roomId, chunkEntity?.nextToken, PaginationDirection.FORWARDS, callback = createCallback(it))
|
||||
paginationRequest.execute(roomId, chunkEntity?.prevToken, PaginationDirection.BACKWARDS, callback = createCallback(it))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -44,7 +44,7 @@ class TimelineBoundaryCallback(private val paginationRequest: PaginationRequest,
|
||||
return@doWithRealm
|
||||
}
|
||||
val chunkEntity = ChunkEntity.findAllIncludingEvents(realm, Collections.singletonList(itemAtFront.eventId)).firstOrNull()
|
||||
paginationRequest.execute(roomId, chunkEntity?.prevToken, PaginationDirection.BACKWARDS, callback = createCallback(it))
|
||||
paginationRequest.execute(roomId, chunkEntity?.nextToken, PaginationDirection.FORWARDS, callback = createCallback(it))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import im.vector.matrix.android.internal.database.mapper.asEntity
|
||||
import im.vector.matrix.android.internal.database.model.ChunkEntity
|
||||
import im.vector.matrix.android.internal.database.model.RoomEntity
|
||||
import im.vector.matrix.android.internal.database.query.findAllIncludingEvents
|
||||
import im.vector.matrix.android.internal.database.query.findLastFromRoom
|
||||
import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoom
|
||||
import im.vector.matrix.android.internal.database.query.where
|
||||
import im.vector.matrix.android.internal.session.sync.model.InvitedRoomSync
|
||||
import im.vector.matrix.android.internal.session.sync.model.RoomSync
|
||||
@ -95,14 +95,13 @@ class RoomSyncHandler(private val monarchy: Monarchy) {
|
||||
isLimited: Boolean = true): ChunkEntity {
|
||||
|
||||
val chunkEntity = if (!isLimited) {
|
||||
ChunkEntity.findLastFromRoom(realm, roomId)
|
||||
ChunkEntity.findLastLiveChunkFromRoom(realm, roomId)
|
||||
} else {
|
||||
val eventIds = eventList.filter { it.eventId != null }.map { it.eventId!! }
|
||||
ChunkEntity.findAllIncludingEvents(realm, eventIds).firstOrNull()
|
||||
} ?: ChunkEntity().apply { this.prevToken = prevToken }
|
||||
|
||||
chunkEntity.nextToken = nextToken
|
||||
chunkEntity.isLimited = isLimited
|
||||
|
||||
eventList.forEach { event ->
|
||||
val eventEntity = event.asEntity().let {
|
||||
|
Reference in New Issue
Block a user