Timeline: fix getContext

This commit is contained in:
ganfra 2020-01-29 16:02:53 +01:00
parent a8f783bbfa
commit 7f72af426b
2 changed files with 23 additions and 30 deletions

View File

@ -43,7 +43,7 @@ internal class DefaultGetContextOfEventTask @Inject constructor(
val filter = filterRepository.getRoomFilter() val filter = filterRepository.getRoomFilter()
val response = executeRequest<EventContextResponse>(eventBus) { val response = executeRequest<EventContextResponse>(eventBus) {
// We are limiting the response to the event with eventId to be sure we don't have any issue with potential merging process. // We are limiting the response to the event with eventId to be sure we don't have any issue with potential merging process.
apiCall = roomAPI.getContextOfEvent(params.roomId, params.eventId, 1, filter) apiCall = roomAPI.getContextOfEvent(params.roomId, params.eventId, 0, filter)
} }
return tokenChunkEventPersistor.insertInDb(response, params.roomId, PaginationDirection.BACKWARDS) return tokenChunkEventPersistor.insertInDb(response, params.roomId, PaginationDirection.BACKWARDS)
} }

View File

@ -30,7 +30,6 @@ import im.vector.matrix.android.internal.database.mapper.asDomain
import im.vector.matrix.android.internal.database.model.ChunkEntity import im.vector.matrix.android.internal.database.model.ChunkEntity
import im.vector.matrix.android.internal.database.model.ChunkEntityFields import im.vector.matrix.android.internal.database.model.ChunkEntityFields
import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryEntity import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryEntity
import im.vector.matrix.android.internal.database.model.EventEntity
import im.vector.matrix.android.internal.database.model.RoomEntity import im.vector.matrix.android.internal.database.model.RoomEntity
import im.vector.matrix.android.internal.database.model.TimelineEventEntity import im.vector.matrix.android.internal.database.model.TimelineEventEntity
import im.vector.matrix.android.internal.database.model.TimelineEventEntityFields import im.vector.matrix.android.internal.database.model.TimelineEventEntityFields
@ -61,7 +60,6 @@ import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicReference import java.util.concurrent.atomic.AtomicReference
import kotlin.math.max import kotlin.math.max
import kotlin.math.min
private const val MIN_FETCHING_COUNT = 30 private const val MIN_FETCHING_COUNT = 30
@ -374,10 +372,9 @@ internal class DefaultTimeline(
*/ */
private fun paginateInternal(startDisplayIndex: Int?, private fun paginateInternal(startDisplayIndex: Int?,
direction: Timeline.Direction, direction: Timeline.Direction,
count: Int, count: Int): Boolean {
strict: Boolean = false): Boolean {
updateState(direction) { it.copy(requestedPaginationCount = count, isPaginating = true) } updateState(direction) { it.copy(requestedPaginationCount = count, isPaginating = true) }
val builtCount = buildTimelineEvents(startDisplayIndex, direction, count.toLong(), strict) val builtCount = buildTimelineEvents(startDisplayIndex, direction, count.toLong())
val shouldFetchMore = builtCount < count && !hasReachedEnd(direction) val shouldFetchMore = builtCount < count && !hasReachedEnd(direction)
if (shouldFetchMore) { if (shouldFetchMore) {
val newRequestedCount = count - builtCount val newRequestedCount = count - builtCount
@ -387,7 +384,6 @@ internal class DefaultTimeline(
} else { } else {
updateState(direction) { it.copy(isPaginating = false, requestedPaginationCount = 0) } updateState(direction) { it.copy(isPaginating = false, requestedPaginationCount = 0) }
} }
return !shouldFetchMore return !shouldFetchMore
} }
@ -451,12 +447,12 @@ internal class DefaultTimeline(
if (currentInitialEventId != null && shouldFetchInitialEvent) { if (currentInitialEventId != null && shouldFetchInitialEvent) {
fetchEvent(currentInitialEventId) fetchEvent(currentInitialEventId)
} else { } else {
val count = min(settings.initialSize, filteredEvents.size) val count = filteredEvents.size.coerceAtMost(settings.initialSize)
if (initialEventId == null) { if (initialEventId == null) {
paginateInternal(initialDisplayIndex, Timeline.Direction.BACKWARDS, count, strict = false) paginateInternal(initialDisplayIndex, Timeline.Direction.BACKWARDS, count)
} else { } else {
paginateInternal(initialDisplayIndex, Timeline.Direction.FORWARDS, count / 2, strict = false) paginateInternal(initialDisplayIndex, Timeline.Direction.FORWARDS, (count / 2).coerceAtLeast(1))
paginateInternal(initialDisplayIndex, Timeline.Direction.BACKWARDS, count / 2, strict = true) paginateInternal(initialDisplayIndex?.minus(1), Timeline.Direction.BACKWARDS, (count / 2).coerceAtLeast(1))
} }
} }
postSnapshot() postSnapshot()
@ -557,7 +553,7 @@ internal class DefaultTimeline(
* This has to be called on TimelineThread as it access realm live results * This has to be called on TimelineThread as it access realm live results
*/ */
private fun getLiveChunk(): ChunkEntity? { private fun getLiveChunk(): ChunkEntity? {
return filteredEvents.firstOrNull()?.chunk?.firstOrNull() return nonFilteredEvents.firstOrNull()?.chunk?.firstOrNull()
} }
/** /**
@ -566,13 +562,12 @@ internal class DefaultTimeline(
*/ */
private fun buildTimelineEvents(startDisplayIndex: Int?, private fun buildTimelineEvents(startDisplayIndex: Int?,
direction: Timeline.Direction, direction: Timeline.Direction,
count: Long, count: Long): Int {
strict: Boolean = false): Int {
if (count < 1 || startDisplayIndex == null) { if (count < 1 || startDisplayIndex == null) {
return 0 return 0
} }
val start = System.currentTimeMillis() val start = System.currentTimeMillis()
val offsetResults = getOffsetResults(startDisplayIndex, direction, count, strict) val offsetResults = getOffsetResults(startDisplayIndex, direction, count)
if (offsetResults.isEmpty()) { if (offsetResults.isEmpty()) {
return 0 return 0
} }
@ -613,23 +608,16 @@ internal class DefaultTimeline(
*/ */
private fun getOffsetResults(startDisplayIndex: Int, private fun getOffsetResults(startDisplayIndex: Int,
direction: Timeline.Direction, direction: Timeline.Direction,
count: Long, count: Long): RealmResults<TimelineEventEntity> {
strict: Boolean): RealmResults<TimelineEventEntity> {
val offsetQuery = filteredEvents.where() val offsetQuery = filteredEvents.where()
if (direction == Timeline.Direction.BACKWARDS) { if (direction == Timeline.Direction.BACKWARDS) {
offsetQuery.sort(TimelineEventEntityFields.DISPLAY_INDEX, Sort.DESCENDING) offsetQuery
if (strict) { .sort(TimelineEventEntityFields.DISPLAY_INDEX, Sort.DESCENDING)
offsetQuery.lessThan(TimelineEventEntityFields.DISPLAY_INDEX, startDisplayIndex) .lessThanOrEqualTo(TimelineEventEntityFields.DISPLAY_INDEX, startDisplayIndex)
} else { } else {
offsetQuery.lessThanOrEqualTo(TimelineEventEntityFields.DISPLAY_INDEX, startDisplayIndex) offsetQuery
} .sort(TimelineEventEntityFields.DISPLAY_INDEX, Sort.ASCENDING)
} else { .greaterThanOrEqualTo(TimelineEventEntityFields.DISPLAY_INDEX, startDisplayIndex)
offsetQuery.sort(TimelineEventEntityFields.DISPLAY_INDEX, Sort.ASCENDING)
if (strict) {
offsetQuery.greaterThan(TimelineEventEntityFields.DISPLAY_INDEX, startDisplayIndex)
} else {
offsetQuery.greaterThanOrEqualTo(TimelineEventEntityFields.DISPLAY_INDEX, startDisplayIndex)
}
} }
return offsetQuery return offsetQuery
.limit(count) .limit(count)
@ -652,6 +640,11 @@ internal class DefaultTimeline(
val params = GetContextOfEventTask.Params(roomId, eventId) val params = GetContextOfEventTask.Params(roomId, eventId)
cancelableBag += contextOfEventTask.configureWith(params) { cancelableBag += contextOfEventTask.configureWith(params) {
callback = object : MatrixCallback<TokenChunkEventPersistor.Result> { callback = object : MatrixCallback<TokenChunkEventPersistor.Result> {
override fun onSuccess(data: TokenChunkEventPersistor.Result) {
postSnapshot()
}
override fun onFailure(failure: Throwable) { override fun onFailure(failure: Throwable) {
postFailure(failure) postFailure(failure)
} }