2021-10-26 22:59:59 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ShareButton: View {
|
|
|
|
let contentItem: ContentItem
|
|
|
|
|
|
|
|
@EnvironmentObject<AccountsModel> private var accounts
|
2022-06-26 11:57:02 +00:00
|
|
|
@EnvironmentObject<NavigationModel> private var navigation
|
2021-11-02 19:40:49 +00:00
|
|
|
@EnvironmentObject<PlayerModel> private var player
|
|
|
|
|
2022-06-26 11:57:02 +00:00
|
|
|
init(contentItem: ContentItem) {
|
2021-11-02 19:40:49 +00:00
|
|
|
self.contentItem = contentItem
|
|
|
|
}
|
2021-10-26 22:59:59 +00:00
|
|
|
|
|
|
|
var body: some View {
|
2021-11-02 19:40:49 +00:00
|
|
|
Menu {
|
|
|
|
instanceActions
|
2021-11-02 23:02:02 +00:00
|
|
|
Divider()
|
2022-10-26 11:11:35 +00:00
|
|
|
if !accounts.isDemo {
|
|
|
|
youtubeActions
|
|
|
|
}
|
2021-11-02 19:40:49 +00:00
|
|
|
} label: {
|
2022-06-26 11:57:02 +00:00
|
|
|
Label("Share...", systemImage: "square.and.arrow.up")
|
2021-11-02 19:40:49 +00:00
|
|
|
}
|
|
|
|
.menuStyle(.borderlessButton)
|
|
|
|
#if os(macOS)
|
|
|
|
.frame(maxWidth: 35)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private var instanceActions: some View {
|
2021-10-28 17:14:55 +00:00
|
|
|
Group {
|
2022-08-23 21:14:13 +00:00
|
|
|
Button(labelForShareURL(accounts.app.name)) {
|
|
|
|
if let url = player.playerAPI.shareURL(contentItem) {
|
2021-11-02 19:40:49 +00:00
|
|
|
shareAction(url)
|
2022-08-23 21:14:13 +00:00
|
|
|
} else {
|
|
|
|
navigation.presentAlert(
|
|
|
|
title: "Could not create share link",
|
|
|
|
message: "For custom locations you can configure Frontend URL in Locations settings"
|
|
|
|
)
|
2021-11-02 19:40:49 +00:00
|
|
|
}
|
2022-08-23 21:14:13 +00:00
|
|
|
}
|
2021-11-02 19:40:49 +00:00
|
|
|
|
2022-08-23 21:14:13 +00:00
|
|
|
if contentItemIsPlayerCurrentVideo {
|
|
|
|
Button(labelForShareURL(accounts.app.name, withTime: true)) {
|
|
|
|
shareAction(
|
|
|
|
player.playerAPI.shareURL(
|
|
|
|
contentItem,
|
|
|
|
time: player.backend.currentTime
|
|
|
|
)!
|
|
|
|
)
|
2021-11-02 19:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var youtubeActions: some View {
|
|
|
|
Group {
|
|
|
|
if let url = accounts.api.shareURL(contentItem, frontendHost: "www.youtube.com") {
|
|
|
|
Button(labelForShareURL("YouTube")) {
|
|
|
|
shareAction(url)
|
|
|
|
}
|
|
|
|
|
2022-06-26 11:57:02 +00:00
|
|
|
if contentItemIsPlayerCurrentVideo {
|
2021-11-02 19:40:49 +00:00
|
|
|
Button(labelForShareURL("YouTube", withTime: true)) {
|
|
|
|
shareAction(
|
|
|
|
accounts.api.shareURL(
|
|
|
|
contentItem,
|
|
|
|
frontendHost: "www.youtube.com",
|
2022-02-16 20:23:11 +00:00
|
|
|
time: player.backend.currentTime
|
2021-11-02 19:40:49 +00:00
|
|
|
)!
|
|
|
|
)
|
|
|
|
}
|
2021-10-28 17:14:55 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-26 22:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-26 11:57:02 +00:00
|
|
|
private var contentItemIsPlayerCurrentVideo: Bool {
|
2022-06-29 22:49:35 +00:00
|
|
|
contentItem.contentType == .video && contentItem.video?.videoID == player.currentVideo?.videoID
|
2022-06-26 11:57:02 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 19:40:49 +00:00
|
|
|
private func shareAction(_ url: URL) {
|
|
|
|
#if os(macOS)
|
|
|
|
NSPasteboard.general.clearContents()
|
|
|
|
NSPasteboard.general.setString(url.absoluteString, forType: .string)
|
|
|
|
#else
|
2022-06-16 17:43:56 +00:00
|
|
|
player.pause()
|
2022-06-26 11:57:02 +00:00
|
|
|
navigation.shareURL = url
|
|
|
|
navigation.presentingShareSheet = true
|
2021-11-02 19:40:49 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private func labelForShareURL(_ app: String, withTime: Bool = false) -> String {
|
2022-09-04 15:28:30 +00:00
|
|
|
if withTime {
|
|
|
|
#if os(macOS)
|
|
|
|
return String(format: "Copy %@ link with time".localized(), app)
|
|
|
|
#else
|
|
|
|
return String(format: "Share %@ link with time".localized(), app)
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
#if os(macOS)
|
|
|
|
return String(format: "Copy %@ link".localized(), app)
|
|
|
|
#else
|
|
|
|
return String(format: "Share %@ link".localized(), app)
|
|
|
|
#endif
|
|
|
|
}
|
2021-10-26 22:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ShareButton_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2021-11-02 19:40:49 +00:00
|
|
|
ShareButton(
|
2022-06-26 11:57:02 +00:00
|
|
|
contentItem: ContentItem(video: Video.fixture)
|
2021-11-02 19:40:49 +00:00
|
|
|
)
|
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-10-26 22:59:59 +00:00
|
|
|
}
|
|
|
|
}
|