From e75070be916b6b93b4327eb5cefc70241db560b4 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 29 Jun 2022 15:35:00 +0200 Subject: [PATCH] Use a DataSrouce instead of a Task and return a non Optional Boolean. --- .../org/matrix/android/sdk/flow/FlowRoom.kt | 4 +- .../session/room/members/MembershipService.kt | 2 +- ...ersLoadStatusTask.kt => RoomDataSource.kt} | 38 ++++++++++++------- .../sdk/internal/session/room/RoomModule.kt | 5 --- .../membership/DefaultMembershipService.kt | 25 +++--------- .../room/membership/LoadRoomMembersTask.kt | 5 ++- .../members/RoomMemberListViewModel.kt | 1 - 7 files changed, 35 insertions(+), 45 deletions(-) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/{membership/GetRoomMembersLoadStatusTask.kt => RoomDataSource.kt} (56%) diff --git a/matrix-sdk-android-flow/src/main/java/org/matrix/android/sdk/flow/FlowRoom.kt b/matrix-sdk-android-flow/src/main/java/org/matrix/android/sdk/flow/FlowRoom.kt index 2b6a2fea50..8be8e83569 100644 --- a/matrix-sdk-android-flow/src/main/java/org/matrix/android/sdk/flow/FlowRoom.kt +++ b/matrix-sdk-android-flow/src/main/java/org/matrix/android/sdk/flow/FlowRoom.kt @@ -53,10 +53,10 @@ class FlowRoom(private val room: Room) { } } - fun liveAreAllMembersLoaded(): Flow> { + fun liveAreAllMembersLoaded(): Flow { return room.membershipService().areAllMembersLoadedLive().asFlow() .startWith(room.coroutineDispatchers.io) { - room.membershipService().areAllMembersLoaded().toOptional() + room.membershipService().areAllMembersLoaded() } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/members/MembershipService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/members/MembershipService.kt index 249da5e5e0..301e395d18 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/members/MembershipService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/members/MembershipService.kt @@ -43,7 +43,7 @@ interface MembershipService { /** * Live version for [areAllMembersLoaded]. */ - fun areAllMembersLoadedLive(): LiveData> + fun areAllMembersLoadedLive(): LiveData /** * Return the roomMember with userId or null. diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/GetRoomMembersLoadStatusTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomDataSource.kt similarity index 56% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/GetRoomMembersLoadStatusTask.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomDataSource.kt index 24db0ad21b..bcbc53f95e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/GetRoomMembersLoadStatusTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomDataSource.kt @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -14,32 +14,42 @@ * limitations under the License. */ -package org.matrix.android.sdk.internal.session.room.membership +package org.matrix.android.sdk.internal.session.room +import androidx.lifecycle.LiveData +import androidx.lifecycle.Transformations import com.zhuinden.monarchy.Monarchy import io.realm.Realm +import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.internal.database.model.RoomEntity import org.matrix.android.sdk.internal.database.model.RoomMembersLoadStatusType import org.matrix.android.sdk.internal.database.query.where import org.matrix.android.sdk.internal.di.SessionDatabase -import org.matrix.android.sdk.internal.task.Task import javax.inject.Inject -internal interface GetRoomMembersLoadStatusTask : Task { - data class Params( - val roomId: String, - ) -} - -internal class DefaultGetRoomMembersLoadStatusTask @Inject constructor( +internal class RoomDataSource @Inject constructor( @SessionDatabase private val monarchy: Monarchy, -) : GetRoomMembersLoadStatusTask { - - override suspend fun execute(params: GetRoomMembersLoadStatusTask.Params): RoomMembersLoadStatusType { +) { + fun getRoomMembersLoadStatus(roomId: String): RoomMembersLoadStatusType { var result: RoomMembersLoadStatusType? Realm.getInstance(monarchy.realmConfiguration).use { - result = RoomEntity.where(it, params.roomId).findFirst()?.membersLoadStatus + result = RoomEntity.where(it, roomId).findFirst()?.membersLoadStatus } return result ?: RoomMembersLoadStatusType.NONE } + + fun getRoomMembersLoadStatusLive(roomId: String): LiveData { + val liveData = monarchy.findAllMappedWithChanges( + { + RoomEntity.where(it, roomId) + }, + { + it.membersLoadStatus == RoomMembersLoadStatusType.LOADED + } + ) + + return Transformations.map(liveData) { results -> + results.firstOrNull().orFalse() + } + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomModule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomModule.kt index d7ae610c3c..271e82a1e0 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomModule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomModule.kt @@ -59,9 +59,7 @@ import org.matrix.android.sdk.internal.session.room.location.SendLiveLocationTas import org.matrix.android.sdk.internal.session.room.location.SendStaticLocationTask import org.matrix.android.sdk.internal.session.room.location.StartLiveLocationShareTask import org.matrix.android.sdk.internal.session.room.location.StopLiveLocationShareTask -import org.matrix.android.sdk.internal.session.room.membership.DefaultGetRoomMembersLoadStatusTask import org.matrix.android.sdk.internal.session.room.membership.DefaultLoadRoomMembersTask -import org.matrix.android.sdk.internal.session.room.membership.GetRoomMembersLoadStatusTask import org.matrix.android.sdk.internal.session.room.membership.LoadRoomMembersTask import org.matrix.android.sdk.internal.session.room.membership.admin.DefaultMembershipAdminTask import org.matrix.android.sdk.internal.session.room.membership.admin.MembershipAdminTask @@ -229,9 +227,6 @@ internal abstract class RoomModule { @Binds abstract fun bindLoadRoomMembersTask(task: DefaultLoadRoomMembersTask): LoadRoomMembersTask - @Binds - abstract fun bindGetRoomMembersLoadStatusTask(task: DefaultGetRoomMembersLoadStatusTask): GetRoomMembersLoadStatusTask - @Binds abstract fun bindSetReadMarkersTask(task: DefaultSetReadMarkersTask): SetReadMarkersTask diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/DefaultMembershipService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/DefaultMembershipService.kt index 650d6c1d65..30d41e460d 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/DefaultMembershipService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/DefaultMembershipService.kt @@ -17,7 +17,6 @@ package org.matrix.android.sdk.internal.session.room.membership import androidx.lifecycle.LiveData -import androidx.lifecycle.Transformations import com.zhuinden.monarchy.Monarchy import dagger.assisted.Assisted import dagger.assisted.AssistedFactory @@ -29,18 +28,15 @@ import org.matrix.android.sdk.api.session.room.members.MembershipService import org.matrix.android.sdk.api.session.room.members.RoomMemberQueryParams import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.model.RoomMemberSummary -import org.matrix.android.sdk.api.util.Optional -import org.matrix.android.sdk.api.util.toOptional import org.matrix.android.sdk.internal.database.mapper.asDomain -import org.matrix.android.sdk.internal.database.model.RoomEntity import org.matrix.android.sdk.internal.database.model.RoomMemberSummaryEntity import org.matrix.android.sdk.internal.database.model.RoomMemberSummaryEntityFields import org.matrix.android.sdk.internal.database.model.RoomMembersLoadStatusType -import org.matrix.android.sdk.internal.database.query.where import org.matrix.android.sdk.internal.di.SessionDatabase import org.matrix.android.sdk.internal.di.UserId import org.matrix.android.sdk.internal.query.QueryStringValueProcessor import org.matrix.android.sdk.internal.query.process +import org.matrix.android.sdk.internal.session.room.RoomDataSource import org.matrix.android.sdk.internal.session.room.membership.admin.MembershipAdminTask import org.matrix.android.sdk.internal.session.room.membership.joining.InviteTask import org.matrix.android.sdk.internal.session.room.membership.threepid.InviteThreePidTask @@ -53,7 +49,7 @@ internal class DefaultMembershipService @AssistedInject constructor( private val inviteTask: InviteTask, private val inviteThreePidTask: InviteThreePidTask, private val membershipAdminTask: MembershipAdminTask, - private val getRoomMembersLoadStatusTask: GetRoomMembersLoadStatusTask, + private val roomDataSource: RoomDataSource, @UserId private val userId: String, private val queryStringValueProcessor: QueryStringValueProcessor @@ -70,23 +66,12 @@ internal class DefaultMembershipService @AssistedInject constructor( } override suspend fun areAllMembersLoaded(): Boolean { - val status = getRoomMembersLoadStatusTask.execute(GetRoomMembersLoadStatusTask.Params(roomId)) + val status = roomDataSource.getRoomMembersLoadStatus(roomId) return status == RoomMembersLoadStatusType.LOADED } - override fun areAllMembersLoadedLive(): LiveData> { - val liveData = monarchy.findAllMappedWithChanges( - { - RoomEntity.where(it, roomId) - }, - { - it.membersLoadStatus == RoomMembersLoadStatusType.LOADED - } - ) - - return Transformations.map(liveData) { results -> - results.firstOrNull().toOptional() - } + override fun areAllMembersLoadedLive(): LiveData { + return roomDataSource.getRoomMembersLoadStatusLive(roomId) } override fun getRoomMember(userId: String): RoomMemberSummary? { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/LoadRoomMembersTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/LoadRoomMembersTask.kt index 0ad7c56d1b..37bc7d776a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/LoadRoomMembersTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/LoadRoomMembersTask.kt @@ -37,6 +37,7 @@ import org.matrix.android.sdk.internal.di.SessionDatabase import org.matrix.android.sdk.internal.network.GlobalErrorReceiver import org.matrix.android.sdk.internal.network.executeRequest import org.matrix.android.sdk.internal.session.room.RoomAPI +import org.matrix.android.sdk.internal.session.room.RoomDataSource import org.matrix.android.sdk.internal.session.room.summary.RoomSummaryUpdater import org.matrix.android.sdk.internal.session.sync.SyncTokenStore import org.matrix.android.sdk.internal.task.Task @@ -56,7 +57,7 @@ internal interface LoadRoomMembersTask : Task internal class DefaultLoadRoomMembersTask @Inject constructor( private val roomAPI: RoomAPI, @SessionDatabase private val monarchy: Monarchy, - private val getRoomMembersLoadStatusTask: GetRoomMembersLoadStatusTask, + private val roomDataSource: RoomDataSource, private val syncTokenStore: SyncTokenStore, private val roomSummaryUpdater: RoomSummaryUpdater, private val roomMemberEventHandler: RoomMemberEventHandler, @@ -67,7 +68,7 @@ internal class DefaultLoadRoomMembersTask @Inject constructor( ) : LoadRoomMembersTask { override suspend fun execute(params: LoadRoomMembersTask.Params) { - when (getRoomMembersLoadStatusTask.execute(GetRoomMembersLoadStatusTask.Params(params.roomId))) { + when (roomDataSource.getRoomMembersLoadStatus(params.roomId)) { RoomMembersLoadStatusType.NONE -> doRequest(params) RoomMembersLoadStatusType.LOADING -> waitPreviousRequestToFinish(params) RoomMembersLoadStatusType.LOADED -> Unit diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberListViewModel.kt b/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberListViewModel.kt index 7a81066482..915ce51d91 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberListViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/members/RoomMemberListViewModel.kt @@ -97,7 +97,6 @@ class RoomMemberListViewModel @AssistedInject constructor( } roomFlow.liveAreAllMembersLoaded() - .unwrap() .distinctUntilChanged() .onEach { setState {