yattee/Shared/Player/Player.swift

57 lines
1.7 KiB
Swift
Raw Normal View History

2021-09-25 08:18:22 +00:00
import Defaults
2021-07-18 22:32:46 +00:00
import SwiftUI
struct Player: UIViewControllerRepresentable {
@EnvironmentObject<PlayerModel> private var player
2021-08-23 21:31:51 +00:00
var controller: PlayerViewController?
init(controller: PlayerViewController? = nil) {
self.controller = controller
}
2021-07-18 22:32:46 +00:00
func makeUIViewController(context _: Context) -> PlayerViewController {
if self.controller != nil {
return self.controller!
}
2021-07-18 22:32:46 +00:00
let controller = PlayerViewController()
2021-08-22 19:13:33 +00:00
controller.playerModel = player
2021-10-16 22:48:58 +00:00
player.controller = controller
2021-07-18 22:32:46 +00:00
2021-10-17 21:49:56 +00:00
#if os(tvOS)
player.controller?.playerViewController.transportBarCustomMenuItems = [streamingQualityMenu]
#endif
2021-07-18 22:32:46 +00:00
return controller
}
2021-10-17 21:49:56 +00:00
func updateUIViewController(_: PlayerViewController, context _: Context) {
#if os(tvOS)
player.controller?.playerViewController.transportBarCustomMenuItems = [streamingQualityMenu]
#endif
}
#if os(tvOS)
var streamingQualityMenu: UIMenu {
UIMenu(
title: "Streaming quality",
image: UIImage(systemName: "antenna.radiowaves.left.and.right"),
children: streamingQualityMenuActions
)
}
var streamingQualityMenuActions: [UIAction] {
player.availableStreamsSorted.map { stream in
let image = player.streamSelection == stream ? UIImage(systemName: "checkmark") : nil
return UIAction(title: stream.description, image: image) { _ in
self.player.streamSelection = stream
self.player.upgradeToStream(stream)
}
}
}
#endif
2021-07-18 22:32:46 +00:00
}