1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Shared/Player/PlayerViewController.swift

211 lines
6.8 KiB
Swift
Raw Normal View History

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