1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 13:50:32 +05:30
yattee/Model/Player/PlayerControlsModel.swift

121 lines
3.1 KiB
Swift
Raw Normal View History

2022-02-17 01:53:11 +05:30
import CoreMedia
import Foundation
import SwiftUI
final class PlayerControlsModel: ObservableObject {
2022-05-22 02:28:11 +05:30
@Published var isLoadingVideo = false
2022-02-17 01:53:11 +05:30
@Published var isPlaying = true
@Published var currentTime = CMTime.zero
@Published var duration = CMTime.zero
@Published var presentingControls = false { didSet { handlePresentationChange() } }
@Published var timer: Timer?
@Published var playingFullscreen = false
2022-03-28 00:54:32 +05:30
private var throttle = Throttle(interval: 1)
2022-02-17 01:53:11 +05:30
var player: PlayerModel!
var playbackTime: String {
guard let current = currentTime.seconds.formattedAsPlaybackTime(),
let duration = duration.seconds.formattedAsPlaybackTime()
else {
return "--:-- / --:--"
}
var withoutSegments = ""
if let withoutSegmentsDuration = playerItemDurationWithoutSponsorSegments,
self.duration.seconds != withoutSegmentsDuration
{
withoutSegments = " (\(withoutSegmentsDuration.formattedAsPlaybackTime() ?? "--:--"))"
}
return "\(current) / \(duration)\(withoutSegments)"
}
var playerItemDurationWithoutSponsorSegments: Double? {
guard let duration = player.playerItemDurationWithoutSponsorSegments else {
return nil
}
return duration.seconds
}
func handlePresentationChange() {
if presentingControls {
DispatchQueue.main.async { [weak self] in
self?.player.backend.startControlsUpdates()
self?.resetTimer()
}
} else {
player.backend.stopControlsUpdates()
timer?.invalidate()
timer = nil
}
}
func show() {
2022-03-28 00:54:32 +05:30
guard !(player?.currentItem.isNil ?? true) else {
return
}
guard !presentingControls else {
return
}
2022-02-22 02:27:12 +05:30
2022-02-17 01:53:11 +05:30
withAnimation(PlayerControls.animation) {
presentingControls = true
}
}
func hide() {
2022-03-28 00:54:32 +05:30
player?.backend.stopControlsUpdates()
guard !(player?.currentItem.isNil ?? true) else {
return
}
guard presentingControls else {
return
}
2022-02-17 01:53:11 +05:30
withAnimation(PlayerControls.animation) {
presentingControls = false
}
}
func toggle() {
2022-02-22 02:27:12 +05:30
withAnimation(PlayerControls.animation) {
2022-02-17 01:53:11 +05:30
presentingControls.toggle()
}
}
func reset() {
currentTime = .zero
duration = .zero
}
func resetTimer() {
2022-03-27 17:12:20 +05:30
if !presentingControls {
show()
}
2022-02-17 01:53:11 +05:30
removeTimer()
timer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: false) { _ in
withAnimation(PlayerControls.animation) { [weak self] in
self?.presentingControls = false
self?.player.backend.stopControlsUpdates()
}
}
}
func removeTimer() {
timer?.invalidate()
timer = nil
}
2022-03-28 00:54:32 +05:30
func update() {
throttle.execute { [weak self] in
self?.player?.backend.updateControls()
}
}
2022-02-17 01:53:11 +05:30
}