improved the code so that we can use the AudioPlayerState directly to check the playback speed and added localization

This commit is contained in:
Mauro Romito
2026-02-19 16:05:58 +01:00
committed by Mauro
parent 285f8b7834
commit 3dfcb6912f
13 changed files with 51 additions and 48 deletions
@@ -422,14 +422,14 @@ class TimelineInteractionHandler {
let availableSpeeds = VoiceMessagePlaybackSpeed.allCases
guard let currentIndex = availableSpeeds.firstIndex(of: appSettings.voiceMessagePlaybackSpeed) else {
appSettings.voiceMessagePlaybackSpeed = .default
audioPlayerState(for: itemID)?.setPlaybackSpeed(VoiceMessagePlaybackSpeed.default.rawValue)
audioPlayerState(for: itemID)?.setPlaybackSpeed(VoiceMessagePlaybackSpeed.default)
return
}
let nextIndex = (currentIndex + 1) % availableSpeeds.count
let nextSpeed = availableSpeeds[nextIndex]
appSettings.voiceMessagePlaybackSpeed = nextSpeed
audioPlayerState(for: itemID)?.setPlaybackSpeed(nextSpeed.rawValue)
audioPlayerState(for: itemID)?.setPlaybackSpeed(nextSpeed)
}
func playPauseAudio(for itemID: TimelineItemIdentifier) async {
@@ -461,7 +461,6 @@ class TimelineInteractionHandler {
// Ensure this one is attached
if !audioPlayerState.isAttached {
audioPlayerState.attachAudioPlayer(audioPlayer)
audioPlayerState.setPlaybackSpeed(appSettings.voiceMessagePlaybackSpeed.rawValue)
}
// Detach all other states
@@ -518,7 +517,9 @@ class TimelineInteractionHandler {
let playerState = AudioPlayerState(id: .timelineItemIdentifier(itemID),
title: L10n.commonVoiceMessage,
duration: voiceMessageRoomTimelineItem.content.duration,
waveform: voiceMessageRoomTimelineItem.content.waveform)
waveform: voiceMessageRoomTimelineItem.content.waveform,
playbackSpeed: appSettings.voiceMessagePlaybackSpeed,
playbackSpeedPublisher: appSettings.$voiceMessagePlaybackSpeed)
mediaPlayerProvider.register(audioPlayerState: playerState)
return playerState
}
@@ -115,7 +115,6 @@ struct TimelineViewState: BindableState {
var isViewSourceEnabled: Bool
var areThreadsEnabled: Bool
var linkPreviewsEnabled: Bool
var voiceMessagePlaybackSpeed = VoiceMessagePlaybackSpeed.default.rawValue
let hasPredecessor: Bool
@@ -102,7 +102,6 @@ class TimelineViewModel: TimelineViewModelType, TimelineViewModelProtocol {
isViewSourceEnabled: appSettings.viewSourceEnabled,
areThreadsEnabled: appSettings.threadsEnabled,
linkPreviewsEnabled: appSettings.linkPreviewsEnabled,
voiceMessagePlaybackSpeed: appSettings.voiceMessagePlaybackSpeed.rawValue,
hasPredecessor: roomProxy.predecessorRoom != nil,
pinnedEventIDs: roomProxy.infoPublisher.value.pinnedEventIDs,
emojiProvider: emojiProvider,
@@ -543,11 +542,6 @@ class TimelineViewModel: TimelineViewModelType, TimelineViewModelProtocol {
appSettings.$threadsEnabled
.weakAssign(to: \.state.areThreadsEnabled, on: self)
.store(in: &cancellables)
appSettings.$voiceMessagePlaybackSpeed
.map(\.rawValue)
.weakAssign(to: \.state.voiceMessagePlaybackSpeed, on: self)
.store(in: &cancellables)
userSession.clientProxy.timelineMediaVisibilityPublisher
.removeDuplicates()
@@ -6,6 +6,7 @@
// Please see LICENSE files in the repository root for full details.
//
import Combine
import Foundation
import SwiftUI
@@ -23,7 +24,7 @@ struct VoiceMessageRoomTimelineView: View {
}
struct VoiceMessageRoomTimelineContent: View {
@EnvironmentObject private var context: TimelineViewModel.Context
@Environment(\.timelineContext) private var context
@State private var resumePlaybackAfterScrubbing = false
let timelineItem: VoiceMessageRoomTimelineItem
@@ -34,32 +35,31 @@ struct VoiceMessageRoomTimelineContent: View {
onPlayPause: onPlaybackPlayPause,
onSeek: { onPlaybackSeek($0) },
onScrubbing: { onPlaybackScrubbing($0) },
playbackSpeed: context.viewState.voiceMessagePlaybackSpeed,
onPlaybackSpeedChange: onPlaybackSpeedChange)
.fixedSize(horizontal: false, vertical: true)
}
private func onPlaybackPlayPause() {
context.send(viewAction: .handleAudioPlayerAction(.playPause(itemID: timelineItem.id)))
context?.send(viewAction: .handleAudioPlayerAction(.playPause(itemID: timelineItem.id)))
}
private func onPlaybackSpeedChange() {
context.send(viewAction: .handleAudioPlayerAction(.changePlaybackSpeed(itemID: timelineItem.id)))
context?.send(viewAction: .handleAudioPlayerAction(.changePlaybackSpeed(itemID: timelineItem.id)))
}
private func onPlaybackSeek(_ progress: Double) {
context.send(viewAction: .handleAudioPlayerAction(.seek(itemID: timelineItem.id, progress: progress)))
context?.send(viewAction: .handleAudioPlayerAction(.seek(itemID: timelineItem.id, progress: progress)))
}
private func onPlaybackScrubbing(_ dragging: Bool) {
if dragging {
if playerState.playbackState == .playing {
resumePlaybackAfterScrubbing = true
context.send(viewAction: .handleAudioPlayerAction(.playPause(itemID: timelineItem.id)))
context?.send(viewAction: .handleAudioPlayerAction(.playPause(itemID: timelineItem.id)))
}
} else {
if resumePlaybackAfterScrubbing {
context.send(viewAction: .handleAudioPlayerAction(.playPause(itemID: timelineItem.id)))
context?.send(viewAction: .handleAudioPlayerAction(.playPause(itemID: timelineItem.id)))
resumePlaybackAfterScrubbing = false
}
}
@@ -19,7 +19,6 @@ struct VoiceMessageRoomPlaybackView: View {
let onPlayPause: () -> Void
let onSeek: (Double) -> Void
let onScrubbing: (Bool) -> Void
let playbackSpeed: Float
let onPlaybackSpeedChange: () -> Void
var body: some View {
@@ -28,7 +27,8 @@ struct VoiceMessageRoomPlaybackView: View {
size: .medium,
action: onPlayPause)
VStack(spacing: 2) {
PlaybackSpeedButton(speed: playbackSpeed, onTap: onPlaybackSpeedChange)
PlaybackSpeedButton(speed: playerState.playbackSpeed,
onTap: onPlaybackSpeedChange)
Text(timeLabelContent)
.lineLimit(1)
.font(.compound.bodyXSSemibold)
@@ -135,7 +135,6 @@ struct VoiceMessageRoomPlaybackView_Previews: PreviewProvider, TestablePreview {
onPlayPause: { },
onSeek: { value in Task { await playerState.updateState(progress: value) } },
onScrubbing: { _ in },
playbackSpeed: playerState.playbackSpeed,
onPlaybackSpeedChange: { })
.fixedSize(horizontal: false, vertical: true)
}