1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 14:20:32 +05:30
yattee/Shared/Player/PlaybackBar.swift

154 lines
5.2 KiB
Swift
Raw Normal View History

2021-10-17 04:18:58 +05:30
import Defaults
2021-08-23 00:43:33 +05:30
import Foundation
import SwiftUI
struct PlaybackBar: View {
2021-08-24 03:01:51 +05:30
@Environment(\.dismiss) private var dismiss
@Environment(\.inNavigationView) private var inNavigationView
2021-10-17 04:18:58 +05:30
@EnvironmentObject<InstancesModel> private var instances
@EnvironmentObject<PlayerModel> private var player
2021-08-24 03:01:51 +05:30
2021-08-23 00:43:33 +05:30
var body: some View {
HStack {
closeButton
if player.currentItem != nil {
Text(playbackStatus)
.foregroundColor(.gray)
.font(.caption2)
2021-10-17 04:18:58 +05:30
Spacer()
HStack(spacing: 4) {
if player.currentVideo!.live {
Image(systemName: "dot.radiowaves.left.and.right")
} else if player.isLoadingAvailableStreams || player.isLoadingStream {
Image(systemName: "bolt.horizontal.fill")
2021-09-14 02:11:16 +05:30
}
2021-10-17 04:18:58 +05:30
streamControl
.disabled(player.isLoadingAvailableStreams)
.frame(alignment: .trailing)
.environment(\.colorScheme, .dark)
.onChange(of: player.streamSelection) { selection in
guard !selection.isNil else {
return
}
player.upgradeToStream(selection!)
}
#if os(macOS)
.frame(maxWidth: 180)
#endif
2021-08-24 03:01:51 +05:30
}
2021-10-17 04:18:58 +05:30
.transaction { t in t.animation = .none }
.foregroundColor(.gray)
.font(.caption2)
} else {
Spacer()
2021-08-24 03:01:51 +05:30
}
2021-08-23 00:43:33 +05:30
}
2021-10-17 04:18:58 +05:30
.frame(minWidth: 0, maxWidth: .infinity)
2021-08-23 00:43:33 +05:30
.padding(4)
.background(.black)
}
2021-10-17 04:18:58 +05:30
private var closeButton: some View {
Button {
dismiss()
} label: {
Label(
"Close",
systemImage: inNavigationView ? "chevron.backward.circle.fill" : "chevron.down.circle.fill"
)
.labelStyle(.iconOnly)
}
.accessibilityLabel(Text("Close"))
.buttonStyle(.borderless)
.foregroundColor(.gray)
.keyboardShortcut(.cancelAction)
2021-08-23 00:43:33 +05:30
}
2021-10-17 04:18:58 +05:30
private var playbackStatus: String {
if player.live {
return "LIVE"
}
guard player.time != nil, player.time!.isValid else {
return "loading..."
2021-08-23 00:43:33 +05:30
}
let remainingSeconds = player.currentVideo!.length - player.time!.seconds
2021-08-23 00:43:33 +05:30
2021-08-24 03:01:51 +05:30
if remainingSeconds < 60 {
return "less than a minute"
}
2021-08-23 00:43:33 +05:30
let timeFinishAt = Date.now.addingTimeInterval(remainingSeconds)
let timeFinishAtString = timeFinishAt.formatted(date: .omitted, time: .shortened)
return "ends at \(timeFinishAtString)"
2021-08-23 00:43:33 +05:30
}
2021-10-17 04:18:58 +05:30
private var streamControl: some View {
#if os(macOS)
Picker("", selection: $player.streamSelection) {
ForEach(instances.all) { instance in
let instanceStreams = availableStreamsForInstance(instance)
if !instanceStreams.values.isEmpty {
let kinds = Array(instanceStreams.keys).sorted { $0 < $1 }
Section(header: Text(instance.longDescription)) {
ForEach(kinds, id: \.self) { key in
ForEach(instanceStreams[key] ?? []) { stream in
Text(stream.quality).tag(Stream?.some(stream))
}
if kinds.count > 1 {
Divider()
}
}
}
}
}
}
#else
Menu {
ForEach(instances.all) { instance in
let instanceStreams = availableStreamsForInstance(instance)
if !instanceStreams.values.isEmpty {
let kinds = Array(instanceStreams.keys).sorted { $0 < $1 }
Picker("", selection: $player.streamSelection) {
ForEach(kinds, id: \.self) { key in
ForEach(instanceStreams[key] ?? []) { stream in
Text(stream.description).tag(Stream?.some(stream))
}
if kinds.count > 1 {
Divider()
}
}
}
}
}
} label: {
Text(player.streamSelection?.quality ?? "")
}
#endif
}
private func availableStreamsForInstance(_ instance: Instance) -> [Stream.Kind: [Stream]] {
2021-10-18 03:19:56 +05:30
let streams = player.availableStreams.filter { $0.instance == instance }.sorted(by: player.streamsSorter)
2021-10-17 04:18:58 +05:30
return Dictionary(grouping: streams, by: \.kind!)
}
2021-08-23 00:43:33 +05:30
}
2021-10-14 03:40:29 +05:30
struct PlaybackBar_Previews: PreviewProvider {
static var previews: some View {
PlaybackBar()
.injectFixtureEnvironmentObjects()
}
}