yattee/Model/Player/PlayerQueue.swift

170 lines
4.3 KiB
Swift
Raw Normal View History

import AVFoundation
import Foundation
2021-10-16 22:48:58 +00:00
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
2021-10-16 22:48:58 +00:00
if self.currentItem.isNil {
self.advanceToItem(item)
}
}
}
2021-10-24 09:16:04 +00:00
func playNow(_ video: Video, at time: TimeInterval? = nil) {
addCurrentItemToHistory()
enqueueVideo(video, prepending: true) { _, item in
2021-10-24 09:16:04 +00:00
self.advanceToItem(item, at: time)
}
}
2021-10-24 09:16:04 +00:00
func playItem(_ item: PlayerQueueItem, video: Video? = nil, at time: TimeInterval? = nil) {
currentItem = item
2021-10-24 09:16:04 +00:00
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!
}
2021-10-24 09:16:04 +00:00
playVideo(currentVideo!, time: currentItem.playbackTime)
}
func advanceToNextItem() {
addCurrentItemToHistory()
if let nextItem = queue.first {
advanceToItem(nextItem)
}
}
2021-10-24 09:16:04 +00:00
func advanceToItem(_ newItem: PlayerQueueItem, at time: TimeInterval? = nil) {
addCurrentItemToHistory()
let item = remove(newItem)!
loadDetails(newItem.video) { video in
2021-10-24 09:16:04 +00:00
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
}
2021-10-20 22:21:50 +00:00
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)
}
}
2021-10-13 22:05:19 +00:00
func playHistory(_ item: PlayerQueueItem) {
var time = item.playbackTime
if item.shouldRestartPlaying {
time = .zero
}
let newItem = enqueueVideo(item.video, atTime: time, prepending: true)
2021-10-13 22:05:19 +00:00
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()
}
}