Files
element-x-ios/ElementX/Sources/Screens/Timeline/TimelineModels.swift
T
Mauro 426fdc70a1 Add a developer option and selection mode for timeline messages (#6102)
* Add a developer option and selection mode for timeline messages

First step of the multi-select messages feature (element-meta#3255, iOS
counterpart of element-x-android#6918). Everything is gated behind the new
`messageMultiSelectEnabled` developer option and nothing changes while it
is off.

- Adds a "Select" action to the timeline item menu for remote messages in
  the room, focused and thread timelines. Choosing it enters selection
  mode with that message selected.
- While selecting, every bubbled row shows a checkbox on its leading edge
  and tapping or long pressing a row toggles it. State events, redacted
  messages, live locations, unsupported events and local echoes cannot be
  selected and stay inert. Selected rows get the accent background.
- The navigation bar swaps to a Cancel button and an "N selected" title,
  the back button is hidden and the composer is removed until the
  selection ends (Cancel or deselecting the last item). This is the hook
  where the bulk action bar will land in the next PR.
- The selection is capped at 30 messages, showing a toast when the cap
  is hit, and is cleared if the developer option is switched off.
- The selection state lives in `TimelineViewModel` so room and thread
  timelines share the behaviour without extra plumbing.

Includes unit tests for the menu action gating and the selection state
transitions, plus RoomScreen and bubble styler previews for the new state.

* Harden the timeline selection mode after review

- Reconcile the selection with timeline updates: selected messages that
  become unselectable (e.g. redacted) are dropped, and the selection is
  cleared when the timeline is swapped for a focused or live one, so the
  count can never refer to items that aren't on screen.
- Keep the composer mounted but collapsed while selecting, instead of
  removing it from the hierarchy. This preserves the draft and composer
  mode and avoids the disappear side effects. The footer with the identity
  warnings stays visible.
- Give the long press modifier an `isEnabled` flag so the recogniser is
  disabled while selecting, rather than only skipping the action while
  still animating and buzzing.
- Gate the Mac context menu on the selection like the other gestures.
- VoiceOver: a selectable bubble becomes a button carrying the selected
  trait whose default action toggles it, non-selectable rows lose their
  message actions while selecting, and reactions, thread summaries and the
  checkbox are hidden from the accessibility tree in selection mode.

* Stop voice message recordings when entering selection mode

The composer is collapsed rather than removed while selecting, so an
in-progress recording would otherwise keep the microphone open out of
sight. Entering the selection now stops the recording, which moves the
composer to the preview state so the message is still there to play or
send once the selection ends. Playback of voice messages is unaffected.

* Use the translated strings for the message selection mode

The selection strings have been imported to Localazy, so the untranslated
placeholders are dropped in favour of `action_select`,
`screen_room_maximum_messages_selected` and the `screen_room_selection_count`
plural. Only these keys are taken from the download to keep the change
focused.

* Apply review suggestions to the message selection mode

Addresses the first review round on the selection mode:

- Undecryptable messages are no longer selectable, as none of the bulk
  actions can be taken on them from the single item menu either.
- Renames for clarity: the menu action is `selectMessages` (leaving room
  for a future `selectText`), the view action is `startSelection`, the
  state exposes `contains(_:)`, `limit` and `isAtLimit`, and the composer
  collapsing modifier is `collapsedInPlace(_:)`.
- `canSelectMessages` uses a switch expression, the selection state doc
  comment refers to a feature flag rather than Labs, the recording helper
  doc describes only what it does, and `toggleSelection()` moves below
  the subviews.

* Keep the accessibility labels of timeline items while selecting

The selection mode used to replace each item's accessibility body with a
plain combined element, dropping the sender name representation and the
gallery label. The per item labelling now always applies, and the selection
only changes the interaction on top of it: the message actions are offered
as before, or the item becomes a button carrying the selected trait whose
default action toggles it. Polls and galleries collapse into a single
element only while selecting, as their children are inert then.

* Keep the timeline types within the lint limits after rebasing

The redaction reason work on develop grew the menu action handler and the
timeline view model right up to SwiftLint's size limits, so the selection
additions tipped them over. The `selectMessages` case now shares the
`downloadMedia` break and the selection flag subscription lives in the
selection extension.

* Fix the snapshot regressions from the selection mode

- Drop `geometryGroup()` from the bubble row: it stopped the avatar
  placeholder initial from rendering, which showed up as a mismatch in
  every timeline item snapshot.
- Collapse the composer with opacity instead of clipping, as the clip also
  removed the composer background from the home indicator area while the
  composer was fully visible.
- Keep the timeline item menu preview on the default flag-off state so the
  existing menu snapshots stay valid.
- Record the new "Selecting" room screen and "Selection" bubble snapshots
  on the same iPhone SE simulator CI uses.
2026-09-09 13:36:57 +02:00

450 lines
17 KiB
Swift

//
// Copyright 2025 Element Creations Ltd.
// Copyright 2022-2025 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import Combine
import MatrixRustSDK
import OrderedCollections
import SwiftUI
enum TimelineViewModelAction {
case displayEmojiPicker(selectedEmojis: Set<String>, continuation: EmojiPickerScreenContinuation)
case displayReportContent(itemID: TimelineItemIdentifier, senderID: String)
case displayCameraPicker
case displayMediaPicker
case displayDocumentPicker
case displayLocationPicker
case displayNewPollForm
case displayEditPollForm(eventID: String, poll: Poll)
case displayMediaUploadPreviewScreen(mediaURLs: [URL])
case displaySenderDetails(userID: String)
case displayMessageForwarding(forwardingItem: MessageForwardingItem)
case displayMediaPreview(TimelineMediaPreviewViewModel)
case displayLocation(StaticLocationData)
case displayLiveLocation(sender: TimelineItemSender, initialLiveLocationShare: LiveLocationShare)
case displayResolveSendFailure(failure: TimelineItemSendFailure.VerifiedUser, sendHandle: SendHandleProxy)
case displayThread(itemID: TimelineItemIdentifier)
case composer(action: TimelineComposerAction)
case hasScrolled(direction: ScrollDirection)
case viewInRoomTimeline(eventID: String, threadRootEventID: String?)
case displayRoom(roomID: String, via: [String])
case displayMediaDetails(item: EventBasedMessageTimelineItemProtocol)
case presentCallScreen(isVoiceCall: Bool)
}
enum TimelineViewPollAction {
case sendResponse(pollStartID: String, answerIDs: [String])
case end(pollStartID: String)
case edit(pollStartID: String, poll: Poll)
}
enum TimelineAudioPlayerAction {
case playPause(itemID: TimelineItemIdentifier)
case seek(itemID: TimelineItemIdentifier, progress: Double)
case changePlaybackSpeed(itemID: TimelineItemIdentifier)
}
enum TimelineViewAction {
case itemAppeared(itemID: TimelineItemIdentifier)
case itemDisappeared(itemID: TimelineItemIdentifier)
case mediaTapped(itemID: TimelineItemIdentifier)
case galleryItemTapped(GalleryItemID)
case itemSendInfoTapped(itemID: TimelineItemIdentifier)
case toggleReaction(key: String, itemID: TimelineItemIdentifier)
case sendReadReceiptIfNeeded(TimelineItemIdentifier)
case paginateBackwards
case paginateForwards
case scrollToBottom
case scrollToFirstItemForCurrentDate
case scrollToReadMarker
case markAllAsRead
case displayTimelineItemMenu(itemID: TimelineItemIdentifier)
case handleTimelineItemMenuAction(itemID: TimelineItemIdentifier, action: TimelineItemMenuAction)
/// The user has confirmed the removal of an item, optionally giving a reason.
case redactConfirmed(itemID: TimelineItemIdentifier, reason: String?)
/// Start a multi-selection with the specified item.
case startSelection(itemID: TimelineItemIdentifier)
/// Add or remove an item from the active multi-selection.
case toggleSelection(itemID: TimelineItemIdentifier)
case clearSelection
case tappedOnSenderDetails(sender: TimelineItemSender)
case displayReactionSummary(itemID: TimelineItemIdentifier, key: String)
case displayEmojiPicker(itemID: TimelineItemIdentifier)
case displayReadReceipts(itemID: TimelineItemIdentifier)
case displayThread(itemID: TimelineItemIdentifier)
case handlePasteOrDrop(providers: [NSItemProvider])
case handlePollAction(TimelineViewPollAction)
case handleAudioPlayerAction(TimelineAudioPlayerAction)
case stopLiveLocationSharing(TimelineItemIdentifier)
/// Focus the timeline onto the specified event ID (switching to a detached timeline if needed).
case focusOnEventID(String)
/// Switch back to a live timeline (from a detached one).
case focusLive
/// The timeline scrolled to reveal the focussed item.
case scrolledToFocussedItem
/// The table view has loaded the first items for a new timeline.
case hasSwitchedTimeline
case hasScrolled(direction: ScrollDirection)
case displayPredecessorRoom
case joinActiveCall(isVoiceCall: Bool)
}
enum TimelineComposerAction {
case setMode(mode: ComposerMode)
case setText(plainText: String, htmlText: String?)
case setFocus
case removeFocus
case clear
}
struct TimelineViewState: BindableState {
let timelineKind: TimelineKind
/// The gallery attachments the timeline includes, or `nil` when it isn't filtered.
let allowedGalleryItemTypes: [TimelineAllowedGalleryItemType]?
var roomID: String
var members: [String: RoomMemberState] = [:]
var typingMembers: [String] = []
var showLoading = false
var showReadReceipts = false
var isDM = false
var timelineState: TimelineState // check the doc before changing this
var ownUserID: String
var canCurrentUserSendMessage = false
var canCurrentUserRedactOthers = false
var canCurrentUserRedactSelf = false
var canCurrentUserPin = false
var canCurrentUserKick = false
var canCurrentUserBan = false
var hideTimelineMedia: Bool
var isViewSourceEnabled: Bool
var areThreadsEnabled: Bool
var linkPreviewsEnabled: Bool
var jumpToReadMarkerEnabled: Bool
var selection: TimelineSelectionState
let hasPredecessor: Bool
/// The `pinnedEventIDs` are used only to determine if an item is already pinned or not.
/// It's updated from the room info, so it's faster than using the timeline
var pinnedEventIDs: Set<String> = []
/// A closure providing the associated audio player state for an item in the timeline.
var audioPlayerStateProvider: (@MainActor (_ itemId: TimelineItemIdentifier) -> AudioPlayerState?)?
/// A closure that updates the associated pill context
var pillContextUpdater: (@MainActor (PillContext) -> Void)?
/// A closure that returns the associated room name give its id
var roomNameForIDResolver: (@MainActor (String) -> String?)?
/// A closure that returns the associated room name give its alias
var roomNameForAliasResolver: (@MainActor (String) -> String?)?
var emojiProvider: EmojiProviderProtocol
var linkMetadataProvider: LinkMetadataProviderProtocol?
var mapTilerConfiguration: MapTilerConfiguration
var stoppedLiveLocationIDs: Set<TimelineItemIdentifier> = []
var bindings: TimelineViewStateBindings
}
struct TimelineViewStateBindings {
var isScrolledToBottom = true
/// Whether the read marker (NEW banner) is currently visible in the timeline viewport.
/// Used to hide the jump-to-unread button once the user has scrolled to the read marker.
var isReadMarkerVisible = false
/// Whether new messages have arrived while the user is scrolled away from the bottom of a
/// live timeline. Drives the presence dot on the scroll-to-bottom button and resets when the
/// user returns to the bottom.
var hasNewMessagesAtBottom = false
/// The timestamp of the topmost visible item, used to drive the floating date badge while scrolling.
var floatingDate: Date?
/// The state of wether reactions listed on the timeline are expanded/collapsed.
/// Key is itemID, value is the collapsed state.
var reactionsCollapsed: [TimelineItemIdentifier: Bool]
var alertInfo: AlertInfo<TimelineAlertInfoType>?
var debugInfo: TimelineItemDebugInfo?
var actionMenuInfo: TimelineItemActionMenuInfo?
var reactionSummaryInfo: ReactionSummaryInfo?
var readReceiptsSummaryInfo: ReadReceiptSummaryInfo?
var manageMemberViewModel: ManageRoomMemberSheetViewModel?
var redactConfirmationInfo: TimelineRedactConfirmationInfo?
var showTranslation = false
var textToBeTranslated: String?
}
/// The state of the multi-selection of messages, active as soon as an item is selected.
struct TimelineSelectionState: Equatable {
static let limit = 30
/// Mirrors the `messageMultiSelectEnabled` feature flag.
var isEnabled = false
/// The event IDs of the selected items. Only remote messages can be selected.
var selectedEventIDs: Set<String> = []
var isActive: Bool {
!selectedEventIDs.isEmpty
}
var count: Int {
selectedEventIDs.count
}
var isAtLimit: Bool {
count >= Self.limit
}
func contains(_ eventID: String?) -> Bool {
guard let eventID else { return false }
return selectedEventIDs.contains(eventID)
}
}
struct TimelineItemActionMenuInfo: Equatable, Identifiable {
static func == (lhs: TimelineItemActionMenuInfo, rhs: TimelineItemActionMenuInfo) -> Bool {
lhs.id == rhs.id
}
let item: EventBasedTimelineItemProtocol
var id: TimelineItemIdentifier {
item.id
}
}
struct ReactionSummaryInfo: Identifiable {
let reactions: [AggregatedReaction]
let selectedKey: String
var id: String {
selectedKey
}
}
struct ReadReceiptSummaryInfo: Identifiable {
let orderedReceipts: [ReadReceipt]
let id: TimelineItemIdentifier
}
/// The item that the user has asked to remove and must confirm, optionally giving a reason.
struct TimelineRedactConfirmationInfo: Identifiable {
let id: TimelineItemIdentifier
}
enum TimelineAlertInfoType: Hashable {
case audioRecodingPermissionError
case pollEndConfirmation(String)
case sendingFailed(reason: String?, sendHandle: SendHandleProxy?)
case encryptionAuthenticity(String)
case encryptionForwarder(String)
case inviteAgain
case unableToInvite
case unknown
}
struct RoomMemberState {
let displayName: String?
let avatarURL: URL?
let status: UserStatus
}
/// Used as the state for the TimelineView, to avoid having the context continuously refresh the list of items on each small change.
/// Is also nice to have this as a wrapper for any state that is directly connected to the timeline.
struct TimelineState {
var isLive = true
var paginationState = TimelinePaginationState.initial
/// The room is in the process of loading items from a new timeline (switching to/from a detached timeline).
var isSwitchingTimelines = false
struct FocussedEvent: Equatable {
enum Appearance {
/// The event should be shown using an animated scroll.
case animated
/// The event should be shown immediately, without any animation.
case immediate
/// The event has already been shown.
case hasAppeared
}
/// The ID of the event.
let eventID: String
/// How the event should be shown, or whether it has already appeared.
var appearance: Appearance
}
/// A focussed event that was navigated to via a permalink.
var focussedEvent: FocussedEvent?
/// These can be removed when we have full swiftUI and moved as @State values in the view
var scrollToBottomPublisher = PassthroughSubject<Void, Never>()
var scrollToFirstItemForDatePublisher = PassthroughSubject<Void, Never>()
var scrollToReadMarkerPublisher = PassthroughSubject<TimelineItemIdentifier.UniqueID, Never>()
var itemsDictionary = OrderedDictionary<TimelineItemIdentifier.UniqueID, RoomTimelineItemViewState>()
var uniqueIDs: [TimelineItemIdentifier.UniqueID] {
itemsDictionary.keys.elements
}
var itemViewStates: [RoomTimelineItemViewState] {
itemsDictionary.values.elements
}
/// The unique ID of the read marker (NEW banner) in the timeline, if present.
/// Recomputed by ``recomputeReadMarkerUniqueID()`` whenever ``itemsDictionary`` changes.
private(set) var readMarkerUniqueID: TimelineItemIdentifier.UniqueID?
/// The user's `m.fully_read` event ID, pushed from `RoomInfo`. Used as a fallback
/// signal when ``readMarkerUniqueID`` is nil because the marker event isn't paginated
/// into the loaded timeline window.
var fullyReadEventID: String?
/// Recomputes ``readMarkerUniqueID`` from ``itemsDictionary``. Call after assigning a new
/// value to ``itemsDictionary``.
mutating func recomputeReadMarkerUniqueID() {
readMarkerUniqueID = itemsDictionary.first { _, viewState in
if case .readMarker = viewState.type {
return true
}
return false
}?.key
}
func hasLoadedItem(with eventID: String) -> Bool {
itemViewStates.contains { $0.identifier.eventID == eventID }
}
}
enum ScrollDirection: Equatable {
case top
case bottom
}
extension TimelineViewState {
/// Multi-selection is only offered in the room and thread timelines.
var canSelectMessages: Bool {
guard selection.isEnabled else { return false }
return switch timelineKind {
case .live, .detached, .thread: true
case .pinned, .media: false
}
}
/// The user is at the bottom of a live timeline (no jump-to-bottom button needed).
var isAtBottomAndLive: Bool {
bindings.isScrolledToBottom && timelineState.isLive
}
/// Whether the scroll-to-bottom button should be shown: the user is scrolled away from the
/// bottom of a live timeline.
var shouldShowScrollToBottomButton: Bool {
!isAtBottomAndLive
}
/// Whether the jump-to-read-marker button should be shown.
///
/// Primary path: the SDK has materialised a virtual `ReadMarker` item in the
/// loaded timeline window. Show while it isn't visible in the viewport.
///
/// Fallback path: the marker event is older than the loaded window, so no
/// virtual item exists. Show only when items have actually loaded AND the
/// marker event isn't among them — if the marker IS loaded but no virtual
/// item was inserted, the user is caught up (no events newer than the marker)
/// and there's nothing to jump to.
var shouldShowJumpToReadMarker: Bool {
guard jumpToReadMarkerEnabled else { return false }
if timelineState.readMarkerUniqueID != nil {
return !bindings.isReadMarkerVisible
}
guard let fullyReadEventID = timelineState.fullyReadEventID else {
return false
}
return !timelineState.itemsDictionary.isEmpty
&& !timelineState.hasLoadedItem(with: fullyReadEventID)
}
/// The string shown as the message preview.
///
/// This converts the formatted body to a plain string to remove formatting
/// and render with a consistent font size. This conversion is done to avoid
/// showing markdown characters in the preview for messages with formatting.
func buildMessagePreview(formattedBody: AttributedString?, plainBody: String) -> String {
guard let formattedBody,
let attributedString = try? NSMutableAttributedString(formattedBody, including: \.elementX) else {
return plainBody
}
let range = NSRange(location: 0, length: attributedString.length)
attributedString.enumerateAttributes(in: range) { attributes, range, _ in
if let userID = attributes[.MatrixUserID] as? String {
if let displayName = members[userID]?.displayName {
attributedString.replaceCharacters(in: range, with: "@\(displayName)")
} else {
attributedString.replaceCharacters(in: range, with: userID)
}
}
if attributes[.MatrixAllUsersMention] as? Bool == true {
attributedString.replaceCharacters(in: range, with: PillUtilities.atRoom)
}
if let roomAlias = attributes[.MatrixRoomAlias] as? String {
let roomName = roomNameForAliasResolver?(roomAlias)
attributedString.replaceCharacters(in: range, with: PillUtilities.roomPillDisplayText(roomName: roomName, rawRoomText: roomAlias))
}
if let roomID = attributes[.MatrixRoomID] as? String {
let roomName = roomNameForIDResolver?(roomID)
attributedString.replaceCharacters(in: range, with: PillUtilities.roomPillDisplayText(roomName: roomName, rawRoomText: roomID))
}
if let eventOnRoomID = attributes[.MatrixEventOnRoomID] as? EventOnRoomIDAttribute.Value {
let roomID = eventOnRoomID.roomID
let roomName = roomNameForIDResolver?(roomID)
attributedString.replaceCharacters(in: range, with: PillUtilities.eventPillDisplayText(roomName: roomName, rawRoomText: roomID))
}
if let eventOnRoomAlias = attributes[.MatrixEventOnRoomAlias] as? EventOnRoomAliasAttribute.Value {
let roomAlias = eventOnRoomAlias.alias
let roomName = roomNameForAliasResolver?(roomAlias)
attributedString.replaceCharacters(in: range, with: PillUtilities.eventPillDisplayText(roomName: roomName, rawRoomText: eventOnRoomAlias.alias))
}
}
return attributedString.string
}
}