#4090 - Various tweaks and fixes following code review. Switched back to DateFormatters for formatting durations, sanitising audio player durations and current times.

This commit is contained in:
Stefan Ceriu
2021-07-19 15:40:17 +03:00
parent f19511d372
commit a7d053e97f
9 changed files with 70 additions and 127 deletions
@@ -26,6 +26,10 @@ enum VoiceMessagePlaybackControllerState {
class VoiceMessagePlaybackController: VoiceMessageAudioPlayerDelegate, VoiceMessagePlaybackViewDelegate {
private enum Constants {
static let elapsedTimeFormat = "m:ss"
}
private let mediaServiceProvider: VoiceMessageMediaServiceProvider
private let cacheManager: VoiceMessageAttachmentCacheManager
@@ -43,6 +47,13 @@ class VoiceMessagePlaybackController: VoiceMessageAudioPlayerDelegate, VoiceMess
}
}
private static let timeFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = Constants.elapsedTimeFormat
return dateFormatter
}()
let playbackView: VoiceMessagePlaybackView
init(mediaServiceProvider: VoiceMessageMediaServiceProvider,
@@ -134,11 +145,11 @@ class VoiceMessagePlaybackController: VoiceMessageAudioPlayerDelegate, VoiceMess
switch state {
case .stopped:
details.currentTime = durationStringFromTimeInterval(self.duration)
details.currentTime = VoiceMessagePlaybackController.timeFormatter.string(from: Date(timeIntervalSinceReferenceDate: self.duration))
details.progress = 0.0
default:
if let audioPlayer = audioPlayer {
details.currentTime = durationStringFromTimeInterval(audioPlayer.currentTime)
details.currentTime = VoiceMessagePlaybackController.timeFormatter.string(from: Date(timeIntervalSinceReferenceDate: audioPlayer.currentTime))
details.progress = (audioPlayer.duration > 0.0 ? audioPlayer.currentTime / audioPlayer.duration : 0.0)
}
}
@@ -199,18 +210,4 @@ class VoiceMessagePlaybackController: VoiceMessageAudioPlayerDelegate, VoiceMess
@objc private func updateTheme() {
playbackView.update(theme: ThemeService.shared().theme)
}
private func durationStringFromTimeInterval(_ interval: TimeInterval) -> String {
guard interval.isFinite else {
return ""
}
var timeInterval = abs(interval)
let hours = trunc(timeInterval / 3600.0)
timeInterval -= hours * 3600.0
let minutes = trunc(timeInterval / 60.0)
timeInterval -= minutes * 60.0
return String(format: "%01.0f:%02.0f", minutes, timeInterval)
}
}