From 6f91eedf4cdf32c726607dac72cb2b5493c5b9a3 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Wed, 7 Jun 2023 22:25:10 +0200 Subject: [PATCH] Update watch time on close item --- Model/HistoryModel.swift | 6 +++--- Model/Player/Backends/AVPlayerBackend.swift | 4 ++-- Model/Player/Backends/MPVBackend.swift | 2 +- Model/Player/PlayerModel.swift | 3 ++- Model/Player/PlayerQueue.swift | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Model/HistoryModel.swift b/Model/HistoryModel.swift index 8e66b313..98a5d7fe 100644 --- a/Model/HistoryModel.swift +++ b/Model/HistoryModel.swift @@ -46,11 +46,11 @@ extension PlayerModel { } } - func updateWatch(finished: Bool = false) { + func updateWatch(finished: Bool = false, time: CMTime? = nil) { guard let currentVideo, saveHistory else { return } let id = currentVideo.videoID - let time = backend.currentTime + let time = time ?? backend.currentTime let seconds = time?.seconds ?? 0 let duration = playerTime.duration.seconds if seconds < 3 { @@ -63,7 +63,7 @@ extension PlayerModel { let results = try? backgroundContext.fetch(watchFetchRequest) backgroundContext.perform { [weak self] in - guard let self, finished || self.backend.isPlaying else { + guard let self, finished || time != nil || self.backend.isPlaying else { return } diff --git a/Model/Player/Backends/AVPlayerBackend.swift b/Model/Player/Backends/AVPlayerBackend.swift index 0ee538f2..291f77ea 100644 --- a/Model/Player/Backends/AVPlayerBackend.swift +++ b/Model/Player/Backends/AVPlayerBackend.swift @@ -612,7 +612,7 @@ final class AVPlayerBackend: PlayerBackend { } self.timeObserverThrottle.execute { - self.model.updateWatch() + self.model.updateWatch(time: self.currentTime) } } } @@ -656,7 +656,7 @@ final class AVPlayerBackend: PlayerBackend { #endif self.timeObserverThrottle.execute { - self.model.updateWatch() + self.model.updateWatch(time: self.currentTime) } } } diff --git a/Model/Player/Backends/MPVBackend.swift b/Model/Player/Backends/MPVBackend.swift index 673c363e..e984ca1e 100644 --- a/Model/Player/Backends/MPVBackend.swift +++ b/Model/Player/Backends/MPVBackend.swift @@ -425,7 +425,7 @@ final class MPVBackend: PlayerBackend { } timeObserverThrottle.execute { - self.model.updateWatch() + self.model.updateWatch(time: self.currentTime) } } diff --git a/Model/Player/PlayerModel.swift b/Model/Player/PlayerModel.swift index b225f772..628c9f50 100644 --- a/Model/Player/PlayerModel.swift +++ b/Model/Player/PlayerModel.swift @@ -624,13 +624,14 @@ final class PlayerModel: ObservableObject { closing = true controls.presentingControls = false + self.prepareCurrentItemForHistory(finished: finished) + self.hide() Delay.by(0.8) { [weak self] in guard let self else { return } self.closePiP() - self.prepareCurrentItemForHistory(finished: finished) withAnimation { self.currentItem = nil } diff --git a/Model/Player/PlayerQueue.swift b/Model/Player/PlayerQueue.swift index d820d361..968c5363 100644 --- a/Model/Player/PlayerQueue.swift +++ b/Model/Player/PlayerQueue.swift @@ -257,7 +257,7 @@ extension PlayerModel { if let video = currentVideo, !historyVideos.contains(where: { $0 == video }) { historyVideos.append(video) } - updateWatch(finished: finished) + updateWatch(finished: finished, time: backend.currentTime) } if let video = currentItem.video,