yattee/Shared/Player/Video Details/VideoActions.swift

117 lines
4.2 KiB
Swift
Raw Normal View History

2022-11-13 17:52:15 +00:00
import Defaults
import SwiftUI
struct VideoActions: View {
@ObservedObject private var accounts = AccountsModel.shared
var navigation = NavigationModel.shared
2022-12-11 15:15:42 +00:00
@ObservedObject private var subscriptions = SubscribedChannelsModel.shared
@ObservedObject private var player = PlayerModel.shared
2022-11-13 17:52:15 +00:00
var video: Video?
2022-12-18 18:39:03 +00:00
@Default(.openWatchNextOnClose) private var openWatchNextOnClose
@Default(.playerActionsButtonLabelStyle) private var playerActionsButtonLabelStyle
2022-11-13 17:52:15 +00:00
var body: some View {
2022-12-18 18:39:03 +00:00
HStack(spacing: 6) {
2022-11-13 17:52:15 +00:00
if let video {
2022-11-13 22:40:18 +00:00
#if !os(tvOS)
2022-12-18 18:39:03 +00:00
if video.isShareable {
2022-11-13 22:40:18 +00:00
ShareButton(contentItem: .init(video: video)) {
actionButton("Share", systemImage: "square.and.arrow.up")
}
2022-11-13 17:52:15 +00:00
2022-11-13 22:40:18 +00:00
Spacer()
}
#endif
2022-11-13 20:55:19 +00:00
if !video.isLocal {
if accounts.signedIn, accounts.app.supportsUserPlaylists {
actionButton("Add", systemImage: "text.badge.plus") {
navigation.presentAddToPlaylist(video)
2022-11-13 17:52:15 +00:00
}
2022-11-13 20:55:19 +00:00
Spacer()
}
if accounts.signedIn, accounts.app.supportsSubscriptions {
if subscriptions.isSubscribing(video.channel.id) {
actionButton("Unsubscribe", systemImage: "xmark.circle") {
#if os(tvOS)
subscriptions.unsubscribe(video.channel.id)
#else
navigation.presentUnsubscribeAlert(video.channel, subscriptions: subscriptions)
#endif
}
} else {
actionButton("Subscribe", systemImage: "star.circle") {
subscriptions.subscribe(video.channel.id) {
navigation.sidebarSectionChanged.toggle()
}
2022-11-13 17:52:15 +00:00
}
}
2022-11-13 20:55:19 +00:00
Spacer()
2022-11-13 17:52:15 +00:00
}
}
2022-12-18 18:39:03 +00:00
} else {
Spacer()
2022-11-13 17:52:15 +00:00
}
2022-12-18 18:39:03 +00:00
actionButton("Next", systemImage: Constants.nextSystemImage) {
WatchNextViewModel.shared.userInteractedOpen(player.currentItem)
}
Spacer()
2022-11-13 20:55:19 +00:00
2022-11-13 17:52:15 +00:00
actionButton("Hide", systemImage: "chevron.down") {
player.hide(animate: true)
}
2022-11-13 20:55:19 +00:00
2022-12-18 12:11:06 +00:00
Spacer()
2022-12-18 18:39:03 +00:00
2022-12-18 12:11:06 +00:00
actionButton("Close", systemImage: "xmark") {
2022-12-18 18:39:03 +00:00
if openWatchNextOnClose {
player.pause()
WatchNextViewModel.shared.closed(player.currentItem)
} else {
player.closeCurrentItem()
}
2022-11-13 17:52:15 +00:00
}
}
.padding(.horizontal)
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity)
.frame(height: 50)
2022-11-18 21:27:13 +00:00
.borderBottom(height: 0.5, color: Color("ControlsBorderColor"))
2022-11-13 17:52:15 +00:00
.foregroundColor(.accentColor)
}
func actionButton(
_ name: String,
systemImage: String,
action: @escaping () -> Void = {}
) -> some View {
Button(action: action) {
VStack(spacing: 3) {
Image(systemName: systemImage)
.frame(width: 20, height: 20)
if playerActionsButtonLabelStyle.text {
2022-11-18 23:06:13 +00:00
Text(name.localized())
.foregroundColor(.secondary)
.font(.caption2)
.allowsTightening(true)
}
2022-11-13 17:52:15 +00:00
}
.padding(.horizontal, playerActionsButtonLabelStyle.text ? 6 : 12)
.padding(.vertical, playerActionsButtonLabelStyle.text ? 5 : 10)
2022-11-13 17:52:15 +00:00
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityLabel(Text(name))
}
}
struct VideoActions_Previews: PreviewProvider {
static var previews: some View {
VideoActions()
.injectFixtureEnvironmentObjects()
}
}