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

148 lines
4.9 KiB
Swift
Raw Normal View History

import Foundation
import SwiftUI
2022-02-17 01:53:11 +05:30
struct BrowserPlayerControls<Content: View, Toolbar: View>: View {
let content: Content
2022-02-04 23:08:29 +05:30
let toolbar: Toolbar?
@Environment(\.navigationStyle) private var navigationStyle
2022-02-17 01:53:11 +05:30
@EnvironmentObject<PlayerControlsModel> private var playerControls
@EnvironmentObject<PlayerModel> private var model
2022-02-04 23:08:29 +05:30
init(@ViewBuilder toolbar: @escaping () -> Toolbar? = { nil }, @ViewBuilder content: @escaping () -> Content) {
self.content = content()
2022-02-04 23:08:29 +05:30
self.toolbar = toolbar()
}
init(@ViewBuilder content: @escaping () -> Content) where Toolbar == EmptyView {
self.init(toolbar: { EmptyView() }, content: content)
}
var body: some View {
ZStack(alignment: .bottomLeading) {
content
#if !os(tvOS)
2021-11-08 21:59:35 +05:30
.frame(minHeight: 0, maxHeight: .infinity)
#endif
2022-02-04 23:08:29 +05:30
Group {
#if !os(tvOS)
#if !os(macOS)
toolbar
.frame(height: 100)
.offset(x: 0, y: -28)
#endif
controls
#endif
}
.borderTop(height: 0.4, color: Color("ControlsBorderColor"))
#if os(macOS)
.background(VisualEffectBlur(material: .sidebar))
#elseif os(iOS)
.background(VisualEffectBlur(blurStyle: .systemThinMaterial).edgesIgnoringSafeArea(.all))
#endif
}
}
private var controls: some View {
2022-02-04 23:08:29 +05:30
HStack {
Button(action: {
model.togglePlayer()
}) {
HStack {
2021-10-23 16:57:41 +05:30
VStack(alignment: .leading, spacing: 3) {
Text(model.currentVideo?.title ?? "Not playing")
2021-10-23 16:57:41 +05:30
.font(.system(size: 14).bold())
.foregroundColor(model.currentItem.isNil ? .secondary : .accentColor)
.lineLimit(1)
if let video = model.currentVideo {
Text(video.author)
.fontWeight(.bold)
.font(.system(size: 10))
.foregroundColor(.secondary)
.lineLimit(1)
}
}
2021-10-23 16:57:41 +05:30
Spacer()
}
2022-02-04 23:08:29 +05:30
.padding(.vertical)
.contentShape(Rectangle())
}
2021-10-23 16:57:41 +05:30
.padding(.vertical, 20)
2021-10-17 04:18:58 +05:30
HStack {
Group {
if !model.currentItem.isNil {
Button {
model.closeCurrentItem()
model.closePiP()
} label: {
Label("Close Video", systemImage: "xmark")
}
}
2021-10-23 16:57:41 +05:30
if playerControls.isPlaying {
Button(action: {
model.pause()
}) {
Label("Pause", systemImage: "pause.fill")
}
} else {
Button(action: {
model.play()
}) {
Label("Play", systemImage: "play.fill")
}
}
}
.disabled(playerControls.isLoadingVideo || model.currentItem.isNil)
.font(.system(size: 30))
.frame(minWidth: 30)
Button(action: { model.advanceToNextItem() }) {
Label("Next", systemImage: "forward.fill")
.padding(.vertical)
.contentShape(Rectangle())
}
.disabled(model.queue.isEmpty)
}
}
.buttonStyle(.plain)
.labelStyle(.iconOnly)
.padding(.horizontal)
2021-10-23 16:57:41 +05:30
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 55)
.padding(.vertical, 0)
2021-11-28 20:07:55 +05:30
.borderTop(height: 0.4, color: Color("ControlsBorderColor"))
.borderBottom(height: navigationStyle == .sidebar ? 0 : 0.4, color: Color("ControlsBorderColor"))
#if !os(tvOS)
.onSwipeGesture(up: {
model.show()
})
#endif
}
private var progressViewValue: Double {
[model.time?.seconds, model.videoDuration].compactMap { $0 }.min() ?? 0
}
private var progressViewTotal: Double {
model.videoDuration ?? 100
}
}
struct PlayerControlsView_Previews: PreviewProvider {
static var previews: some View {
2022-02-17 01:53:11 +05:30
BrowserPlayerControls {
VStack {
Spacer()
Text("Hello")
Spacer()
}
}
.injectFixtureEnvironmentObjects()
}
}