Add multi selection to the Forward action (#6190)

* Start message selection from the Forward action

Behind the multi-select developer option, choosing Forward on a message
now starts a selection with that message checked, instead of the
dedicated Select menu action which is removed. Only forwardable
messages can be selected: remote, decrypted, non-redacted events that
aren't state, unsupported, poll or live location items.

The chrome follows the Figma exploration: the room header stays, an X
in the trailing toolbar position ends the selection, rows show only the
checkbox (no highlight, 10pt gap to the bubble), and the composer is
replaced by a single primary "Forward N messages" button. The limits
are now 10 messages into 5 rooms.

Tapping the button forwards the selection in timeline order, never in
the order the messages were tapped. MessageForwardingItem carries
several contents and the forwarding screen sends them one after the
other per room, reporting a single error toast on partial failures.
The selection is kept while the forwarding screen is open so cancelling
it returns to the selection; it is cleared by the flow coordinator once
the messages are sent, or by the X in the timeline.

New plural action_forward_messages lives in Untranslated for now;
action_select and screen_room_selection_count are no longer used.

* Tidy the selection code after review

Reuse ToolbarButton's cancel role for the selection's toolbar item so it
matches the other screens on every OS version, and disable the room
header while selecting so the room details can't cover the selection.

Move the forwardable/selected item helpers next to the existing
nonisolated array extension, drop the unused MessageForwardingItem
initialisers and the single-item makeForwardingItem overload, and
simplify the forwarding loop now that a room either sends or isn't
reachable.

The action bar preview builds its own view model instead of putting
the shared mock into selection mode for every later preview, and a
test locks that Forward keeps forwarding directly in the pinned
timeline.

* Select messages to forward from the pinned timeline too

Forward is offered in the pinned events timeline, so with the flag on
it starts a selection there as well, with the same cancel item in the
toolbar and the same forward button at the bottom. Forwarding from
that sheet already dismisses the whole sheet, so the selection is
discarded with it. Only the media timelines keep forwarding a single
item directly.

* Confirm with a toast when messages are forwarded to several rooms

When a single room is picked the flow opens it, which is confirmation
enough. When several rooms are picked the user stays in the origin room,
so a "Messages forwarded" toast tells them it worked, as in the designs.
The new screen_room_message_forwarded / screen_room_messages_forwarded
strings live in Untranslated for now.

* Apply review suggestions to multi-message forwarding

Strings
- The forward button and the multi-room toast now use the Localazy
  plurals action_forward_messages and screen_room_messages_forwarded;
  the toast shows the count. The Untranslated copies and the unused
  action_select are gone.

Selection
- Drop the dead startSelection view action and the redundant
  canSelectMessages guard; tests start a selection through Forward
  like the app does.
- Remove two comments on the selection state that restated the code.

Forwarding
- After a send, clear the selection in the room and any open thread
  instead of working out which one started it; only one can be
  selecting at a time.
- Explain that stickers are excluded because the SDK can only forward
  m.room.message content.

* Rename the timeline selection to message selection

"Selection" alone was too generic in a timeline that also has text and
room selections. The selection state, toolbar, action bar and
accessibility types, the timeline state's property, the view actions
and the public clear method now say "message selection". Private
helpers keep their short names.

* Rename MessageForwardingItem to MessageForwardingPayload

The type now carries several messages, so "item" no longer described
it. The factory method and the variables and action labels holding it
follow the new name.
This commit is contained in:
Mauro
2026-09-25 13:51:14 +02:00
committed by GitHub
parent fc8ee9d93d
commit ad1d7a3010
71 changed files with 826 additions and 388 deletions
@@ -103,7 +103,7 @@ class TimelineViewModel: TimelineViewModelType, TimelineViewModelProtocol {
areThreadsEnabled: appSettings.threadsEnabled,
linkPreviewsEnabled: appSettings.linkPreviewsEnabled,
jumpToReadMarkerEnabled: appSettings.jumpToReadMarkerEnabled,
selection: .init(isEnabled: appSettings.messageMultiSelectEnabled),
messageSelection: .init(isEnabled: appSettings.messageMultiSelectEnabled),
hasPredecessor: roomProxy.predecessorRoom != nil,
pinnedEventIDs: roomProxy.infoPublisher.value.pinnedEventIDs,
emojiProvider: emojiProvider,
@@ -198,7 +198,7 @@ class TimelineViewModel: TimelineViewModelType, TimelineViewModelProtocol {
}
case .displayTimelineItemMenu(let itemID):
timelineInteractionHandler.displayTimelineItemActionMenu(for: itemID)
case .handleTimelineItemMenuAction(let itemID, .selectMessages):
case .handleTimelineItemMenuAction(let itemID, .forward) where state.canSelectMessages:
startSelection(itemID: itemID)
case .handleTimelineItemMenuAction(let itemID, let action):
timelineInteractionHandler.handleTimelineItemMenuAction(action, itemID: itemID)
@@ -206,12 +206,12 @@ class TimelineViewModel: TimelineViewModelType, TimelineViewModelProtocol {
state.bindings.redactConfirmationInfo = nil
// A blank reason is no reason at all, so don't send one.
timelineInteractionHandler.redact(itemID, reason: reason?.isBlank == false ? reason : nil)
case .startSelection(let itemID):
startSelection(itemID: itemID)
case .toggleSelection(let itemID):
case .toggleMessageSelection(let itemID):
toggleSelection(itemID: itemID)
case .clearSelection:
state.selection.selectedEventIDs.removeAll()
case .clearMessageSelection:
clearMessageSelection()
case .forwardMessageSelection:
forwardSelection()
case .tappedOnSenderDetails(let sender):
handleTappedOnSenderDetails(sender: sender)
case .displayEmojiPicker(let itemID):
@@ -313,11 +313,6 @@ class TimelineViewModel: TimelineViewModelType, TimelineViewModelProtocol {
await userSession.liveLocationManager.stopLiveLocation(roomID: roomProxy.id)
}
func makeForwardingItem(for itemID: TimelineItemIdentifier) async -> MessageForwardingItem? {
guard let content = await timelineController.messageEventContent(for: itemID) else { return nil }
return .init(id: itemID, roomID: roomProxy.id, content: content)
}
// MARK: - Private
private func handleTappedOnSenderDetails(sender: TimelineItemSender) {
@@ -1059,13 +1054,6 @@ class TimelineViewModel: TimelineViewModelType, TimelineViewModelProtocol {
state.bindings.readReceiptsSummaryInfo = .init(orderedReceipts: eventTimelineItem.properties.orderedReadReceipts, id: eventTimelineItem.id)
}
// MARK: - Message forwarding
private func forwardMessage(itemID: TimelineItemIdentifier) async {
guard let forwardingItem = await makeForwardingItem(for: itemID) else { return }
actionsSubject.send(.displayMessageForwarding(forwardingItem: forwardingItem))
}
// MARK: Pills
private func pillContextUpdater(_ pillContext: PillContext) {
@@ -1185,68 +1173,106 @@ class TimelineViewModel: TimelineViewModelType, TimelineViewModelProtocol {
}
}
// MARK: - Message forwarding
extension TimelineViewModel {
func makeForwardingPayload(for itemIDs: [TimelineItemIdentifier]) async -> MessageForwardingPayload? {
var ids = [TimelineItemIdentifier]()
var contents = [RoomMessageEventContentWithoutRelation]()
for itemID in itemIDs {
guard let content = await timelineController.messageEventContent(for: itemID) else { continue }
ids.append(itemID)
contents.append(content)
}
guard !contents.isEmpty else { return nil }
return .init(ids: ids, roomID: roomProxy.id, contents: contents)
}
private func forwardMessage(itemID: TimelineItemIdentifier) async {
guard let forwardingPayload = await makeForwardingPayload(for: [itemID]) else { return }
actionsSubject.send(.displayMessageForwarding(forwardingPayload: forwardingPayload))
}
}
// MARK: - Selection
extension TimelineViewModel {
private func setupSelectionSubscriptions() {
appSettings.messageMultiSelectEnabledPublisher
.sink { [weak self] isEnabled in
self?.state.selection.isEnabled = isEnabled
self?.state.messageSelection.isEnabled = isEnabled
if !isEnabled {
self?.state.selection.selectedEventIDs.removeAll()
self?.state.messageSelection.selectedEventIDs.removeAll()
}
}
.store(in: &cancellables)
}
private func startSelection(itemID: TimelineItemIdentifier) {
guard state.canSelectMessages, let eventID = selectableEventID(for: itemID) else { return }
guard let eventID = selectableEventID(for: itemID) else { return }
// The composer is collapsed while selecting, so don't leave the microphone open behind it.
Task { await timelineInteractionHandler.stopRecordingVoiceMessageIfNeeded() }
actionsSubject.send(.composer(action: .removeFocus))
guard !state.selection.isAtLimit || state.selection.selectedEventIDs.contains(eventID) else {
guard !state.messageSelection.isAtLimit || state.messageSelection.selectedEventIDs.contains(eventID) else {
showSelectionLimitToast()
return
}
state.selection.selectedEventIDs.insert(eventID)
state.messageSelection.selectedEventIDs.insert(eventID)
}
private func toggleSelection(itemID: TimelineItemIdentifier) {
guard state.selection.isActive, let eventID = selectableEventID(for: itemID) else { return }
guard state.messageSelection.isActive, let eventID = selectableEventID(for: itemID) else { return }
if state.selection.selectedEventIDs.contains(eventID) {
state.selection.selectedEventIDs.remove(eventID)
} else if state.selection.isAtLimit {
if state.messageSelection.selectedEventIDs.contains(eventID) {
state.messageSelection.selectedEventIDs.remove(eventID)
} else if state.messageSelection.isAtLimit {
showSelectionLimitToast()
} else {
state.selection.selectedEventIDs.insert(eventID)
state.messageSelection.selectedEventIDs.insert(eventID)
}
}
/// Drops selected items that are no longer selectable (e.g. redacted), or the whole selection
/// when the timeline is swapped, so the selection always refers to items that are on screen.
private func reconcileSelection(with timelineItems: [RoomTimelineItemProtocol], isSwitchingTimelines: Bool) {
guard state.selection.isActive else { return }
guard state.messageSelection.isActive else { return }
if isSwitchingTimelines {
state.selection.selectedEventIDs.removeAll()
state.messageSelection.selectedEventIDs.removeAll()
return
}
let selectableEventIDs = timelineItems.compactMap { item -> String? in
guard let item = item as? EventBasedTimelineItemProtocol, item.isBulkSelectable else { return nil }
return item.id.eventID
}
state.selection.selectedEventIDs.formIntersection(selectableEventIDs)
let forwardableEventIDs = timelineItems.forwardableItems.compactMap(\.id.eventID)
state.messageSelection.selectedEventIDs.formIntersection(forwardableEventIDs)
}
/// The event ID of the item, when it is part of this timeline and can be bulk selected.
/// Ends the selection, either from the close button or once the selected messages have been forwarded.
func clearMessageSelection() {
state.messageSelection.selectedEventIDs.removeAll()
}
/// Forwards the selection in timeline order, which is unrelated to the order the messages were selected in.
/// The selection is kept until the forwarding completes, so cancelling the picker returns to it.
private func forwardSelection() {
let itemIDs = timelineController.timelineItems.selectedItems(state.messageSelection.selectedEventIDs).map(\.id)
Task {
guard let forwardingPayload = await makeForwardingPayload(for: itemIDs) else {
displayErrorToast(L10n.errorUnknown)
return
}
actionsSubject.send(.displayMessageForwarding(forwardingPayload: forwardingPayload))
}
}
/// The event ID of the item, when it is part of this timeline and can be forwarded.
private func selectableEventID(for itemID: TimelineItemIdentifier) -> String? {
guard let item = timelineController.timelineItems.firstUsingStableID(itemID) as? EventBasedTimelineItemProtocol,
item.isBulkSelectable else {
item.isForwardable else {
return nil
}
return item.id.eventID