1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Shared/Views/ShareButton.swift

140 lines
4.4 KiB
Swift
Raw Normal View History

2021-10-27 04:29:59 +05:30
import SwiftUI
2022-11-13 23:22:15 +05:30
struct ShareButton<LabelView: View>: View {
2021-10-27 04:29:59 +05:30
let contentItem: ContentItem
@ObservedObject private var accounts = AccountsModel.shared
private var navigation: NavigationModel { .shared }
@ObservedObject private var player = PlayerModel.shared
2021-11-03 01:10:49 +05:30
2022-11-13 23:22:15 +05:30
let label: LabelView?
init(
contentItem: ContentItem,
@ViewBuilder label: @escaping () -> LabelView? = {
Label("Share...", systemImage: "square.and.arrow.up")
}
) {
2021-11-03 01:10:49 +05:30
self.contentItem = contentItem
2022-11-13 23:22:15 +05:30
self.label = label()
2021-11-03 01:10:49 +05:30
}
2021-10-27 04:29:59 +05:30
2022-11-11 02:28:45 +05:30
@ViewBuilder var body: some View {
if let video = contentItem.video, !video.localStreamIsFile {
Menu {
if video.localStreamIsRemoteURL {
remoteURLAction
} else {
instanceActions
Divider()
2022-11-12 01:04:20 +05:30
if !accounts.isEmpty {
2022-11-11 02:28:45 +05:30
youtubeActions
}
}
} label: {
2022-11-13 23:22:15 +05:30
label
}
2022-11-11 02:28:45 +05:30
.menuStyle(.borderlessButton)
#if os(macOS)
2022-11-13 23:22:15 +05:30
.frame(maxWidth: 60)
2022-11-11 02:28:45 +05:30
#endif
2021-11-03 01:10:49 +05:30
}
}
private var instanceActions: some View {
2021-10-28 22:44:55 +05:30
Group {
2022-08-24 02:44:13 +05:30
Button(labelForShareURL(accounts.app.name)) {
if let url = player.playerAPI.shareURL(contentItem) {
2021-11-03 01:10:49 +05:30
shareAction(url)
2022-08-24 02:44:13 +05:30
} else {
navigation.presentAlert(
title: "Could not create share link",
message: "For custom locations you can configure Frontend URL in Locations settings"
)
2021-11-03 01:10:49 +05:30
}
2022-08-24 02:44:13 +05:30
}
2021-11-03 01:10:49 +05:30
2022-08-24 02:44:13 +05:30
if contentItemIsPlayerCurrentVideo {
Button(labelForShareURL(accounts.app.name, withTime: true)) {
shareAction(
player.playerAPI.shareURL(
contentItem,
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)
}
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
}
}
private var contentItemIsPlayerCurrentVideo: Bool {
2022-06-30 04:19:35 +05:30
contentItem.contentType == .video && contentItem.video?.videoID == player.currentVideo?.videoID
}
2022-11-11 02:28:45 +05:30
@ViewBuilder private var remoteURLAction: some View {
if let url = contentItem.video.localStream?.localURL {
Button(labelForShareURL()) {
shareAction(url)
}
}
}
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()
navigation.shareURL = url
navigation.presentingShareSheet = true
2021-11-03 01:10:49 +05:30
#endif
}
2022-11-11 02:28:45 +05:30
private func labelForShareURL(_ app: String? = nil, withTime: Bool = false) -> String {
2022-09-04 20:58:30 +05:30
if withTime {
#if os(macOS)
2022-11-11 02:28:45 +05:30
return String(format: "Copy %@ link with time".localized(), app ?? "")
2022-09-04 20:58:30 +05:30
#else
2022-11-11 02:28:45 +05:30
return String(format: "Share %@ link with time".localized(), app ?? "")
2022-09-04 20:58:30 +05:30
#endif
} else {
#if os(macOS)
2022-11-11 02:28:45 +05:30
return String(format: "Copy%@link".localized(), app == nil ? " " : " \(app!) ")
2022-09-04 20:58:30 +05:30
#else
2022-11-11 02:28:45 +05:30
return String(format: "Share%@link".localized(), app == nil ? " " : " \(app!) ")
2022-09-04 20:58:30 +05:30
#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(
contentItem: ContentItem(video: Video.fixture)
2021-11-03 01:10:49 +05:30
)
.injectFixtureEnvironmentObjects()
2021-10-27 04:29:59 +05:30
}
}