From 7ba67d6c2ba1100340cdfd9e72943abda4060100 Mon Sep 17 00:00:00 2001 From: ganfra Date: Thu, 25 Oct 2018 17:25:58 +0200 Subject: [PATCH] Deport timeline in his own class --- .../matrix/android/api/session/room/Room.kt | 7 +-- .../api/session/room/TimelineHolder.kt | 11 ++++ .../internal/session/room/DefaultRoom.kt | 50 ++-------------- .../internal/session/room/RoomModule.kt | 19 +++++- .../room/timeline/DefaultTimelineHolder.kt | 58 +++++++++++++++++++ .../room/timeline/TimelineBoundaryCallback.kt | 4 +- 6 files changed, 93 insertions(+), 56 deletions(-) create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/TimelineHolder.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimelineHolder.kt diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/Room.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/Room.kt index febe786c..26cb54b1 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/Room.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/Room.kt @@ -1,19 +1,14 @@ 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.util.Cancelable -interface Room { +interface Room: TimelineHolder { val roomId: String val myMembership: MyMembership - fun liveTimeline(): LiveData> - fun getNumberOfJoinedMembers(): Int fun loadRoomMembersIfNeeded(): Cancelable diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/TimelineHolder.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/TimelineHolder.kt new file mode 100644 index 00000000..535f681a --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/TimelineHolder.kt @@ -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> + +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoom.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoom.kt index d01eba5f..284e066c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoom.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoom.kt @@ -1,78 +1,36 @@ package im.vector.matrix.android.internal.session.room 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.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.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.MyMembership 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.RoomSummaryEntity 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.timeline.PaginationRequest -import im.vector.matrix.android.internal.session.room.timeline.TimelineBoundaryCallback 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.inject -import java.util.concurrent.Executors data class DefaultRoom( override val roomId: String, override val myMembership: MyMembership ) : Room, KoinComponent { - private val paginationRequest by inject() private val loadRoomMembersRequest by inject() private val syncTokenStore by inject() private val monarchy by inject() - - private val boundaryCallback = TimelineBoundaryCallback(paginationRequest, roomId, monarchy, Executors.newSingleThreadExecutor()) - private val eventInterceptors = ArrayList() - - init { - eventInterceptors.add(MessageEventInterceptor(monarchy)) - } + private val timelineHolder by inject(parameters = { parametersOf(roomId) }) override fun liveTimeline(): LiveData> { - 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) + return timelineHolder.liveTimeline() } override fun getNumberOfJoinedMembers(): Int { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomModule.kt index 0aee01b8..6a5538b5 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomModule.kt @@ -1,12 +1,17 @@ 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.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.TimelineBoundaryCallback +import org.koin.core.parameter.parametersOf import org.koin.dsl.context.ModuleDefinition import org.koin.dsl.module.Module import org.koin.dsl.module.module import retrofit2.Retrofit +import java.util.concurrent.Executors class RoomModule : Module { @@ -19,11 +24,21 @@ class RoomModule : Module { } scope(DefaultSession.SCOPE) { - PaginationRequest(get(), get(), get(), get()) + LoadRoomMembersRequest(get(), get(), get(), get()) } 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() diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimelineHolder.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimelineHolder.kt new file mode 100644 index 00000000..9ac389d4 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimelineHolder.kt @@ -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() + + init { + eventInterceptors.add(MessageEventInterceptor(monarchy)) + } + + override fun liveTimeline(): LiveData> { + 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) + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineBoundaryCallback.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineBoundaryCallback.kt index 92e7d5af..0c0408ac 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineBoundaryCallback.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineBoundaryCallback.kt @@ -13,8 +13,8 @@ import im.vector.matrix.android.internal.util.PagingRequestHelper import java.util.* import java.util.concurrent.Executor -class TimelineBoundaryCallback(private val paginationRequest: PaginationRequest, - private val roomId: String, +class TimelineBoundaryCallback(private val roomId: String, + private val paginationRequest: PaginationRequest, private val monarchy: Monarchy, ioExecutor: Executor ) : PagedList.BoundaryCallback() {