Deport timeline in his own class

This commit is contained in:
ganfra 2018-10-25 17:25:58 +02:00
parent 2faba01662
commit 7ba67d6c2b
6 changed files with 93 additions and 56 deletions

View File

@ -1,19 +1,14 @@
package im.vector.matrix.android.api.session.room package im.vector.matrix.android.api.session.room


import android.arch.lifecycle.LiveData
import android.arch.paging.PagedList
import im.vector.matrix.android.api.session.events.model.EnrichedEvent
import im.vector.matrix.android.api.session.room.model.MyMembership import im.vector.matrix.android.api.session.room.model.MyMembership
import im.vector.matrix.android.api.util.Cancelable import im.vector.matrix.android.api.util.Cancelable


interface Room { interface Room: TimelineHolder {


val roomId: String val roomId: String


val myMembership: MyMembership val myMembership: MyMembership


fun liveTimeline(): LiveData<PagedList<EnrichedEvent>>

fun getNumberOfJoinedMembers(): Int fun getNumberOfJoinedMembers(): Int


fun loadRoomMembersIfNeeded(): Cancelable fun loadRoomMembersIfNeeded(): Cancelable

View File

@ -0,0 +1,11 @@
package im.vector.matrix.android.api.session.room

import android.arch.lifecycle.LiveData
import android.arch.paging.PagedList
import im.vector.matrix.android.api.session.events.model.EnrichedEvent

interface TimelineHolder {

fun liveTimeline(): LiveData<PagedList<EnrichedEvent>>

}

View File

@ -1,78 +1,36 @@
package im.vector.matrix.android.internal.session.room package im.vector.matrix.android.internal.session.room


import android.arch.lifecycle.LiveData import android.arch.lifecycle.LiveData
import android.arch.paging.LivePagedListBuilder
import android.arch.paging.PagedList import android.arch.paging.PagedList
import com.zhuinden.monarchy.Monarchy import com.zhuinden.monarchy.Monarchy
import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.session.events.interceptor.EnrichedEventInterceptor
import im.vector.matrix.android.api.session.events.model.EnrichedEvent import im.vector.matrix.android.api.session.events.model.EnrichedEvent
import im.vector.matrix.android.api.session.room.Room import im.vector.matrix.android.api.session.room.Room
import im.vector.matrix.android.api.session.room.TimelineHolder
import im.vector.matrix.android.api.session.room.model.Membership import im.vector.matrix.android.api.session.room.model.Membership
import im.vector.matrix.android.api.session.room.model.MyMembership import im.vector.matrix.android.api.session.room.model.MyMembership
import im.vector.matrix.android.api.util.Cancelable import im.vector.matrix.android.api.util.Cancelable
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.RoomEntity import im.vector.matrix.android.internal.database.model.RoomEntity
import im.vector.matrix.android.internal.database.model.RoomSummaryEntity import im.vector.matrix.android.internal.database.model.RoomSummaryEntity
import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.session.events.interceptor.MessageEventInterceptor
import im.vector.matrix.android.internal.session.room.members.LoadRoomMembersRequest import im.vector.matrix.android.internal.session.room.members.LoadRoomMembersRequest
import im.vector.matrix.android.internal.session.room.timeline.PaginationRequest
import im.vector.matrix.android.internal.session.room.timeline.TimelineBoundaryCallback
import im.vector.matrix.android.internal.session.sync.SyncTokenStore import im.vector.matrix.android.internal.session.sync.SyncTokenStore
import io.realm.Sort import org.koin.core.parameter.parametersOf
import org.koin.standalone.KoinComponent import org.koin.standalone.KoinComponent
import org.koin.standalone.inject import org.koin.standalone.inject
import java.util.concurrent.Executors


data class DefaultRoom( data class DefaultRoom(
override val roomId: String, override val roomId: String,
override val myMembership: MyMembership override val myMembership: MyMembership
) : Room, KoinComponent { ) : Room, KoinComponent {


private val paginationRequest by inject<PaginationRequest>()
private val loadRoomMembersRequest by inject<LoadRoomMembersRequest>() private val loadRoomMembersRequest by inject<LoadRoomMembersRequest>()
private val syncTokenStore by inject<SyncTokenStore>() private val syncTokenStore by inject<SyncTokenStore>()
private val monarchy by inject<Monarchy>() private val monarchy by inject<Monarchy>()

private val timelineHolder by inject<TimelineHolder>(parameters = { parametersOf(roomId) })
private val boundaryCallback = TimelineBoundaryCallback(paginationRequest, roomId, monarchy, Executors.newSingleThreadExecutor())
private val eventInterceptors = ArrayList<EnrichedEventInterceptor>()

init {
eventInterceptors.add(MessageEventInterceptor(monarchy))
}


override fun liveTimeline(): LiveData<PagedList<EnrichedEvent>> { override fun liveTimeline(): LiveData<PagedList<EnrichedEvent>> {
val realmDataSourceFactory = monarchy.createDataSourceFactory { realm -> return timelineHolder.liveTimeline()
ChunkEntity.where(realm, roomId)
.findAll()
.last(null)
?.let {
it.events.where().sort("originServerTs", Sort.DESCENDING)
}
}
val domainSourceFactory = realmDataSourceFactory
.map { it.asDomain() }
.map { event ->
val enrichedEvent = EnrichedEvent(event)
eventInterceptors
.filter {
it.canEnrich(enrichedEvent)
}.forEach {
it.enrich(roomId, enrichedEvent)
}
enrichedEvent
}

val pagedListConfig = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPageSize(10)
.setPrefetchDistance(10)
.build()

val livePagedListBuilder = LivePagedListBuilder(domainSourceFactory, pagedListConfig).setBoundaryCallback(boundaryCallback)
return monarchy.findAllPagedWithChanges(realmDataSourceFactory, livePagedListBuilder)
} }


override fun getNumberOfJoinedMembers(): Int { override fun getNumberOfJoinedMembers(): Int {

View File

@ -1,12 +1,17 @@
package im.vector.matrix.android.internal.session.room package im.vector.matrix.android.internal.session.room


import im.vector.matrix.android.api.session.room.TimelineHolder
import im.vector.matrix.android.internal.session.DefaultSession import im.vector.matrix.android.internal.session.DefaultSession
import im.vector.matrix.android.internal.session.room.members.LoadRoomMembersRequest import im.vector.matrix.android.internal.session.room.members.LoadRoomMembersRequest
import im.vector.matrix.android.internal.session.room.timeline.DefaultTimelineHolder
import im.vector.matrix.android.internal.session.room.timeline.PaginationRequest import im.vector.matrix.android.internal.session.room.timeline.PaginationRequest
import im.vector.matrix.android.internal.session.room.timeline.TimelineBoundaryCallback
import org.koin.core.parameter.parametersOf
import org.koin.dsl.context.ModuleDefinition import org.koin.dsl.context.ModuleDefinition
import org.koin.dsl.module.Module import org.koin.dsl.module.Module
import org.koin.dsl.module.module import org.koin.dsl.module.module
import retrofit2.Retrofit import retrofit2.Retrofit
import java.util.concurrent.Executors




class RoomModule : Module { class RoomModule : Module {
@ -19,11 +24,21 @@ class RoomModule : Module {
} }


scope(DefaultSession.SCOPE) { scope(DefaultSession.SCOPE) {
PaginationRequest(get(), get(), get(), get()) LoadRoomMembersRequest(get(), get(), get(), get())
} }


scope(DefaultSession.SCOPE) { scope(DefaultSession.SCOPE) {
LoadRoomMembersRequest(get(), get(), get(), get()) PaginationRequest(get(), get(), get(), get())
}

factory {
val roomId: String = it[0]
TimelineBoundaryCallback(roomId, get(), get(), Executors.newSingleThreadExecutor())
}

factory {
val roomId: String = it[0]
DefaultTimelineHolder(roomId, get(), get(parameters = { parametersOf(roomId) })) as TimelineHolder
} }


}.invoke() }.invoke()

View File

@ -0,0 +1,58 @@
package im.vector.matrix.android.internal.session.room.timeline

import android.arch.lifecycle.LiveData
import android.arch.paging.LivePagedListBuilder
import android.arch.paging.PagedList
import com.zhuinden.monarchy.Monarchy
import im.vector.matrix.android.api.session.events.interceptor.EnrichedEventInterceptor
import im.vector.matrix.android.api.session.events.model.EnrichedEvent
import im.vector.matrix.android.api.session.room.TimelineHolder
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.query.where
import im.vector.matrix.android.internal.session.events.interceptor.MessageEventInterceptor
import io.realm.Sort

class DefaultTimelineHolder(private val roomId: String,
private val monarchy: Monarchy,
private val boundaryCallback: TimelineBoundaryCallback
) : TimelineHolder {

private val eventInterceptors = ArrayList<EnrichedEventInterceptor>()

init {
eventInterceptors.add(MessageEventInterceptor(monarchy))
}

override fun liveTimeline(): LiveData<PagedList<EnrichedEvent>> {
val realmDataSourceFactory = monarchy.createDataSourceFactory { realm ->
ChunkEntity.where(realm, roomId)
.findAll()
.last(null)
?.let {
it.events.where().sort("originServerTs", Sort.DESCENDING)
}
}
val domainSourceFactory = realmDataSourceFactory
.map { it.asDomain() }
.map { event ->
val enrichedEvent = EnrichedEvent(event)
eventInterceptors
.filter {
it.canEnrich(enrichedEvent)
}.forEach {
it.enrich(roomId, enrichedEvent)
}
enrichedEvent
}

val pagedListConfig = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPageSize(10)
.setPrefetchDistance(10)
.build()

val livePagedListBuilder = LivePagedListBuilder(domainSourceFactory, pagedListConfig).setBoundaryCallback(boundaryCallback)
return monarchy.findAllPagedWithChanges(realmDataSourceFactory, livePagedListBuilder)
}
}

View File

@ -13,8 +13,8 @@ import im.vector.matrix.android.internal.util.PagingRequestHelper
import java.util.* import java.util.*
import java.util.concurrent.Executor import java.util.concurrent.Executor


class TimelineBoundaryCallback(private val paginationRequest: PaginationRequest, class TimelineBoundaryCallback(private val roomId: String,
private val roomId: String, private val paginationRequest: PaginationRequest,
private val monarchy: Monarchy, private val monarchy: Monarchy,
ioExecutor: Executor ioExecutor: Executor
) : PagedList.BoundaryCallback<EnrichedEvent>() { ) : PagedList.BoundaryCallback<EnrichedEvent>() {