1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 22:30:32 +05:30
yattee/Shared/Player/Video Details/VideoActions.swift

188 lines
6.8 KiB
Swift
Raw Normal View History

2022-11-13 23:22:15 +05:30
import Defaults
import SwiftUI
struct VideoActions: View {
2022-12-19 15:59:18 +05:30
enum Action: String, CaseIterable {
case share
case addToPlaylist
case subscribe
case settings
case next
case hide
case close
}
@ObservedObject private var accounts = AccountsModel.shared
var navigation = NavigationModel.shared
2022-12-11 20:45:42 +05:30
@ObservedObject private var subscriptions = SubscribedChannelsModel.shared
@ObservedObject private var player = PlayerModel.shared
2022-11-13 23:22:15 +05:30
var video: Video?
2022-12-19 00:09:03 +05:30
@Default(.openWatchNextOnClose) private var openWatchNextOnClose
@Default(.playerActionsButtonLabelStyle) private var playerActionsButtonLabelStyle
2022-12-19 15:59:18 +05:30
@Default(.actionButtonShareEnabled) private var actionButtonShareEnabled
@Default(.actionButtonAddToPlaylistEnabled) private var actionButtonAddToPlaylistEnabled
@Default(.actionButtonSubscribeEnabled) private var actionButtonSubscribeEnabled
@Default(.actionButtonSettingsEnabled) private var actionButtonSettingsEnabled
@Default(.actionButtonNextEnabled) private var actionButtonNextEnabled
@Default(.actionButtonHideEnabled) private var actionButtonHideEnabled
@Default(.actionButtonCloseEnabled) private var actionButtonCloseEnabled
@Default(.actionButtonNextQueueCountEnabled) private var actionButtonNextQueueCountEnabled
2022-11-13 23:22:15 +05:30
var body: some View {
2022-12-19 00:09:03 +05:30
HStack(spacing: 6) {
2022-12-19 15:59:18 +05:30
ForEach(Action.allCases, id: \.self) { action in
actionBody(action)
.frame(maxWidth: .infinity)
}
}
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity)
.frame(height: 50)
.borderBottom(height: 0.5, color: Color("ControlsBorderColor"))
.foregroundColor(.accentColor)
}
func isVisible(_ action: Action) -> Bool {
switch action {
case .share:
return actionButtonShareEnabled
case .addToPlaylist:
return actionButtonAddToPlaylistEnabled
case .subscribe:
return actionButtonSubscribeEnabled
case .settings:
return actionButtonSettingsEnabled
case .next:
return actionButtonNextEnabled
case .hide:
return actionButtonHideEnabled
case .close:
return actionButtonCloseEnabled
}
}
2022-11-13 23:22:15 +05:30
2022-12-19 15:59:18 +05:30
func isActionable(_ action: Action) -> Bool {
switch action {
case .share:
return video?.isShareable ?? false
case .addToPlaylist:
return !(video?.isLocal ?? true)
case .subscribe:
return !(video?.isLocal ?? true) && accounts.signedIn && accounts.app.supportsSubscriptions
case .settings:
return video != nil
default:
return true
}
}
@ViewBuilder func actionBody(_ action: Action) -> some View {
if isVisible(action) {
Group {
switch action {
case .share:
2022-12-19 18:05:37 +05:30
#if os(tvOS)
EmptyView()
#else
ShareButton(contentItem: .init(video: video)) {
actionButton("Share", systemImage: "square.and.arrow.up")
}
#endif
2022-12-19 15:59:18 +05:30
case .addToPlaylist:
actionButton("Add", systemImage: "text.badge.plus") {
guard let video else { return }
navigation.presentAddToPlaylist(video)
2022-11-14 04:10:18 +05:30
}
2022-12-19 15:59:18 +05:30
case .subscribe:
if let channel = video?.channel,
subscriptions.isSubscribing(channel.id)
{
actionButton("Unsubscribe", systemImage: "xmark.circle") {
#if os(tvOS)
subscriptions.unsubscribe(channel.id)
#else
navigation.presentUnsubscribeAlert(channel, subscriptions: subscriptions)
#endif
}
} else {
actionButton("Subscribe", systemImage: "star.circle") {
guard let video else { return }
2022-11-14 02:25:19 +05:30
2022-12-19 15:59:18 +05:30
subscriptions.subscribe(video.channel.id) {
navigation.sidebarSectionChanged.toggle()
}
2022-11-13 23:22:15 +05:30
}
2022-11-14 02:25:19 +05:30
}
2022-12-19 15:59:18 +05:30
case .settings:
actionButton("Settings", systemImage: "gear") {
withAnimation(ControlOverlaysModel.animation) {
ControlOverlaysModel.shared.show()
}
}
case .next:
actionButton(nextLabel, systemImage: Constants.nextSystemImage) {
WatchNextViewModel.shared.userInteractedOpen(player.currentItem)
}
case .hide:
actionButton("Hide", systemImage: "chevron.down") {
player.hide(animate: true)
}
case .close:
actionButton("Close", systemImage: "xmark") {
2022-12-21 03:54:39 +05:30
if player.presentingPlayer, openWatchNextOnClose {
2022-12-19 15:59:18 +05:30
player.pause()
WatchNextViewModel.shared.closed(player.currentItem)
2022-11-14 02:25:19 +05:30
} else {
2022-12-19 15:59:18 +05:30
player.closeCurrentItem()
2022-11-13 23:22:15 +05:30
}
}
}
}
2022-12-19 15:59:18 +05:30
.disabled(!isActionable(action))
}
}
2022-12-19 00:09:03 +05:30
2022-12-19 15:59:18 +05:30
var nextLabel: String {
if actionButtonNextQueueCountEnabled, !player.queue.isEmpty {
return "\("Next".localized())\(player.queue.count)"
2022-11-13 23:22:15 +05:30
}
2022-12-19 15:59:18 +05:30
return "Next".localized()
2022-11-13 23:22:15 +05:30
}
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-19 04:36:13 +05:30
Text(name.localized())
.foregroundColor(.secondary)
.font(.caption2)
.allowsTightening(true)
}
2022-11-13 23:22:15 +05:30
}
.padding(.horizontal, playerActionsButtonLabelStyle.text ? 6 : 12)
.padding(.vertical, playerActionsButtonLabelStyle.text ? 5 : 10)
2022-11-13 23:22:15 +05:30
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityLabel(Text(name))
}
}
struct VideoActions_Previews: PreviewProvider {
static var previews: some View {
VideoActions()
.injectFixtureEnvironmentObjects()
}
}