2022-06-29 21:43:39 +00:00
|
|
|
import Combine
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct TVControls: UIViewRepresentable {
|
|
|
|
var model: PlayerControlsModel!
|
2022-11-24 20:36:05 +00:00
|
|
|
var player: PlayerModel { .shared }
|
2023-05-20 22:18:10 +00:00
|
|
|
var safeArea: SafeAreaModel { .shared }
|
2022-11-24 20:36:05 +00:00
|
|
|
var thumbnails: ThumbnailsModel { .shared }
|
2022-06-29 21:43:39 +00:00
|
|
|
|
|
|
|
@State private var direction = ""
|
|
|
|
@State private var controlsArea = UIView()
|
|
|
|
|
|
|
|
func makeUIView(context: Context) -> UIView {
|
|
|
|
let leftSwipe = UISwipeGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.handleSwipe(sender:)))
|
|
|
|
leftSwipe.direction = .left
|
|
|
|
|
|
|
|
let rightSwipe = UISwipeGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.handleSwipe(sender:)))
|
|
|
|
rightSwipe.direction = .right
|
|
|
|
|
|
|
|
let upSwipe = UISwipeGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.handleSwipe(sender:)))
|
|
|
|
upSwipe.direction = .up
|
|
|
|
|
2022-08-14 22:14:28 +00:00
|
|
|
let downSwipe = UISwipeGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.handleSwipeDown(sender:)))
|
2022-06-29 21:43:39 +00:00
|
|
|
downSwipe.direction = .down
|
|
|
|
|
2022-08-28 17:18:49 +00:00
|
|
|
let tap = UITapGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.handleTap(sender:)))
|
|
|
|
|
2022-06-29 21:43:39 +00:00
|
|
|
controlsArea.addGestureRecognizer(leftSwipe)
|
|
|
|
controlsArea.addGestureRecognizer(rightSwipe)
|
|
|
|
controlsArea.addGestureRecognizer(upSwipe)
|
|
|
|
controlsArea.addGestureRecognizer(downSwipe)
|
2022-08-28 17:18:49 +00:00
|
|
|
controlsArea.addGestureRecognizer(tap)
|
2022-06-29 21:43:39 +00:00
|
|
|
|
2022-11-24 20:36:05 +00:00
|
|
|
let controls = UIHostingController(rootView: PlayerControls())
|
2022-06-29 21:43:39 +00:00
|
|
|
controls.view.frame = .init(
|
2023-05-20 22:18:10 +00:00
|
|
|
origin: .init(x: safeArea.safeArea.leading, y: safeArea.safeArea.top),
|
2022-08-31 22:41:31 +00:00
|
|
|
size: .init(
|
2023-05-20 22:18:10 +00:00
|
|
|
width: UIScreen.main.bounds.width - safeArea.horizontalInsets,
|
|
|
|
height: UIScreen.main.bounds.height - safeArea.verticalInsets
|
2022-08-31 22:41:31 +00:00
|
|
|
)
|
2022-06-29 21:43:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
controlsArea.addSubview(controls.view)
|
|
|
|
|
|
|
|
return controlsArea
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateUIView(_: UIView, context _: Context) {}
|
|
|
|
|
2023-06-17 12:09:51 +00:00
|
|
|
func makeCoordinator() -> Self.Coordinator {
|
2022-09-01 23:05:31 +00:00
|
|
|
Coordinator(controlsArea)
|
2022-06-29 21:43:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final class Coordinator: NSObject {
|
|
|
|
private let view: UIView
|
|
|
|
private let model: PlayerControlsModel
|
|
|
|
|
2022-09-01 23:05:31 +00:00
|
|
|
init(_ view: UIView) {
|
2022-06-29 21:43:39 +00:00
|
|
|
self.view = view
|
2022-09-01 23:05:31 +00:00
|
|
|
model = .shared
|
2022-06-29 21:43:39 +00:00
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(*, unavailable)
|
|
|
|
required init?(coder _: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func handleSwipe(sender: UISwipeGestureRecognizer) {
|
|
|
|
let location = sender.location(in: view)
|
|
|
|
model.reporter.send("swipe \(location)")
|
|
|
|
}
|
2022-08-14 22:14:28 +00:00
|
|
|
|
|
|
|
@objc func handleSwipeDown(sender _: UISwipeGestureRecognizer) {
|
|
|
|
model.reporter.send("swipe down")
|
|
|
|
}
|
2022-08-28 17:18:49 +00:00
|
|
|
|
|
|
|
@objc func handleTap(sender _: UITapGestureRecognizer) {
|
2022-08-29 11:55:23 +00:00
|
|
|
if !model.presentingControls {
|
2022-08-28 17:18:49 +00:00
|
|
|
model.show()
|
|
|
|
}
|
|
|
|
}
|
2022-06-29 21:43:39 +00:00
|
|
|
}
|
|
|
|
}
|