yattee/Model/Player/PlayerTimeModel.swift

45 lines
1.2 KiB
Swift
Raw Normal View History

import CoreMedia
import Foundation
final class PlayerTimeModel: ObservableObject {
static let timePlaceholder = "--:--"
@Published var currentTime = CMTime.zero
@Published var duration = CMTime.zero
var player: PlayerModel?
2022-08-27 00:43:47 +00:00
var forceHours: Bool {
duration.seconds >= 60 * 60
}
var currentPlaybackTime: String {
if player?.currentItem.isNil ?? true || duration.seconds.isZero {
return Self.timePlaceholder
}
2022-08-27 00:43:47 +00:00
return currentTime.seconds.formattedAsPlaybackTime(allowZero: true, forceHours: forceHours) ?? Self.timePlaceholder
}
var durationPlaybackTime: String {
if player?.currentItem.isNil ?? true {
return Self.timePlaceholder
}
return duration.seconds.formattedAsPlaybackTime() ?? Self.timePlaceholder
}
var withoutSegmentsPlaybackTime: String {
guard let withoutSegmentsDuration = player?.playerItemDurationWithoutSponsorSegments?.seconds else {
return Self.timePlaceholder
}
2022-08-27 00:43:47 +00:00
return withoutSegmentsDuration.formattedAsPlaybackTime(forceHours: forceHours) ?? Self.timePlaceholder
}
func reset() {
currentTime = .zero
duration = .zero
}
}