Timeline : make tests compile and pass

This commit is contained in:
ganfra
2019-04-01 15:18:52 +02:00
parent 94db36d6c4
commit be6a4efacb
8 changed files with 150 additions and 69 deletions

View File

@ -35,6 +35,18 @@ inline fun <reified T> Content?.toModel(): T? {
}
}
/**
* This methods is a facility method to map a model to a json Content
*/
@Suppress("UNCHECKED_CAST")
inline fun <reified T> T?.toContent(): Content? {
return this?.let {
val moshi = MoshiProvider.providesMoshi()
val moshiAdapter = moshi.adapter(T::class.java)
return moshiAdapter.toJsonValue(it) as Content
}
}
/**
* Generic event class with all possible fields for events.
* The content and prevContent json fields can easily be mapped to a model with [toModel] method.

View File

@ -54,10 +54,14 @@ internal fun ChunkEntity.merge(roomId: String,
if (direction == PaginationDirection.FORWARDS) {
this.nextToken = chunkToMerge.nextToken
this.isLastForward = chunkToMerge.isLastForward
this.forwardsStateIndex = chunkToMerge.forwardsStateIndex
this.forwardsDisplayIndex = chunkToMerge.forwardsDisplayIndex
eventsToMerge = chunkToMerge.events.sort(EventEntityFields.DISPLAY_INDEX, Sort.ASCENDING)
} else {
this.prevToken = chunkToMerge.prevToken
this.isLastBackward = chunkToMerge.isLastBackward
this.backwardsStateIndex = chunkToMerge.backwardsStateIndex
this.backwardsDisplayIndex = chunkToMerge.backwardsDisplayIndex
eventsToMerge = chunkToMerge.events.sort(EventEntityFields.DISPLAY_INDEX, Sort.DESCENDING)
}
eventsToMerge.forEach {
@ -111,8 +115,7 @@ internal fun ChunkEntity.add(roomId: String,
this.displayIndex = currentDisplayIndex
}
// We are not using the order of the list, but will be sorting with displayIndex field
val position = if (direction == PaginationDirection.FORWARDS) 0 else this.events.size
events.add(position, eventEntity)
events.add(eventEntity)
}
private fun ChunkEntity.assertIsManaged() {

View File

@ -48,7 +48,6 @@ internal class DefaultLoadRoomMembersTask(private val roomAPI: RoomAPI,
return if (areAllMembersAlreadyLoaded(params.roomId)) {
Try.just(true)
} else {
//TODO use this token
val lastToken = syncTokenStore.getLastToken()
executeRequest<RoomMembersResponse> {
apiCall = roomAPI.getMembers(params.roomId, lastToken, null, params.excludeMembership?.value)
@ -63,7 +62,7 @@ internal class DefaultLoadRoomMembersTask(private val roomAPI: RoomAPI,
.tryTransactionSync { realm ->
// We ignore all the already known members
val roomEntity = RoomEntity.where(realm, roomId).findFirst()
?: realm.createObject(roomId)
?: realm.createObject(roomId)
val roomMembers = RoomMembers(realm, roomId).getLoaded()
val eventsToInsert = response.roomMemberEvents.filter { !roomMembers.containsKey(it.stateKey) }
@ -78,9 +77,9 @@ internal class DefaultLoadRoomMembersTask(private val roomAPI: RoomAPI,
private fun areAllMembersAlreadyLoaded(roomId: String): Boolean {
return monarchy
.fetchAllCopiedSync { RoomEntity.where(it, roomId) }
.firstOrNull()
?.areAllMembersLoaded ?: false
.fetchAllCopiedSync { RoomEntity.where(it, roomId) }
.firstOrNull()
?.areAllMembersLoaded ?: false
}
}

View File

@ -36,7 +36,12 @@ import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoo
import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.task.TaskExecutor
import im.vector.matrix.android.internal.task.configureWith
import io.realm.*
import io.realm.OrderedRealmCollectionChangeListener
import io.realm.Realm
import io.realm.RealmConfiguration
import io.realm.RealmQuery
import io.realm.RealmResults
import io.realm.Sort
import timber.log.Timber
import java.util.*
import java.util.concurrent.atomic.AtomicBoolean
@ -97,10 +102,14 @@ internal class DefaultTimeline(
val state = getPaginationState(direction)
if (state.isPaginating) {
// We are getting new items from pagination
paginateInternal(startDisplayIndex, direction, state.requestedCount)
val shouldPostSnapshot = paginateInternal(startDisplayIndex, direction, state.requestedCount)
if (shouldPostSnapshot) {
postSnapshot()
}
} else {
// We are getting new items from sync
buildTimelineEvents(startDisplayIndex, direction, range.length.toLong())
postSnapshot()
}
}
}
@ -114,7 +123,10 @@ internal class DefaultTimeline(
}
Timber.v("Paginate $direction of $count items")
val startDisplayIndex = if (direction == Timeline.Direction.BACKWARDS) prevDisplayIndex else nextDisplayIndex
paginateInternal(startDisplayIndex, direction, count)
val shouldPostSnapshot = paginateInternal(startDisplayIndex, direction, count)
if (shouldPostSnapshot) {
postSnapshot()
}
}
}
@ -191,13 +203,15 @@ internal class DefaultTimeline(
/**
* This has to be called on TimelineThread as it access realm live results
* @return true if snapshot should be posted
*/
private fun paginateInternal(startDisplayIndex: Int,
direction: Timeline.Direction,
count: Int) {
count: Int): Boolean {
updatePaginationState(direction) { it.copy(requestedCount = count, isPaginating = true) }
val builtCount = buildTimelineEvents(startDisplayIndex, direction, count.toLong())
if (builtCount < count && !hasReachedEnd(direction)) {
val shouldFetchMore = builtCount < count && !hasReachedEnd(direction)
if (shouldFetchMore) {
val newRequestedCount = count - builtCount
updatePaginationState(direction) { it.copy(requestedCount = newRequestedCount) }
val fetchingCount = Math.max(MIN_FETCHING_COUNT, newRequestedCount)
@ -205,6 +219,7 @@ internal class DefaultTimeline(
} else {
updatePaginationState(direction) { it.copy(isPaginating = false, requestedCount = 0) }
}
return !shouldFetchMore
}
private fun snapshot(): List<TimelineEvent> {
@ -252,12 +267,13 @@ internal class DefaultTimeline(
} else {
val count = Math.min(INITIAL_LOAD_SIZE, liveEvents.size)
if (isLive) {
paginate(Timeline.Direction.BACKWARDS, count)
paginateInternal(initialDisplayIndex, Timeline.Direction.BACKWARDS, count)
} else {
paginate(Timeline.Direction.FORWARDS, count / 2)
paginate(Timeline.Direction.BACKWARDS, count / 2)
paginateInternal(initialDisplayIndex, Timeline.Direction.FORWARDS, count / 2)
paginateInternal(initialDisplayIndex, Timeline.Direction.BACKWARDS, count / 2)
}
}
postSnapshot()
}
/**
@ -266,9 +282,9 @@ internal class DefaultTimeline(
private fun executePaginationTask(direction: Timeline.Direction, limit: Int) {
val token = getTokenLive(direction) ?: return
val params = PaginationTask.Params(roomId = roomId,
from = token,
direction = direction.toPaginationDirection(),
limit = limit)
from = token,
direction = direction.toPaginationDirection(),
limit = limit)
Timber.v("Should fetch $limit items $direction")
paginationTask.configureWith(params)
@ -336,8 +352,6 @@ internal class DefaultTimeline(
builtEvents.add(position, timelineEvent)
}
Timber.v("Built ${offsetResults.size} items from db")
val snapshot = snapshot()
mainHandler.post { listener?.onUpdated(snapshot) }
return offsetResults.size
}
@ -399,6 +413,11 @@ internal class DefaultTimeline(
contextOfEventTask.configureWith(params).executeBy(taskExecutor)
}
private fun postSnapshot() {
val snapshot = snapshot()
mainHandler.post { listener?.onUpdated(snapshot) }
}
// Extension methods ***************************************************************************
private fun Timeline.Direction.toPaginationDirection(): PaginationDirection {