mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 12:07:55 +00:00
426fdc70a1
* 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.
740 lines
35 KiB
Swift
740 lines
35 KiB
Swift
//
|
|
// Copyright 2025 Element Creations Ltd.
|
|
// Copyright 2023-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 UIKit
|
|
|
|
enum TimelineInteractionHandlerAction {
|
|
case composer(action: TimelineComposerAction)
|
|
|
|
case displayEmojiPicker(selectedEmojis: Set<String>, continuation: EmojiPickerScreenContinuation)
|
|
case displayReportContent(itemID: TimelineItemIdentifier, senderID: String)
|
|
case displayMessageForwarding(itemID: TimelineItemIdentifier)
|
|
case displayMediaUploadPreviewScreen(mediaURLs: [URL])
|
|
case displayEditPollForm(eventID: String, poll: Poll)
|
|
|
|
case showActionMenu(TimelineItemActionMenuInfo)
|
|
case showRedactConfirmation(itemID: TimelineItemIdentifier)
|
|
case showDebugInfo(TimelineItemDebugInfo)
|
|
|
|
case displayAudioRecorderPermissionError
|
|
case displayErrorToast(String)
|
|
|
|
case viewInRoomTimeline(eventID: String)
|
|
case displayThread(itemID: TimelineItemIdentifier)
|
|
case showTranslation(text: String)
|
|
}
|
|
|
|
/// The interaction handler groups logic for dealing with various actions the user can take on a timeline's
|
|
/// view that would've normally been part of the ``TimelineViewModel``
|
|
class TimelineInteractionHandler {
|
|
private let roomProxy: JoinedRoomProxyProtocol
|
|
private let timelineController: TimelineControllerProtocol
|
|
private let userSession: UserSessionProtocol
|
|
private let mediaPlayerProvider: MediaPlayerProviderProtocol
|
|
private let voiceMessageRecorder: VoiceMessageRecorderProtocol
|
|
private let userIndicatorController: UserIndicatorControllerProtocol
|
|
private let appMediator: AppMediatorProtocol
|
|
private let appSettings: AppSettings
|
|
private let analyticsService: AnalyticsServiceProtocol
|
|
private let emojiProvider: EmojiProviderProtocol
|
|
private let linkMetadataProvider: LinkMetadataProviderProtocol
|
|
private let timelineControllerFactory: TimelineControllerFactoryProtocol
|
|
private let pollInteractionHandler: PollInteractionHandlerProtocol
|
|
|
|
private let actionsSubject: PassthroughSubject<TimelineInteractionHandlerAction, Never> = .init()
|
|
var actions: AnyPublisher<TimelineInteractionHandlerAction, Never> {
|
|
actionsSubject.eraseToAnyPublisher()
|
|
}
|
|
|
|
private var voiceMessageRecorderObserver: AnyCancellable? {
|
|
didSet {
|
|
appMediator.setIdleTimerDisabled(voiceMessageRecorderObserver != nil)
|
|
}
|
|
}
|
|
|
|
private var resumeVoiceMessagePlaybackAfterScrubbing = false
|
|
|
|
/// The voice message playback that was last asked for, and whether the player has begun it.
|
|
private enum VoiceMessagePlayback {
|
|
case requested(TimelineItemIdentifier)
|
|
case playing(TimelineItemIdentifier)
|
|
}
|
|
|
|
private var voiceMessagePlayback: VoiceMessagePlayback?
|
|
private var audioPlayerActionsCancellable: AnyCancellable?
|
|
|
|
private var emojiPickerCancellable: AnyCancellable?
|
|
|
|
init(roomProxy: JoinedRoomProxyProtocol,
|
|
timelineController: TimelineControllerProtocol,
|
|
userSession: UserSessionProtocol,
|
|
mediaPlayerProvider: MediaPlayerProviderProtocol,
|
|
voiceMessageRecorder: VoiceMessageRecorderProtocol,
|
|
userIndicatorController: UserIndicatorControllerProtocol,
|
|
appMediator: AppMediatorProtocol,
|
|
appSettings: AppSettings,
|
|
analyticsService: AnalyticsServiceProtocol,
|
|
emojiProvider: EmojiProviderProtocol,
|
|
linkMetadataProvider: LinkMetadataProviderProtocol,
|
|
timelineControllerFactory: TimelineControllerFactoryProtocol) {
|
|
self.roomProxy = roomProxy
|
|
self.timelineController = timelineController
|
|
self.userSession = userSession
|
|
self.mediaPlayerProvider = mediaPlayerProvider
|
|
self.voiceMessageRecorder = voiceMessageRecorder
|
|
self.userIndicatorController = userIndicatorController
|
|
self.appMediator = appMediator
|
|
self.appSettings = appSettings
|
|
self.analyticsService = analyticsService
|
|
self.emojiProvider = emojiProvider
|
|
self.linkMetadataProvider = linkMetadataProvider
|
|
self.timelineControllerFactory = timelineControllerFactory
|
|
|
|
pollInteractionHandler = PollInteractionHandler(analyticsService: analyticsService,
|
|
timelineController: timelineController)
|
|
}
|
|
|
|
// MARK: Timeline Item Action Menu
|
|
|
|
func displayTimelineItemActionMenu(for itemID: TimelineItemIdentifier) {
|
|
Task {
|
|
guard let timelineItem = timelineController.timelineItems.firstUsingStableID(itemID),
|
|
let eventTimelineItem = timelineItem as? EventBasedTimelineItemProtocol else {
|
|
// Don't show a menu for non-event based items.
|
|
return
|
|
}
|
|
|
|
actionsSubject.send(.composer(action: .removeFocus))
|
|
actionsSubject.send(.showActionMenu(.init(item: eventTimelineItem)))
|
|
}
|
|
}
|
|
|
|
func redact(_ itemID: TimelineItemIdentifier, reason: String?) {
|
|
// Redacting needs the event alone, so it works even when the item isn't part of this timeline,
|
|
// such as one held by a media preview that was built from a different one.
|
|
guard case let .event(_, eventOrTransactionID) = itemID else { fatalError() }
|
|
Task { await timelineController.redact(eventOrTransactionID, reason: reason) }
|
|
}
|
|
|
|
// swiftlint:disable:next cyclomatic_complexity
|
|
func handleTimelineItemMenuAction(_ action: TimelineItemMenuAction, itemID: TimelineItemIdentifier) {
|
|
if case .redact = action {
|
|
// An unsent message is only dropped from the send queue. No redaction event reaches
|
|
// the server, so there is nothing to attach a reason to, and asking for one would
|
|
// only delay the abort while the message might still go out.
|
|
if case .event(_, .eventID) = itemID {
|
|
actionsSubject.send(.showRedactConfirmation(itemID: itemID))
|
|
} else {
|
|
redact(itemID, reason: nil)
|
|
}
|
|
return
|
|
}
|
|
|
|
guard let timelineItem = timelineController.timelineItems.firstUsingStableID(itemID),
|
|
let eventTimelineItem = timelineItem as? EventBasedTimelineItemProtocol else {
|
|
return
|
|
}
|
|
|
|
switch action {
|
|
case .copy:
|
|
guard let messageTimelineItem = timelineItem as? EventBasedMessageTimelineItemProtocol else { return }
|
|
UIPasteboard.general.string = messageTimelineItem.body
|
|
case .copyCaption:
|
|
guard let messageTimelineItem = timelineItem as? EventBasedMessageTimelineItemProtocol,
|
|
let caption = messageTimelineItem.mediaCaption else {
|
|
return
|
|
}
|
|
UIPasteboard.general.string = caption
|
|
case .edit, .addCaption, .editCaption, .editPoll:
|
|
switch timelineItem {
|
|
case let messageTimelineItem as EventBasedMessageTimelineItemProtocol:
|
|
processEditMessageEvent(messageTimelineItem)
|
|
case let pollTimelineItem as PollRoomTimelineItem:
|
|
guard let eventID = pollTimelineItem.id.eventID else {
|
|
MXLog.error("Cannot edit poll with id: \(timelineItem.id)")
|
|
return
|
|
}
|
|
actionsSubject.send(.displayEditPollForm(eventID: eventID, poll: pollTimelineItem.poll))
|
|
default:
|
|
MXLog.error("Cannot edit item with id: \(timelineItem.id)")
|
|
}
|
|
case .removeCaption:
|
|
guard case let .event(_, eventOrTransactionID) = timelineItem.id else {
|
|
MXLog.error("Failed removing caption, missing event ID")
|
|
return
|
|
}
|
|
Task { await timelineController.removeCaption(eventOrTransactionID) }
|
|
case .copyPermalink:
|
|
guard let eventID = eventTimelineItem.id.eventID else {
|
|
actionsSubject.send(.displayErrorToast(L10n.errorFailedCreatingThePermalink))
|
|
return
|
|
}
|
|
|
|
Task {
|
|
guard case let .success(permalinkURL) = await roomProxy.matrixToEventPermalink(eventID) else {
|
|
actionsSubject.send(.displayErrorToast(L10n.errorFailedCreatingThePermalink))
|
|
return
|
|
}
|
|
|
|
UIPasteboard.general.url = permalinkURL
|
|
}
|
|
case .redact:
|
|
break // Handled above, before the timeline item is looked up.
|
|
case .reply:
|
|
guard let eventID = eventTimelineItem.id.eventID else { return }
|
|
|
|
let replyInfo = buildReplyInfo(for: eventTimelineItem)
|
|
let replyDetails = TimelineItemReplyDetails.loaded(sender: eventTimelineItem.sender, eventID: eventID, eventContent: replyInfo.type)
|
|
|
|
actionsSubject.send(.composer(action: .setMode(mode: .reply(eventID: eventID, replyDetails: replyDetails, isThread: replyInfo.isThread))))
|
|
case .replyInThread:
|
|
actionsSubject.send(.displayThread(itemID: eventTimelineItem.id))
|
|
case .forward(let itemID):
|
|
actionsSubject.send(.displayMessageForwarding(itemID: itemID))
|
|
case .viewSource:
|
|
let debugInfo = timelineController.debugInfo(for: eventTimelineItem.id)
|
|
MXLog.info("Showing debug info for \(eventTimelineItem.id)")
|
|
actionsSubject.send(.showDebugInfo(debugInfo))
|
|
case .report:
|
|
actionsSubject.send(.displayReportContent(itemID: itemID, senderID: eventTimelineItem.sender.id))
|
|
case .react:
|
|
displayEmojiPicker(for: itemID)
|
|
case .toggleReaction(let key):
|
|
guard case let .event(_, eventOrTransactionID) = itemID else { fatalError() }
|
|
Task { await timelineController.toggleReaction(key, to: eventOrTransactionID) }
|
|
case .endPoll(let pollStartID):
|
|
endPoll(pollStartID: pollStartID)
|
|
case .pin:
|
|
analyticsService.trackPinUnpinEvent(.init(from: timelineController.timelineKind == .pinned ? .MessagePinningList : .Timeline,
|
|
kind: .Pin))
|
|
guard let eventID = itemID.eventID else { return }
|
|
Task { await timelineController.pin(eventID: eventID) }
|
|
case .unpin:
|
|
analyticsService.trackPinUnpinEvent(.init(from: timelineController.timelineKind == .pinned ? .MessagePinningList : .Timeline,
|
|
kind: .Unpin))
|
|
guard let eventID = itemID.eventID else { return }
|
|
Task { await timelineController.unpin(eventID: eventID) }
|
|
case .viewInRoomTimeline:
|
|
analyticsService.trackInteraction(name: .PinnedMessageListViewTimeline)
|
|
guard let eventID = itemID.eventID else { return }
|
|
actionsSubject.send(.viewInRoomTimeline(eventID: eventID))
|
|
case .downloadMedia, .selectMessages:
|
|
break // Handled by the media preview screen and the TimelineViewModel respectively.
|
|
case .translate:
|
|
guard let messageTimelineItem = timelineItem as? EventBasedMessageTimelineItemProtocol else { return }
|
|
actionsSubject.send(.showTranslation(text: messageTimelineItem.body))
|
|
}
|
|
|
|
if action.switchToDefaultComposer {
|
|
actionsSubject.send(.composer(action: .setMode(mode: .default)))
|
|
}
|
|
}
|
|
|
|
private func processEditMessageEvent(_ messageTimelineItem: EventBasedMessageTimelineItemProtocol) {
|
|
guard case let .event(_, eventOrTransactionID) = messageTimelineItem.id else {
|
|
MXLog.error("Failed editing message, missing event id")
|
|
return
|
|
}
|
|
|
|
let text: String
|
|
var htmlText: String?
|
|
var editType = ComposerMode.EditType.default
|
|
switch messageTimelineItem.contentType {
|
|
case .text(let content):
|
|
text = content.body
|
|
htmlText = content.formattedBodyHTMLString
|
|
case .emote(let content):
|
|
text = "/me " + content.body
|
|
case .audio(let content):
|
|
text = content.caption ?? ""
|
|
htmlText = content.formattedCaptionHTMLString
|
|
editType = text.isEmpty ? .addCaption : .editCaption
|
|
case .file(let content):
|
|
text = content.caption ?? ""
|
|
htmlText = content.formattedCaptionHTMLString
|
|
editType = text.isEmpty ? .addCaption : .editCaption
|
|
case .image(let content):
|
|
text = content.caption ?? ""
|
|
htmlText = content.formattedCaptionHTMLString
|
|
editType = text.isEmpty ? .addCaption : .editCaption
|
|
case .video(let content):
|
|
text = content.caption ?? ""
|
|
htmlText = content.formattedCaptionHTMLString
|
|
editType = text.isEmpty ? .addCaption : .editCaption
|
|
case .gallery(let content):
|
|
text = content.caption ?? ""
|
|
htmlText = content.formattedCaptionHTMLString
|
|
editType = text.isEmpty ? .addCaption : .editCaption
|
|
default:
|
|
text = messageTimelineItem.body
|
|
}
|
|
|
|
// Always update the mode first and then the text so that the composer has time to save the text draft
|
|
actionsSubject.send(.composer(action: .setMode(mode: .edit(originalEventOrTransactionID: eventOrTransactionID, type: editType))))
|
|
actionsSubject.send(.composer(action: .setText(plainText: text, htmlText: htmlText)))
|
|
}
|
|
|
|
// MARK: Polls
|
|
|
|
func sendPollResponse(pollStartID: String, answerIDs: [String]) {
|
|
Task {
|
|
let sendPollResponseResult = await pollInteractionHandler.sendPollResponse(pollStartID: pollStartID, answerIDs: answerIDs)
|
|
|
|
switch sendPollResponseResult {
|
|
case .success:
|
|
break
|
|
case .failure:
|
|
actionsSubject.send(.displayErrorToast(L10n.errorUnknown))
|
|
}
|
|
}
|
|
}
|
|
|
|
func endPoll(pollStartID: String) {
|
|
Task {
|
|
let endPollResult = await pollInteractionHandler.endPoll(pollStartID: pollStartID)
|
|
|
|
switch endPollResult {
|
|
case .success:
|
|
break
|
|
case .failure:
|
|
actionsSubject.send(.displayErrorToast(L10n.errorUnknown))
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: Pasting and dropping
|
|
|
|
func handlePasteOrDrop(_ providers: [NSItemProvider]) {
|
|
Task {
|
|
let loadingIndicatorIdentifier = UUID().uuidString
|
|
self.userIndicatorController.submitIndicator(UserIndicator(id: loadingIndicatorIdentifier, type: .modal, title: L10n.commonLoading, persistent: true))
|
|
defer {
|
|
self.userIndicatorController.retractIndicatorWithId(loadingIndicatorIdentifier)
|
|
}
|
|
|
|
var mediaURLs = [URL]()
|
|
for provider in providers {
|
|
if let fileURL = await provider.storeData() {
|
|
mediaURLs.append(fileURL)
|
|
} else {
|
|
MXLog.error("Failed storing NSItemProvider data \(provider)")
|
|
self.actionsSubject.send(.displayErrorToast(L10n.screenRoomErrorFailedProcessingMedia))
|
|
}
|
|
}
|
|
|
|
if !mediaURLs.isEmpty {
|
|
self.actionsSubject.send(.displayMediaUploadPreviewScreen(mediaURLs: mediaURLs))
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: Voice messages
|
|
|
|
private func handleVoiceMessageRecorderAction(_ action: VoiceMessageRecorderAction) {
|
|
MXLog.debug("handling voice recorder action: \(action) - (audio)")
|
|
switch action {
|
|
case .didStartRecording(let audioRecorder):
|
|
let audioRecordState = AudioRecorderState()
|
|
audioRecordState.attachAudioRecorder(audioRecorder)
|
|
actionsSubject.send(.composer(action: .setMode(mode: .recordVoiceMessage(state: audioRecordState))))
|
|
case .didStopRecording(let previewAudioPlayerState, let url):
|
|
actionsSubject.send(.composer(action: .setMode(mode: .previewVoiceMessage(state: previewAudioPlayerState, waveform: .url(url), isUploading: false))))
|
|
voiceMessageRecorderObserver = nil
|
|
case .didFailWithError(let error):
|
|
switch error {
|
|
case .audioRecorderError(.recordPermissionNotGranted):
|
|
MXLog.info("permission to record audio has not been granted.")
|
|
actionsSubject.send(.displayAudioRecorderPermissionError)
|
|
default:
|
|
MXLog.error("failed to start voice message recording. \(error)")
|
|
actionsSubject.send(.composer(action: .setMode(mode: .default)))
|
|
}
|
|
}
|
|
}
|
|
|
|
func startRecordingVoiceMessage() async {
|
|
voiceMessageRecorderObserver = voiceMessageRecorder.actions
|
|
.receive(on: DispatchQueue.main)
|
|
.sink { [weak self] action in
|
|
self?.handleVoiceMessageRecorderAction(action)
|
|
}
|
|
|
|
await voiceMessageRecorder.startRecording()
|
|
}
|
|
|
|
func stopRecordingVoiceMessage() async {
|
|
await voiceMessageRecorder.stopRecording()
|
|
}
|
|
|
|
/// Stops the recording when one is in progress, moving the composer to the preview state.
|
|
func stopRecordingVoiceMessageIfNeeded() async {
|
|
guard voiceMessageRecorder.isRecording else { return }
|
|
await voiceMessageRecorder.stopRecording()
|
|
}
|
|
|
|
func cancelRecordingVoiceMessage() async {
|
|
await voiceMessageRecorder.cancelRecording()
|
|
voiceMessageRecorderObserver = nil
|
|
actionsSubject.send(.composer(action: .setMode(mode: .default)))
|
|
}
|
|
|
|
func deleteCurrentVoiceMessage() async {
|
|
if voiceMessageRecorder.isRecording {
|
|
await voiceMessageRecorder.cancelRecording()
|
|
} else {
|
|
await voiceMessageRecorder.deleteRecording()
|
|
}
|
|
|
|
voiceMessageRecorderObserver = nil
|
|
actionsSubject.send(.composer(action: .setMode(mode: .default)))
|
|
}
|
|
|
|
func sendCurrentVoiceMessage() async {
|
|
guard let audioPlayerState = voiceMessageRecorder.previewAudioPlayerState, let recordingURL = voiceMessageRecorder.recordingURL else {
|
|
actionsSubject.send(.displayErrorToast(L10n.errorFailedUploadingVoiceMessage))
|
|
return
|
|
}
|
|
|
|
analyticsService.trackComposer(inThread: false,
|
|
isEditing: false,
|
|
isReply: false,
|
|
messageType: .VoiceMessage,
|
|
startsThread: nil)
|
|
|
|
actionsSubject.send(.composer(action: .setMode(mode: .previewVoiceMessage(state: audioPlayerState, waveform: .url(recordingURL), isUploading: true))))
|
|
await voiceMessageRecorder.stopPlayback()
|
|
|
|
switch await voiceMessageRecorder.sendVoiceMessage(timelineController: timelineController, audioConverter: AudioConverter()) {
|
|
case .success:
|
|
await deleteCurrentVoiceMessage()
|
|
case .failure(let error):
|
|
MXLog.error("failed to send the voice message. \(error)")
|
|
actionsSubject.send(.composer(action: .setMode(mode: .previewVoiceMessage(state: audioPlayerState, waveform: .url(recordingURL), isUploading: false))))
|
|
actionsSubject.send(.displayErrorToast(L10n.errorFailedUploadingVoiceMessage))
|
|
}
|
|
}
|
|
|
|
func startPlayingRecordedVoiceMessage() async {
|
|
await mediaPlayerProvider.detachAllStates(except: voiceMessageRecorder.previewAudioPlayerState)
|
|
if case .failure(let error) = await voiceMessageRecorder.startPlayback() {
|
|
MXLog.error("failed to play recorded voice message. \(error)")
|
|
}
|
|
}
|
|
|
|
func pausePlayingRecordedVoiceMessage() {
|
|
voiceMessageRecorder.pausePlayback()
|
|
}
|
|
|
|
func seekRecordedVoiceMessage(to progress: Double) async {
|
|
await mediaPlayerProvider.detachAllStates(except: voiceMessageRecorder.previewAudioPlayerState)
|
|
await voiceMessageRecorder.seekPlayback(to: progress)
|
|
}
|
|
|
|
func scrubVoiceMessagePlayback(scrubbing: Bool) async {
|
|
guard let audioPlayerState = voiceMessageRecorder.previewAudioPlayerState else {
|
|
return
|
|
}
|
|
if scrubbing {
|
|
if audioPlayerState.playbackState == .playing {
|
|
resumeVoiceMessagePlaybackAfterScrubbing = true
|
|
pausePlayingRecordedVoiceMessage()
|
|
}
|
|
} else {
|
|
if resumeVoiceMessagePlaybackAfterScrubbing {
|
|
resumeVoiceMessagePlaybackAfterScrubbing = false
|
|
await startPlayingRecordedVoiceMessage()
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: Audio Playback
|
|
|
|
func changePlaybackSpeed(for itemID: TimelineItemIdentifier) {
|
|
let nextSpeed = appSettings.voiceMessagePlaybackSpeed.next
|
|
appSettings.voiceMessagePlaybackSpeed = nextSpeed
|
|
audioPlayerState(for: itemID)?.setPlaybackSpeed(nextSpeed)
|
|
}
|
|
|
|
func playPauseAudio(for itemID: TimelineItemIdentifier) async {
|
|
MXLog.info("Toggle play/pause audio for itemID \(itemID)")
|
|
guard let timelineItem = timelineController.timelineItems.firstUsingStableID(itemID) else {
|
|
fatalError("TimelineItem \(itemID) not found")
|
|
}
|
|
|
|
guard let voiceMessageRoomTimelineItem = timelineItem as? VoiceMessageRoomTimelineItem else {
|
|
fatalError("Invalid TimelineItem type for itemID \(itemID) (expecting `VoiceMessageRoomTimelineItem` but found \(type(of: timelineItem)) instead")
|
|
}
|
|
|
|
guard let source = voiceMessageRoomTimelineItem.content.source else {
|
|
MXLog.error("Cannot start voice message playback, source is not defined for itemID \(itemID)")
|
|
return
|
|
}
|
|
|
|
// Loading a new message tears the player down, which cancels any autoplay that was pending for the previous one.
|
|
voiceMessagePlayback = .requested(itemID)
|
|
|
|
let audioPlayer = mediaPlayerProvider.player
|
|
|
|
// Observe the player lazily so that the mocks used by the previews and the UI tests don't need one.
|
|
if audioPlayerActionsCancellable == nil {
|
|
audioPlayerActionsCancellable = audioPlayer.actions
|
|
.sink { [weak self] action in
|
|
Task { await self?.handleAudioPlayerAction(action) }
|
|
}
|
|
}
|
|
|
|
// Stop any recording in progress
|
|
if voiceMessageRecorder.isRecording {
|
|
await voiceMessageRecorder.stopRecording()
|
|
}
|
|
|
|
guard let audioPlayerState = audioPlayerState(for: itemID) else {
|
|
fatalError("Audio player state not found for \(itemID)")
|
|
}
|
|
|
|
// Ensure this one is attached
|
|
if !audioPlayerState.isAttached {
|
|
audioPlayerState.attachAudioPlayer(audioPlayer)
|
|
}
|
|
|
|
// Detach all other states
|
|
await mediaPlayerProvider.detachAllStates(except: audioPlayerState)
|
|
|
|
guard audioPlayer.sourceURL == source.url, audioPlayer.state != .error else {
|
|
// Load content
|
|
do {
|
|
MXLog.info("Loading voice message audio content from source for itemID \(itemID)")
|
|
let url = try await userSession.voiceMessageMediaManager.loadVoiceMessageFromSource(source, body: nil)
|
|
|
|
// Make sure that the player is still attached, as it may have been detached while waiting for the voice message to be loaded.
|
|
if audioPlayerState.isAttached {
|
|
audioPlayer.load(sourceURL: source.url, playbackURL: url, autoplay: true)
|
|
}
|
|
} catch {
|
|
MXLog.error("Failed to load voice message: \(error)")
|
|
audioPlayerState.reportError()
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
if audioPlayer.state == .playing {
|
|
audioPlayer.pause()
|
|
} else {
|
|
audioPlayer.play()
|
|
}
|
|
}
|
|
|
|
private func handleAudioPlayerAction(_ action: AudioPlayerAction) async {
|
|
switch action {
|
|
case .didStartPlaying:
|
|
if case .requested(let itemID) = voiceMessagePlayback {
|
|
voiceMessagePlayback = .playing(itemID)
|
|
}
|
|
case .didFinishPlaying:
|
|
// Anything else means the player was torn down to play something different
|
|
// rather than reaching the end of the message it had started.
|
|
guard case .playing(let finishedItemID) = voiceMessagePlayback else { return }
|
|
voiceMessagePlayback = nil
|
|
await autoplayVoiceMessage(following: finishedItemID)
|
|
case .didStartLoading, .didFinishLoading, .didPausePlaying, .didStopPlaying, .didFailWithError:
|
|
break
|
|
}
|
|
}
|
|
|
|
/// Plays the voice message directly following the given one.
|
|
private func autoplayVoiceMessage(following finishedItemID: TimelineItemIdentifier) async {
|
|
// The playback may have been taken over by another player state, such as the recorder's preview.
|
|
guard audioPlayerState(for: finishedItemID)?.isAttached == true,
|
|
let nextVoiceMessage = timelineController.timelineItems.voiceMessageDirectlyFollowing(finishedItemID) else {
|
|
return
|
|
}
|
|
|
|
MXLog.info("Autoplaying the voice message following itemID \(finishedItemID)")
|
|
mediaPlayerProvider.play(soundEffect: .tink)
|
|
await playPauseAudio(for: nextVoiceMessage.id)
|
|
}
|
|
|
|
func seekAudio(for itemID: TimelineItemIdentifier, progress: Double) async {
|
|
guard let playerState = mediaPlayerProvider.playerState(for: .timelineItemIdentifier(itemID)) else {
|
|
return
|
|
}
|
|
await mediaPlayerProvider.detachAllStates(except: playerState)
|
|
await playerState.updateState(progress: progress)
|
|
}
|
|
|
|
func audioPlayerState(for itemID: TimelineItemIdentifier) -> AudioPlayerState? {
|
|
guard let timelineItem = timelineController.timelineItems.firstUsingStableID(itemID) else {
|
|
MXLog.error("TimelineItem \(itemID) not found")
|
|
return nil
|
|
}
|
|
|
|
guard let voiceMessageRoomTimelineItem = timelineItem as? VoiceMessageRoomTimelineItem else {
|
|
MXLog.error("Invalid TimelineItem type (expecting `VoiceMessageRoomTimelineItem` but found \(type(of: timelineItem)) instead")
|
|
return nil
|
|
}
|
|
|
|
if let playerState = mediaPlayerProvider.playerState(for: .timelineItemIdentifier(itemID)) {
|
|
return playerState
|
|
}
|
|
|
|
let playerState = AudioPlayerState(id: .timelineItemIdentifier(itemID),
|
|
title: L10n.commonVoiceMessage,
|
|
duration: voiceMessageRoomTimelineItem.content.duration,
|
|
waveform: voiceMessageRoomTimelineItem.content.waveform,
|
|
playbackSpeed: appSettings.voiceMessagePlaybackSpeed,
|
|
playbackSpeedPublisher: appSettings.voiceMessagePlaybackSpeedPublisher)
|
|
mediaPlayerProvider.register(audioPlayerState: playerState)
|
|
return playerState
|
|
}
|
|
|
|
// MARK: Other
|
|
|
|
func displayEmojiPicker(for itemID: TimelineItemIdentifier) {
|
|
guard let timelineItem = timelineController.timelineItems.firstUsingStableID(itemID),
|
|
timelineItem.isReactable,
|
|
let eventTimelineItem = timelineItem as? EventBasedTimelineItemProtocol,
|
|
case let .event(_, eventOrTransactionID) = itemID else {
|
|
return
|
|
}
|
|
let selectedEmojis = Set(eventTimelineItem.properties.reactions.compactMap { $0.isHighlighted ? $0.key : nil })
|
|
|
|
let (stream, continuation) = AsyncStream<String>.makeStream()
|
|
actionsSubject.send(.displayEmojiPicker(selectedEmojis: selectedEmojis, continuation: continuation))
|
|
|
|
emojiPickerCancellable = Task { [weak self] in
|
|
for await emoji in stream {
|
|
await self?.timelineController.toggleReaction(emoji, to: eventOrTransactionID)
|
|
}
|
|
}
|
|
.asCancellable()
|
|
}
|
|
|
|
func processItemTap(_ itemID: TimelineItemIdentifier) async -> TimelineControllerAction {
|
|
guard let timelineItem = timelineController.timelineItems.firstUsingStableID(itemID) as? EventBasedTimelineItemProtocol else {
|
|
return .none
|
|
}
|
|
|
|
switch timelineItem {
|
|
case let item as LocationRoomTimelineItem:
|
|
guard let geoURI = item.content.geoURI else { return .none }
|
|
return .displayLocation(.init(sender: item.sender,
|
|
geoURI: geoURI,
|
|
kind: item.content.kind,
|
|
timestamp: item.timestamp))
|
|
case let item as LiveLocationRoomTimelineItem:
|
|
guard let geoURI = item.content.lastGeoURI else { return .none }
|
|
let initialLiveLocationShare = LiveLocationShare(userID: item.sender.id,
|
|
geoURI: geoURI,
|
|
timestamp: item.timestamp,
|
|
timeoutDate: item.content.timeoutDate)
|
|
return .displayLiveLocation(sender: item.sender, initialLiveLocationShare: initialLiveLocationShare)
|
|
// Galleries are included so that their attachments can be browsed as individual media.
|
|
case let item as ImageRoomTimelineItem:
|
|
return await mediaPreviewAction(for: item, messageTypes: [.image, .video, .gallery])
|
|
case let item as VideoRoomTimelineItem:
|
|
return await mediaPreviewAction(for: item, messageTypes: [.image, .video, .gallery])
|
|
case let item as AudioRoomTimelineItem:
|
|
return await mediaPreviewAction(for: item, messageTypes: [.audio, .file, .gallery])
|
|
case let item as FileRoomTimelineItem:
|
|
return await mediaPreviewAction(for: item, messageTypes: [.audio, .file, .gallery])
|
|
case let item as GalleryRoomTimelineItem:
|
|
// Only galleries are needed as the preview is scoped to the attachments of the tapped one.
|
|
return await mediaPreviewAction(for: item, messageTypes: [.gallery])
|
|
default:
|
|
return .none
|
|
}
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private func buildReplyInfo(for item: EventBasedTimelineItemProtocol) -> ReplyInfo {
|
|
switch item {
|
|
case let messageItem as EventBasedMessageTimelineItemProtocol:
|
|
return .init(type: .message(messageItem.contentType), isThread: messageItem.properties.isThreaded)
|
|
case let pollItem as PollRoomTimelineItem:
|
|
return .init(type: .poll(question: pollItem.poll.question), isThread: false)
|
|
default:
|
|
return .init(type: .message(.text(.init(body: item.body))), isThread: false)
|
|
}
|
|
}
|
|
|
|
private func mediaPreviewAction(for item: EventBasedMessageTimelineItemProtocol, messageTypes: [TimelineAllowedMessageType]) async -> TimelineControllerAction {
|
|
var newTimelineFocus: TimelineFocus?
|
|
var newTimelinePresentation: TimelineKind.MediaPresentation?
|
|
switch timelineController.timelineKind {
|
|
case .live:
|
|
newTimelineFocus = .live
|
|
newTimelinePresentation = .roomScreenLive
|
|
case .detached:
|
|
guard case let .event(_, eventOrTransactionID: .eventID(eventID)) = item.id else {
|
|
MXLog.error("Unexpected event type on a detached timeline.")
|
|
return .none
|
|
}
|
|
newTimelineFocus = .eventID(eventID)
|
|
newTimelinePresentation = .roomScreenDetached
|
|
case .pinned:
|
|
newTimelineFocus = .pinned
|
|
newTimelinePresentation = .pinnedEventsScreen
|
|
case .thread(let rootEventID):
|
|
newTimelineFocus = .thread(eventID: rootEventID)
|
|
newTimelinePresentation = .roomScreenThread
|
|
case .media:
|
|
break // We don't need to create a new timeline as it is already filtered.
|
|
}
|
|
|
|
if let newTimelineFocus, let newTimelinePresentation {
|
|
let timelineItemFactory = RoomTimelineItemFactory(userID: roomProxy.ownUserID,
|
|
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
|
|
stateEventStringBuilder: RoomStateEventStringBuilder(userID: roomProxy.ownUserID))
|
|
|
|
guard case let .success(timelineController) = await timelineControllerFactory.buildMessageFilteredTimelineController(focus: newTimelineFocus,
|
|
allowedMessageTypes: messageTypes,
|
|
presentation: newTimelinePresentation,
|
|
roomProxy: roomProxy,
|
|
timelineItemFactory: timelineItemFactory,
|
|
mediaProvider: userSession.mediaProvider) else {
|
|
MXLog.error("Failed presenting media timeline")
|
|
return .none
|
|
}
|
|
|
|
let timelineViewModel = TimelineViewModel(roomProxy: roomProxy,
|
|
timelineController: timelineController,
|
|
userSession: userSession,
|
|
mediaPlayerProvider: mediaPlayerProvider,
|
|
userIndicatorController: userIndicatorController,
|
|
appMediator: appMediator,
|
|
appSettings: appSettings,
|
|
analyticsService: analyticsService,
|
|
emojiProvider: emojiProvider,
|
|
linkMetadataProvider: linkMetadataProvider,
|
|
timelineControllerFactory: timelineControllerFactory)
|
|
|
|
return previewAction(for: item, timelineViewModel: .new(timelineViewModel))
|
|
} else {
|
|
return previewAction(for: item, timelineViewModel: .active)
|
|
}
|
|
}
|
|
|
|
/// A gallery is previewed scoped to its own attachments rather than the wider timeline's media.
|
|
private func previewAction(for item: EventBasedMessageTimelineItemProtocol,
|
|
timelineViewModel: TimelineControllerAction.TimelineViewModelKind) -> TimelineControllerAction {
|
|
guard let galleryItem = item as? GalleryRoomTimelineItem else {
|
|
return .displayMediaPreview(item: item, timelineViewModel: timelineViewModel)
|
|
}
|
|
|
|
return .displayGalleryPreview(galleryItem: galleryItem, timelineViewModel: timelineViewModel)
|
|
}
|
|
}
|
|
|
|
private struct ReplyInfo {
|
|
let type: TimelineEventContent
|
|
let isThread: Bool
|
|
}
|