Skip to content

Commit

Permalink
Architecture: get rid of RoomSummariesHolder as it can lead to errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ganfra committed Aug 18, 2021
1 parent 799cd99 commit 50a0426
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 147 deletions.
4 changes: 0 additions & 4 deletions vector/src/main/java/im/vector/app/core/di/VectorComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import im.vector.app.features.home.AvatarRenderer
import im.vector.app.features.home.CurrentSpaceSuggestedRoomListDataSource
import im.vector.app.features.home.room.detail.RoomDetailPendingActionStore
import im.vector.app.features.home.room.detail.timeline.helper.MatrixItemColorProvider
import im.vector.app.features.home.room.detail.timeline.helper.RoomSummariesHolder
import im.vector.app.features.html.EventHtmlRenderer
import im.vector.app.features.html.VectorHtmlCompressor
import im.vector.app.features.invite.AutoAcceptInvites
Expand All @@ -66,7 +65,6 @@ import org.matrix.android.sdk.api.auth.AuthenticationService
import org.matrix.android.sdk.api.auth.HomeServerHistoryService
import org.matrix.android.sdk.api.raw.RawService
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.widgets.model.WidgetType
import javax.inject.Singleton

@Component(modules = [VectorModule::class])
Expand Down Expand Up @@ -167,8 +165,6 @@ interface VectorComponent {

fun webRtcCallManager(): WebRtcCallManager

fun roomSummaryHolder(): RoomSummariesHolder

fun jitsiActiveConferenceHolder(): JitsiActiveConferenceHolder

@Component.Factory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import im.vector.app.features.home.room.detail.composer.VoiceMessageHelper
import im.vector.app.features.home.room.detail.composer.rainbow.RainbowGenerator
import im.vector.app.features.home.room.detail.sticker.StickerPickerActionHandler
import im.vector.app.features.home.room.detail.timeline.factory.TimelineFactory
import im.vector.app.features.home.room.detail.timeline.helper.RoomSummariesHolder
import im.vector.app.features.home.room.detail.timeline.url.PreviewUrlRetriever
import im.vector.app.features.home.room.typing.TypingHelper
import im.vector.app.features.powerlevel.PowerLevelsObservableFactory
Expand Down Expand Up @@ -117,7 +116,6 @@ class RoomDetailViewModel @AssistedInject constructor(
private val session: Session,
private val supportedVerificationMethodsProvider: SupportedVerificationMethodsProvider,
private val stickerPickerActionHandler: StickerPickerActionHandler,
private val roomSummariesHolder: RoomSummariesHolder,
private val typingHelper: TypingHelper,
private val callManager: WebRtcCallManager,
private val chatEffectManager: ChatEffectManager,
Expand Down Expand Up @@ -1576,7 +1574,6 @@ class RoomDetailViewModel @AssistedInject constructor(

private fun observeSummaryState() {
asyncSubscribe(RoomDetailViewState::asyncRoomSummary) { summary ->
roomSummariesHolder.set(summary)
setState {
val typingMessage = typingHelper.getTypingMessage(summary.typingUsers)
copy(
Expand Down Expand Up @@ -1624,7 +1621,6 @@ class RoomDetailViewModel @AssistedInject constructor(
}

override fun onCleared() {
roomSummariesHolder.remove(room.roomId)
timeline.dispose()
timeline.removeAllListeners()
if (vectorPreferences.sendTypingNotifs()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import org.matrix.android.sdk.api.session.events.model.toModel
import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.api.session.room.model.ReadReceipt
import org.matrix.android.sdk.api.session.room.model.RoomMemberContent
import org.matrix.android.sdk.api.session.room.model.RoomSummary
import org.matrix.android.sdk.api.session.room.model.message.MessageAudioContent
import org.matrix.android.sdk.api.session.room.model.message.MessageImageInfoContent
import org.matrix.android.sdk.api.session.room.model.message.MessageVideoContent
Expand Down Expand Up @@ -91,13 +92,15 @@ class TimelineEventController @Inject constructor(private val dateFormatter: Vec
data class PartialState(
val unreadState: UnreadState = UnreadState.Unknown,
val highlightedEventId: String? = null,
val jitsiState: JitsiState = JitsiState()
val jitsiState: JitsiState = JitsiState(),
val roomSummary: RoomSummary? = null
) {

constructor(state: RoomDetailViewState) : this(
unreadState = state.unreadState,
highlightedEventId = state.highlightedEventId,
jitsiState = state.jitsiState
jitsiState = state.jitsiState,
roomSummary = state.asyncRoomSummary()
)
}

Expand Down Expand Up @@ -407,6 +410,7 @@ class TimelineEventController @Inject constructor(private val dateFormatter: Vec
val wantsDateSeparator = wantsDateSeparator(event, nextEvent)
val mergedHeaderModel = mergedHeaderItemFactory.create(event,
nextEvent = nextEvent,
partialState = partialState,
items = this@TimelineEventController.currentSnapshot,
addDaySeparator = wantsDateSeparator,
currentPosition = position,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import im.vector.app.features.html.VectorHtmlCompressor
import im.vector.app.features.powerlevel.PowerLevelsObservableFactory
import im.vector.app.features.reactions.data.EmojiDataSource
import im.vector.app.features.settings.VectorPreferences
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupState
import org.matrix.android.sdk.api.session.events.model.EventType
Expand Down Expand Up @@ -207,7 +208,7 @@ class MessageActionsViewModel @AssistedInject constructor(@Assisted
EventType.CALL_CANDIDATES,
EventType.CALL_HANGUP,
EventType.CALL_ANSWER -> {
noticeEventFormatter.format(timelineEvent)
noticeEventFormatter.format(timelineEvent, room?.roomSummary()?.isDirect.orFalse())
}
else -> null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@ package im.vector.app.features.home.room.detail.timeline.factory

import im.vector.app.core.epoxy.VectorEpoxyModel
import im.vector.app.core.resources.UserPreferencesProvider
import im.vector.app.features.call.vectorCallService
import im.vector.app.features.home.room.detail.timeline.MessageColorProvider
import im.vector.app.features.home.room.detail.timeline.TimelineEventController
import im.vector.app.features.home.room.detail.timeline.helper.AvatarSizeProvider
import im.vector.app.features.home.room.detail.timeline.helper.CallSignalingEventsGroup
import im.vector.app.features.home.room.detail.timeline.helper.MessageInformationDataFactory
import im.vector.app.features.home.room.detail.timeline.helper.MessageItemAttributesFactory
import im.vector.app.features.home.room.detail.timeline.helper.RoomSummariesHolder
import im.vector.app.features.home.room.detail.timeline.item.CallTileTimelineItem
import im.vector.app.features.home.room.detail.timeline.item.CallTileTimelineItem_
import im.vector.app.features.home.room.detail.timeline.item.MessageInformationData
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.events.model.EventType
import org.matrix.android.sdk.api.session.room.model.RoomSummary
import org.matrix.android.sdk.api.util.toMatrixItem
import javax.inject.Inject

Expand All @@ -40,22 +39,21 @@ class CallItemFactory @Inject constructor(
private val messageInformationDataFactory: MessageInformationDataFactory,
private val messageItemAttributesFactory: MessageItemAttributesFactory,
private val avatarSizeProvider: AvatarSizeProvider,
private val noticeItemFactory: NoticeItemFactory,
private val roomSummariesHolder: RoomSummariesHolder) {
private val noticeItemFactory: NoticeItemFactory) {

fun create(params: TimelineItemFactoryParams): VectorEpoxyModel<*>? {
val event = params.event
if (event.root.eventId == null) return null
val showHiddenEvents = userPreferencesProvider.shouldShowHiddenEvents()
val callEventGrouper = params.eventsGroup?.let { CallSignalingEventsGroup(it) } ?: return null
val roomId = event.roomId
val roomSummary = params.partialState.roomSummary ?: return null
val informationData = messageInformationDataFactory.create(params)
val callKind = if (callEventGrouper.isVideo()) CallTileTimelineItem.CallKind.VIDEO else CallTileTimelineItem.CallKind.AUDIO
val callItem = when (event.root.getClearType()) {
EventType.CALL_ANSWER -> {
if (callEventGrouper.isInCall()) {
createCallTileTimelineItem(
roomId = roomId,
roomSummary = roomSummary,
callId = callEventGrouper.callId,
callStatus = CallTileTimelineItem.CallStatus.IN_CALL,
callKind = callKind,
Expand All @@ -72,7 +70,7 @@ class CallItemFactory @Inject constructor(
EventType.CALL_INVITE -> {
if (callEventGrouper.isRinging()) {
createCallTileTimelineItem(
roomId = roomId,
roomSummary = roomSummary,
callId = callEventGrouper.callId,
callStatus = CallTileTimelineItem.CallStatus.INVITED,
callKind = callKind,
Expand All @@ -88,7 +86,7 @@ class CallItemFactory @Inject constructor(
}
EventType.CALL_REJECT -> {
createCallTileTimelineItem(
roomId = roomId,
roomSummary = roomSummary,
callId = callEventGrouper.callId,
callStatus = CallTileTimelineItem.CallStatus.REJECTED,
callKind = callKind,
Expand All @@ -101,7 +99,7 @@ class CallItemFactory @Inject constructor(
}
EventType.CALL_HANGUP -> {
createCallTileTimelineItem(
roomId = roomId,
roomSummary = roomSummary,
callId = callEventGrouper.callId,
callStatus = if (callEventGrouper.callWasMissed()) CallTileTimelineItem.CallStatus.MISSED else CallTileTimelineItem.CallStatus.ENDED,
callKind = callKind,
Expand All @@ -123,7 +121,7 @@ class CallItemFactory @Inject constructor(
}

private fun createCallTileTimelineItem(
roomId: String,
roomSummary: RoomSummary,
callId: String,
callKind: CallTileTimelineItem.CallKind,
callStatus: CallTileTimelineItem.CallStatus,
Expand All @@ -133,8 +131,7 @@ class CallItemFactory @Inject constructor(
formattedDuration: String,
callback: TimelineEventController.Callback?
): CallTileTimelineItem? {
val correctedRoomId = session.vectorCallService.userMapper.nativeRoomForVirtualRoom(roomId) ?: roomId
val userOfInterest = roomSummariesHolder.get(correctedRoomId)?.toMatrixItem() ?: return null
val userOfInterest = roomSummary.toMatrixItem()
val attributes = messageItemAttributesFactory.create(null, informationData, callback).let {
CallTileTimelineItem.Attributes(
callId = callId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import im.vector.app.features.home.AvatarRenderer
import im.vector.app.features.home.room.detail.timeline.TimelineEventController
import im.vector.app.features.home.room.detail.timeline.helper.AvatarSizeProvider
import im.vector.app.features.home.room.detail.timeline.helper.MergedTimelineEventVisibilityStateChangedListener
import im.vector.app.features.home.room.detail.timeline.helper.RoomSummariesHolder
import im.vector.app.features.home.room.detail.timeline.helper.TimelineEventVisibilityHelper
import im.vector.app.features.home.room.detail.timeline.helper.canBeMerged
import im.vector.app.features.home.room.detail.timeline.helper.isRoomConfiguration
Expand All @@ -47,8 +46,7 @@ import javax.inject.Inject
class MergedHeaderItemFactory @Inject constructor(private val activeSessionHolder: ActiveSessionHolder,
private val avatarRenderer: AvatarRenderer,
private val avatarSizeProvider: AvatarSizeProvider,
private val roomSummariesHolder: RoomSummariesHolder,
private val timelineEventVisibilityHelper: TimelineEventVisibilityHelper) {
private val timelineEventVisibilityHelper: TimelineEventVisibilityHelper) {

private val collapsedEventIds = linkedSetOf<Long>()
private val mergeItemCollapseStates = HashMap<Long, Boolean>()
Expand All @@ -60,6 +58,7 @@ private val timelineEventVisibilityHelper: TimelineEventVisibilityHelper) {
fun create(event: TimelineEvent,
nextEvent: TimelineEvent?,
items: List<TimelineEvent>,
partialState: TimelineEventController.PartialState,
addDaySeparator: Boolean,
currentPosition: Int,
eventIdToHighlight: String?,
Expand All @@ -70,18 +69,17 @@ private val timelineEventVisibilityHelper: TimelineEventVisibilityHelper) {
&& event.isRoomConfiguration(nextEvent.root.getClearContent()?.toModel<RoomCreateContent>()?.creator)) {
// It's the first item before room.create
// Collapse all room configuration events
buildRoomCreationMergedSummary(currentPosition, items, event, eventIdToHighlight, requestModelBuild, callback)
buildRoomCreationMergedSummary(currentPosition, items, partialState, event, eventIdToHighlight, requestModelBuild, callback)
} else if (!event.canBeMerged() || (nextEvent?.root?.getClearType() == event.root.getClearType() && !addDaySeparator)) {
null
} else {
buildMembershipEventsMergedSummary(currentPosition, items, event, eventIdToHighlight, requestModelBuild, callback)
buildMembershipEventsMergedSummary(currentPosition, items,partialState, event, eventIdToHighlight, requestModelBuild, callback)
}
}

private fun isDirectRoom(roomId: String) = roomSummariesHolder.get(roomId)?.isDirect.orFalse()

private fun buildMembershipEventsMergedSummary(currentPosition: Int,
items: List<TimelineEvent>,
partialState: TimelineEventController.PartialState,
event: TimelineEvent,
eventIdToHighlight: String?,
requestModelBuild: () -> Unit,
Expand All @@ -102,7 +100,7 @@ private val timelineEventVisibilityHelper: TimelineEventVisibilityHelper) {
memberName = mergedEvent.senderInfo.disambiguatedDisplayName,
localId = mergedEvent.localId,
eventId = mergedEvent.root.eventId ?: "",
isDirectRoom = isDirectRoom(event.roomId)
isDirectRoom = partialState.isDirectRoom()
)
mergedData.add(data)
}
Expand Down Expand Up @@ -141,6 +139,7 @@ private val timelineEventVisibilityHelper: TimelineEventVisibilityHelper) {

private fun buildRoomCreationMergedSummary(currentPosition: Int,
items: List<TimelineEvent>,
partialState: TimelineEventController.PartialState,
event: TimelineEvent,
eventIdToHighlight: String?,
requestModelBuild: () -> Unit,
Expand Down Expand Up @@ -173,7 +172,7 @@ private val timelineEventVisibilityHelper: TimelineEventVisibilityHelper) {
memberName = mergedEvent.senderInfo.disambiguatedDisplayName,
localId = mergedEvent.localId,
eventId = mergedEvent.root.eventId ?: "",
isDirectRoom = isDirectRoom(event.roomId)
isDirectRoom = partialState.isDirectRoom()
)
mergedData.add(data)
}
Expand Down Expand Up @@ -206,7 +205,7 @@ private val timelineEventVisibilityHelper: TimelineEventVisibilityHelper) {
isEncryptionAlgorithmSecure = encryptionAlgorithm == MXCRYPTO_ALGORITHM_MEGOLM,
callback = callback,
currentUserId = currentUserId,
roomSummary = roomSummariesHolder.get(event.roomId),
roomSummary = partialState.roomSummary,
canChangeAvatar = powerLevelsHelper?.isUserAllowedToSend(currentUserId, true, EventType.STATE_ROOM_AVATAR) ?: false,
canChangeTopic = powerLevelsHelper?.isUserAllowedToSend(currentUserId, true, EventType.STATE_ROOM_TOPIC) ?: false,
canChangeName = powerLevelsHelper?.isUserAllowedToSend(currentUserId, true, EventType.STATE_ROOM_NAME) ?: false
Expand All @@ -223,6 +222,10 @@ private val timelineEventVisibilityHelper: TimelineEventVisibilityHelper) {
} else null
}

private fun TimelineEventController.PartialState.isDirectRoom(): Boolean {
return roomSummary?.isDirect.orFalse()
}

fun isCollapsed(localId: Long): Boolean {
return collapsedEventIds.contains(localId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import im.vector.app.features.home.room.detail.timeline.helper.AvatarSizeProvide
import im.vector.app.features.home.room.detail.timeline.helper.MessageInformationDataFactory
import im.vector.app.features.home.room.detail.timeline.item.NoticeItem
import im.vector.app.features.home.room.detail.timeline.item.NoticeItem_
import org.matrix.android.sdk.api.extensions.orFalse
import javax.inject.Inject

class NoticeItemFactory @Inject constructor(private val eventFormatter: NoticeEventFormatter,
Expand All @@ -31,7 +32,7 @@ class NoticeItemFactory @Inject constructor(private val eventFormatter: NoticeEv

fun create(params: TimelineItemFactoryParams): NoticeItem? {
val event = params.event
val formattedText = eventFormatter.format(event) ?: return null
val formattedText = eventFormatter.format(event, isDm = params.partialState.roomSummary?.isDirect.orFalse()) ?: return null
val informationData = informationDataFactory.create(params)
val attributes = NoticeItem.Attributes(
avatarRenderer = avatarRenderer,
Expand Down
Loading

0 comments on commit 50a0426

Please sign in to comment.