forked from GitHub-Mirror/riotX-android
Start introducing tests
This commit is contained in:
@ -2,7 +2,6 @@ package im.vector.matrix.android.internal.database.helper
|
||||
|
||||
import im.vector.matrix.android.api.session.events.model.Event
|
||||
import im.vector.matrix.android.api.session.events.model.EventType
|
||||
import im.vector.matrix.android.internal.database.mapper.asDomain
|
||||
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.EventEntity
|
||||
@ -41,7 +40,7 @@ internal fun ChunkEntity.merge(chunkToMerge: ChunkEntity,
|
||||
eventsToMerge = chunkToMerge.events
|
||||
}
|
||||
eventsToMerge.forEach {
|
||||
add(it.asDomain(), direction, isUnlinked = isUnlinked)
|
||||
add(it, direction, isUnlinked = isUnlinked)
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,16 +62,23 @@ internal fun ChunkEntity.add(event: Event,
|
||||
direction: PaginationDirection,
|
||||
stateIndexOffset: Int = 0,
|
||||
isUnlinked: Boolean = false) {
|
||||
add(event.asEntity(), direction, stateIndexOffset, isUnlinked)
|
||||
}
|
||||
|
||||
internal fun ChunkEntity.add(eventEntity: EventEntity,
|
||||
direction: PaginationDirection,
|
||||
stateIndexOffset: Int = 0,
|
||||
isUnlinked: Boolean = false) {
|
||||
if (!isManaged) {
|
||||
throw IllegalStateException("Chunk entity should be managed to use fast contains")
|
||||
}
|
||||
|
||||
if (event.eventId == null || events.fastContains(event.eventId)) {
|
||||
if (eventEntity.eventId.isEmpty() || events.fastContains(eventEntity.eventId)) {
|
||||
return
|
||||
}
|
||||
|
||||
var currentStateIndex = lastStateIndex(direction, defaultValue = stateIndexOffset)
|
||||
if (direction == PaginationDirection.FORWARDS && event.isStateEvent()) {
|
||||
if (direction == PaginationDirection.FORWARDS && EventType.isStateEvent(eventEntity.type)) {
|
||||
currentStateIndex += 1
|
||||
} else if (direction == PaginationDirection.BACKWARDS && events.isNotEmpty()) {
|
||||
val lastEventType = events.last()?.type ?: ""
|
||||
@ -81,7 +87,6 @@ internal fun ChunkEntity.add(event: Event,
|
||||
}
|
||||
}
|
||||
|
||||
val eventEntity = event.asEntity()
|
||||
eventEntity.stateIndex = currentStateIndex
|
||||
eventEntity.isUnlinked = isUnlinked
|
||||
val position = if (direction == PaginationDirection.FORWARDS) 0 else this.events.size
|
||||
@ -90,7 +95,7 @@ internal fun ChunkEntity.add(event: Event,
|
||||
|
||||
internal fun ChunkEntity.lastStateIndex(direction: PaginationDirection, defaultValue: Int = 0): Int {
|
||||
return when (direction) {
|
||||
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
|
||||
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
|
||||
}
|
@ -69,6 +69,7 @@ internal fun RealmList<EventEntity>.find(eventId: String): EventEntity? {
|
||||
return this.where().equalTo(EventEntityFields.EVENT_ID, eventId).findFirst()
|
||||
}
|
||||
|
||||
internal fun RealmList<EventEntity>.fastContains(eventId: String): Boolean {
|
||||
internal fun RealmList<EventEntity>.
|
||||
fastContains(eventId: String): Boolean {
|
||||
return this.find(eventId) != null
|
||||
}
|
||||
|
Reference in New Issue
Block a user