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

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