yattee/Shared/Player/StreamControl.swift

82 lines
2.7 KiB
Swift
Raw Normal View History

2022-02-16 20:23:11 +00:00
import SwiftUI
struct StreamControl: View {
2022-09-02 00:06:33 +00:00
#if os(tvOS)
var focusedField: FocusState<ControlsOverlay.Field?>.Binding?
2022-08-14 22:17:00 +00:00
2022-09-02 00:06:33 +00:00
init(focusedField: FocusState<ControlsOverlay.Field?>.Binding?) {
self.focusedField = focusedField
}
#endif
2022-02-16 20:23:11 +00:00
@ObservedObject private var player = PlayerModel.shared
2022-08-14 22:17:00 +00:00
2022-02-16 20:23:11 +00:00
var body: some View {
Group {
2022-08-29 13:20:47 +00:00
#if !os(tvOS)
2022-02-16 20:23:11 +00:00
Picker("", selection: $player.streamSelection) {
2022-08-29 13:20:47 +00:00
if !availableStreamsByKind.values.isEmpty {
let kinds = Array(availableStreamsByKind.keys).sorted { $0 < $1 }
2022-02-16 20:23:11 +00:00
2022-08-29 13:20:47 +00:00
ForEach(kinds, id: \.self) { key in
ForEach(availableStreamsByKind[key] ?? []) { stream in
Text(stream.description).tag(Stream?.some(stream))
2022-02-16 20:23:11 +00:00
}
2022-08-29 13:20:47 +00:00
#if os(macOS)
if kinds.count > 1 {
Divider()
2022-02-16 20:23:11 +00:00
}
2022-08-29 13:20:47 +00:00
#endif
2022-02-16 20:23:11 +00:00
}
}
}
.disabled(player.isLoadingAvailableStreams)
2022-08-29 13:20:47 +00:00
#if os(iOS)
.frame(minWidth: 110)
.fixedSize(horizontal: true, vertical: true)
.disabled(player.isLoadingAvailableStreams)
#endif
2022-08-14 17:06:22 +00:00
#else
2022-09-02 00:06:33 +00:00
ControlsOverlayButton(focusedField: focusedField!, field: .stream) {
2022-08-14 17:06:22 +00:00
Text(player.streamSelection?.shortQuality ?? "loading")
.frame(maxWidth: 320)
}
.contextMenu {
2022-08-20 21:05:40 +00:00
ForEach(streams) { stream in
2022-08-14 17:06:22 +00:00
Button(stream.description) { player.streamSelection = stream }
}
Button("Close", role: .cancel) {}
}
2022-02-16 20:23:11 +00:00
#endif
}
.transaction { t in t.animation = .none }
.onChange(of: player.streamSelection) { selection in
2022-09-28 14:27:01 +00:00
guard let selection else { return }
2022-08-14 17:06:22 +00:00
player.upgradeToStream(selection)
player.controls.hideOverlays()
2022-02-16 20:23:11 +00:00
}
.frame(alignment: .trailing)
}
2022-08-29 13:20:47 +00:00
private var availableStreamsByKind: [Stream.Kind: [Stream]] {
Dictionary(grouping: streams, by: \.kind!)
2022-02-16 20:23:11 +00:00
}
2022-08-20 21:05:40 +00:00
var streams: [Stream] {
player.availableStreamsSorted.filter { player.backend.canPlay($0) }
}
2022-02-16 20:23:11 +00:00
}
struct StreamControl_Previews: PreviewProvider {
static var previews: some View {
2022-09-02 00:06:33 +00:00
#if os(tvOS)
StreamControl(focusedField: .none)
.injectFixtureEnvironmentObjects()
#else
StreamControl()
#endif
2022-02-16 20:23:11 +00:00
}
}