mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-23 01:52:44 +02:00
Add scrubbing
This commit is contained in:
@@ -16,9 +16,13 @@
|
||||
|
||||
import Foundation
|
||||
import Reusable
|
||||
import UIKit
|
||||
import MatrixSDK
|
||||
|
||||
protocol VoiceMessagePlaybackViewDelegate: AnyObject {
|
||||
func voiceMessagePlaybackViewDidRequestPlaybackToggle()
|
||||
func voiceMessagePlaybackViewRequestedFormattedTimestamp(for progress: CGFloat) -> String?
|
||||
func voiceMessagePlaybackViewDidRequestSeek(to progress: CGFloat)
|
||||
func voiceMessagePlaybackViewDidChangeWidth()
|
||||
}
|
||||
|
||||
@@ -40,12 +44,19 @@ class VoiceMessagePlaybackView: UIView, NibLoadable, Themable {
|
||||
|
||||
private var _waveformView: VoiceMessageWaveformView!
|
||||
private var currentTheme: Theme?
|
||||
private var scrubProgress: CGFloat? {
|
||||
didSet {
|
||||
MXLog.debug("change progress \(scrubProgress)")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@IBOutlet private var backgroundView: UIView!
|
||||
@IBOutlet private var recordingIcon: UIView!
|
||||
@IBOutlet private var playButton: UIButton!
|
||||
@IBOutlet private var elapsedTimeLabel: UILabel!
|
||||
@IBOutlet private var waveformContainerView: UIView!
|
||||
@IBOutlet private var panGestureRecognizer: UIPanGestureRecognizer!
|
||||
|
||||
weak var delegate: VoiceMessagePlaybackViewDelegate?
|
||||
|
||||
@@ -94,19 +105,17 @@ class VoiceMessagePlaybackView: UIView, NibLoadable, Themable {
|
||||
}
|
||||
|
||||
if details.loading {
|
||||
elapsedTimeLabel.text = "--:--"
|
||||
_waveformView.progress = 0
|
||||
_waveformView.samples = []
|
||||
_waveformView.alpha = 0.3
|
||||
} else {
|
||||
elapsedTimeLabel.text = details.currentTime
|
||||
_waveformView.progress = details.progress
|
||||
_waveformView.samples = details.samples
|
||||
_waveformView.alpha = 1.0
|
||||
}
|
||||
|
||||
self.details = details
|
||||
|
||||
updateElapsedTime()
|
||||
guard let theme = currentTheme else {
|
||||
return
|
||||
}
|
||||
@@ -120,6 +129,20 @@ class VoiceMessagePlaybackView: UIView, NibLoadable, Themable {
|
||||
elapsedTimeLabel.textColor = theme.colors.tertiaryContent
|
||||
}
|
||||
|
||||
private func updateElapsedTime() {
|
||||
guard let details = details,
|
||||
!details.loading else {
|
||||
elapsedTimeLabel.text = "--:--"
|
||||
return
|
||||
}
|
||||
if let scrubProgress = scrubProgress,
|
||||
let scrubTime = delegate?.voiceMessagePlaybackViewRequestedFormattedTimestamp(for: scrubProgress) {
|
||||
elapsedTimeLabel.text = scrubTime
|
||||
} else {
|
||||
elapsedTimeLabel.text = details.currentTime
|
||||
}
|
||||
}
|
||||
|
||||
func getRequiredNumberOfSamples() -> Int {
|
||||
_waveformView.setNeedsLayout()
|
||||
_waveformView.layoutIfNeeded()
|
||||
@@ -138,4 +161,31 @@ class VoiceMessagePlaybackView: UIView, NibLoadable, Themable {
|
||||
@IBAction private func onPlayButtonTap() {
|
||||
delegate?.voiceMessagePlaybackViewDidRequestPlaybackToggle()
|
||||
}
|
||||
|
||||
@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)
|
||||
}
|
||||
|
||||
@IBAction private func pan(gestureRecognizer: UIPanGestureRecognizer) {
|
||||
switch gestureRecognizer.state {
|
||||
|
||||
case .possible, .cancelled, .failed:
|
||||
scrubProgress = nil
|
||||
case .began, .changed:
|
||||
let x = gestureRecognizer.location(in: waveformContainerView).x.clamped(to: 0...waveformContainerView.bounds.width)
|
||||
scrubProgress = x / waveformContainerView.bounds.width
|
||||
case .ended:
|
||||
let seekPoint = scrubProgress == 1 ? 0 : scrubProgress ?? 0
|
||||
MXLog.debug("progress end seekPoint --->\(seekPoint)")
|
||||
delegate?.voiceMessagePlaybackViewDidRequestSeek(to: seekPoint)
|
||||
scrubProgress = nil
|
||||
@unknown default:
|
||||
break
|
||||
}
|
||||
updateElapsedTime()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user