From 91fa4ea2ff0344de7206837c8ad2442802d606ff Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Fri, 24 Dec 2021 20:20:05 +0100 Subject: [PATCH] Extract open URL action --- Model/Accounts/AccountsModel.swift | 4 +++ Shared/Navigation/ContentView.swift | 24 +---------------- Shared/OpenURLHandler.swift | 41 +++++++++++++++++++++++++++++ Shared/Player/VideoPlayerView.swift | 28 +++----------------- Shared/YatteeApp.swift | 16 ++++++----- Yattee.xcodeproj/project.pbxproj | 6 +++++ 6 files changed, 65 insertions(+), 54 deletions(-) create mode 100644 Shared/OpenURLHandler.swift diff --git a/Model/Accounts/AccountsModel.swift b/Model/Accounts/AccountsModel.swift index 928d7f32..2f067641 100644 --- a/Model/Accounts/AccountsModel.swift +++ b/Model/Accounts/AccountsModel.swift @@ -22,6 +22,10 @@ final class AccountsModel: ObservableObject { return AccountsModel.find(id) } + var any: Account? { + lastUsed ?? all.randomElement() + } + var app: VideosApp { current?.instance?.app ?? .invidious } diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift index f91446e5..9a6f686d 100644 --- a/Shared/Navigation/ContentView.swift +++ b/Shared/Navigation/ContentView.swift @@ -65,7 +65,7 @@ struct ContentView: View { } ) #if !os(tvOS) - .onOpenURL(perform: handleOpenedURL) + .onOpenURL { OpenURLHandler(accounts: accounts, player: player).handle($0) } .background( EmptyView().sheet(isPresented: $navigation.presentingAddToPlaylist) { AddToPlaylistView(video: navigation.videoToAddToPlaylist) @@ -161,28 +161,6 @@ struct ContentView: View { navigation.presentingWelcomeScreen = true } - - #if !os(tvOS) - func handleOpenedURL(_ url: URL) { - guard !accounts.current.isNil else { - return - } - - let parser = VideoURLParser(url: url) - - guard let id = parser.id else { - return - } - - accounts.api.video(id).load().onSuccess { response in - if let video: Video = response.typedContent() { - player.addCurrentItemToHistory() - self.player.playNow(video, at: parser.time) - self.player.show() - } - } - } - #endif } struct ContentView_Previews: PreviewProvider { diff --git a/Shared/OpenURLHandler.swift b/Shared/OpenURLHandler.swift new file mode 100644 index 00000000..556d04a4 --- /dev/null +++ b/Shared/OpenURLHandler.swift @@ -0,0 +1,41 @@ +import Foundation + +struct OpenURLHandler { + var accounts: AccountsModel + var player: PlayerModel + + func handle(_ url: URL) { + if accounts.current.isNil { + accounts.setCurrent(accounts.any) + } + + guard !accounts.current.isNil else { + return + } + + #if os(macOS) + guard url.host != OpenWindow.player.location else { + return + } + #endif + + let parser = VideoURLParser(url: url) + + guard let id = parser.id, + id != player.currentVideo?.id + else { + return + } + + #if os(macOS) + OpenWindow.main.open() + #endif + + accounts.api.video(id).load().onSuccess { response in + if let video: Video = response.typedContent() { + self.player.playNow(video, at: parser.time) + self.player.show() + } + } + } +} diff --git a/Shared/Player/VideoPlayerView.swift b/Shared/Player/VideoPlayerView.swift index ce8468f7..d70bec64 100644 --- a/Shared/Player/VideoPlayerView.swift +++ b/Shared/Player/VideoPlayerView.swift @@ -24,6 +24,7 @@ struct VideoPlayerView: View { @Environment(\.verticalSizeClass) private var verticalSizeClass #endif + @EnvironmentObject private var accounts @EnvironmentObject private var player var body: some View { @@ -31,7 +32,7 @@ struct VideoPlayerView: View { HSplitView { content } - .onOpenURL(perform: handleOpenedURL) + .onOpenURL { OpenURLHandler(accounts: accounts, player: player).handle($0) } .frame(minWidth: 950, minHeight: 700) #else GeometryReader { geometry in @@ -83,10 +84,10 @@ struct VideoPlayerView: View { .onSwipeGesture( up: { withAnimation { - fullScreen = true + fullScreenDetails = true } }, - down: { presentationMode.wrappedValue.dismiss() } + down: { player.hide() } ) #endif @@ -191,27 +192,6 @@ struct VideoPlayerView: View { set: { _ in } ) } - - #if !os(tvOS) - func handleOpenedURL(_ url: URL) { - guard !player.accounts.current.isNil else { - return - } - - let parser = VideoURLParser(url: url) - - guard let id = parser.id else { - return - } - - player.accounts.api.video(id).load().onSuccess { response in - if let video: Video = response.typedContent() { - self.player.playNow(video, at: parser.time) - self.player.show() - } - } - } - #endif } struct VideoPlayerView_Previews: PreviewProvider { diff --git a/Shared/YatteeApp.swift b/Shared/YatteeApp.swift index 9517b0ac..e42ee094 100644 --- a/Shared/YatteeApp.swift +++ b/Shared/YatteeApp.swift @@ -41,12 +41,14 @@ struct YatteeApp: App { player.handleEnterForeground() } #endif - #if !os(tvOS) - .handlesExternalEvents(preferring: Set(["watch"]), allowing: Set(["watch"])) + #if os(iOS) + .handlesExternalEvents(preferring: Set(["*"]), allowing: Set(["*"])) #endif } + #if os(iOS) + .handlesExternalEvents(matching: Set(["*"])) + #endif #if !os(tvOS) - .handlesExternalEvents(matching: Set(arrayLiteral: "watch")) .commands { SidebarCommands() @@ -78,14 +80,14 @@ struct YatteeApp: App { .environmentObject(recents) .environmentObject(subscriptions) .environmentObject(thumbnails) - .handlesExternalEvents(preferring: Set(["player"]), allowing: Set(["player"])) + .handlesExternalEvents(preferring: Set(["player", "*"]), allowing: Set(["player", "*"])) } - .handlesExternalEvents(matching: Set(["player"])) + .handlesExternalEvents(matching: Set(["player", "*"])) Settings { SettingsView() - .environmentObject(AccountsModel()) - .environmentObject(InstancesModel()) + .environmentObject(accounts) + .environmentObject(instances) .environmentObject(updater) } #endif diff --git a/Yattee.xcodeproj/project.pbxproj b/Yattee.xcodeproj/project.pbxproj index 11003496..6b7b3f83 100644 --- a/Yattee.xcodeproj/project.pbxproj +++ b/Yattee.xcodeproj/project.pbxproj @@ -359,6 +359,8 @@ 37B767DC2677C3CA0098BAA8 /* PlayerModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37B767DA2677C3CA0098BAA8 /* PlayerModel.swift */; }; 37B767DD2677C3CA0098BAA8 /* PlayerModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37B767DA2677C3CA0098BAA8 /* PlayerModel.swift */; }; 37B767E02678C5BF0098BAA8 /* Logging in Frameworks */ = {isa = PBXBuildFile; productRef = 37B767DF2678C5BF0098BAA8 /* Logging */; }; + 37B795902771DAE0001CF27B /* OpenURLHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37B7958F2771DAE0001CF27B /* OpenURLHandler.swift */; }; + 37B795912771DAE0001CF27B /* OpenURLHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37B7958F2771DAE0001CF27B /* OpenURLHandler.swift */; }; 37B81AF926D2C9A700675966 /* VideoPlayerSizeModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37B81AF826D2C9A700675966 /* VideoPlayerSizeModifier.swift */; }; 37B81AFA26D2C9A700675966 /* VideoPlayerSizeModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37B81AF826D2C9A700675966 /* VideoPlayerSizeModifier.swift */; }; 37B81AFC26D2C9C900675966 /* VideoDetailsPaddingModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37B81AFB26D2C9C900675966 /* VideoDetailsPaddingModifier.swift */; }; @@ -676,6 +678,7 @@ 37B17D9F268A1F25006AEE9B /* VideoContextMenuView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideoContextMenuView.swift; sourceTree = ""; }; 37B263192735EAAB00FE0D40 /* FavoriteResourceObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoriteResourceObserver.swift; sourceTree = ""; }; 37B767DA2677C3CA0098BAA8 /* PlayerModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerModel.swift; sourceTree = ""; }; + 37B7958F2771DAE0001CF27B /* OpenURLHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenURLHandler.swift; sourceTree = ""; }; 37B81AF826D2C9A700675966 /* VideoPlayerSizeModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideoPlayerSizeModifier.swift; sourceTree = ""; }; 37B81AFB26D2C9C900675966 /* VideoDetailsPaddingModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideoDetailsPaddingModifier.swift; sourceTree = ""; }; 37B81AFE26D2CA3700675966 /* VideoDetails.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideoDetails.swift; sourceTree = ""; }; @@ -1189,6 +1192,7 @@ 372915E52687E3B900F5A35B /* Defaults.swift */, 3761ABFC26F0F8DE00AA496F /* EnvironmentValues.swift */, 3729037D2739E47400EA99F6 /* MenuCommands.swift */, + 37B7958F2771DAE0001CF27B /* OpenURLHandler.swift */, 3700155E271B12DD0049C794 /* SiestaConfiguration.swift */, 37FFC43F272734C3009FFD26 /* Throttle.swift */, 37CB12782724C76D00213B45 /* VideoURLParser.swift */, @@ -1916,6 +1920,7 @@ 37C3A241272359900087A57A /* Double+Format.swift in Sources */, 37FB285E272225E800A57617 /* ContentItemView.swift in Sources */, 3797758B2689345500DD52A8 /* Store.swift in Sources */, + 37B795902771DAE0001CF27B /* OpenURLHandler.swift in Sources */, 37732FF02703A26300F04329 /* AccountValidationStatus.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -1998,6 +2003,7 @@ 3748186F26A769D60084E870 /* DetailBadge.swift in Sources */, 372915E72687E3B900F5A35B /* Defaults.swift in Sources */, 37C3A242272359900087A57A /* Double+Format.swift in Sources */, + 37B795912771DAE0001CF27B /* OpenURLHandler.swift in Sources */, 37C90AF1275F9D450015EAF7 /* UpdaterModel.swift in Sources */, 37DD87C8271C9CFE0027CBF9 /* PlayerStreams.swift in Sources */, 376578922685490700D4EA09 /* PlaylistsView.swift in Sources */,