yattee/Model/Player/PlayerQueue.swift
2021-10-24 11:16:04 +02:00

170 lines
4.3 KiB
Swift

import AVFoundation
import Foundation
import Siesta
extension PlayerModel {
var currentVideo: Video? {
currentItem?.video
}
func playAll(_ videos: [Video]) {
let first = videos.first
videos.forEach { video in
enqueueVideo(video) { _, item in
if item.video == first {
self.advanceToItem(item)
}
}
}
}
func playNext(_ video: Video) {
enqueueVideo(video, prepending: true) { _, item in
if self.currentItem.isNil {
self.advanceToItem(item)
}
}
}
func playNow(_ video: Video, at time: TimeInterval? = nil) {
addCurrentItemToHistory()
enqueueVideo(video, prepending: true) { _, item in
self.advanceToItem(item, at: time)
}
}
func playItem(_ item: PlayerQueueItem, video: Video? = nil, at time: TimeInterval? = nil) {
currentItem = item
if !time.isNil {
currentItem.playbackTime = CMTime(seconds: time!, preferredTimescale: 1_000_000)
} else if currentItem.playbackTime.isNil {
currentItem.playbackTime = .zero
}
if video != nil {
currentItem.video = video!
}
playVideo(currentVideo!, time: currentItem.playbackTime)
}
func advanceToNextItem() {
addCurrentItemToHistory()
if let nextItem = queue.first {
advanceToItem(nextItem)
}
}
func advanceToItem(_ newItem: PlayerQueueItem, at time: TimeInterval? = nil) {
addCurrentItemToHistory()
let item = remove(newItem)!
loadDetails(newItem.video) { video in
self.playItem(item, video: video, at: time)
}
}
@discardableResult func remove(_ item: PlayerQueueItem) -> PlayerQueueItem? {
if let index = queue.firstIndex(where: { $0 == item }) {
return queue.remove(at: index)
}
return nil
}
func resetQueue() {
DispatchQueue.main.async {
self.currentItem = nil
self.stream = nil
self.removeQueueItems()
self.timeObserver = nil
}
player.replaceCurrentItem(with: nil)
}
func isAutoplaying(_ item: AVPlayerItem) -> Bool {
player.currentItem == item
}
@discardableResult func enqueueVideo(
_ video: Video,
play: Bool = false,
atTime: CMTime? = nil,
prepending: Bool = false,
videoDetailsLoadHandler: @escaping (Video, PlayerQueueItem) -> Void = { _, _ in }
) -> PlayerQueueItem? {
let item = PlayerQueueItem(video, playbackTime: atTime)
queue.insert(item, at: prepending ? 0 : queue.endIndex)
loadDetails(video) { video in
videoDetailsLoadHandler(video, item)
if play {
self.playItem(item, video: video)
}
}
return item
}
private func loadDetails(_ video: Video?, onSuccess: @escaping (Video) -> Void) {
guard video != nil else {
return
}
accounts.api.video(video!.videoID).load().onSuccess { response in
if let video: Video = response.typedContent() {
onSuccess(video)
}
}
}
func addCurrentItemToHistory() {
if let item = currentItem {
if let index = history.firstIndex(where: { $0.video.videoID == item.video.videoID }) {
history.remove(at: index)
}
history.insert(item, at: 0)
}
}
func playHistory(_ item: PlayerQueueItem) {
var time = item.playbackTime
if item.shouldRestartPlaying {
time = .zero
}
let newItem = enqueueVideo(item.video, atTime: time, prepending: true)
advanceToItem(newItem!)
if let historyItemIndex = history.firstIndex(of: item) {
history.remove(at: historyItemIndex)
}
}
@discardableResult func removeHistory(_ item: PlayerQueueItem) -> PlayerQueueItem? {
if let index = history.firstIndex(where: { $0 == item }) {
return history.remove(at: index)
}
return nil
}
func removeQueueItems() {
queue.removeAll()
}
func removeHistoryItems() {
history.removeAll()
}
}