From 442708524e934fa46e07d1ed4ef0a6eed016ef58 Mon Sep 17 00:00:00 2001 From: David Langley Date: Wed, 22 Sep 2021 16:26:20 +0100 Subject: [PATCH] Simplify scrubbing logic. --- .../VoiceMessageAudioPlayer.swift | 1 - .../VoiceMessageController.swift | 20 +++++------ .../VoiceMessagePlaybackController.swift | 33 ++++++------------- .../VoiceMessagePlaybackView.swift | 9 +++-- 4 files changed, 23 insertions(+), 40 deletions(-) diff --git a/Riot/Modules/Room/VoiceMessages/VoiceMessageAudioPlayer.swift b/Riot/Modules/Room/VoiceMessages/VoiceMessageAudioPlayer.swift index cb2cbc19d..ebe038c6d 100644 --- a/Riot/Modules/Room/VoiceMessages/VoiceMessageAudioPlayer.swift +++ b/Riot/Modules/Room/VoiceMessages/VoiceMessageAudioPlayer.swift @@ -122,7 +122,6 @@ class VoiceMessageAudioPlayer: NSObject { } func seekToTime(_ time: TimeInterval, completionHandler:@escaping (Bool) -> Void = { _ in }) { - isStopped = false audioPlayer?.seek(to: CMTime(seconds: time, preferredTimescale: 60000), completionHandler: completionHandler) } diff --git a/Riot/Modules/Room/VoiceMessages/VoiceMessageController.swift b/Riot/Modules/Room/VoiceMessages/VoiceMessageController.swift index 5f326f073..c13f29a92 100644 --- a/Riot/Modules/Room/VoiceMessages/VoiceMessageController.swift +++ b/Riot/Modules/Room/VoiceMessages/VoiceMessageController.swift @@ -163,11 +163,6 @@ public class VoiceMessageController: NSObject, VoiceMessageToolbarViewDelegate, return } - guard progress > 0 else { - audioPlayer.stop() - return - } - if audioPlayer.url == nil { audioPlayer.loadContentFromURL(temporaryFileURL) } @@ -220,8 +215,9 @@ public class VoiceMessageController: NSObject, VoiceMessageToolbarViewDelegate, } func audioPlayerDidFinishPlaying(_ audioPlayer: VoiceMessageAudioPlayer) { - audioPlayer.seekToTime(0.0) - updateUI() + audioPlayer.seekToTime(0.0) { [weak self] _ in + self?.updateUI() + } } func audioPlayer(_ audioPlayer: VoiceMessageAudioPlayer, didFailWithError: Error) { @@ -428,12 +424,14 @@ public class VoiceMessageController: NSObject, VoiceMessageToolbarViewDelegate, var details = VoiceMessageToolbarViewDetails() details.state = (audioRecorder?.isRecording ?? false ? (isInLockedMode ? .lockedModeRecord : .record) : (isInLockedMode ? .lockedModePlayback : .idle)) - // Show the current time if the player is paused or playing but not when stopped. - details.elapsedTime = VoiceMessageController.elapsedTimeFormatter.string(from: Date(timeIntervalSinceReferenceDate: (!audioPlayer.isStopped ? audioPlayer.currentTime : audioPlayer.duration))) + // Show the current time if the player is paused, show duration when at 0. + let currentTime = audioPlayer.currentTime + let duration = recordDuration ?? 0 + let displayTime = currentTime > 0 ? currentTime : duration + details.elapsedTime = VoiceMessageController.elapsedTimeFormatter.string(from: Date(timeIntervalSinceReferenceDate: displayTime)) + details.progress = duration > 0 ? currentTime / duration : 0 details.audioSamples = audioSamples details.isPlaying = audioPlayer.isPlaying - // Set progress if the player is paused or playing but not when stopped. - details.progress = (!audioPlayer.isStopped ? (audioPlayer.duration > 0.0 ? audioPlayer.currentTime / audioPlayer.duration : 0.0) : 0.0) _voiceMessageToolbarView.configureWithDetails(details) } diff --git a/Riot/Modules/Room/VoiceMessages/VoiceMessagePlaybackController.swift b/Riot/Modules/Room/VoiceMessages/VoiceMessagePlaybackController.swift index d16f3978a..36a894adf 100644 --- a/Riot/Modules/Room/VoiceMessages/VoiceMessagePlaybackController.swift +++ b/Riot/Modules/Room/VoiceMessages/VoiceMessagePlaybackController.swift @@ -102,11 +102,6 @@ class VoiceMessagePlaybackController: VoiceMessageAudioPlayerDelegate, VoiceMess return } - guard progress > 0 else { - audioPlayer.stop() - return - } - if audioPlayer.url == nil, let url = urlToLoad { audioPlayer.loadContentFromURL(url, displayName: attachment?.originalFileName) @@ -114,9 +109,6 @@ class VoiceMessagePlaybackController: VoiceMessageAudioPlayerDelegate, VoiceMess audioPlayer.seekToTime(self.duration * Double(progress)) { [weak self] _ in guard let self = self else { return } - if self.state == .stopped { - self.state = .paused - } self.updateUI() } } @@ -149,8 +141,10 @@ class VoiceMessagePlaybackController: VoiceMessageAudioPlayerDelegate, VoiceMess } func audioPlayerDidFinishPlaying(_ audioPlayer: VoiceMessageAudioPlayer) { - audioPlayer.seekToTime(0.0) - state = .stopped + audioPlayer.seekToTime(0.0) { [weak self] _ in + guard let self = self else { return } + self.state = .stopped + } } // MARK: - Private @@ -165,20 +159,13 @@ class VoiceMessagePlaybackController: VoiceMessageAudioPlayerDelegate, VoiceMess details.playbackEnabled = (state != .error) details.playing = (state == .playing) details.samples = samples - - switch state { - case .stopped: - details.currentTime = VoiceMessagePlaybackController.timeFormatter.string(from: Date(timeIntervalSinceReferenceDate: self.duration)) - details.progress = 0.0 - default: - if let audioPlayer = audioPlayer { - details.currentTime = VoiceMessagePlaybackController.timeFormatter.string(from: Date(timeIntervalSinceReferenceDate: audioPlayer.currentTime)) - details.progress = (audioPlayer.duration > 0.0 ? audioPlayer.currentTime / audioPlayer.duration : 0.0) - } - } - + // Show the current time if the player is paused, show duration when at 0. + let duration = self.duration + let currentTime = audioPlayer?.currentTime ?? 0 + let displayTime = currentTime > 0 ? currentTime : duration + details.currentTime = VoiceMessagePlaybackController.timeFormatter.string(from: Date(timeIntervalSinceReferenceDate: displayTime)) + details.progress = duration > 0 ? currentTime / duration : 0 details.loading = self.loading - playbackView.configureWithDetails(details) } diff --git a/Riot/Modules/Room/VoiceMessages/VoiceMessagePlaybackView.swift b/Riot/Modules/Room/VoiceMessages/VoiceMessagePlaybackView.swift index d767a34bb..77638d38b 100644 --- a/Riot/Modules/Room/VoiceMessages/VoiceMessagePlaybackView.swift +++ b/Riot/Modules/Room/VoiceMessages/VoiceMessagePlaybackView.swift @@ -146,17 +146,16 @@ class VoiceMessagePlaybackView: UIView, NibLoadable, Themable { @IBAction private func tap(gestureRecognizer: UITapGestureRecognizer) { let x = gestureRecognizer.location(in: waveformContainerView).x.clamped(to: 0...waveformContainerView.bounds.width) let progress = x / waveformContainerView.bounds.width - let seekPoint = progress == 1 ? 0 : progress - delegate?.voiceMessagePlaybackViewDidRequestSeek(to: seekPoint) + delegate?.voiceMessagePlaybackViewDidRequestSeek(to: progress) } @IBAction private func pan(gestureRecognizer: UIPanGestureRecognizer) { switch gestureRecognizer.state { case .began, .changed: let x = gestureRecognizer.location(in: waveformContainerView).x.clamped(to: 0...waveformContainerView.bounds.width) - scrubProgress = x / waveformContainerView.bounds.width - let seekPoint = scrubProgress == 1 ? 0 : scrubProgress ?? 0 - delegate?.voiceMessagePlaybackViewDidRequestSeek(to: seekPoint) + let progress = x / waveformContainerView.bounds.width + scrubProgress = progress + delegate?.voiceMessagePlaybackViewDidRequestSeek(to: progress) default: scrubProgress = nil }