mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-10-31 16:24:11 +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.
244 lines
11 KiB
Swift
244 lines
11 KiB
Swift
//
|
|
// Copyright 2026 Element Creations 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
|
|
@testable import ElementX
|
|
import Foundation
|
|
import Testing
|
|
|
|
@MainActor
|
|
struct TimelineInteractionHandlerTests {
|
|
private static let firstSourceURL = URL("mxc://matrix.org/voice1")
|
|
private static let secondSourceURL = URL("mxc://matrix.org/voice2")
|
|
|
|
private let audioPlayer: AudioPlayerMock
|
|
private let mediaPlayerProvider: MediaPlayerProviderMock
|
|
private let actionsSubject: PassthroughSubject<AudioPlayerAction, Never>
|
|
private let loadedSourceURLsSubject: PassthroughSubject<URL, Never>
|
|
|
|
private static let firstVoiceMessage = makeVoiceMessage(id: "voice1", sourceURL: firstSourceURL)
|
|
private static let secondVoiceMessage = makeVoiceMessage(id: "voice2", sourceURL: secondSourceURL)
|
|
private static let textMessage = TextRoomTimelineItem(id: .event(uniqueID: .init("text"), eventOrTransactionID: .eventID("text")),
|
|
timestamp: .mock,
|
|
isOutgoing: false,
|
|
isEditable: false,
|
|
canBeRepliedTo: true,
|
|
sender: .init(id: "@sender:example.com"),
|
|
content: .init(body: "Test message"))
|
|
|
|
init() {
|
|
let player = AudioPlayerMock()
|
|
let actionsSubject = PassthroughSubject<AudioPlayerAction, Never>()
|
|
let loadedSourceURLsSubject = PassthroughSubject<URL, Never>()
|
|
|
|
player.actions = actionsSubject.eraseToAnyPublisher()
|
|
player.state = .stopped
|
|
player.currentTime = 0
|
|
player.duration = 10
|
|
player.loadSourceURLPlaybackURLAutoplayClosure = { sourceURL, _, _ in
|
|
player.sourceURL = sourceURL
|
|
player.state = .playing
|
|
loadedSourceURLsSubject.send(sourceURL)
|
|
}
|
|
player.playClosure = { player.state = .playing }
|
|
player.pauseClosure = { player.state = .paused }
|
|
player.stopClosure = { player.state = .stopped }
|
|
|
|
let states = [Self.firstVoiceMessage, Self.secondVoiceMessage]
|
|
.reduce(into: [TimelineItemIdentifier.UniqueID: AudioPlayerState]()) { states, item in
|
|
states[item.id.uniqueID] = AudioPlayerState(id: .timelineItemIdentifier(item.id),
|
|
title: L10n.commonVoiceMessage,
|
|
duration: item.content.duration)
|
|
}
|
|
|
|
// Hands out one stable player state per voice message, as the real provider does.
|
|
let mediaPlayerProvider = MediaPlayerProviderMock()
|
|
mediaPlayerProvider.player = player
|
|
mediaPlayerProvider.playerStateForClosure = { identifier in
|
|
guard case .timelineItemIdentifier(let itemID) = identifier else { return nil }
|
|
return states[itemID.uniqueID]
|
|
}
|
|
|
|
audioPlayer = player
|
|
self.mediaPlayerProvider = mediaPlayerProvider
|
|
self.actionsSubject = actionsSubject
|
|
self.loadedSourceURLsSubject = loadedSourceURLsSubject
|
|
}
|
|
|
|
// MARK: - Voice message autoplay
|
|
|
|
@Test
|
|
func autoplaysTheFollowingVoiceMessage() async throws {
|
|
let handler = makeHandler(timelineItems: [Self.firstVoiceMessage, Self.secondVoiceMessage])
|
|
|
|
await handler.playPauseAudio(for: Self.firstVoiceMessage.id)
|
|
#expect(loadedSourceURLs == [Self.firstSourceURL])
|
|
|
|
let deferred = deferFulfillment(loadedSourceURLsSubject) { $0 == Self.secondSourceURL }
|
|
actionsSubject.send(.didStartPlaying)
|
|
actionsSubject.send(.didFinishPlaying)
|
|
try await deferred.fulfill()
|
|
|
|
#expect(loadedSourceURLs == [Self.firstSourceURL, Self.secondSourceURL])
|
|
#expect(mediaPlayerProvider.playSoundEffectReceivedSoundEffect == .tink)
|
|
}
|
|
|
|
@Test
|
|
func doesNotAutoplayAcrossAnotherMessage() async {
|
|
let handler = makeHandler(timelineItems: [Self.firstVoiceMessage, Self.textMessage, Self.secondVoiceMessage])
|
|
|
|
await handler.playPauseAudio(for: Self.firstVoiceMessage.id)
|
|
actionsSubject.send(.didStartPlaying)
|
|
actionsSubject.send(.didFinishPlaying)
|
|
await allowPlayerActionsToBeHandled()
|
|
|
|
#expect(loadedSourceURLs == [Self.firstSourceURL])
|
|
#expect(!mediaPlayerProvider.playSoundEffectCalled)
|
|
}
|
|
|
|
@Test
|
|
func doesNotAutoplayAfterTheLastVoiceMessage() async {
|
|
let handler = makeHandler(timelineItems: [Self.firstVoiceMessage])
|
|
|
|
await handler.playPauseAudio(for: Self.firstVoiceMessage.id)
|
|
actionsSubject.send(.didStartPlaying)
|
|
actionsSubject.send(.didFinishPlaying)
|
|
await allowPlayerActionsToBeHandled()
|
|
|
|
#expect(loadedSourceURLs == [Self.firstSourceURL])
|
|
#expect(!mediaPlayerProvider.playSoundEffectCalled)
|
|
}
|
|
|
|
@Test
|
|
func doesNotAutoplayAfterPausing() async {
|
|
let handler = makeHandler(timelineItems: [Self.firstVoiceMessage, Self.secondVoiceMessage])
|
|
|
|
await handler.playPauseAudio(for: Self.firstVoiceMessage.id)
|
|
actionsSubject.send(.didStartPlaying)
|
|
await allowPlayerActionsToBeHandled()
|
|
|
|
await handler.playPauseAudio(for: Self.firstVoiceMessage.id)
|
|
#expect(audioPlayer.pauseCalled)
|
|
|
|
actionsSubject.send(.didFinishPlaying)
|
|
await allowPlayerActionsToBeHandled()
|
|
|
|
#expect(loadedSourceURLs == [Self.firstSourceURL])
|
|
}
|
|
|
|
@Test
|
|
func replayingAVoiceMessageDoesNotJumpToTheFollowingOne() async throws {
|
|
let handler = makeHandler(timelineItems: [Self.firstVoiceMessage, Self.secondVoiceMessage])
|
|
|
|
// Play the first message all the way through so that the second one takes over.
|
|
await handler.playPauseAudio(for: Self.firstVoiceMessage.id)
|
|
let autoplay = deferFulfillment(loadedSourceURLsSubject) { $0 == Self.secondSourceURL }
|
|
actionsSubject.send(.didStartPlaying)
|
|
actionsSubject.send(.didFinishPlaying)
|
|
try await autoplay.fulfill()
|
|
actionsSubject.send(.didStartPlaying)
|
|
await allowPlayerActionsToBeHandled()
|
|
|
|
// Loading tears the player down, which is where the spurious end of playback came from.
|
|
audioPlayer.loadSourceURLPlaybackURLAutoplayClosure = { [audioPlayer, actionsSubject, loadedSourceURLsSubject] sourceURL, _, _ in
|
|
actionsSubject.send(.didFinishPlaying)
|
|
audioPlayer.sourceURL = sourceURL
|
|
audioPlayer.state = .playing
|
|
loadedSourceURLsSubject.send(sourceURL)
|
|
}
|
|
|
|
let replay = deferFulfillment(loadedSourceURLsSubject) { $0 == Self.firstSourceURL }
|
|
await handler.playPauseAudio(for: Self.firstVoiceMessage.id)
|
|
try await replay.fulfill()
|
|
await allowPlayerActionsToBeHandled()
|
|
|
|
#expect(loadedSourceURLs == [Self.firstSourceURL, Self.secondSourceURL, Self.firstSourceURL])
|
|
}
|
|
|
|
// MARK: - Voice message recording
|
|
|
|
@Test
|
|
func stopRecordingIfNeededStopsAnActiveRecording() async {
|
|
let voiceMessageRecorder = VoiceMessageRecorderMock()
|
|
voiceMessageRecorder.isRecording = true
|
|
let handler = makeHandler(timelineItems: [Self.textMessage], voiceMessageRecorder: voiceMessageRecorder)
|
|
|
|
await handler.stopRecordingVoiceMessageIfNeeded()
|
|
|
|
#expect(voiceMessageRecorder.stopRecordingCallsCount == 1)
|
|
}
|
|
|
|
@Test
|
|
func stopRecordingIfNeededIgnoresAnIdleRecorder() async {
|
|
let voiceMessageRecorder = VoiceMessageRecorderMock()
|
|
voiceMessageRecorder.isRecording = false
|
|
let handler = makeHandler(timelineItems: [Self.textMessage], voiceMessageRecorder: voiceMessageRecorder)
|
|
|
|
await handler.stopRecordingVoiceMessageIfNeeded()
|
|
|
|
#expect(!voiceMessageRecorder.stopRecordingCalled)
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private var loadedSourceURLs: [URL] {
|
|
audioPlayer.loadSourceURLPlaybackURLAutoplayReceivedInvocations.map(\.sourceURL)
|
|
}
|
|
|
|
/// The handler reacts to the player's actions in detached tasks, this gives them a chance to run.
|
|
private func allowPlayerActionsToBeHandled() async {
|
|
for _ in 0..<10 {
|
|
await Task.yield()
|
|
}
|
|
}
|
|
|
|
private func makeHandler(timelineItems: [RoomTimelineItemProtocol],
|
|
voiceMessageRecorder: VoiceMessageRecorderMock? = nil) -> TimelineInteractionHandler {
|
|
let voiceMessageRecorder = voiceMessageRecorder ?? {
|
|
let recorder = VoiceMessageRecorderMock()
|
|
recorder.isRecording = false
|
|
return recorder
|
|
}()
|
|
|
|
let voiceMessageMediaManager = VoiceMessageMediaManagerMock()
|
|
voiceMessageMediaManager.loadVoiceMessageFromSourceBodyReturnValue = URL("file:///voice-message.m4a")
|
|
|
|
let userSession = UserSessionMock(.init())
|
|
userSession.voiceMessageMediaManager = voiceMessageMediaManager
|
|
|
|
let appSettings = AppSettings.volatile()
|
|
|
|
return TimelineInteractionHandler(roomProxy: JoinedRoomProxyMock(.init()),
|
|
timelineController: TimelineControllerMock(.init(timelineItems: timelineItems)),
|
|
userSession: userSession,
|
|
mediaPlayerProvider: mediaPlayerProvider,
|
|
voiceMessageRecorder: voiceMessageRecorder,
|
|
userIndicatorController: UserIndicatorControllerMock(),
|
|
appMediator: AppMediatorMock(.init()),
|
|
appSettings: appSettings,
|
|
analyticsService: AnalyticsServiceMock(.init()),
|
|
emojiProvider: EmojiProvider(appSettings: appSettings),
|
|
linkMetadataProvider: LinkMetadataProvider(),
|
|
timelineControllerFactory: TimelineControllerFactoryMock(.init()))
|
|
}
|
|
|
|
private static func makeVoiceMessage(id: String, sourceURL: URL) -> VoiceMessageRoomTimelineItem {
|
|
VoiceMessageRoomTimelineItem(id: .event(uniqueID: .init(id), eventOrTransactionID: .eventID(id)),
|
|
timestamp: .mock,
|
|
isOutgoing: false,
|
|
isEditable: false,
|
|
canBeRepliedTo: true,
|
|
sender: .init(id: "@sender:example.com"),
|
|
content: .init(filename: "audio.ogg",
|
|
duration: 10,
|
|
waveform: .mockWaveform,
|
|
source: try? MediaSourceProxy(url: sourceURL, mimeType: "audio/ogg"),
|
|
fileSize: nil,
|
|
contentType: nil))
|
|
}
|
|
}
|