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? private var statusObservation: NSKeyValueObservation?
var autoPlayItems = false
init(accounts: AccountsModel? = nil, instances: InstancesModel? = nil) { init(accounts: AccountsModel? = nil, instances: InstancesModel? = nil) {
self.accounts = accounts ?? AccountsModel() self.accounts = accounts ?? AccountsModel()
self.instances = instances ?? InstancesModel() self.instances = instances ?? InstancesModel()
addItemDidPlayToEndTimeObserver() addItemDidPlayToEndTimeObserver()
addFrequentTimeObserver() addFrequentTimeObserver()
addInfrequentTimeObserver() addInfrequentTimeObserver()
addPlayerTimeControlStatusObserver() 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() { func presentPlayer() {
presentingPlayer = true presentingPlayer = true
} }
@ -193,15 +160,7 @@ final class PlayerModel: ObservableObject {
#endif #endif
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
guard let self = self else { self?.play()
return
}
guard self.autoPlayItems else {
return
}
self.play()
} }
} }

View File

@ -1,4 +1,5 @@
import AVFoundation import AVFoundation
import Defaults
import Foundation import Foundation
import Siesta import Siesta
@ -105,16 +106,7 @@ extension PlayerModel {
} }
func isAutoplaying(_ item: AVPlayerItem) -> Bool { func isAutoplaying(_ item: AVPlayerItem) -> Bool {
guard player.currentItem == item else { player.currentItem == item
return false
}
if !autoPlayItems {
autoPlayItems = true
return false
}
return true
} }
@discardableResult func enqueueVideo( @discardableResult func enqueueVideo(
@ -141,13 +133,17 @@ extension PlayerModel {
func addCurrentItemToHistory() { func addCurrentItemToHistory() {
if let item = currentItem { if let item = currentItem {
addItemToHistory(item)
}
}
func addItemToHistory(_ item: PlayerQueueItem) {
if let index = history.firstIndex(where: { $0.video.videoID == item.video?.videoID }) { if let index = history.firstIndex(where: { $0.video.videoID == item.video?.videoID }) {
history.remove(at: index) history.remove(at: index)
} }
history.insert(currentItem, at: 0) history.insert(currentItem, at: 0)
} }
}
func playHistory(_ item: PlayerQueueItem) { func playHistory(_ item: PlayerQueueItem) {
var time = item.playbackTime var time = item.playbackTime
@ -180,4 +176,30 @@ extension PlayerModel {
func removeHistoryItems() { func removeHistoryItems() {
history.removeAll() 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
}
} }

View File

@ -1078,19 +1078,19 @@
children = ( children = (
3743B86627216A1E00261544 /* Accounts */, 3743B86627216A1E00261544 /* Accounts */,
3743B864272169E200261544 /* Applications */, 3743B864272169E200261544 /* Applications */,
3743B86527216A0600261544 /* Player */,
37FB283F2721B20800A57617 /* Search */,
374C0539272436DA009BDDBE /* SponsorBlock */,
37AAF28F26740715007FC770 /* Channel.swift */, 37AAF28F26740715007FC770 /* Channel.swift */,
37C3A24427235DA70087A57A /* ChannelPlaylist.swift */, 37C3A24427235DA70087A57A /* ChannelPlaylist.swift */,
37FB28402721B22200A57617 /* ContentItem.swift */, 37FB28402721B22200A57617 /* ContentItem.swift */,
37141672267A8E10006CA35D /* Country.swift */, 37141672267A8E10006CA35D /* Country.swift */,
371F2F19269B43D300E4A7AB /* NavigationModel.swift */, 371F2F19269B43D300E4A7AB /* NavigationModel.swift */,
3743B86527216A0600261544 /* Player */,
376578882685471400D4EA09 /* Playlist.swift */, 376578882685471400D4EA09 /* Playlist.swift */,
37BA794226DBA973002A0235 /* PlaylistsModel.swift */, 37BA794226DBA973002A0235 /* PlaylistsModel.swift */,
37C194C626F6A9C8005D3B96 /* RecentsModel.swift */, 37C194C626F6A9C8005D3B96 /* RecentsModel.swift */,
37FB283F2721B20800A57617 /* Search */,
37EAD86E267B9ED100D9E01B /* Segment.swift */, 37EAD86E267B9ED100D9E01B /* Segment.swift */,
37CEE4BC2677B670005A1EFE /* SingleAssetStream.swift */, 37CEE4BC2677B670005A1EFE /* SingleAssetStream.swift */,
374C0539272436DA009BDDBE /* SponsorBlock */,
3797758A2689345500DD52A8 /* Store.swift */, 3797758A2689345500DD52A8 /* Store.swift */,
37CEE4C02677B697005A1EFE /* Stream.swift */, 37CEE4C02677B697005A1EFE /* Stream.swift */,
37E64DD026D597EB00C71877 /* SubscriptionsModel.swift */, 37E64DD026D597EB00C71877 /* SubscriptionsModel.swift */,

View File

@ -144,7 +144,7 @@ struct ContentView: View {
accounts.api.video(id).load().onSuccess { response in accounts.api.video(id).load().onSuccess { response in
if let video: Video = response.typedContent() { if let video: Video = response.typedContent() {
self.player.autoPlayItems = true player.addCurrentItemToHistory()
self.player.playNow(video, at: parser.time) self.player.playNow(video, at: parser.time)
self.player.presentPlayer() self.player.presentPlayer()
} }