subscribe chapters to currentTime notification

This commit is contained in:
Toni Förster 2023-12-04 00:07:39 +01:00
parent aa5d6733b2
commit 586cea7d44
No known key found for this signature in database
GPG Key ID: 292F3E5086C83FC7
2 changed files with 18 additions and 7 deletions

View File

@ -191,6 +191,10 @@ final class MPVBackend: PlayerBackend {
} }
} }
deinit {
NotificationCenter.default.removeObserver(self, name: .getTimeUpdatesNotification, object: self.currentTime)
}
typealias AreInIncreasingOrder = (Stream, Stream) -> Bool typealias AreInIncreasingOrder = (Stream, Stream) -> Bool
func bestPlayable(_ streams: [Stream], maxResolution: ResolutionSetting) -> Stream? { func bestPlayable(_ streams: [Stream], maxResolution: ResolutionSetting) -> Stream? {
@ -432,6 +436,8 @@ final class MPVBackend: PlayerBackend {
timeObserverThrottle.execute { timeObserverThrottle.execute {
self.model.updateWatch(time: self.currentTime) self.model.updateWatch(time: self.currentTime)
} }
NotificationCenter.default.post(name: .getTimeUpdatesNotification, object: self.currentTime)
} }
private func stopClientUpdates() { private func stopClientUpdates() {
@ -618,3 +624,7 @@ final class MPVBackend: PlayerBackend {
} }
} }
} }
extension Notification.Name {
static let getTimeUpdatesNotification = Notification.Name("getTimeUpdatesNotification")
}

View File

@ -18,10 +18,6 @@ import SwiftUI
var body: some View { var body: some View {
Button(action: { Button(action: {
player.backend.seek(to: chapter.start, seekType: .userInteracted) 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 { Group {
verticalChapter verticalChapter
@ -29,9 +25,14 @@ import SwiftUI
.contentShape(Rectangle()) .contentShape(Rectangle())
} }
.buttonStyle(.plain) .buttonStyle(.plain)
.onReceive(PlayerTimeModel.shared.$currentTime) { cmTime in .onReceive(
NotificationCenter.default
.publisher(for: .getTimeUpdatesNotification)
.receive(on: DispatchQueue.main)
) { notification in
if let cmTime = notification.object as? CMTime {
self.handleTimeUpdate(cmTime) self.handleTimeUpdate(cmTime)
print("currentChapterIndex:", player.currentChapter ?? 0) }
} }
} }