mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-10-31 16:24:11 +00:00
ad1d7a3010
* 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.
158 lines
6.1 KiB
Swift
158 lines
6.1 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 SwiftUI
|
|
|
|
typealias MessageForwardingScreenViewModelType = StateStoreViewModel<MessageForwardingScreenViewState, MessageForwardingScreenViewAction>
|
|
|
|
class MessageForwardingScreenViewModel: MessageForwardingScreenViewModelType, MessageForwardingScreenViewModelProtocol {
|
|
private let forwardingPayload: MessageForwardingPayload
|
|
private let clientProxy: ClientProxyProtocol
|
|
private let roomSummaryProvider: RoomSummaryProviderProtocol
|
|
private let userIndicatorController: UserIndicatorControllerProtocol
|
|
|
|
private var actionsSubject: PassthroughSubject<MessageForwardingScreenViewModelAction, Never> = .init()
|
|
|
|
var actions: AnyPublisher<MessageForwardingScreenViewModelAction, Never> {
|
|
actionsSubject.eraseToAnyPublisher()
|
|
}
|
|
|
|
init(forwardingPayload: MessageForwardingPayload,
|
|
userSession: UserSessionProtocol,
|
|
roomSummaryProvider: RoomSummaryProviderProtocol,
|
|
userIndicatorController: UserIndicatorControllerProtocol) {
|
|
self.forwardingPayload = forwardingPayload
|
|
clientProxy = userSession.clientProxy
|
|
self.roomSummaryProvider = roomSummaryProvider
|
|
self.userIndicatorController = userIndicatorController
|
|
|
|
super.init(initialViewState: MessageForwardingScreenViewState(), mediaProvider: userSession.mediaProvider)
|
|
|
|
roomSummaryProvider.roomListPublisher
|
|
.receive(on: DispatchQueue.main)
|
|
.sink { [weak self] _ in
|
|
self?.updateRooms()
|
|
}
|
|
.store(in: &cancellables)
|
|
|
|
context.$viewState
|
|
.map(\.bindings.searchQuery)
|
|
.removeDuplicates()
|
|
.sink { [weak self] searchQuery in
|
|
if searchQuery.isEmpty {
|
|
self?.roomSummaryProvider.setFilter(.all(filters: []))
|
|
} else {
|
|
self?.roomSummaryProvider.setFilter(.search(query: searchQuery))
|
|
}
|
|
}
|
|
.store(in: &cancellables)
|
|
|
|
updateRooms()
|
|
}
|
|
|
|
override func process(viewAction: MessageForwardingScreenViewAction) {
|
|
switch viewAction {
|
|
case .cancel:
|
|
actionsSubject.send(.dismiss)
|
|
case .send:
|
|
Task { await forward() }
|
|
case .selectRoom(let roomID):
|
|
if state.selectedRoomIDs.contains(roomID) == false, state.selectedRoomIDs.count < state.maxRoomSelectionCount {
|
|
state.selectedRoomIDs.insert(roomID)
|
|
} else {
|
|
state.selectedRoomIDs.remove(roomID)
|
|
}
|
|
case .reachedTop:
|
|
updateVisibleRange(edge: .top)
|
|
case .reachedBottom:
|
|
updateVisibleRange(edge: .bottom)
|
|
}
|
|
}
|
|
|
|
func stop() {
|
|
// This is a shared provider so we should reset the filtering when we are done with the view
|
|
roomSummaryProvider.setFilter(.all(filters: []))
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private func updateRooms() {
|
|
var rooms = [MessageForwardingRoom]()
|
|
|
|
for summary in roomSummaryProvider.roomListPublisher.value {
|
|
if summary.id == forwardingPayload.roomID {
|
|
continue
|
|
}
|
|
|
|
rooms.append(.init(id: summary.id,
|
|
title: summary.name,
|
|
description: summary.roomListDescription,
|
|
avatar: summary.avatar))
|
|
}
|
|
|
|
state.rooms = rooms
|
|
}
|
|
|
|
/// The actual range values don't matter as long as they contain the lower
|
|
/// or upper bounds. updateVisibleRange is a hybrid API that powers both
|
|
/// sliding sync visible range update and list paginations
|
|
/// For lists other than the home screen one we don't care about visible ranges,
|
|
/// we just need the respective bounds to be there to trigger a next page load or
|
|
/// a reset to just one page
|
|
private func updateVisibleRange(edge: UIRectEdge) {
|
|
switch edge {
|
|
case .top:
|
|
roomSummaryProvider.updateVisibleRange(0..<0)
|
|
case .bottom:
|
|
let roomCount = roomSummaryProvider.roomListPublisher.value.count
|
|
roomSummaryProvider.updateVisibleRange(roomCount..<roomCount)
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
private func forward() async {
|
|
guard !state.selectedRoomIDs.isEmpty else {
|
|
fatalError()
|
|
}
|
|
|
|
var succeededRoomIdentifiers = [String]()
|
|
var hasFailures = false
|
|
|
|
for roomID in state.selectedRoomIDs {
|
|
guard case let .joined(targetRoomProxy) = await clientProxy.roomForIdentifier(roomID) else {
|
|
MXLog.error("Failed retrieving room to forward to with id: \(roomID)")
|
|
hasFailures = true
|
|
continue
|
|
}
|
|
|
|
// The contents are already in timeline order and the send queue preserves it.
|
|
for content in forwardingPayload.contents {
|
|
if case .failure(let error) = await targetRoomProxy.timeline.sendMessageEventContent(content) {
|
|
MXLog.error("Failed forwarding message with error: \(error)")
|
|
hasFailures = true
|
|
}
|
|
}
|
|
|
|
succeededRoomIdentifiers.append(roomID)
|
|
}
|
|
|
|
if hasFailures {
|
|
userIndicatorController.submitIndicator(UserIndicator(title: L10n.errorUnknown))
|
|
} else if succeededRoomIdentifiers.count > 1 {
|
|
// The flow opens the room when there is a single one, otherwise the user stays here and needs to know it worked.
|
|
userIndicatorController.submitIndicator(UserIndicator(title: L10n.screenRoomMessagesForwarded(forwardingPayload.contents.count)))
|
|
}
|
|
|
|
if !succeededRoomIdentifiers.isEmpty {
|
|
actionsSubject.send(.sent(roomIDs: succeededRoomIdentifiers))
|
|
}
|
|
}
|
|
}
|