2021-10-27 04:29:59 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ShareButton: View {
|
|
|
|
let contentItem: ContentItem
|
|
|
|
|
|
|
|
@EnvironmentObject<AccountsModel> private var accounts
|
2022-06-26 17:27:02 +05:30
|
|
|
@EnvironmentObject<NavigationModel> private var navigation
|
2021-11-03 01:10:49 +05:30
|
|
|
@EnvironmentObject<PlayerModel> private var player
|
|
|
|
|
2022-06-26 17:27:02 +05:30
|
|
|
init(contentItem: ContentItem) {
|
2021-11-03 01:10:49 +05:30
|
|
|
self.contentItem = contentItem
|
|
|
|
}
|
2021-10-27 04:29:59 +05:30
|
|
|
|
|
|
|
var body: some View {
|
2021-11-03 01:10:49 +05:30
|
|
|
Menu {
|
|
|
|
instanceActions
|
2021-11-03 04:32:02 +05:30
|
|
|
Divider()
|
2021-11-03 01:10:49 +05:30
|
|
|
youtubeActions
|
|
|
|
} label: {
|
2022-06-26 17:27:02 +05:30
|
|
|
Label("Share...", systemImage: "square.and.arrow.up")
|
2021-11-03 01:10:49 +05:30
|
|
|
}
|
|
|
|
.menuStyle(.borderlessButton)
|
|
|
|
#if os(macOS)
|
|
|
|
.frame(maxWidth: 35)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private var instanceActions: some View {
|
2021-10-28 22:44:55 +05:30
|
|
|
Group {
|
2021-11-03 01:10:49 +05:30
|
|
|
if let url = accounts.api.shareURL(contentItem) {
|
|
|
|
Button(labelForShareURL(accounts.app.name)) {
|
|
|
|
shareAction(url)
|
|
|
|
}
|
|
|
|
|
2022-06-26 17:27:02 +05:30
|
|
|
if contentItemIsPlayerCurrentVideo {
|
2021-11-03 01:10:49 +05:30
|
|
|
Button(labelForShareURL(accounts.app.name, withTime: true)) {
|
|
|
|
shareAction(
|
|
|
|
accounts.api.shareURL(
|
|
|
|
contentItem,
|
2022-02-17 01:53:11 +05:30
|
|
|
time: player.backend.currentTime
|
2021-11-03 01:10:49 +05:30
|
|
|
)!
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 17:27:02 +05:30
|
|
|
if contentItemIsPlayerCurrentVideo {
|
2021-11-03 01:10:49 +05:30
|
|
|
Button(labelForShareURL("YouTube", withTime: true)) {
|
|
|
|
shareAction(
|
|
|
|
accounts.api.shareURL(
|
|
|
|
contentItem,
|
|
|
|
frontendHost: "www.youtube.com",
|
2022-02-17 01:53:11 +05:30
|
|
|
time: player.backend.currentTime
|
2021-11-03 01:10:49 +05:30
|
|
|
)!
|
|
|
|
)
|
|
|
|
}
|
2021-10-28 22:44:55 +05:30
|
|
|
}
|
|
|
|
}
|
2021-10-27 04:29:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-26 17:27:02 +05:30
|
|
|
private var contentItemIsPlayerCurrentVideo: Bool {
|
2022-06-30 04:19:35 +05:30
|
|
|
contentItem.contentType == .video && contentItem.video?.videoID == player.currentVideo?.videoID
|
2022-06-26 17:27:02 +05:30
|
|
|
}
|
|
|
|
|
2021-11-03 01:10:49 +05:30
|
|
|
private func shareAction(_ url: URL) {
|
|
|
|
#if os(macOS)
|
|
|
|
NSPasteboard.general.clearContents()
|
|
|
|
NSPasteboard.general.setString(url.absoluteString, forType: .string)
|
|
|
|
#else
|
2022-06-16 23:13:56 +05:30
|
|
|
player.pause()
|
2022-06-26 17:27:02 +05:30
|
|
|
navigation.shareURL = url
|
|
|
|
navigation.presentingShareSheet = true
|
2021-11-03 01:10:49 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private func labelForShareURL(_ app: String, withTime: Bool = false) -> String {
|
|
|
|
let time = withTime ? "with time" : ""
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
return "Copy \(app) link \(time)"
|
|
|
|
#else
|
|
|
|
return "Share \(app) link \(time)"
|
|
|
|
#endif
|
2021-10-27 04:29:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ShareButton_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2021-11-03 01:10:49 +05:30
|
|
|
ShareButton(
|
2022-06-26 17:27:02 +05:30
|
|
|
contentItem: ContentItem(video: Video.fixture)
|
2021-11-03 01:10:49 +05:30
|
|
|
)
|
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-10-27 04:29:59 +05:30
|
|
|
}
|
|
|
|
}
|