From 8cef299878f49de1d68e44abb3b2cf462d96d60e Mon Sep 17 00:00:00 2001 From: ganfra Date: Tue, 16 Jun 2020 11:08:38 +0200 Subject: [PATCH] Timeline: add room summary holder to avoid fetching in item factory --- .../home/room/detail/RoomDetailViewModel.kt | 6 ++- .../helper/MessageInformationDataFactory.kt | 12 +++--- .../timeline/helper/RoomSummaryHolder.kt | 40 +++++++++++++++++++ 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/helper/RoomSummaryHolder.kt 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 69a6429efb..0ac277d3ae 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 @@ -71,6 +71,7 @@ import im.vector.riotx.features.command.ParsedCommand import im.vector.riotx.features.crypto.verification.SupportedVerificationMethodsProvider import im.vector.riotx.features.home.room.detail.composer.rainbow.RainbowGenerator import im.vector.riotx.features.home.room.detail.sticker.StickerPickerActionHandler +import im.vector.riotx.features.home.room.detail.timeline.helper.RoomSummaryHolder import im.vector.riotx.features.home.room.detail.timeline.helper.TimelineDisplayableEvents import im.vector.riotx.features.home.room.typing.TypingHelper import im.vector.riotx.features.powerlevel.PowerLevelsObservableFactory @@ -96,7 +97,8 @@ class RoomDetailViewModel @AssistedInject constructor( private val rainbowGenerator: RainbowGenerator, private val session: Session, private val supportedVerificationMethodsProvider: SupportedVerificationMethodsProvider, - private val stickerPickerActionHandler: StickerPickerActionHandler + private val stickerPickerActionHandler: StickerPickerActionHandler, + private val roomSummaryHolder: RoomSummaryHolder ) : VectorViewModel(initialState), Timeline.Listener { private val room = session.getRoom(initialState.roomId)!! @@ -1065,6 +1067,7 @@ class RoomDetailViewModel @AssistedInject constructor( private fun observeSummaryState() { asyncSubscribe(RoomDetailViewState::asyncRoomSummary) { summary -> + roomSummaryHolder.set(summary) if (summary.membership == Membership.INVITE) { summary.inviterId?.let { inviterId -> session.getUser(inviterId) @@ -1094,6 +1097,7 @@ class RoomDetailViewModel @AssistedInject constructor( } override fun onCleared() { + roomSummaryHolder.clear() timeline.dispose() timeline.removeAllListeners() if (vectorPreferences.sendTypingNotifs()) { diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/helper/MessageInformationDataFactory.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/helper/MessageInformationDataFactory.kt index c36dc5af9b..06c9544881 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/helper/MessageInformationDataFactory.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/helper/MessageInformationDataFactory.kt @@ -22,7 +22,6 @@ import im.vector.matrix.android.api.extensions.orFalse import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.events.model.EventType import im.vector.matrix.android.api.session.events.model.toModel -import im.vector.matrix.android.api.session.room.Room import im.vector.matrix.android.api.session.room.model.ReferencesAggregatedContent import im.vector.matrix.android.api.session.room.model.message.MessageVerificationRequestContent import im.vector.matrix.android.api.session.room.send.SendState @@ -49,6 +48,7 @@ import javax.inject.Inject * This class compute if data of an event (such has avatar, display name, ...) should be displayed, depending on the previous event in the timeline */ class MessageInformationDataFactory @Inject constructor(private val session: Session, + private val roomSummaryHolder: RoomSummaryHolder, private val dateFormatter: VectorDateFormatter, private val colorProvider: ColorProvider) { @@ -75,8 +75,7 @@ class MessageInformationDataFactory @Inject constructor(private val session: Ses textColor = colorProvider.getColor(getColorFromUserId(event.root.senderId)) } - val room = event.root.roomId?.let { session.getRoom(it) } - val e2eDecoration = getE2EDecoration(room, event) + val e2eDecoration = getE2EDecoration(event) return MessageInformationData( eventId = eventId, senderId = event.root.senderId ?: "", @@ -121,13 +120,14 @@ class MessageInformationDataFactory @Inject constructor(private val session: Ses ) } - private fun getE2EDecoration(room: Room?, event: TimelineEvent): E2EDecoration { + private fun getE2EDecoration(event: TimelineEvent): E2EDecoration { + val roomSummary = roomSummaryHolder.roomSummary return if ( event.root.sendState == SendState.SYNCED - && room?.isEncrypted() == true + && roomSummary?.isEncrypted.orFalse() // is user verified && session.cryptoService().crossSigningService().getUserCrossSigningKeys(event.root.senderId ?: "")?.isTrusted() == true) { - val ts = room.roomSummary()?.encryptionEventTs ?: 0 + val ts = roomSummary?.encryptionEventTs ?: 0 val eventTs = event.root.originServerTs ?: 0 if (event.isEncrypted()) { // Do not decorate failed to decrypt, or redaction (we lost sender device info) diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/helper/RoomSummaryHolder.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/helper/RoomSummaryHolder.kt new file mode 100644 index 0000000000..a5025c3082 --- /dev/null +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/helper/RoomSummaryHolder.kt @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotx.features.home.room.detail.timeline.helper + +import im.vector.matrix.android.api.session.room.model.RoomSummary +import im.vector.riotx.core.di.ScreenScope +import javax.inject.Inject + +/* + This holds an instance of the current room summary. + You should use this in the context of the timeline. + */ +@ScreenScope +class RoomSummaryHolder @Inject constructor() { + + var roomSummary: RoomSummary? = null + private set + + fun set(roomSummary: RoomSummary) { + this.roomSummary = roomSummary + } + + fun clear() { + roomSummary = null + } +}