Ensure proper double linking of TimelineChunks

We need both directions so getOffsetIndex() produces correct results in
all cases.

Change-Id: Icbb3810c664febba976b81ae882a6581dc0f6056
This commit is contained in:
SpiritCroc 2022-03-17 11:02:32 +01:00
parent b982841ed5
commit 0d43fc6861

View file

@ -83,12 +83,16 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity,
isLastBackward.set(chunkEntity.isLastBackward)
}
if (changeSet.isFieldChanged(ChunkEntityFields.NEXT_CHUNK.`$`)) {
nextChunk = createTimelineChunk(chunkEntity.nextChunk)
nextChunk = createTimelineChunk(chunkEntity.nextChunk).also {
it?.prevChunk = this
}
dimber.i{"TimelineChunk.$dbgId set next to ${nextChunk?.dbgId}"}
nextChunkLatch?.complete(Unit)
}
if (changeSet.isFieldChanged(ChunkEntityFields.PREV_CHUNK.`$`)) {
prevChunk = createTimelineChunk(chunkEntity.prevChunk)
prevChunk = createTimelineChunk(chunkEntity.prevChunk).also {
it?.nextChunk = this
}
dimber.i{"TimelineChunk.$dbgId set prev to ${prevChunk?.dbgId}"}
prevChunkLatch?.complete(Unit)
}
@ -186,7 +190,9 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity,
when {
nextChunkEntity != null -> {
if (nextChunk == null) {
nextChunk = createTimelineChunk(nextChunkEntity)
nextChunk = createTimelineChunk(nextChunkEntity).also {
it?.prevChunk = this
}
}
nextChunk?.loadMore(offsetCount, direction, fetchFromServerIfNeeded) ?: LoadMoreResult.FAILURE
}
@ -202,7 +208,9 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity,
when {
prevChunkEntity != null -> {
if (prevChunk == null) {
prevChunk = createTimelineChunk(prevChunkEntity)
prevChunk = createTimelineChunk(prevChunkEntity).also {
it?.nextChunk = this
}
}
prevChunk?.loadMore(offsetCount, direction, fetchFromServerIfNeeded) ?: LoadMoreResult.FAILURE
}