Add share actions to video context menu

This commit is contained in:
Arkadiusz Fal 2022-06-26 13:57:02 +02:00
parent 72ec6094bc
commit 8ebadd4758
8 changed files with 35 additions and 47 deletions

View File

@ -66,6 +66,9 @@ final class NavigationModel: ObservableObject {
@Published var presentingSettings = false @Published var presentingSettings = false
@Published var presentingWelcomeScreen = false @Published var presentingWelcomeScreen = false
@Published var presentingShareSheet = false
@Published var shareURL: URL?
@Published var alert = Alert(title: Text("Error")) @Published var alert = Alert(title: Text("Error"))
@Published var presentingAlert = false @Published var presentingAlert = false
#if os(macOS) #if os(macOS)
@ -205,6 +208,11 @@ final class NavigationModel: ObservableObject {
alert = Alert(title: Text(title), message: Text(message)) alert = Alert(title: Text(title), message: Text(message))
presentingAlert = true presentingAlert = true
} }
func presentShareSheet(_ url: URL) {
shareURL = url
presentingShareSheet = true
}
} }
typealias TabSelection = NavigationModel.TabSelection typealias TabSelection = NavigationModel.TabSelection

View File

@ -26,7 +26,7 @@ class Segment: ObservableObject, Hashable {
formatter.minimumFractionDigits = 0 formatter.minimumFractionDigits = 0
formatter.maximumFractionDigits = 1 formatter.maximumFractionDigits = 1
return formatter.string(from: NSNumber(value: duration)) ?? "" return formatter.string(from: NSNumber(value: duration.rounded())) ?? ""
} }
var endTime: CMTime { var endTime: CMTime {

View File

@ -64,6 +64,11 @@ struct ContentView: View {
#if os(iOS) #if os(iOS)
.overlay(videoPlayer) .overlay(videoPlayer)
.sheet(isPresented: $navigation.presentingShareSheet) {
if let shareURL = navigation.shareURL {
ShareSheet(activityItems: [shareURL])
}
}
#endif #endif
// iOS 14 has problem with multiple sheets in one view // iOS 14 has problem with multiple sheets in one view

View File

@ -95,13 +95,6 @@ struct ChannelPlaylistView: View {
VerticalCells(items: items) VerticalCells(items: items)
.environment(\.inChannelPlaylistView, true) .environment(\.inChannelPlaylistView, true)
} }
#if os(iOS)
.sheet(isPresented: $presentingShareSheet) {
if let shareURL = shareURL {
ShareSheet(activityItems: [shareURL])
}
}
#endif
.onAppear { .onAppear {
resource?.loadIfNeeded() resource?.loadIfNeeded()
} }
@ -119,11 +112,7 @@ struct ChannelPlaylistView: View {
ToolbarItem(placement: playlistButtonsPlacement) { ToolbarItem(placement: playlistButtonsPlacement) {
HStack { HStack {
ShareButton( ShareButton(contentItem: contentItem)
contentItem: contentItem,
presentingShareSheet: $presentingShareSheet,
shareURL: $shareURL
)
if let playlist = presentedPlaylist { if let playlist = presentedPlaylist {
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(playlist.id, playlist.title))) FavoriteButton(item: FavoriteItem(section: .channelPlaylist(playlist.id, playlist.title)))

View File

@ -120,11 +120,7 @@ struct ChannelVideosView: View {
.opacity(store.item?.subscriptionsString != nil ? 1 : 0) .opacity(store.item?.subscriptionsString != nil ? 1 : 0)
} }
ShareButton( ShareButton(contentItem: contentItem)
contentItem: contentItem,
presentingShareSheet: $presentingShareSheet,
shareURL: $shareURL
)
subscriptionToggleButton subscriptionToggleButton
@ -135,13 +131,6 @@ struct ChannelVideosView: View {
} }
} }
#endif #endif
#if os(iOS)
.sheet(isPresented: $presentingShareSheet) {
if let shareURL = shareURL {
ShareSheet(activityItems: [shareURL])
}
}
#endif
.onAppear { .onAppear {
resource?.loadIfNeeded() resource?.loadIfNeeded()
} }

View File

@ -162,11 +162,7 @@ struct ControlsBar: View {
} }
} }
ShareButton( ShareButton(contentItem: .init(video: model.currentVideo))
contentItem: .init(video: model.currentVideo),
presentingShareSheet: $presentingShareSheet,
shareURL: $shareURL
)
Section { Section {
Button { Button {
@ -225,7 +221,8 @@ struct ControlsBar: View {
Text(model.currentVideo?.author ?? "") Text(model.currentVideo?.author ?? "")
.font(.system(size: 12)) .font(.system(size: 12))
if let channel = model.currentVideo?.channel, if !presentingControls,
let channel = model.currentVideo?.channel,
let subsriptions = channel.subscriptionsString let subsriptions = channel.subscriptionsString
{ {
HStack(spacing: 2) { HStack(spacing: 2) {

View File

@ -2,20 +2,13 @@ import SwiftUI
struct ShareButton: View { struct ShareButton: View {
let contentItem: ContentItem let contentItem: ContentItem
@Binding var presentingShareSheet: Bool
@Binding var shareURL: URL?
@EnvironmentObject<AccountsModel> private var accounts @EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<PlayerModel> private var player @EnvironmentObject<PlayerModel> private var player
init( init(contentItem: ContentItem) {
contentItem: ContentItem,
presentingShareSheet: Binding<Bool>,
shareURL: Binding<URL?>? = nil
) {
self.contentItem = contentItem self.contentItem = contentItem
_presentingShareSheet = presentingShareSheet
_shareURL = shareURL ?? .constant(nil)
} }
var body: some View { var body: some View {
@ -24,7 +17,7 @@ struct ShareButton: View {
Divider() Divider()
youtubeActions youtubeActions
} label: { } label: {
Label("Share", systemImage: "square.and.arrow.up") Label("Share...", systemImage: "square.and.arrow.up")
} }
.menuStyle(.borderlessButton) .menuStyle(.borderlessButton)
#if os(macOS) #if os(macOS)
@ -39,7 +32,7 @@ struct ShareButton: View {
shareAction(url) shareAction(url)
} }
if contentItem.contentType == .video { if contentItemIsPlayerCurrentVideo {
Button(labelForShareURL(accounts.app.name, withTime: true)) { Button(labelForShareURL(accounts.app.name, withTime: true)) {
shareAction( shareAction(
accounts.api.shareURL( accounts.api.shareURL(
@ -60,7 +53,7 @@ struct ShareButton: View {
shareAction(url) shareAction(url)
} }
if contentItem.contentType == .video { if contentItemIsPlayerCurrentVideo {
Button(labelForShareURL("YouTube", withTime: true)) { Button(labelForShareURL("YouTube", withTime: true)) {
shareAction( shareAction(
accounts.api.shareURL( 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) { private func shareAction(_ url: URL) {
#if os(macOS) #if os(macOS)
NSPasteboard.general.clearContents() NSPasteboard.general.clearContents()
NSPasteboard.general.setString(url.absoluteString, forType: .string) NSPasteboard.general.setString(url.absoluteString, forType: .string)
#else #else
player.pause() player.pause()
shareURL = url navigation.shareURL = url
presentingShareSheet = true navigation.presentingShareSheet = true
#endif #endif
} }
@ -100,8 +97,7 @@ struct ShareButton: View {
struct ShareButton_Previews: PreviewProvider { struct ShareButton_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
ShareButton( ShareButton(
contentItem: ContentItem(video: Video.fixture), contentItem: ContentItem(video: Video.fixture)
presentingShareSheet: .constant(false)
) )
.injectFixtureEnvironmentObjects() .injectFixtureEnvironmentObjects()
} }

View File

@ -78,6 +78,10 @@ struct VideoContextMenuView: View {
} }
} }
Section {
ShareButton(contentItem: .init(video: video))
}
if !inChannelView, !inChannelPlaylistView { if !inChannelView, !inChannelPlaylistView {
Section { Section {
openChannelButton openChannelButton