2021-07-18 22:32:46 +00:00
|
|
|
import AVKit
|
2021-12-19 17:17:04 +00:00
|
|
|
import Defaults
|
2021-07-18 22:32:46 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
final class PlayerViewController: UIViewController {
|
|
|
|
var playerLoaded = false
|
2021-12-04 19:35:41 +00:00
|
|
|
var commentsModel: CommentsModel!
|
2021-10-28 17:14:55 +00:00
|
|
|
var navigationModel: NavigationModel!
|
2021-09-25 08:18:22 +00:00
|
|
|
var playerModel: PlayerModel!
|
2021-12-17 20:01:05 +00:00
|
|
|
var subscriptionsModel: SubscriptionsModel!
|
2021-12-19 17:17:04 +00:00
|
|
|
var playerView = AVPlayerViewController()
|
2021-07-18 22:32:46 +00:00
|
|
|
|
2022-01-06 16:06:03 +00:00
|
|
|
let persistenceController = PersistenceController.shared
|
|
|
|
|
2021-11-07 20:51:22 +00:00
|
|
|
#if !os(tvOS)
|
|
|
|
var aspectRatio: Double? {
|
2021-12-19 17:17:04 +00:00
|
|
|
let ratio = Double(playerView.videoBounds.width) / Double(playerView.videoBounds.height)
|
2021-11-07 17:53:00 +00:00
|
|
|
|
2021-11-07 20:51:22 +00:00
|
|
|
guard ratio.isFinite else {
|
|
|
|
return VideoPlayerView.defaultAspectRatio // swiftlint:disable:this implicit_return
|
|
|
|
}
|
2021-11-07 17:53:00 +00:00
|
|
|
|
2021-11-07 20:51:22 +00:00
|
|
|
return [ratio, 1.0].max()!
|
|
|
|
}
|
|
|
|
#endif
|
2021-11-07 17:53:00 +00:00
|
|
|
|
2021-07-18 22:32:46 +00:00
|
|
|
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)
|
2021-12-19 17:17:04 +00:00
|
|
|
if !playerView.isBeingPresented, !playerView.isBeingDismissed {
|
|
|
|
present(playerView, animated: false)
|
2021-10-28 17:14:55 +00:00
|
|
|
}
|
|
|
|
#endif
|
2021-07-18 22:32:46 +00:00
|
|
|
}
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
override func viewDidDisappear(_ animated: Bool) {
|
|
|
|
super.viewDidDisappear(animated)
|
|
|
|
|
|
|
|
if !playerModel.presentingPlayer, !Defaults[.pauseOnHidingPlayer], !playerModel.isPlaying {
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
|
|
|
|
self?.playerModel.play()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-07-18 22:32:46 +00:00
|
|
|
func loadPlayer() {
|
2021-08-17 22:00:53 +00:00
|
|
|
guard !playerLoaded else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
playerModel.controller = self
|
2021-12-19 17:17:04 +00:00
|
|
|
playerView.player = playerModel.player
|
|
|
|
playerView.allowsPictureInPicturePlayback = true
|
2021-12-24 19:21:42 +00:00
|
|
|
#if os(iOS)
|
|
|
|
if #available(iOS 14.2, *) {
|
|
|
|
playerView.canStartPictureInPictureAutomaticallyFromInline = true
|
|
|
|
}
|
|
|
|
#endif
|
2021-12-19 17:17:04 +00:00
|
|
|
playerView.delegate = self
|
2021-07-18 22:32:46 +00:00
|
|
|
|
|
|
|
#if os(tvOS)
|
2021-12-06 18:12:59 +00:00
|
|
|
var infoViewControllers = [UIHostingController<AnyView>]()
|
|
|
|
if CommentsModel.enabled {
|
|
|
|
infoViewControllers.append(infoViewController([.comments], title: "Comments"))
|
|
|
|
}
|
2022-01-02 19:36:18 +00:00
|
|
|
|
|
|
|
var queueSections = [NowPlayingView.ViewSection.playingNext]
|
|
|
|
if Defaults[.showHistoryInPlayer] {
|
|
|
|
queueSections.append(.playedPreviously)
|
|
|
|
}
|
|
|
|
|
2021-12-06 18:12:59 +00:00
|
|
|
infoViewControllers.append(contentsOf: [
|
2021-11-02 23:02:02 +00:00
|
|
|
infoViewController([.related], title: "Related"),
|
2022-01-02 19:36:18 +00:00
|
|
|
infoViewController(queueSections, title: "Queue")
|
2021-12-06 18:12:59 +00:00
|
|
|
])
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
playerView.customInfoViewControllers = infoViewControllers
|
2021-07-18 22:32:46 +00:00
|
|
|
#else
|
2021-08-17 22:00:53 +00:00
|
|
|
embedViewController()
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-08-17 22:12:59 +00:00
|
|
|
#if os(tvOS)
|
2021-11-02 23:02:02 +00:00
|
|
|
func infoViewController(
|
|
|
|
_ sections: [NowPlayingView.ViewSection],
|
|
|
|
title: String
|
|
|
|
) -> UIHostingController<AnyView> {
|
2021-10-05 20:20:09 +00:00
|
|
|
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)
|
2021-12-04 19:35:41 +00:00
|
|
|
.environmentObject(commentsModel)
|
2021-10-05 20:20:09 +00:00
|
|
|
.environmentObject(playerModel)
|
2021-12-17 20:01:05 +00:00
|
|
|
.environmentObject(subscriptionsModel)
|
2022-01-06 16:06:03 +00:00
|
|
|
.environment(\.managedObjectContext, persistenceController.container.viewContext)
|
2021-10-05 20:20:09 +00:00
|
|
|
)
|
2021-08-17 22:12:59 +00:00
|
|
|
)
|
|
|
|
|
2021-11-02 23:02:02 +00:00
|
|
|
controller.title = title
|
2021-10-05 20:20:09 +00:00
|
|
|
|
|
|
|
return controller
|
2021-08-17 22:12:59 +00:00
|
|
|
}
|
|
|
|
#else
|
2021-08-17 22:00:53 +00:00
|
|
|
func embedViewController() {
|
2021-12-19 17:17:04 +00:00
|
|
|
playerView.view.frame = view.bounds
|
2021-07-18 22:32:46 +00:00
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
addChild(playerView)
|
|
|
|
view.addSubview(playerView.view)
|
2021-07-18 22:32:46 +00:00
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
playerView.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
|
|
|
}
|
|
|
|
|
2022-01-05 16:08:48 +00:00
|
|
|
func playerViewControllerWillBeginDismissalTransition(_: AVPlayerViewController) {
|
2022-01-07 18:48:03 +00:00
|
|
|
if Defaults[.pauseOnHidingPlayer] {
|
|
|
|
playerModel.pause()
|
|
|
|
}
|
2021-07-18 22:32:46 +00:00
|
|
|
dismiss(animated: false)
|
|
|
|
}
|
|
|
|
|
2022-01-05 16:08:48 +00:00
|
|
|
func playerViewControllerDidEndDismissalTransition(_: AVPlayerViewController) {}
|
|
|
|
|
2021-07-18 22:32:46 +00:00
|
|
|
func playerViewController(
|
|
|
|
_: AVPlayerViewController,
|
2021-08-01 21:25:50 +00:00
|
|
|
willBeginFullScreenPresentationWithAnimationCoordinator _: UIViewControllerTransitionCoordinator
|
2022-01-02 19:43:30 +00:00
|
|
|
) {
|
|
|
|
playerModel.playingFullscreen = true
|
|
|
|
}
|
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
|
|
|
) {
|
2022-01-02 19:43:30 +00:00
|
|
|
let wasPlaying = playerModel.isPlaying
|
2021-07-29 22:28:28 +00:00
|
|
|
coordinator.animate(alongsideTransition: nil) { context in
|
2022-01-02 19:43:30 +00:00
|
|
|
#if os(iOS)
|
|
|
|
if wasPlaying {
|
|
|
|
self.playerModel.play()
|
|
|
|
}
|
|
|
|
#endif
|
2021-07-29 22:28:28 +00:00
|
|
|
if !context.isCancelled {
|
2021-08-22 19:13:33 +00:00
|
|
|
#if os(iOS)
|
2022-01-02 19:43:30 +00:00
|
|
|
self.playerModel.lockedOrientation = nil
|
|
|
|
if Defaults[.enterFullscreenInLandscape] {
|
|
|
|
Orientation.lockOrientation(.portrait, andRotateTo: .portrait)
|
|
|
|
}
|
|
|
|
|
|
|
|
self.playerModel.playingFullscreen = false
|
|
|
|
|
|
|
|
if wasPlaying {
|
|
|
|
self.playerModel.play()
|
2021-08-22 19:13:33 +00:00
|
|
|
}
|
|
|
|
#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(
|
2021-12-19 17:17:04 +00:00
|
|
|
_: AVPlayerViewController,
|
2021-10-28 17:14:55 +00:00
|
|
|
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void
|
|
|
|
) {
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
|
|
if self.navigationModel.presentingChannel {
|
|
|
|
self.playerModel.playerNavigationLinkActive = true
|
|
|
|
} else {
|
2021-12-19 17:17:04 +00:00
|
|
|
self.playerModel.show()
|
2021-10-28 17:14:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if os(tvOS)
|
2021-10-28 19:32:03 +00:00
|
|
|
if self.playerModel.playingInPictureInPicture {
|
2021-12-19 17:17:04 +00:00
|
|
|
self.present(self.playerView, animated: false) {
|
2021-10-28 17:14:55 +00:00
|
|
|
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
|
|
|
}
|