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