Skip to content

Commit f6ec638

Browse files
committed
Enhance the open URL action by directly handling video URLs
1 parent b381250 commit f6ec638

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

Sources/Models/YouTubePlayer+OpenURLAction.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,20 @@ public extension YouTubePlayer.OpenURLAction {
4444
/// - Parameters:
4545
/// - url: The URL to open.
4646
/// - player: The ``YouTubePlayer`` instance.
47+
@MainActor
4748
func callAsFunction(
4849
url: URL,
4950
player: YouTubePlayer
5051
) async {
52+
// Check if the url is a YouTube video url and can be loaded
53+
if let source = YouTubePlayer.Source(url: url),
54+
source.videoID != player.source?.videoID,
55+
(try? await player.load(source: source)) != nil {
56+
// Return out of function as the url is a video
57+
// which is now loaded in the player
58+
return
59+
}
60+
// Invoke the handler
5161
await self.handler(url, player)
5262
}
5363

@@ -88,14 +98,6 @@ public extension YouTubePlayer.OpenURLAction {
8898

8999
/// The default OpenURLAction
90100
static let `default` = Self { url, player in
91-
// Check if YouTube player source can be initialize from url
92-
// and it was successfully loaded
93-
if let source = YouTubePlayer.Source(url: url),
94-
source.videoID != player.source?.videoID,
95-
(try? await player.load(source: source)) != nil {
96-
// Return out of function
97-
return
98-
}
99101
// Open url externally
100102
#if os(macOS)
101103
_ = try? await NSWorkspace

0 commit comments

Comments
 (0)