Don't autoplay last video at start

This commit is contained in:
Arkadiusz Fal
2021-10-25 10:25:41 +02:00
parent 3a092fc411
commit 47ad6a4410
4 changed files with 43 additions and 62 deletions

View File

@@ -50,49 +50,16 @@ final class PlayerModel: ObservableObject {
private var statusObservation: NSKeyValueObservation?
var autoPlayItems = false
init(accounts: AccountsModel? = nil, instances: InstancesModel? = nil) {
self.accounts = accounts ?? AccountsModel()
self.instances = instances ?? InstancesModel()
addItemDidPlayToEndTimeObserver()
addFrequentTimeObserver()
addInfrequentTimeObserver()
addPlayerTimeControlStatusObserver()
}
func loadHistoryDetails() {
guard !accounts.current.isNil else {
return
}
queue = Defaults[.queue]
queue.forEach { item in
accounts.api.loadDetails(item) { newItem in
if let index = self.queue.firstIndex(where: { $0.id == item.id }) {
self.queue[index] = newItem
}
}
}
history = Defaults[.history]
history.forEach { item in
accounts.api.loadDetails(item) { newItem in
if let index = self.history.firstIndex(where: { $0.id == item.id }) {
self.history[index] = newItem
}
}
}
if let item = Defaults[.lastPlayed] {
accounts.api.loadDetails(item) { [weak self] newItem in
self?.playNow(newItem.video, at: newItem.playbackTime?.seconds)
}
} else {
autoPlayItems = true
}
}
func presentPlayer() {
presentingPlayer = true
}
@@ -193,15 +160,7 @@ final class PlayerModel: ObservableObject {
#endif
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
guard let self = self else {
return
}
guard self.autoPlayItems else {
return
}
self.play()
self?.play()
}
}

View File

@@ -1,4 +1,5 @@
import AVFoundation
import Defaults
import Foundation
import Siesta
@@ -105,16 +106,7 @@ extension PlayerModel {
}
func isAutoplaying(_ item: AVPlayerItem) -> Bool {
guard player.currentItem == item else {
return false
}
if !autoPlayItems {
autoPlayItems = true
return false
}
return true
player.currentItem == item
}
@discardableResult func enqueueVideo(
@@ -141,14 +133,18 @@ extension PlayerModel {
func addCurrentItemToHistory() {
if let item = currentItem {
if let index = history.firstIndex(where: { $0.video.videoID == item.video?.videoID }) {
history.remove(at: index)
}
history.insert(currentItem, at: 0)
addItemToHistory(item)
}
}
func addItemToHistory(_ item: PlayerQueueItem) {
if let index = history.firstIndex(where: { $0.video.videoID == item.video?.videoID }) {
history.remove(at: index)
}
history.insert(currentItem, at: 0)
}
func playHistory(_ item: PlayerQueueItem) {
var time = item.playbackTime
@@ -180,4 +176,30 @@ extension PlayerModel {
func removeHistoryItems() {
history.removeAll()
}
func loadHistoryDetails() {
guard !accounts.current.isNil else {
return
}
queue = Defaults[.queue]
queue.forEach { item in
accounts.api.loadDetails(item) { newItem in
if let index = self.queue.firstIndex(where: { $0.id == item.id }) {
self.queue[index] = newItem
}
}
}
history = [Defaults[.lastPlayed]].compactMap { $0 } + Defaults[.history]
history.forEach { item in
accounts.api.loadDetails(item) { newItem in
if let index = self.history.firstIndex(where: { $0.id == item.id }) {
self.history[index] = newItem
}
}
}
Defaults[.lastPlayed] = nil
}
}