Fix timeline view time padding

This commit is contained in:
Arkadiusz Fal 2022-08-27 02:43:47 +02:00
parent 1cf37e5a00
commit 5b785cc9c2
3 changed files with 10 additions and 16 deletions

View File

@ -1,7 +1,7 @@
import Foundation
extension Double {
func formattedAsPlaybackTime(allowZero: Bool = false) -> String? {
func formattedAsPlaybackTime(allowZero: Bool = false, forceHours: Bool = false) -> String? {
guard allowZero || !isZero, isFinite else {
return nil
}
@ -9,7 +9,7 @@ extension Double {
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional
formatter.allowedUnits = self >= (60 * 60) ? [.hour, .minute, .second] : [.minute, .second]
formatter.allowedUnits = self >= (60 * 60) || forceHours ? [.hour, .minute, .second] : [.minute, .second]
formatter.zeroFormattingBehavior = [.pad]
return formatter.string(from: self)

View File

@ -9,12 +9,16 @@ final class PlayerTimeModel: ObservableObject {
var player: PlayerModel?
var forceHours: Bool {
duration.seconds >= 60 * 60
}
var currentPlaybackTime: String {
if player?.currentItem.isNil ?? true || duration.seconds.isZero {
return Self.timePlaceholder
}
return currentTime.seconds.formattedAsPlaybackTime(allowZero: true) ?? Self.timePlaceholder
return currentTime.seconds.formattedAsPlaybackTime(allowZero: true, forceHours: forceHours) ?? Self.timePlaceholder
}
var durationPlaybackTime: String {
@ -30,17 +34,7 @@ final class PlayerTimeModel: ObservableObject {
return Self.timePlaceholder
}
return withoutSegmentsDuration.formattedAsPlaybackTime() ?? Self.timePlaceholder
}
var durationAndWithoutSegmentsPlaybackTime: String {
var durationAndWithoutSegmentsPlaybackTime = "\(durationPlaybackTime)"
if withoutSegmentsPlaybackTime != durationPlaybackTime {
durationAndWithoutSegmentsPlaybackTime += " (\(withoutSegmentsPlaybackTime))"
}
return durationAndWithoutSegmentsPlaybackTime
return withoutSegmentsDuration.formattedAsPlaybackTime(forceHours: forceHours) ?? Self.timePlaceholder
}
func reset() {

View File

@ -77,7 +77,7 @@ struct TimelineView: View {
.fixedSize()
}
}
Text((dragging ? projectedValue : current).formattedAsPlaybackTime(allowZero: true) ?? PlayerTimeModel.timePlaceholder)
Text((dragging ? projectedValue : current).formattedAsPlaybackTime(allowZero: true, forceHours: playerTime.forceHours) ?? PlayerTimeModel.timePlaceholder)
.font(.system(size: 11).monospacedDigit())
}
@ -107,7 +107,7 @@ struct TimelineView: View {
.opacity(dragging ? 1 : 0)
.animation(.easeOut, value: thumbTooltipOffset)
HStack(spacing: 4) {
Text((dragging ? projectedValue : nil)?.formattedAsPlaybackTime(allowZero: true) ?? playerTime.currentPlaybackTime)
Text((dragging ? projectedValue : nil)?.formattedAsPlaybackTime(allowZero: true, forceHours: playerTime.forceHours) ?? playerTime.currentPlaybackTime)
.opacity(player.liveStreamInAVPlayer ? 0 : 1)
.frame(minWidth: 35)
#if os(tvOS)