Share button

This commit is contained in:
Arkadiusz Fal
2021-10-27 00:59:59 +02:00
parent b50d915d8e
commit 544dc70c5d
11 changed files with 205 additions and 19 deletions

View File

@@ -15,6 +15,7 @@ struct PearvidiousApp: App {
.handlesExternalEvents(matching: Set(["*"]))
.commands {
SidebarCommands()
CommandGroup(replacing: .newItem, addition: {})
}
#endif

View File

@@ -12,6 +12,7 @@ struct VideoDetails: View {
@State private var subscribed = false
@State private var confirmationShown = false
@State private var presentingAddToPlaylist = false
@State private var presentingShareSheet = false
@State private var currentPage = Page.details
@@ -249,6 +250,11 @@ struct VideoDetails: View {
Group {
if let video = player.currentVideo {
HStack {
ShareButton(
contentItem: ContentItem(video: video),
presentingShareSheet: $presentingShareSheet
)
Spacer()
if let views = video.viewsCount {
@@ -269,14 +275,17 @@ struct VideoDetails: View {
Spacer()
Button {
presentingAddToPlaylist = true
} label: {
Label("Add to Playlist", systemImage: "text.badge.plus")
.labelStyle(.iconOnly)
.help("Add to Playlist...")
if accounts.app.supportsUserPlaylists {
Button {
presentingAddToPlaylist = true
} label: {
Label("Add to Playlist", systemImage: "text.badge.plus")
.labelStyle(.iconOnly)
.help("Add to Playlist...")
}
.buttonStyle(.plain)
.foregroundColor(.blue)
}
.buttonStyle(.plain)
}
.frame(maxHeight: 35)
.foregroundColor(.secondary)
@@ -287,6 +296,17 @@ struct VideoDetails: View {
AddToPlaylistView(video: video)
}
}
#if os(iOS)
.sheet(isPresented: $presentingShareSheet) {
ShareSheet(activityItems: [
accounts.api.shareURL(contentItem)
])
}
#endif
}
private var contentItem: ContentItem {
ContentItem(video: player.currentVideo!)
}
var detailsPage: some View {

View File

@@ -4,6 +4,8 @@ import SwiftUI
struct ChannelPlaylistView: View {
var playlist: ChannelPlaylist
@State private var presentingShareSheet = false
@StateObject private var store = Store<ChannelPlaylist>()
@Environment(\.dismiss) private var dismiss
@@ -44,12 +46,26 @@ struct ChannelPlaylistView: View {
#endif
VerticalCells(items: items)
}
#if os(iOS)
.sheet(isPresented: $presentingShareSheet) {
ShareSheet(activityItems: [
accounts.api.shareURL(contentItem)
])
}
#endif
.onAppear {
resource?.addObserver(store)
resource?.loadIfNeeded()
}
#if !os(tvOS)
.toolbar {
ToolbarItem(placement: shareButtonPlacement) {
ShareButton(
contentItem: contentItem,
presentingShareSheet: $presentingShareSheet
)
}
ToolbarItem(placement: .cancellationAction) {
if inNavigationView {
Button("Done") {
@@ -64,6 +80,18 @@ struct ChannelPlaylistView: View {
.background(.thickMaterial)
#endif
}
private var shareButtonPlacement: ToolbarItemPlacement {
#if os(iOS)
.navigation
#else
.automatic
#endif
}
private var contentItem: ContentItem {
ContentItem(playlist: playlist)
}
}
struct ChannelPlaylistView_Previews: PreviewProvider {

View File

@@ -4,6 +4,8 @@ import SwiftUI
struct ChannelVideosView: View {
let channel: Channel
@State private var presentingShareSheet = false
@StateObject private var store = Store<Channel>()
@Environment(\.dismiss) private var dismiss
@@ -70,6 +72,13 @@ struct ChannelVideosView: View {
#endif
#if !os(tvOS)
.toolbar {
ToolbarItem(placement: shareButtonPlacement) {
ShareButton(
contentItem: contentItem,
presentingShareSheet: $presentingShareSheet
)
}
ToolbarItem {
HStack {
Text("**\(store.item?.subscriptionsString ?? "loading")** subscribers")
@@ -91,6 +100,13 @@ struct ChannelVideosView: View {
#else
.background(.thickMaterial)
#endif
#if os(iOS)
.sheet(isPresented: $presentingShareSheet) {
ShareSheet(activityItems: [
accounts.api.shareURL(contentItem)
])
}
#endif
.modifier(UnsubscribeAlertModifier())
.onAppear {
if store.item.isNil {
@@ -101,14 +117,14 @@ struct ChannelVideosView: View {
.navigationTitle(navigationTitle)
}
var resource: Resource {
private var resource: Resource {
let resource = accounts.api.channel(channel.id)
resource.addObserver(store)
return resource
}
var subscriptionToggleButton: some View {
private var subscriptionToggleButton: some View {
Group {
if accounts.app.supportsSubscriptions && accounts.signedIn {
if subscriptions.isSubscribing(channel.id) {
@@ -126,7 +142,19 @@ struct ChannelVideosView: View {
}
}
var navigationTitle: String {
private var shareButtonPlacement: ToolbarItemPlacement {
#if os(iOS)
.navigation
#else
.automatic
#endif
}
private var contentItem: ContentItem {
ContentItem(channel: channel)
}
private var navigationTitle: String {
store.item?.name ?? channel.name
}
}

View File

@@ -0,0 +1,39 @@
import SwiftUI
struct ShareButton: View {
let contentItem: ContentItem
@Binding var presentingShareSheet: Bool
@EnvironmentObject<AccountsModel> private var accounts
var body: some View {
Button {
#if os(iOS)
presentingShareSheet = true
#else
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(shareURL, forType: .string)
#endif
} label: {
#if os(iOS)
Label("Share", systemImage: "square.and.arrow.up")
#else
EmptyView()
#endif
}
.keyboardShortcut("c")
.foregroundColor(.blue)
.buttonStyle(.plain)
.labelStyle(.iconOnly)
}
private var shareURL: String {
accounts.api.shareURL(contentItem).absoluteString
}
}
struct ShareButton_Previews: PreviewProvider {
static var previews: some View {
ShareButton(contentItem: ContentItem(video: Video.fixture), presentingShareSheet: .constant(false))
}
}