From 5b785cc9c2450705dc82c6aebd7ea8bd165d2c58 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Sat, 27 Aug 2022 02:43:47 +0200 Subject: [PATCH] Fix timeline view time padding --- Extensions/Double+Format.swift | 4 ++-- Model/Player/PlayerTimeModel.swift | 18 ++++++------------ Shared/Player/Controls/TimelineView.swift | 4 ++-- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/Extensions/Double+Format.swift b/Extensions/Double+Format.swift index c3ebc79f..488c27cb 100644 --- a/Extensions/Double+Format.swift +++ b/Extensions/Double+Format.swift @@ -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) diff --git a/Model/Player/PlayerTimeModel.swift b/Model/Player/PlayerTimeModel.swift index 39dafd30..b4305090 100644 --- a/Model/Player/PlayerTimeModel.swift +++ b/Model/Player/PlayerTimeModel.swift @@ -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() { diff --git a/Shared/Player/Controls/TimelineView.swift b/Shared/Player/Controls/TimelineView.swift index bd1818a9..f80a4344 100644 --- a/Shared/Player/Controls/TimelineView.swift +++ b/Shared/Player/Controls/TimelineView.swift @@ -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)