From 586cea7d441d1e86731adc29f18803c51a932aae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20F=C3=B6rster?= Date: Mon, 4 Dec 2023 00:07:39 +0100 Subject: [PATCH] subscribe chapters to currentTime notification --- Model/Player/Backends/MPVBackend.swift | 10 ++++++++++ Shared/Player/Video Details/ChapterView.swift | 15 ++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Model/Player/Backends/MPVBackend.swift b/Model/Player/Backends/MPVBackend.swift index 773251d8..09a5d05b 100644 --- a/Model/Player/Backends/MPVBackend.swift +++ b/Model/Player/Backends/MPVBackend.swift @@ -191,6 +191,10 @@ final class MPVBackend: PlayerBackend { } } + deinit { + NotificationCenter.default.removeObserver(self, name: .getTimeUpdatesNotification, object: self.currentTime) + } + typealias AreInIncreasingOrder = (Stream, Stream) -> Bool func bestPlayable(_ streams: [Stream], maxResolution: ResolutionSetting) -> Stream? { @@ -432,6 +436,8 @@ final class MPVBackend: PlayerBackend { timeObserverThrottle.execute { self.model.updateWatch(time: self.currentTime) } + + NotificationCenter.default.post(name: .getTimeUpdatesNotification, object: self.currentTime) } private func stopClientUpdates() { @@ -618,3 +624,7 @@ final class MPVBackend: PlayerBackend { } } } + +extension Notification.Name { + static let getTimeUpdatesNotification = Notification.Name("getTimeUpdatesNotification") +} diff --git a/Shared/Player/Video Details/ChapterView.swift b/Shared/Player/Video Details/ChapterView.swift index 52e2dc35..216cd5e2 100644 --- a/Shared/Player/Video Details/ChapterView.swift +++ b/Shared/Player/Video Details/ChapterView.swift @@ -18,10 +18,6 @@ import SwiftUI var body: some View { Button(action: { player.backend.seek(to: chapter.start, seekType: .userInteracted) - DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { // Introducing a delay to give the player a chance to skip to the chapter - PlayerTimeModel.shared.currentTime = CMTime(seconds: chapter.start, preferredTimescale: 1) - handleTimeUpdate(PlayerTimeModel.shared.currentTime) - } }) { Group { verticalChapter @@ -29,9 +25,14 @@ import SwiftUI .contentShape(Rectangle()) } .buttonStyle(.plain) - .onReceive(PlayerTimeModel.shared.$currentTime) { cmTime in - self.handleTimeUpdate(cmTime) - print("currentChapterIndex:", player.currentChapter ?? 0) + .onReceive( + NotificationCenter.default + .publisher(for: .getTimeUpdatesNotification) + .receive(on: DispatchQueue.main) + ) { notification in + if let cmTime = notification.object as? CMTime { + self.handleTimeUpdate(cmTime) + } } }