2021-10-05 20:20:09 +00:00
|
|
|
import Foundation
|
|
|
|
import SwiftUI
|
2022-01-06 14:58:16 +00:00
|
|
|
|
2022-02-04 17:38:29 +00:00
|
|
|
struct PlayerControlsView<Content: View, Toolbar: View>: View {
|
2021-10-05 20:20:09 +00:00
|
|
|
let content: Content
|
2022-02-04 17:38:29 +00:00
|
|
|
let toolbar: Toolbar?
|
2021-10-05 20:20:09 +00:00
|
|
|
|
|
|
|
@Environment(\.navigationStyle) private var navigationStyle
|
|
|
|
@EnvironmentObject<PlayerModel> private var model
|
|
|
|
|
2022-02-04 17:38:29 +00:00
|
|
|
init(@ViewBuilder toolbar: @escaping () -> Toolbar? = { nil }, @ViewBuilder content: @escaping () -> Content) {
|
2021-10-05 20:20:09 +00:00
|
|
|
self.content = content()
|
2022-02-04 17:38:29 +00:00
|
|
|
self.toolbar = toolbar()
|
|
|
|
}
|
|
|
|
|
|
|
|
init(@ViewBuilder content: @escaping () -> Content) where Toolbar == EmptyView {
|
|
|
|
self.init(toolbar: { EmptyView() }, content: content)
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
ZStack(alignment: .bottomLeading) {
|
|
|
|
content
|
|
|
|
#if !os(tvOS)
|
2021-11-08 16:29:35 +00:00
|
|
|
.frame(minHeight: 0, maxHeight: .infinity)
|
2021-10-05 20:20:09 +00:00
|
|
|
#endif
|
|
|
|
|
2022-02-04 17:38:29 +00:00
|
|
|
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))
|
2021-10-05 20:20:09 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var controls: some View {
|
2022-02-04 17:38:29 +00:00
|
|
|
HStack {
|
2021-10-05 20:20:09 +00:00
|
|
|
Button(action: {
|
2021-12-19 17:17:04 +00:00
|
|
|
model.togglePlayer()
|
2021-10-05 20:20:09 +00:00
|
|
|
}) {
|
|
|
|
HStack {
|
2021-10-23 11:27:41 +00:00
|
|
|
VStack(alignment: .leading, spacing: 3) {
|
2021-12-26 21:14:46 +00:00
|
|
|
Text(model.currentVideo?.title ?? "Not playing")
|
2021-10-23 11:27:41 +00:00
|
|
|
.font(.system(size: 14).bold())
|
|
|
|
.foregroundColor(model.currentItem.isNil ? .secondary : .accentColor)
|
|
|
|
.lineLimit(1)
|
2021-10-05 20:20:09 +00:00
|
|
|
|
2022-01-06 14:58:16 +00:00
|
|
|
if let video = model.currentVideo {
|
|
|
|
Text(video.author)
|
|
|
|
.fontWeight(.bold)
|
|
|
|
.font(.system(size: 10))
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.lineLimit(1)
|
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
2021-12-19 17:19:21 +00:00
|
|
|
.contextMenu {
|
|
|
|
Button {
|
|
|
|
model.closeCurrentItem()
|
|
|
|
} label: {
|
|
|
|
Label("Close Video", systemImage: "xmark.circle")
|
|
|
|
.labelStyle(.automatic)
|
|
|
|
}
|
|
|
|
.disabled(model.currentItem.isNil)
|
|
|
|
}
|
2021-10-23 11:27:41 +00:00
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
Spacer()
|
|
|
|
}
|
2022-02-04 17:38:29 +00:00
|
|
|
.padding(.vertical)
|
2021-10-05 20:20:09 +00:00
|
|
|
.contentShape(Rectangle())
|
|
|
|
}
|
2021-10-23 11:27:41 +00:00
|
|
|
.padding(.vertical, 20)
|
2021-10-16 22:48:58 +00:00
|
|
|
|
2021-10-22 20:49:31 +00:00
|
|
|
ZStack(alignment: .bottom) {
|
|
|
|
HStack {
|
|
|
|
Group {
|
|
|
|
if model.isPlaying {
|
|
|
|
Button(action: {
|
|
|
|
model.pause()
|
|
|
|
}) {
|
|
|
|
Label("Pause", systemImage: "pause.fill")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Button(action: {
|
|
|
|
model.play()
|
|
|
|
}) {
|
|
|
|
Label("Play", systemImage: "play.fill")
|
|
|
|
}
|
|
|
|
.disabled(model.player.currentItem.isNil)
|
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
2021-10-23 11:27:41 +00:00
|
|
|
.font(.system(size: 30))
|
2021-10-22 20:49:31 +00:00
|
|
|
.frame(minWidth: 30)
|
2021-10-23 11:27:41 +00:00
|
|
|
|
2021-10-22 20:49:31 +00:00
|
|
|
Button(action: { model.advanceToNextItem() }) {
|
|
|
|
Label("Next", systemImage: "forward.fill")
|
2022-02-04 17:38:29 +00:00
|
|
|
.padding(.vertical)
|
|
|
|
.contentShape(Rectangle())
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
2021-10-22 20:49:31 +00:00
|
|
|
.disabled(model.queue.isEmpty)
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 20:49:31 +00:00
|
|
|
ProgressView(value: progressViewValue, total: progressViewTotal)
|
|
|
|
.progressViewStyle(.linear)
|
|
|
|
#if os(iOS)
|
|
|
|
.frame(maxWidth: 60)
|
|
|
|
#else
|
2022-02-04 17:38:29 +00:00
|
|
|
.offset(y: 6)
|
2021-10-22 20:49:31 +00:00
|
|
|
.frame(maxWidth: 70)
|
|
|
|
#endif
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.buttonStyle(.plain)
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
.padding(.horizontal)
|
2021-10-23 11:27:41 +00:00
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 55)
|
2021-10-05 20:20:09 +00:00
|
|
|
.padding(.vertical, 0)
|
2021-11-28 14:37:55 +00:00
|
|
|
.borderTop(height: 0.4, color: Color("ControlsBorderColor"))
|
|
|
|
.borderBottom(height: navigationStyle == .sidebar ? 0 : 0.4, color: Color("ControlsBorderColor"))
|
2021-10-05 20:20:09 +00:00
|
|
|
#if !os(tvOS)
|
|
|
|
.onSwipeGesture(up: {
|
2021-12-19 17:17:04 +00:00
|
|
|
model.show()
|
2021-10-05 20:20:09 +00:00
|
|
|
})
|
|
|
|
#endif
|
|
|
|
}
|
2021-10-22 20:49:31 +00:00
|
|
|
|
|
|
|
private var progressViewValue: Double {
|
|
|
|
[model.time?.seconds, model.videoDuration].compactMap { $0 }.min() ?? 0
|
|
|
|
}
|
|
|
|
|
|
|
|
private var progressViewTotal: Double {
|
2022-01-06 16:47:07 +00:00
|
|
|
model.videoDuration ?? 100
|
2021-10-22 20:49:31 +00:00
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct PlayerControlsView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
PlayerControlsView {
|
|
|
|
VStack {
|
|
|
|
Spacer()
|
|
|
|
Text("Hello")
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.injectFixtureEnvironmentObjects()
|
|
|
|
}
|
|
|
|
}
|