yattee/Shared/Views/PlayerControlsView.swift

143 lines
4.7 KiB
Swift
Raw Normal View History

import Foundation
import SwiftUI
struct PlayerControlsView<Content: View>: View {
let content: Content
@Environment(\.navigationStyle) private var navigationStyle
@EnvironmentObject<PlayerModel> private var model
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content()
}
var body: some View {
ZStack(alignment: .bottomLeading) {
content
#if !os(tvOS)
2021-11-08 16:29:35 +00:00
.frame(minHeight: 0, maxHeight: .infinity)
.padding(.bottom, 50)
#endif
#if !os(tvOS)
controls
#endif
}
}
private var controls: some View {
2021-11-28 14:37:55 +00:00
let controls = HStack {
Button(action: {
model.presentingPlayer.toggle()
}) {
HStack {
2021-10-23 11:27:41 +00:00
VStack(alignment: .leading, spacing: 3) {
Text(model.currentItem?.video?.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-12-01 22:54:09 +00:00
Text(model.currentItem?.video?.author ?? "Yattee v\(appVersion) (build \(appBuild))")
2021-10-23 11:27:41 +00:00
.fontWeight(model.currentItem.isNil ? .light : .bold)
.font(.system(size: 10))
.foregroundColor(.secondary)
2021-10-23 11:27:41 +00:00
.lineLimit(1)
}
2021-10-23 11:27:41 +00:00
Spacer()
}
.contentShape(Rectangle())
}
2021-10-23 11:27:41 +00:00
.padding(.vertical, 20)
2021-10-16 22:48:58 +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-23 11:27:41 +00:00
.font(.system(size: 30))
.frame(minWidth: 30)
2021-10-23 11:27:41 +00:00
Button(action: { model.advanceToNextItem() }) {
Label("Next", systemImage: "forward.fill")
}
.disabled(model.queue.isEmpty)
}
ProgressView(value: progressViewValue, total: progressViewTotal)
.progressViewStyle(.linear)
#if os(iOS)
2021-10-23 11:27:41 +00:00
.offset(x: 0, y: 8)
.frame(maxWidth: 60)
#else
2021-10-23 11:27:41 +00:00
.offset(x: 0, y: 15)
.frame(maxWidth: 70)
#endif
}
}
.buttonStyle(.plain)
.labelStyle(.iconOnly)
.padding(.horizontal)
2021-10-23 11:27:41 +00:00
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 55)
.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"))
#if !os(tvOS)
.onSwipeGesture(up: {
model.presentingPlayer = true
})
#endif
2021-11-28 14:37:55 +00:00
return Group {
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, *) {
controls
.background(Material.ultraThinMaterial)
} else {
controls
.background(Color.tertiaryBackground)
}
}
}
2021-11-08 20:07:36 +00:00
private var appVersion: String {
Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "unknown"
}
2021-12-01 22:54:09 +00:00
private var appBuild: String {
Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "unknown"
}
private var progressViewValue: Double {
[model.time?.seconds, model.videoDuration].compactMap { $0 }.min() ?? 0
}
private var progressViewTotal: Double {
model.playerItemDuration?.seconds ?? model.currentVideo?.length ?? progressViewValue
}
}
struct PlayerControlsView_Previews: PreviewProvider {
static var previews: some View {
PlayerControlsView {
VStack {
Spacer()
Text("Hello")
Spacer()
}
}
.injectFixtureEnvironmentObjects()
}
}