From 280f90e4c6440bafebec628d2ef945ca7f3070d1 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Sun, 26 Jun 2022 13:57:02 +0200 Subject: [PATCH] Add share actions to video context menu --- Model/NavigationModel.swift | 8 +++++++ Model/Segment.swift | 2 +- Shared/Navigation/ContentView.swift | 5 +++++ Shared/Views/ChannelPlaylistView.swift | 13 +----------- Shared/Views/ChannelVideosView.swift | 13 +----------- Shared/Views/ControlsBar.swift | 9 +++----- Shared/Views/ShareButton.swift | 28 +++++++++++-------------- Shared/Views/VideoContextMenuView.swift | 4 ++++ 8 files changed, 35 insertions(+), 47 deletions(-) diff --git a/Model/NavigationModel.swift b/Model/NavigationModel.swift index d50b4863..91299ddf 100644 --- a/Model/NavigationModel.swift +++ b/Model/NavigationModel.swift @@ -66,6 +66,9 @@ final class NavigationModel: ObservableObject { @Published var presentingSettings = false @Published var presentingWelcomeScreen = false + @Published var presentingShareSheet = false + @Published var shareURL: URL? + @Published var alert = Alert(title: Text("Error")) @Published var presentingAlert = false #if os(macOS) @@ -205,6 +208,11 @@ final class NavigationModel: ObservableObject { alert = Alert(title: Text(title), message: Text(message)) presentingAlert = true } + + func presentShareSheet(_ url: URL) { + shareURL = url + presentingShareSheet = true + } } typealias TabSelection = NavigationModel.TabSelection diff --git a/Model/Segment.swift b/Model/Segment.swift index 6fe3b26e..5b1d24eb 100644 --- a/Model/Segment.swift +++ b/Model/Segment.swift @@ -26,7 +26,7 @@ class Segment: ObservableObject, Hashable { formatter.minimumFractionDigits = 0 formatter.maximumFractionDigits = 1 - return formatter.string(from: NSNumber(value: duration)) ?? "" + return formatter.string(from: NSNumber(value: duration.rounded())) ?? "" } var endTime: CMTime { diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift index 402153e7..ff5fd887 100644 --- a/Shared/Navigation/ContentView.swift +++ b/Shared/Navigation/ContentView.swift @@ -64,6 +64,11 @@ struct ContentView: View { #if os(iOS) .overlay(videoPlayer) + .sheet(isPresented: $navigation.presentingShareSheet) { + if let shareURL = navigation.shareURL { + ShareSheet(activityItems: [shareURL]) + } + } #endif // iOS 14 has problem with multiple sheets in one view diff --git a/Shared/Views/ChannelPlaylistView.swift b/Shared/Views/ChannelPlaylistView.swift index d8653d18..85f610b2 100644 --- a/Shared/Views/ChannelPlaylistView.swift +++ b/Shared/Views/ChannelPlaylistView.swift @@ -95,13 +95,6 @@ struct ChannelPlaylistView: View { VerticalCells(items: items) .environment(\.inChannelPlaylistView, true) } - #if os(iOS) - .sheet(isPresented: $presentingShareSheet) { - if let shareURL = shareURL { - ShareSheet(activityItems: [shareURL]) - } - } - #endif .onAppear { resource?.loadIfNeeded() } @@ -119,11 +112,7 @@ struct ChannelPlaylistView: View { ToolbarItem(placement: playlistButtonsPlacement) { HStack { - ShareButton( - contentItem: contentItem, - presentingShareSheet: $presentingShareSheet, - shareURL: $shareURL - ) + ShareButton(contentItem: contentItem) if let playlist = presentedPlaylist { FavoriteButton(item: FavoriteItem(section: .channelPlaylist(playlist.id, playlist.title))) diff --git a/Shared/Views/ChannelVideosView.swift b/Shared/Views/ChannelVideosView.swift index 0cd4d03d..373c5247 100644 --- a/Shared/Views/ChannelVideosView.swift +++ b/Shared/Views/ChannelVideosView.swift @@ -120,11 +120,7 @@ struct ChannelVideosView: View { .opacity(store.item?.subscriptionsString != nil ? 1 : 0) } - ShareButton( - contentItem: contentItem, - presentingShareSheet: $presentingShareSheet, - shareURL: $shareURL - ) + ShareButton(contentItem: contentItem) subscriptionToggleButton @@ -135,13 +131,6 @@ struct ChannelVideosView: View { } } #endif - #if os(iOS) - .sheet(isPresented: $presentingShareSheet) { - if let shareURL = shareURL { - ShareSheet(activityItems: [shareURL]) - } - } - #endif .onAppear { resource?.loadIfNeeded() } diff --git a/Shared/Views/ControlsBar.swift b/Shared/Views/ControlsBar.swift index 775a4d65..990e4f77 100644 --- a/Shared/Views/ControlsBar.swift +++ b/Shared/Views/ControlsBar.swift @@ -162,11 +162,7 @@ struct ControlsBar: View { } } - ShareButton( - contentItem: .init(video: model.currentVideo), - presentingShareSheet: $presentingShareSheet, - shareURL: $shareURL - ) + ShareButton(contentItem: .init(video: model.currentVideo)) Section { Button { @@ -225,7 +221,8 @@ struct ControlsBar: View { Text(model.currentVideo?.author ?? "") .font(.system(size: 12)) - if let channel = model.currentVideo?.channel, + if !presentingControls, + let channel = model.currentVideo?.channel, let subsriptions = channel.subscriptionsString { HStack(spacing: 2) { diff --git a/Shared/Views/ShareButton.swift b/Shared/Views/ShareButton.swift index f1c7ecfd..3f6f2f82 100644 --- a/Shared/Views/ShareButton.swift +++ b/Shared/Views/ShareButton.swift @@ -2,20 +2,13 @@ import SwiftUI struct ShareButton: View { let contentItem: ContentItem - @Binding var presentingShareSheet: Bool - @Binding var shareURL: URL? @EnvironmentObject private var accounts + @EnvironmentObject private var navigation @EnvironmentObject private var player - init( - contentItem: ContentItem, - presentingShareSheet: Binding, - shareURL: Binding? = nil - ) { + init(contentItem: ContentItem) { self.contentItem = contentItem - _presentingShareSheet = presentingShareSheet - _shareURL = shareURL ?? .constant(nil) } var body: some View { @@ -24,7 +17,7 @@ struct ShareButton: View { Divider() youtubeActions } label: { - Label("Share", systemImage: "square.and.arrow.up") + Label("Share...", systemImage: "square.and.arrow.up") } .menuStyle(.borderlessButton) #if os(macOS) @@ -39,7 +32,7 @@ struct ShareButton: View { shareAction(url) } - if contentItem.contentType == .video { + if contentItemIsPlayerCurrentVideo { Button(labelForShareURL(accounts.app.name, withTime: true)) { shareAction( accounts.api.shareURL( @@ -60,7 +53,7 @@ struct ShareButton: View { shareAction(url) } - if contentItem.contentType == .video { + if contentItemIsPlayerCurrentVideo { Button(labelForShareURL("YouTube", withTime: true)) { shareAction( accounts.api.shareURL( @@ -75,14 +68,18 @@ struct ShareButton: View { } } + private var contentItemIsPlayerCurrentVideo: Bool { + contentItem.contentType == .video && contentItem.video.videoID == player.currentVideo?.id + } + private func shareAction(_ url: URL) { #if os(macOS) NSPasteboard.general.clearContents() NSPasteboard.general.setString(url.absoluteString, forType: .string) #else player.pause() - shareURL = url - presentingShareSheet = true + navigation.shareURL = url + navigation.presentingShareSheet = true #endif } @@ -100,8 +97,7 @@ struct ShareButton: View { struct ShareButton_Previews: PreviewProvider { static var previews: some View { ShareButton( - contentItem: ContentItem(video: Video.fixture), - presentingShareSheet: .constant(false) + contentItem: ContentItem(video: Video.fixture) ) .injectFixtureEnvironmentObjects() } diff --git a/Shared/Views/VideoContextMenuView.swift b/Shared/Views/VideoContextMenuView.swift index 068790fa..13de210c 100644 --- a/Shared/Views/VideoContextMenuView.swift +++ b/Shared/Views/VideoContextMenuView.swift @@ -78,6 +78,10 @@ struct VideoContextMenuView: View { } } + Section { + ShareButton(contentItem: .init(video: video)) + } + if !inChannelView, !inChannelPlaylistView { Section { openChannelButton