1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/Shared/Views/ControlsBar.swift

305 lines
12 KiB
Swift
Raw Normal View History

import Defaults
import SDWebImageSwiftUI
import SwiftUI
struct ControlsBar: View {
2022-06-30 13:35:32 +05:30
@Binding var fullScreen: Bool
@State private var presentingShareSheet = false
@State private var shareURL: URL?
@Environment(\.navigationStyle) private var navigationStyle
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<PlayerModel> private var model
@EnvironmentObject<PlaylistsModel> private var playlists
@EnvironmentObject<RecentsModel> private var recents
2022-06-25 04:51:05 +05:30
@EnvironmentObject<SubscriptionsModel> private var subscriptions
2022-09-02 18:41:20 +05:30
@ObservedObject private var controls = PlayerControlsModel.shared
2022-06-25 05:09:29 +05:30
var presentingControls = true
var backgroundEnabled = true
var borderTop = true
var borderBottom = true
var detailsTogglePlayer = true
2022-06-25 22:03:35 +05:30
var detailsToggleFullScreen = false
2022-07-10 23:21:46 +05:30
var titleLineLimit = 2
2022-06-25 22:03:35 +05:30
2022-09-02 04:35:31 +05:30
private let controlsOverlayModel = ControlOverlaysModel.shared
var body: some View {
2022-06-25 05:09:29 +05:30
HStack(spacing: 0) {
detailsButton
if presentingControls {
2022-09-02 18:41:20 +05:30
controlsView
2022-06-25 05:09:29 +05:30
.frame(maxWidth: 120)
}
}
.buttonStyle(.plain)
.labelStyle(.iconOnly)
.padding(.horizontal)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: barHeight)
2022-06-25 05:09:29 +05:30
.borderTop(height: borderTop ? 0.4 : 0, color: Color("ControlsBorderColor"))
.borderBottom(height: borderBottom ? 0.4 : 0, color: Color("ControlsBorderColor"))
.modifier(ControlBackgroundModifier(enabled: backgroundEnabled, edgesIgnoringSafeArea: .bottom))
2022-06-25 04:51:05 +05:30
#if os(iOS)
.background(
EmptyView().sheet(isPresented: $presentingShareSheet) {
2022-09-28 19:57:01 +05:30
if let shareURL {
2022-06-25 04:51:05 +05:30
ShareSheet(activityItems: [shareURL])
}
}
)
#endif
}
2022-06-25 05:09:29 +05:30
@ViewBuilder var detailsButton: some View {
if detailsTogglePlayer {
Button {
model.togglePlayer()
} label: {
details
.contentShape(Rectangle())
}
2022-06-25 22:03:35 +05:30
} else if detailsToggleFullScreen {
Button {
2022-09-02 04:35:31 +05:30
controlsOverlayModel.presenting = false
2022-09-02 18:41:20 +05:30
controls.presentingControls = false
2022-06-25 22:03:35 +05:30
withAnimation {
fullScreen.toggle()
}
} label: {
details
.contentShape(Rectangle())
}
2022-07-10 23:21:46 +05:30
#if !os(tvOS)
2022-06-25 22:03:35 +05:30
.keyboardShortcut("t")
2022-07-10 23:21:46 +05:30
#endif
2022-06-25 05:09:29 +05:30
} else {
details
}
}
2022-09-02 18:41:20 +05:30
var controlsView: some View {
HStack(spacing: 4) {
Group {
2022-09-02 18:41:20 +05:30
if controls.isPlaying {
Button(action: {
model.pause()
}) {
Label("Pause", systemImage: "pause.fill")
2022-06-25 05:09:29 +05:30
.padding(.vertical, 10)
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
}
} else {
Button(action: {
model.play()
}) {
Label("Play", systemImage: "play.fill")
2022-06-25 05:09:29 +05:30
.padding(.vertical, 10)
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
}
}
}
2022-09-02 18:41:20 +05:30
.disabled(controls.isLoadingVideo || model.currentItem.isNil)
Button(action: { model.advanceToNextItem() }) {
Label("Next", systemImage: "forward.fill")
2022-06-25 05:09:29 +05:30
.padding(.vertical, 10)
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
}
2022-07-11 03:54:56 +05:30
.disabled(!model.isAdvanceToNextItemAvailable)
2022-07-02 04:22:27 +05:30
Button {
model.closeCurrentItem()
} label: {
Label("Close Video", systemImage: "xmark")
.padding(.vertical, 10)
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
}
.disabled(model.currentItem.isNil)
}
2022-07-02 04:22:27 +05:30
.imageScale(.small)
.font(.system(size: 24))
}
var barHeight: Double {
2022-06-25 05:09:29 +05:30
55
}
var details: some View {
HStack {
HStack(spacing: 8) {
2022-06-25 04:51:05 +05:30
ZStack(alignment: .bottomTrailing) {
authorAvatar
if accounts.app.supportsSubscriptions,
accounts.signedIn,
let video = model.currentVideo,
subscriptions.isSubscribing(video.channel.id)
{
Image(systemName: "star.circle.fill")
2022-07-10 23:21:46 +05:30
#if !os(tvOS)
2022-06-25 04:51:05 +05:30
.background(Color.background)
2022-07-10 23:21:46 +05:30
#endif
2022-06-25 04:51:05 +05:30
.clipShape(Circle())
.foregroundColor(.secondary)
}
}
.contextMenu {
if let video = model.currentVideo {
Group {
Section {
if accounts.app.supportsUserPlaylists && accounts.signedIn {
Section {
Button {
navigation.presentAddToPlaylist(video)
} label: {
Label("Add to Playlist...", systemImage: "text.badge.plus")
}
2022-06-25 04:51:05 +05:30
if let playlist = playlists.lastUsed, let video = model.currentVideo {
Button {
2022-06-25 04:51:05 +05:30
playlists.addVideo(playlistID: playlist.id, videoID: video.videoID, navigation: navigation)
} label: {
2022-06-25 04:51:05 +05:30
Label("Add to \(playlist.title)", systemImage: "text.badge.star")
}
}
}
2022-06-25 04:51:05 +05:30
}
2022-07-10 23:21:46 +05:30
#if !os(tvOS)
ShareButton(contentItem: .init(video: model.currentVideo))
#endif
2022-06-25 05:09:29 +05:30
2022-06-25 04:51:05 +05:30
Section {
Button {
NavigationModel.openChannel(
video.channel,
player: model,
recents: recents,
2022-06-30 13:35:32 +05:30
navigation: navigation,
navigationStyle: navigationStyle
2022-06-25 04:51:05 +05:30
)
} label: {
Label("\(video.author) Channel", systemImage: "rectangle.stack.fill.badge.person.crop")
}
2022-06-25 04:51:05 +05:30
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: "xmark.circle")
}
} else {
Button {
subscriptions.subscribe(video.channel.id) {
navigation.sidebarSectionChanged.toggle()
}
} label: {
Label("Subscribe", systemImage: "star.circle")
}
}
}
}
}
2022-06-25 05:09:29 +05:30
Button {
model.closeCurrentItem()
} label: {
Label("Close Video", systemImage: "xmark")
}
}
2022-06-25 04:51:05 +05:30
.labelStyle(.automatic)
}
2022-06-25 04:51:05 +05:30
}
2022-07-10 23:21:46 +05:30
VStack(alignment: .leading, spacing: 0) {
2022-09-04 20:58:30 +05:30
let notPlaying = "Not Playing".localized()
Text(model.currentVideo?.title ?? notPlaying)
.font(.system(size: 14))
.fontWeight(.semibold)
.foregroundColor(model.currentVideo.isNil ? .secondary : .accentColor)
2022-07-10 23:21:46 +05:30
.fixedSize(horizontal: false, vertical: true)
.lineLimit(titleLineLimit)
.multilineTextAlignment(.leading)
2022-07-02 04:22:27 +05:30
if let video = model.currentVideo {
HStack(spacing: 2) {
Text(video.author)
.font(.system(size: 12))
2022-06-25 22:24:05 +05:30
2022-07-11 23:14:25 +05:30
if !presentingControls {
2022-07-02 04:22:27 +05:30
HStack(spacing: 2) {
Image(systemName: "person.2.fill")
2022-06-25 22:24:05 +05:30
2022-07-11 23:14:25 +05:30
if let channel = model.currentVideo?.channel {
if let subscriptions = channel.subscriptionsString {
Text(subscriptions)
} else {
Text("1234").redacted(reason: .placeholder)
}
}
2022-07-02 04:22:27 +05:30
}
.padding(.leading, 4)
.font(.system(size: 9))
2022-06-25 22:24:05 +05:30
}
}
2022-07-02 04:22:27 +05:30
.lineLimit(1)
.foregroundColor(.secondary)
2022-06-25 22:24:05 +05:30
}
}
}
.buttonStyle(.plain)
.padding(.vertical)
Spacer()
}
}
private var authorAvatar: some View {
2022-06-25 05:09:29 +05:30
Group {
if let video = model.currentItem?.video, let url = video.channel.thumbnailURL {
2022-09-12 01:03:08 +05:30
WebImage(url: url)
.resizable()
.placeholder {
Rectangle().fill(Color("PlaceholderColor"))
}
2022-09-12 01:03:08 +05:30
.retryOnAppear(true)
.indicator(.activity)
} else {
2022-06-25 05:09:29 +05:30
ZStack {
2022-06-25 22:24:05 +05:30
Color(white: 0.6)
2022-06-25 05:09:29 +05:30
.opacity(0.5)
Image(systemName: "play.rectangle")
.foregroundColor(.accentColor)
.font(.system(size: 20))
.contentShape(Rectangle())
}
}
}
.frame(width: 44, height: 44, alignment: .leading)
.clipShape(Circle())
}
}
struct ControlsBar_Previews: PreviewProvider {
static var previews: some View {
2022-06-25 22:03:35 +05:30
ControlsBar(fullScreen: .constant(false))
.injectFixtureEnvironmentObjects()
}
}