diff --git a/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxRoom.kt b/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxRoom.kt index 8a137518a4..e209a0ca3b 100644 --- a/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxRoom.kt +++ b/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxRoom.kt @@ -24,8 +24,8 @@ import io.reactivex.Observable class RxRoom(private val room: Room) { - fun liveRoomSummary(): Observable { - return room.liveRoomSummary.asObservable() + fun liveRoomSummary(fetchLastEvent: Boolean): Observable { + return room.liveRoomSummary(fetchLastEvent).asObservable() } fun liveRoomMemberIds(): Observable> { diff --git a/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxSession.kt b/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxSession.kt index af14911644..6490083932 100644 --- a/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxSession.kt +++ b/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxSession.kt @@ -25,8 +25,8 @@ import io.reactivex.Observable class RxSession(private val session: Session) { - fun liveRoomSummaries(): Observable> { - return session.liveRoomSummaries().asObservable() + fun liveRoomSummaries(fetchLastEvents: Boolean): Observable> { + return session.liveRoomSummaries(fetchLastEvents).asObservable() } fun liveGroupSummaries(): Observable> { 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 3e893a091d..35efa0fb83 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 @@ -47,8 +47,8 @@ interface Room : * A live [RoomSummary] associated with the room * You can observe this summary to get dynamic data from this room. */ - val liveRoomSummary: LiveData + fun liveRoomSummary(fetchLastEvent: Boolean = false): LiveData - val roomSummary: RoomSummary? + fun roomSummary(fetchLastEvent: Boolean = false): RoomSummary? } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomService.kt index fc0bf49955..8fd6605f64 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomService.kt @@ -43,6 +43,6 @@ interface RoomService { * Get a live list of room summaries. This list is refreshed as soon as the data changes. * @return the [LiveData] of [RoomSummary] */ - fun liveRoomSummaries(): LiveData> + fun liveRoomSummaries(fetchLastEvents: Boolean = true): LiveData> } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/RoomSummaryMapper.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/RoomSummaryMapper.kt index 0ebb7ebd88..b5fdabc702 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/RoomSummaryMapper.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/RoomSummaryMapper.kt @@ -16,29 +16,24 @@ package im.vector.matrix.android.internal.database.mapper -import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.session.room.model.RoomSummary import im.vector.matrix.android.api.session.room.model.tag.RoomTag -import im.vector.matrix.android.api.session.room.timeline.TimelineEvent import im.vector.matrix.android.internal.database.model.RoomSummaryEntity import im.vector.matrix.android.internal.session.room.timeline.TimelineEventFactory import javax.inject.Inject +internal class RoomSummaryMapper @Inject constructor(private val timelineEventFactory: TimelineEventFactory) { -internal class RoomSummaryMapper @Inject constructor( - private val timelineEventFactory: TimelineEventFactory, - private val monarchy: Monarchy) { - - fun map(roomSummaryEntity: RoomSummaryEntity): RoomSummary { + fun map(roomSummaryEntity: RoomSummaryEntity, getLatestEvent: Boolean = false): RoomSummary { val tags = roomSummaryEntity.tags.map { RoomTag(it.tagName, it.tagOrder) } - val latestEvent = roomSummaryEntity.latestEvent?.let { - var ev: TimelineEvent? = null - monarchy.doWithRealm { realm -> - ev = timelineEventFactory.create(it, realm) + val latestEvent = if (getLatestEvent) { + roomSummaryEntity.latestEvent?.let { + timelineEventFactory.create(it, it.realm) } - ev + } else { + null } return RoomSummary( roomId = roomSummaryEntity.roomId, 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 66cc74055c..9c53f5b9ad 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 @@ -34,6 +34,7 @@ import im.vector.matrix.android.internal.database.model.RoomSummaryEntity import im.vector.matrix.android.internal.database.model.RoomSummaryEntityFields import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.util.fetchCopied +import im.vector.matrix.android.internal.util.fetchCopyMap import javax.inject.Inject internal class DefaultRoom @Inject constructor(override val roomId: String, @@ -47,19 +48,19 @@ internal class DefaultRoom @Inject constructor(override val roomId: String, private val relationService: RelationService, private val roomMembersService: MembershipService ) : Room, - TimelineService by timelineService, - SendService by sendService, - StateService by stateService, - ReadService by readService, - RelationService by relationService, - MembershipService by roomMembersService { + TimelineService by timelineService, + SendService by sendService, + StateService by stateService, + ReadService by readService, + RelationService by relationService, + MembershipService by roomMembersService { - override val liveRoomSummary: LiveData by lazy { + override fun liveRoomSummary(fetchLastEvent: Boolean): LiveData { val liveRealmData = RealmLiveData(monarchy.realmConfiguration) { realm -> RoomSummaryEntity.where(realm, roomId).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME) } - Transformations.map(liveRealmData) { results -> - val roomSummaries = results.map { roomSummaryMapper.map(it) } + return Transformations.map(liveRealmData) { results -> + val roomSummaries = results.map { roomSummaryMapper.map(it, fetchLastEvent) } if (roomSummaries.isEmpty()) { // Create a dummy RoomSummary to avoid Crash during Sign Out or clear cache @@ -70,11 +71,12 @@ internal class DefaultRoom @Inject constructor(override val roomId: String, } } - override val roomSummary: RoomSummary? - get() { - var sum: RoomSummaryEntity? = monarchy.fetchCopied { RoomSummaryEntity.where(it, roomId).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME).findFirst() } - return sum?.let { roomSummaryMapper.map(it) } - } + override fun roomSummary(fetchLastEvent: Boolean): RoomSummary? { + return monarchy.fetchAllMappedSync( + { realm -> RoomSummaryEntity.where(realm).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME) }, + { roomSummaryMapper.map(it, fetchLastEvent) } + ).firstOrNull() + } override fun isEncrypted(): Boolean { return cryptoService.isRoomEncrypted(roomId) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt index 2c0f1ce90c..d40184a0a7 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt @@ -52,10 +52,10 @@ internal class DefaultRoomService @Inject constructor(private val monarchy: Mona return roomFactory.create(roomId) } - override fun liveRoomSummaries(): LiveData> { + override fun liveRoomSummaries(fetchLastEvents: Boolean): LiveData> { return monarchy.findAllMappedWithChanges( { realm -> RoomSummaryEntity.where(realm).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME) }, - { roomSummaryMapper.map(it) } + { roomSummaryMapper.map(it, fetchLastEvents) } ) } } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineEventFactory.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineEventFactory.kt index e9a1090249..aeb35e10cf 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineEventFactory.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineEventFactory.kt @@ -80,7 +80,7 @@ internal class SimpleTimelineEventFactory @Inject constructor(private val roomMe val result = cryptoService.decryptEvent(event, UUID.randomUUID().toString()) event.setClearData(result) } catch (failure: Throwable) { - Timber.e(failure, "Encrypted event: decryption failed") + Timber.e("Encrypted event: decryption failed") if (failure is MXDecryptionException) { event.setCryptoError(failure.cryptoError) } diff --git a/vector/src/main/java/im/vector/riotx/features/home/HomeActivityViewModel.kt b/vector/src/main/java/im/vector/riotx/features/home/HomeActivityViewModel.kt index 36d5725561..d3b95f2695 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/HomeActivityViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/HomeActivityViewModel.kt @@ -73,7 +73,7 @@ class HomeActivityViewModel @AssistedInject constructor(@Assisted initialState: private fun observeRoomAndGroup() { Observable .combineLatest, Option, List>( - session.rx().liveRoomSummaries().throttleLast(300, TimeUnit.MILLISECONDS), + session.rx().liveRoomSummaries(fetchLastEvents = true).throttleLast(300, TimeUnit.MILLISECONDS), selectedGroupStore.observe(), BiFunction { rooms, selectedGroupOption -> val selectedGroup = selectedGroupOption.orNull() diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailViewModel.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailViewModel.kt index 61cea3ccd5..60428713c9 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailViewModel.kt @@ -498,7 +498,7 @@ class RoomDetailViewModel @AssistedInject constructor(@Assisted initialState: Ro } private fun observeRoomSummary() { - room.rx().liveRoomSummary() + room.rx().liveRoomSummary(false) .execute { async -> copy( asyncRoomSummary = async, diff --git a/vector/src/main/java/im/vector/riotx/features/notifications/NotifiableEventResolver.kt b/vector/src/main/java/im/vector/riotx/features/notifications/NotifiableEventResolver.kt index 2ec916526c..2ab3c03005 100644 --- a/vector/src/main/java/im/vector/riotx/features/notifications/NotifiableEventResolver.kt +++ b/vector/src/main/java/im/vector/riotx/features/notifications/NotifiableEventResolver.kt @@ -117,7 +117,7 @@ class NotifiableEventResolver @Inject constructor(private val stringProvider: St val body = event.annotations?.editSummary?.aggregatedContent?.toModel()?.body ?: event.root.getClearContent().toModel()?.body ?: stringProvider.getString(R.string.notification_unknown_new_event) - val roomName = room.roomSummary?.displayName ?: "" + val roomName = room.roomSummary()?.displayName ?: "" val senderDisplayName = event.senderName ?: event.root.senderId val notifiableEvent = NotifiableMessageEvent( @@ -129,7 +129,7 @@ class NotifiableEventResolver @Inject constructor(private val stringProvider: St body = body, roomId = event.root.roomId!!, roomName = roomName, - roomIsDirect = room.roomSummary?.isDirect ?: false) + roomIsDirect = room.roomSummary()?.isDirect ?: false) notifiableEvent.matrixID = session.sessionParams.credentials.userId notifiableEvent.soundName = null @@ -137,7 +137,7 @@ class NotifiableEventResolver @Inject constructor(private val stringProvider: St // Get the avatars URL // TODO They will be not displayed the first time (known limitation) notifiableEvent.roomAvatarPath = session.contentUrlResolver() - .resolveThumbnail(room.roomSummary?.avatarUrl, + .resolveThumbnail(room.roomSummary()?.avatarUrl, 250, 250, ContentUrlResolver.ThumbnailMethod.SCALE) diff --git a/vector/src/main/java/im/vector/riotx/features/notifications/NotificationBroadcastReceiver.kt b/vector/src/main/java/im/vector/riotx/features/notifications/NotificationBroadcastReceiver.kt index 791072a27b..aff7160b6b 100644 --- a/vector/src/main/java/im/vector/riotx/features/notifications/NotificationBroadcastReceiver.kt +++ b/vector/src/main/java/im/vector/riotx/features/notifications/NotificationBroadcastReceiver.kt @@ -100,8 +100,8 @@ class NotificationBroadcastReceiver : BroadcastReceiver() { session.sessionParams.credentials.userId, message, room.roomId, - room.roomSummary?.displayName ?: room.roomId, - room.roomSummary?.isDirect == true + room.roomSummary()?.displayName ?: room.roomId, + room.roomSummary()?.isDirect == true ) notifiableMessageEvent.outGoingMessage = true diff --git a/vector/src/main/java/im/vector/riotx/features/roomdirectory/RoomDirectoryViewModel.kt b/vector/src/main/java/im/vector/riotx/features/roomdirectory/RoomDirectoryViewModel.kt index b34618e240..f7d6c2fb06 100644 --- a/vector/src/main/java/im/vector/riotx/features/roomdirectory/RoomDirectoryViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/roomdirectory/RoomDirectoryViewModel.kt @@ -86,7 +86,7 @@ class RoomDirectoryViewModel @AssistedInject constructor(@Assisted initialState: private fun observeJoinedRooms() { session .rx() - .liveRoomSummaries() + .liveRoomSummaries(fetchLastEvents = false) .subscribe { list -> val joinedRoomIds = list // Keep only joined room diff --git a/vector/src/main/java/im/vector/riotx/features/roomdirectory/roompreview/RoomPreviewViewModel.kt b/vector/src/main/java/im/vector/riotx/features/roomdirectory/roompreview/RoomPreviewViewModel.kt index bf5fa74321..e91b4b0ff6 100644 --- a/vector/src/main/java/im/vector/riotx/features/roomdirectory/roompreview/RoomPreviewViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/roomdirectory/roompreview/RoomPreviewViewModel.kt @@ -54,7 +54,7 @@ class RoomPreviewViewModel @AssistedInject constructor(@Assisted initialState: R private fun observeJoinedRooms() { session .rx() - .liveRoomSummaries() + .liveRoomSummaries(fetchLastEvents = false) .subscribe { list -> withState { state -> val isRoomJoined = list