Add share actions to video context menu

This commit is contained in:
Arkadiusz Fal 2022-06-26 13:57:02 +02:00
parent 820d5ce962
commit 280f90e4c6
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 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

View File

@ -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 {

View File

@ -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

View File

@ -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)))

View File

@ -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()
}

View File

@ -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) {

View File

@ -2,20 +2,13 @@ import SwiftUI
struct ShareButton: View {
let contentItem: ContentItem
@Binding var presentingShareSheet: Bool
@Binding var shareURL: URL?
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<PlayerModel> private var player
init(
contentItem: ContentItem,
presentingShareSheet: Binding<Bool>,
shareURL: Binding<URL?>? = 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()
}

View File

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