yattee/Shared/Views/ControlsBar.swift

341 lines
13 KiB
Swift
Raw Normal View History

import Defaults
import SDWebImageSwiftUI
import SwiftUI
struct ControlsBar: View {
enum ExpansionState {
case mini
case full
}
2022-06-30 08:05:32 +00:00
@Binding var fullScreen: Bool
2022-06-30 08:05:32 +00:00
@State private var presentingShareSheet = false
@State private var shareURL: URL?
@Binding var expansionState: ExpansionState
2022-09-02 13:11:20 +00:00
2022-06-24 23:39:29 +00:00
var presentingControls = true
var backgroundEnabled = true
var borderTop = true
var borderBottom = true
var detailsTogglePlayer = true
2022-06-25 16:33:35 +00:00
var detailsToggleFullScreen = false
var playerBar = false
2022-07-10 17:51:46 +00:00
var titleLineLimit = 2
2022-06-25 16:33:35 +00:00
@ObservedObject private var accounts = AccountsModel.shared
@ObservedObject private var model = PlayerModel.shared
@ObservedObject private var playlists = PlaylistsModel.shared
@ObservedObject private var subscriptions = SubscribedChannelsModel.shared
@ObservedObject private var controls = PlayerControlsModel.shared
@Environment(\.navigationStyle) private var navigationStyle
private let navigation = NavigationModel.shared
2022-09-01 23:05:31 +00:00
private let controlsOverlayModel = ControlOverlaysModel.shared
@Default(.playerButtonShowsControlButtonsWhenMinimized) private var controlsWhenMinimized
@Default(.playerButtonSingleTapGesture) private var playerButtonSingleTapGesture
@Default(.playerButtonDoubleTapGesture) private var playerButtonDoubleTapGesture
var body: some View {
2022-06-24 23:39:29 +00:00
HStack(spacing: 0) {
detailsButton
if presentingControls, expansionState == .full || (controlsWhenMinimized && model.currentItem != nil) {
if expansionState == .full {
Spacer()
}
2022-09-02 13:11:20 +00:00
controlsView
2022-06-24 23:39:29 +00:00
.frame(maxWidth: 120)
}
}
.buttonStyle(.plain)
.labelStyle(.iconOnly)
.padding(.horizontal, 10)
.padding(.vertical, 2)
.frame(maxHeight: barHeight)
.padding(.trailing, expansionState == .mini && !controlsWhenMinimized ? 8 : 0)
.modifier(ControlBackgroundModifier(enabled: backgroundEnabled))
.clipShape(RoundedRectangle(cornerRadius: expansionState == .full || !playerBar ? 0 : 6))
.overlay(
RoundedRectangle(cornerRadius: expansionState == .full || !playerBar ? 0 : 6)
2022-12-18 12:11:06 +00:00
.stroke(Color("ControlsBorderColor"), lineWidth: 0.5)
)
2022-06-24 23:21:05 +00:00
#if os(iOS)
.background(
EmptyView().sheet(isPresented: $presentingShareSheet) {
if let shareURL {
ShareSheet(activityItems: [shareURL])
2022-06-24 23:21:05 +00:00
}
}
)
2022-06-24 23:21:05 +00:00
#endif
}
2022-06-24 23:39:29 +00:00
@ViewBuilder var detailsButton: some View {
if detailsTogglePlayer {
Button {
model.togglePlayer()
} label: {
details
.contentShape(Rectangle())
}
2022-06-25 16:33:35 +00:00
} else if detailsToggleFullScreen {
Button {
2022-11-17 21:47:45 +00:00
controlsOverlayModel.hide()
2022-09-02 13:11:20 +00:00
controls.presentingControls = false
2022-06-25 16:33:35 +00:00
withAnimation {
fullScreen.toggle()
}
} label: {
details
.contentShape(Rectangle())
}
2022-07-10 17:51:46 +00:00
#if !os(tvOS)
2022-06-25 16:33:35 +00:00
.keyboardShortcut("t")
2022-07-10 17:51:46 +00:00
#endif
2022-06-24 23:39:29 +00:00
} else {
details
}
}
2022-09-02 13:11:20 +00:00
var controlsView: some View {
HStack(spacing: 4) {
Group {
2022-09-02 13:11:20 +00:00
if controls.isPlaying {
Button(action: {
model.pause()
}) {
Label("Pause", systemImage: "pause.fill")
2022-06-24 23:39:29 +00:00
.padding(.vertical, 10)
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
}
} else {
Button(action: {
model.play()
}) {
Label("Play", systemImage: "play.fill")
2022-06-24 23:39:29 +00:00
.padding(.vertical, 10)
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
}
}
}
2022-09-02 13:11:20 +00:00
.disabled(controls.isLoadingVideo || model.currentItem.isNil)
Button(action: { model.advanceToNextItem() }) {
Label("Next", systemImage: "forward.fill")
2022-06-24 23:39:29 +00:00
.padding(.vertical, 10)
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
}
2022-07-10 22:24:56 +00:00
.disabled(!model.isAdvanceToNextItemAvailable)
2022-07-01 22:52:27 +00:00
Button {
model.closeCurrentItem()
} label: {
Label("Close Video", systemImage: "xmark")
.padding(.vertical, 10)
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
}
.disabled(model.currentItem.isNil)
}
2022-07-01 22:52:27 +00:00
.imageScale(.small)
.font(.system(size: 24))
}
var barHeight: Double {
2022-06-24 23:39:29 +00:00
55
}
var details: some View {
HStack {
HStack(spacing: 8) {
if !playerBar {
Button {
2022-12-18 12:11:06 +00:00
if let video = model.videoForDisplay, !video.isLocal {
navigation.openChannel(
video.channel,
navigationStyle: navigationStyle
)
}
} label: {
ChannelAvatarView(
2022-12-18 12:11:06 +00:00
channel: model.videoForDisplay?.channel,
video: model.videoForDisplay
2022-11-13 17:52:15 +00:00
)
.frame(width: barHeight - 10, height: barHeight - 10)
2022-11-13 17:52:15 +00:00
}
.contextMenu { contextMenu }
.zIndex(3)
} else {
ChannelAvatarView(
2022-12-18 12:11:06 +00:00
channel: model.videoForDisplay?.channel,
video: model.videoForDisplay
)
#if !os(tvOS)
.highPriorityGesture(playerButtonDoubleTapGesture != .nothing ? doubleTapGesture : nil)
.gesture(playerButtonSingleTapGesture != .nothing ? singleTapGesture : nil)
#endif
.frame(width: barHeight - 10, height: barHeight - 10)
.contextMenu { contextMenu }
2022-06-24 23:21:05 +00:00
}
if expansionState == .full {
VStack(alignment: .leading, spacing: 0) {
let notPlaying = "Not Playing".localized()
2022-12-18 12:11:06 +00:00
Text(model.videoForDisplay?.displayTitle ?? notPlaying)
.font(.system(size: 14))
.fontWeight(.semibold)
2022-12-18 12:11:06 +00:00
.foregroundColor(model.videoForDisplay.isNil ? .secondary : .accentColor)
.fixedSize(horizontal: false, vertical: true)
.lineLimit(titleLineLimit)
.multilineTextAlignment(.leading)
2022-06-24 23:21:05 +00:00
2022-12-18 12:11:06 +00:00
if let video = model.videoForDisplay, !video.localStreamIsFile {
HStack(spacing: 2) {
Text(video.displayAuthor)
.font(.system(size: 12))
2022-06-24 23:39:29 +00:00
if !presentingControls && !video.isLocal {
HStack(spacing: 2) {
Image(systemName: "person.2.fill")
2022-12-18 12:11:06 +00:00
if let channel = model.videoForDisplay?.channel {
if let subscriptions = channel.subscriptionsString {
Text(subscriptions)
2022-11-10 17:11:28 +00:00
} else {
Text("1234").redacted(reason: .placeholder)
2022-06-24 23:21:05 +00:00
}
}
}
.padding(.leading, 4)
.font(.system(size: 9))
}
}
.lineLimit(1)
.foregroundColor(.secondary)
}
}
.zIndex(0)
.transition(.opacity)
if !playerBar {
Spacer()
}
}
}
.buttonStyle(.plain)
.padding(.vertical)
}
}
2022-06-24 23:39:29 +00:00
#if !os(tvOS)
var singleTapGesture: some Gesture {
TapGesture(count: 1).onEnded { gestureAction(playerButtonSingleTapGesture) }
}
var doubleTapGesture: some Gesture {
TapGesture(count: 2).onEnded { gestureAction(playerButtonDoubleTapGesture) }
}
func gestureAction(_ action: PlayerTapGestureAction) {
switch action {
case .togglePlayer:
model.togglePlayer()
case .openChannel:
2022-12-18 12:11:06 +00:00
guard let channel = model.videoForDisplay?.channel else { return }
navigation.openChannel(channel, navigationStyle: navigationStyle)
case .togglePlayerVisibility:
withAnimation(.spring(response: 0.25)) {
expansionState = expansionState == .full ? .mini : .full
}
default:
return
}
}
#endif
@ViewBuilder var contextMenu: some View {
2022-12-18 12:11:06 +00:00
if let video = model.videoForDisplay {
Group {
Section {
if accounts.app.supportsUserPlaylists && accounts.signedIn, !video.isLocal {
Section {
2022-06-24 23:39:29 +00:00
Button {
navigation.presentAddToPlaylist(video)
2022-06-24 23:39:29 +00:00
} label: {
Label("Add to Playlist...", systemImage: "text.badge.plus")
}
2022-12-18 12:11:06 +00:00
if let playlist = playlists.lastUsed, let video = model.videoForDisplay {
Button {
playlists.addVideo(playlistID: playlist.id, videoID: video.videoID)
} label: {
Label("Add to \(playlist.title)", systemImage: "text.badge.star")
}
2022-06-24 23:39:29 +00:00
}
}
}
#if !os(tvOS)
2022-12-18 12:11:06 +00:00
ShareButton(contentItem: .init(video: model.videoForDisplay))
#endif
Section {
if !video.isLocal {
Button {
navigation.openChannel(
video.channel,
navigationStyle: navigationStyle
)
} label: {
Label("\(video.author) Channel", systemImage: "rectangle.stack.fill.badge.person.crop")
}
2022-06-25 16:54:05 +00:00
if accounts.app.supportsSubscriptions, accounts.signedIn {
if subscriptions.isSubscribing(video.channel.id) {
Button {
#if os(tvOS)
subscriptions.unsubscribe(video.channel.id)
#else
navigation.presentUnsubscribeAlert(video.channel, subscriptions: subscriptions)
#endif
} label: {
Label("Unsubscribe", systemImage: "star.circle")
}
} else {
Button {
subscriptions.subscribe(video.channel.id) {
navigation.sidebarSectionChanged.toggle()
2022-07-11 17:44:25 +00:00
}
} label: {
Label("Subscribe", systemImage: "star.circle")
2022-07-11 17:44:25 +00:00
}
2022-07-01 22:52:27 +00:00
}
2022-06-25 16:54:05 +00:00
}
}
}
}
Button {
model.closeCurrentItem()
} label: {
Label("Close Video", systemImage: "xmark")
}
}
.labelStyle(.automatic)
}
}
}
struct ControlsBar_Previews: PreviewProvider {
static var previews: some View {
ControlsBar(fullScreen: .constant(false), expansionState: .constant(.full))
.injectFixtureEnvironmentObjects()
}
}