yattee/Shared/Player/PlayerViewController.swift

140 lines
4.4 KiB
Swift
Raw Normal View History

2021-07-18 22:32:46 +00:00
import AVKit
import Logging
import SwiftUI
final class PlayerViewController: UIViewController {
var playerLoaded = false
2021-10-28 17:14:55 +00:00
var navigationModel: NavigationModel!
2021-09-25 08:18:22 +00:00
var playerModel: PlayerModel!
2021-07-18 22:32:46 +00:00
var playerViewController = AVPlayerViewController()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
2021-08-17 22:00:53 +00:00
loadPlayer()
2021-10-28 17:14:55 +00:00
#if os(tvOS)
if !playerViewController.isBeingPresented, !playerViewController.isBeingDismissed {
present(playerViewController, animated: false)
}
#endif
2021-07-18 22:32:46 +00:00
}
func loadPlayer() {
2021-08-17 22:00:53 +00:00
guard !playerLoaded else {
return
}
playerModel.controller = self
2021-09-25 08:18:22 +00:00
playerViewController.player = playerModel.player
playerViewController.allowsPictureInPicturePlayback = true
playerViewController.delegate = self
2021-07-18 22:32:46 +00:00
#if os(tvOS)
playerModel.avPlayerViewController = playerViewController
2021-11-02 23:02:02 +00:00
playerViewController.customInfoViewControllers = [
infoViewController([.related], title: "Related"),
infoViewController([.playingNext, .playedPreviously], title: "Playing Next")
]
2021-07-18 22:32:46 +00:00
#else
2021-08-17 22:00:53 +00:00
embedViewController()
#endif
}
#if os(tvOS)
2021-11-02 23:02:02 +00:00
func infoViewController(
_ sections: [NowPlayingView.ViewSection],
title: String
) -> UIHostingController<AnyView> {
let controller = UIHostingController(rootView:
AnyView(
2021-11-02 23:02:02 +00:00
NowPlayingView(sections: sections, inInfoViewController: true)
2021-10-13 22:05:19 +00:00
.frame(maxHeight: 600)
.environmentObject(playerModel)
)
)
2021-11-02 23:02:02 +00:00
controller.title = title
return controller
}
#else
2021-08-17 22:00:53 +00:00
func embedViewController() {
2021-07-18 22:32:46 +00:00
playerViewController.view.frame = view.bounds
addChild(playerViewController)
view.addSubview(playerViewController.view)
playerViewController.didMove(toParent: self)
2021-08-17 22:00:53 +00:00
}
#endif
2021-07-18 22:32:46 +00:00
}
extension PlayerViewController: AVPlayerViewControllerDelegate {
func playerViewControllerShouldDismiss(_: AVPlayerViewController) -> Bool {
true
}
2021-08-17 22:00:53 +00:00
func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_: AVPlayerViewController) -> Bool {
2021-10-28 17:14:55 +00:00
true
2021-08-17 22:00:53 +00:00
}
func playerViewControllerWillBeginDismissalTransition(_: AVPlayerViewController) {}
2021-07-29 22:28:28 +00:00
func playerViewControllerDidEndDismissalTransition(_: AVPlayerViewController) {
2021-07-18 22:32:46 +00:00
dismiss(animated: false)
}
func playerViewController(
_: AVPlayerViewController,
willBeginFullScreenPresentationWithAnimationCoordinator _: UIViewControllerTransitionCoordinator
) {}
2021-07-18 22:32:46 +00:00
func playerViewController(
_: AVPlayerViewController,
2021-07-29 22:28:28 +00:00
willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator
2021-07-18 22:32:46 +00:00
) {
2021-07-29 22:28:28 +00:00
coordinator.animate(alongsideTransition: nil) { context in
if !context.isCancelled {
2021-08-22 19:13:33 +00:00
#if os(iOS)
if self.traitCollection.verticalSizeClass == .compact {
self.dismiss(animated: true)
}
#endif
2021-07-29 22:28:28 +00:00
}
}
2021-07-18 22:32:46 +00:00
}
2021-08-17 22:00:53 +00:00
2021-10-28 17:14:55 +00:00
func playerViewController(
_ playerViewController: AVPlayerViewController,
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void
) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
if self.navigationModel.presentingChannel {
self.playerModel.playerNavigationLinkActive = true
} else {
self.playerModel.presentPlayer()
}
#if os(tvOS)
if self.playerModel.playingInPictureInPicture {
2021-10-28 17:14:55 +00:00
self.present(playerViewController, animated: false) {
completionHandler(true)
}
}
#else
completionHandler(true)
#endif
}
}
func playerViewControllerWillStartPictureInPicture(_: AVPlayerViewController) {
playerModel.playingInPictureInPicture = true
playerModel.playerNavigationLinkActive = false
}
2021-08-17 22:00:53 +00:00
2021-10-28 17:14:55 +00:00
func playerViewControllerWillStopPictureInPicture(_: AVPlayerViewController) {
playerModel.playingInPictureInPicture = false
}
2021-07-18 22:32:46 +00:00
}