From f8785fbb21c5ba9420144a7e93a62212e481b9ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Menu?= Date: Mon, 25 Nov 2024 16:57:31 +0100 Subject: [PATCH] Main actor conformance for the navigator delegates (#511) --- Sources/Navigator/Audiobook/AudioNavigator.swift | 12 +++++++----- Sources/Navigator/SelectableNavigator.swift | 2 +- Sources/Navigator/VisualNavigator.swift | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Sources/Navigator/Audiobook/AudioNavigator.swift b/Sources/Navigator/Audiobook/AudioNavigator.swift index fbe106fae..e51ec9c7b 100644 --- a/Sources/Navigator/Audiobook/AudioNavigator.swift +++ b/Sources/Navigator/Audiobook/AudioNavigator.swift @@ -50,7 +50,7 @@ public struct MediaPlaybackInfo { } } -public protocol AudioNavigatorDelegate: NavigatorDelegate { +@MainActor public protocol AudioNavigatorDelegate: NavigatorDelegate { /// Called when the playback updates. func navigator(_ navigator: AudioNavigator, playbackDidChange info: MediaPlaybackInfo) @@ -108,7 +108,7 @@ public final class AudioNavigator: Navigator, Configurable, AudioSessionUser, Lo } } - public let publication: Publication + public nonisolated let publication: Publication private let initialLocation: Locator? private let config: Configuration @@ -326,7 +326,7 @@ public final class AudioNavigator: Navigator, Configurable, AudioSessionUser, Lo /// A deadlock can occur when loading HTTP assets and creating the playback info from the main thread. /// To fix this, this is an asynchronous operation. - private func makePlaybackInfo(forTime time: Double? = nil, completion: @escaping (MediaPlaybackInfo) -> Void) { + private func makePlaybackInfo(forTime time: Double? = nil, completion: @escaping @MainActor (MediaPlaybackInfo) -> Void) { DispatchQueue.global(qos: .userInteractive).async { let info = MediaPlaybackInfo( resourceIndex: self.resourceIndex, @@ -389,7 +389,9 @@ public final class AudioNavigator: Navigator, Configurable, AudioSessionUser, Lo } self.lastLoadedTimeRanges = ranges - self.delegate?.navigator(self, loadedTimeRangesDidChange: ranges) + Task { @MainActor in + self.delegate?.navigator(self, loadedTimeRangesDidChange: ranges) + } } // MARK: - Navigator @@ -411,7 +413,7 @@ public final class AudioNavigator: Navigator, Configurable, AudioSessionUser, Lo player.replaceCurrentItem(with: AVPlayerItem(asset: asset)) resourceIndex = newResourceIndex loadedTimeRangesTimer.fire() - delegate?.navigator(self, loadedTimeRangesDidChange: []) + await delegate?.navigator(self, loadedTimeRangesDidChange: []) } // Seeks to time diff --git a/Sources/Navigator/SelectableNavigator.swift b/Sources/Navigator/SelectableNavigator.swift index 26d8f0117..29dead6d1 100644 --- a/Sources/Navigator/SelectableNavigator.swift +++ b/Sources/Navigator/SelectableNavigator.swift @@ -30,7 +30,7 @@ public struct Selection { public let frame: CGRect? } -public protocol SelectableNavigatorDelegate: NavigatorDelegate { +@MainActor public protocol SelectableNavigatorDelegate: NavigatorDelegate { /// Returns whether the default edit menu (`UIMenuController`) should be displayed for the given `selection`. /// /// To implement a custom selection pop-up, return false and display your own view using `selection.frame`. diff --git a/Sources/Navigator/VisualNavigator.swift b/Sources/Navigator/VisualNavigator.swift index e439b9aa5..c84b51298 100644 --- a/Sources/Navigator/VisualNavigator.swift +++ b/Sources/Navigator/VisualNavigator.swift @@ -106,7 +106,7 @@ public struct VisualNavigatorPresentation { } } -public protocol VisualNavigatorDelegate: NavigatorDelegate { +@MainActor public protocol VisualNavigatorDelegate: NavigatorDelegate { /// Called when the navigator presentation changed, for example after /// applying a new set of preferences. func navigator(_ navigator: Navigator, presentationDidChange presentation: VisualNavigatorPresentation)