2018-11-13 18:17:59 +00:00
|
|
|
package im.vector.matrix.android.internal.database.helper
|
|
|
|
|
|
|
|
import im.vector.matrix.android.api.session.events.model.Event
|
2018-11-22 10:20:50 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.EventType
|
2018-11-15 17:32:39 +00:00
|
|
|
import im.vector.matrix.android.internal.database.mapper.asDomain
|
2018-11-13 18:17:59 +00:00
|
|
|
import im.vector.matrix.android.internal.database.mapper.asEntity
|
|
|
|
import im.vector.matrix.android.internal.database.model.ChunkEntity
|
2018-11-22 10:20:50 +00:00
|
|
|
import im.vector.matrix.android.internal.database.model.EventEntityFields
|
2018-11-13 18:17:59 +00:00
|
|
|
import im.vector.matrix.android.internal.database.query.fastContains
|
2018-11-15 17:32:39 +00:00
|
|
|
import im.vector.matrix.android.internal.database.query.find
|
2018-11-13 18:17:59 +00:00
|
|
|
import im.vector.matrix.android.internal.session.room.timeline.PaginationDirection
|
2018-11-22 10:20:50 +00:00
|
|
|
import io.realm.Sort
|
2018-11-13 18:17:59 +00:00
|
|
|
|
2018-11-15 17:32:39 +00:00
|
|
|
|
|
|
|
internal fun ChunkEntity.merge(chunkEntity: ChunkEntity,
|
|
|
|
direction: PaginationDirection) {
|
|
|
|
|
2018-11-19 14:47:54 +00:00
|
|
|
|
|
|
|
chunkEntity.events.forEach {
|
|
|
|
addOrUpdate(it.asDomain(), direction)
|
|
|
|
}
|
2018-11-15 17:32:39 +00:00
|
|
|
if (direction == PaginationDirection.FORWARDS) {
|
|
|
|
nextToken = chunkEntity.nextToken
|
|
|
|
} else {
|
|
|
|
prevToken = chunkEntity.prevToken
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal fun ChunkEntity.addAll(events: List<Event>,
|
|
|
|
direction: PaginationDirection,
|
2018-11-22 10:20:50 +00:00
|
|
|
stateIndexOffset: Int = 0) {
|
2018-11-15 17:32:39 +00:00
|
|
|
|
|
|
|
events.forEach { event ->
|
2018-11-22 10:20:50 +00:00
|
|
|
addOrUpdate(event, direction, stateIndexOffset)
|
2018-11-15 17:32:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-25 15:17:47 +00:00
|
|
|
internal fun ChunkEntity.updateDisplayIndexes() {
|
|
|
|
events.forEachIndexed { index, eventEntity -> eventEntity.displayIndex = index }
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:32:39 +00:00
|
|
|
internal fun ChunkEntity.addOrUpdate(event: Event,
|
2018-11-19 14:47:54 +00:00
|
|
|
direction: PaginationDirection,
|
2018-11-22 10:20:50 +00:00
|
|
|
stateIndexOffset: Int = 0) {
|
2018-11-15 17:32:39 +00:00
|
|
|
if (!isManaged) {
|
2018-11-13 18:17:59 +00:00
|
|
|
throw IllegalStateException("Chunk entity should be managed to use fast contains")
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:32:42 +00:00
|
|
|
if (event.eventId == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-22 10:20:50 +00:00
|
|
|
var currentStateIndex = lastStateIndex(direction, defaultValue = stateIndexOffset)
|
|
|
|
if (direction == PaginationDirection.FORWARDS && event.isStateEvent()) {
|
|
|
|
currentStateIndex += 1
|
|
|
|
} else if (direction == PaginationDirection.BACKWARDS && events.isNotEmpty()) {
|
|
|
|
val lastEventType = events.last()?.type ?: ""
|
|
|
|
if (EventType.isStateEvent(lastEventType)) {
|
|
|
|
currentStateIndex -= 1
|
|
|
|
}
|
2018-11-19 14:47:54 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 17:32:39 +00:00
|
|
|
if (!events.fastContains(event.eventId)) {
|
|
|
|
val eventEntity = event.asEntity()
|
|
|
|
eventEntity.stateIndex = currentStateIndex
|
|
|
|
val position = if (direction == PaginationDirection.FORWARDS) 0 else this.events.size
|
|
|
|
events.add(position, eventEntity)
|
|
|
|
} else {
|
|
|
|
val eventEntity = events.find(event.eventId)
|
|
|
|
eventEntity?.stateIndex = currentStateIndex
|
2018-11-13 18:17:59 +00:00
|
|
|
}
|
2018-11-22 10:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal fun ChunkEntity.lastStateIndex(direction: PaginationDirection, defaultValue: Int = 0): Int {
|
|
|
|
return when (direction) {
|
2018-11-25 15:17:47 +00:00
|
|
|
PaginationDirection.FORWARDS -> events.where().sort(EventEntityFields.STATE_INDEX, Sort.DESCENDING).findFirst()?.stateIndex
|
|
|
|
PaginationDirection.BACKWARDS -> events.where().sort(EventEntityFields.STATE_INDEX, Sort.ASCENDING).findFirst()?.stateIndex
|
|
|
|
} ?: defaultValue
|
2018-11-13 18:17:59 +00:00
|
|
|
}
|