Use cache for loading player queue and history

This commit is contained in:
Arkadiusz Fal 2022-12-13 00:38:26 +01:00
parent 33a131d2ca
commit 1c746bc8e0
11 changed files with 66 additions and 53 deletions

View File

@ -97,6 +97,8 @@ extension VideosAPI {
return
}
VideosCacheModel.shared.storeVideo(video)
var newItem = item
newItem.id = UUID()
newItem.video = video

View File

@ -39,15 +39,6 @@ extension PlayerModel {
}
.onCompletion { _ in
self.logger.info("LOADED history details: \(watch.videoID)")
if self.historyItemBeingLoaded == watch.videoID {
self.logger.info("setting no history loaded")
self.historyItemBeingLoaded = nil
}
if let watch = self.historyItemsToLoad.popLast() {
self.loadHistoryVideoDetails(watch)
}
}
}
@ -70,10 +61,11 @@ extension PlayerModel {
let watch: Watch!
let duration = self.playerTime.duration.seconds
if results?.isEmpty ?? true {
if seconds < 1 {
return
}
if seconds < 3, duration > 3 { return }
watch = Watch(context: self.backgroundContext)
watch.videoID = id
watch.appName = currentVideo.app.rawValue
@ -82,7 +74,6 @@ extension PlayerModel {
watch = results?.first
}
let duration = self.playerTime.duration.seconds
if duration.isFinite, duration > 0 {
watch.videoDuration = duration
}
@ -98,6 +89,8 @@ extension PlayerModel {
watch.watchedAt = Date()
try? self.backgroundContext.save()
FeedModel.shared.calculateUnwatchedFeed()
}
}

View File

@ -96,11 +96,6 @@ final class PlayerModel: ObservableObject {
@Published var currentItem: PlayerQueueItem! { didSet { handleCurrentItemChange() } }
@Published var videoBeingOpened: Video? { didSet { seek.reset() } }
@Published var historyVideos = [Video]()
@Published var queueItemBeingLoaded: PlayerQueueItem?
@Published var queueItemsToLoad = [PlayerQueueItem]()
@Published var historyItemBeingLoaded: Video.ID?
@Published var historyItemsToLoad = [Watch]()
@Published var preservedTime: CMTime?
@Published var sponsorBlock = SponsorBlockAPI()

View File

@ -165,7 +165,8 @@ extension PlayerModel {
currentItem.playbackTime = time
let playTime = currentItem.shouldRestartPlaying ? CMTime.zero : time
playerAPI(newItem.video).loadDetails(currentItem, failureHandler: { self.videoLoadFailureHandler($0, video: self.currentItem.video) }) { newItem in
guard let video = newItem.video else { return }
playerAPI(video).loadDetails(currentItem, failureHandler: { self.videoLoadFailureHandler($0, video: self.currentItem.video) }) { newItem in
self.playItem(newItem, at: playTime)
}
}
@ -276,41 +277,39 @@ extension PlayerModel {
restoredQueue.append(contentsOf: Defaults[.queue])
queue = restoredQueue.compactMap { $0 }
queue.forEach { loadQueueVideoDetails($0) }
}
func loadQueueVideoDetails(_ item: PlayerQueueItem) {
guard !accounts.current.isNil, !item.hasDetailsLoaded, let video = item.video else { return }
guard !accounts.current.isNil, !item.hasDetailsLoaded else { return }
let videoID = item.video?.videoID ?? item.videoID
if queueItemBeingLoaded == nil {
logger.info("loading queue details: \(videoID)")
queueItemBeingLoaded = item
} else {
logger.info("POSTPONING details load: \(videoID)")
queueItemsToLoad.append(item)
return
}
let video = item.video ?? Video(app: item.app ?? .local, instanceURL: item.instanceURL, videoID: videoID)
playerAPI(video)?.loadDetails(item, completionHandler: { [weak self] newItem in
guard let self else { return }
self.queue.filter { $0.videoID == item.videoID }.forEach { item in
let replaceQueueItem: (PlayerQueueItem) -> Void = { newItem in
self.queue.filter { $0.videoID == videoID }.forEach { item in
if let index = self.queue.firstIndex(of: item) {
self.queue[index] = newItem
}
}
}
if let video = VideosCacheModel.shared.retrieveVideo(video.cacheKey) {
var item = item
item.id = UUID()
item.video = video
replaceQueueItem(item)
return
}
playerAPI(video)?
.loadDetails(item, completionHandler: { [weak self] newItem in
guard let self else { return }
replaceQueueItem(newItem)
self.logger.info("LOADED queue details: \(videoID)")
if self.queueItemBeingLoaded == item {
self.logger.info("setting nothing loaded")
self.queueItemBeingLoaded = nil
}
if let item = self.queueItemsToLoad.popLast() {
self.loadQueueVideoDetails(item)
}
})
}

View File

@ -8,6 +8,8 @@ struct PlayerQueueItem: Hashable, Identifiable, Defaults.Serializable {
var id = UUID()
var video: Video!
var videoID: Video.ID
var app: VideosApp?
var instanceURL: URL?
var playbackTime: CMTime?
var videoDuration: TimeInterval?
@ -15,14 +17,25 @@ struct PlayerQueueItem: Hashable, Identifiable, Defaults.Serializable {
.init(
video,
videoID: watch.videoID,
app: watch.app,
instanceURL: watch.instanceURL,
playbackTime: CMTime.secondsInDefaultTimescale(watch.stoppedAt),
videoDuration: watch.videoDuration
)
}
init(_ video: Video? = nil, videoID: Video.ID? = nil, playbackTime: CMTime? = nil, videoDuration: TimeInterval? = nil) {
init(
_ video: Video? = nil,
videoID: Video.ID? = nil,
app: VideosApp? = nil,
instanceURL: URL? = nil,
playbackTime: CMTime? = nil,
videoDuration: TimeInterval? = nil
) {
self.video = video
self.videoID = videoID ?? video!.videoID
self.app = app
self.instanceURL = instanceURL
self.playbackTime = playbackTime
self.videoDuration = videoDuration
}

View File

@ -33,6 +33,8 @@ struct PlayerQueueItemBridge: Defaults.Bridge {
return [
"localURL": localURL,
"videoID": value.videoID,
"app": (value.app ?? value.video.app)?.rawValue ?? "",
"instanceURL": (value.instanceURL ?? value.video.instanceURL)?.absoluteString ?? "",
"playbackTime": playbackTime,
"videoDuration": videoDuration
]
@ -41,6 +43,8 @@ struct PlayerQueueItemBridge: Defaults.Bridge {
func deserialize(_ object: Serializable?) -> Value? {
guard let object else { return nil }
var app: VideosApp?
var instanceURL: URL?
var playbackTime: CMTime?
var videoDuration: TimeInterval?
@ -57,6 +61,16 @@ struct PlayerQueueItemBridge: Defaults.Bridge {
videoDuration = TimeInterval(duration)
}
if let appString = object["app"],
!appString.isEmpty
{
app = VideosApp(rawValue: appString)
}
if let url = object["instanceURL"]?.url {
instanceURL = url
}
if let localUrlString = object["localURL"],
!localUrlString.isEmpty,
let localURL = URL(string: localUrlString)
@ -71,7 +85,10 @@ struct PlayerQueueItemBridge: Defaults.Bridge {
guard let videoID = object["videoID"] else { return nil }
return PlayerQueueItem(
.init(app: app ?? .local, instanceURL: instanceURL, videoID: videoID),
videoID: videoID,
app: app,
instanceURL: instanceURL,
playbackTime: playbackTime,
videoDuration: videoDuration
)

View File

@ -197,7 +197,7 @@ struct Video: Identifiable, Equatable, Hashable {
}
var isLocal: Bool {
!VideoID.isValid(videoID)
!VideoID.isValid(videoID) && videoID != Self.fixtureID
}
var displayTitle: String {

View File

@ -71,7 +71,10 @@ extension Watch {
}
var finished: Bool {
progress >= Double(watchedThreshold)
guard videoDuration.isFinite, !videoDuration.isZero else {
return true
}
return progress >= Double(watchedThreshold)
}
var watchedAtString: String? {

View File

@ -1,8 +1,6 @@
import SwiftUI
struct HistoryView: View {
static let detailsPreloadLimit = 50
var limit = 10
@FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)])
@ -33,7 +31,6 @@ struct HistoryView: View {
}
.onAppear {
visibleWatches
.prefix(Self.detailsPreloadLimit)
.forEach(player.loadHistoryVideoDetails)
}
#if os(tvOS)

View File

@ -86,9 +86,6 @@ struct PlayerQueueView: View {
ForEach(player.queue) { item in
PlayerQueueRow(item: item, fullScreen: $fullScreen)
.onAppear {
player.loadQueueVideoDetails(item)
}
.contextMenu {
removeButton(item)
removeAllButton()

View File

@ -66,9 +66,6 @@ struct NowPlayingView: View {
} label: {
VideoBanner(video: item.video)
}
.onAppear {
player.loadQueueVideoDetails(item)
}
.contextMenu {
Button("Remove", role: .destructive) {
player.remove(item)