yattee/Shared/Player/AppleAVPlayerViewController.swift

144 lines
4.8 KiB
Swift
Raw Normal View History

2022-02-16 20:23:11 +00:00
import AVKit
import Defaults
import SwiftUI
final class AppleAVPlayerViewController: UIViewController {
var playerLoaded = false
2022-08-20 21:05:40 +00:00
var accountsModel: AccountsModel!
2022-02-16 20:23:11 +00:00
var commentsModel: CommentsModel!
var navigationModel: NavigationModel!
var playerModel: PlayerModel!
2022-08-20 21:05:40 +00:00
var playlistsModel: PlaylistsModel!
2022-02-16 20:23:11 +00:00
var subscriptionsModel: SubscriptionsModel!
var playerView = AVPlayerViewController()
let persistenceController = PersistenceController.shared
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
loadPlayer()
2022-08-20 21:05:40 +00:00
if playerModel.presentingPlayer, !playerView.isBeingPresented, !playerView.isBeingDismissed {
present(playerView, animated: false)
}
2022-02-16 20:23:11 +00:00
}
2022-08-20 21:05:40 +00:00
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
2022-02-16 20:23:11 +00:00
2022-08-20 21:05:40 +00:00
if !playerModel.presentingPlayer, !Defaults[.pauseOnHidingPlayer], !playerModel.isPlaying {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
self?.playerModel.play()
2022-02-16 20:23:11 +00:00
}
}
2022-08-20 21:05:40 +00:00
}
2022-02-16 20:23:11 +00:00
func loadPlayer() {
guard !playerLoaded else {
return
}
playerView.player = playerModel.avPlayerBackend.avPlayer
playerView.allowsPictureInPicturePlayback = true
2022-08-20 21:05:40 +00:00
playerView.showsPlaybackControls = true
2022-02-16 20:23:11 +00:00
playerView.delegate = self
2022-08-20 21:05:40 +00:00
var infoViewControllers = [UIHostingController<AnyView>]()
infoViewControllers.append(infoViewController([.chapters], title: "Chapters"))
infoViewControllers.append(infoViewController([.comments], title: "Comments"))
2022-02-16 20:23:11 +00:00
2022-08-20 21:05:40 +00:00
var queueSections = [NowPlayingView.ViewSection.playingNext]
if Defaults[.showHistoryInPlayer] {
queueSections.append(.playedPreviously)
}
2022-02-16 20:23:11 +00:00
2022-08-20 21:05:40 +00:00
infoViewControllers.append(contentsOf: [
infoViewController([.related], title: "Related"),
infoViewController(queueSections, title: "Queue")
])
2022-02-16 20:23:11 +00:00
2022-08-20 21:05:40 +00:00
playerView.customInfoViewControllers = infoViewControllers
2022-02-16 20:23:11 +00:00
}
2022-08-20 21:05:40 +00:00
func infoViewController(
_ sections: [NowPlayingView.ViewSection],
title: String
) -> UIHostingController<AnyView> {
let controller = UIHostingController(rootView:
AnyView(
NowPlayingView(sections: sections, inInfoViewController: true)
.frame(maxHeight: 600)
.environmentObject(accountsModel)
.environmentObject(commentsModel)
2022-08-23 15:35:39 +00:00
.environmentObject(navigationModel)
2022-08-20 21:05:40 +00:00
.environmentObject(playerModel)
.environmentObject(playlistsModel)
.environmentObject(subscriptionsModel)
.environment(\.managedObjectContext, persistenceController.container.viewContext)
2022-02-16 20:23:11 +00:00
)
2022-08-20 21:05:40 +00:00
)
2022-02-16 20:23:11 +00:00
2022-08-20 21:05:40 +00:00
controller.title = title
2022-02-16 20:23:11 +00:00
2022-08-20 21:05:40 +00:00
return controller
}
2022-02-16 20:23:11 +00:00
}
extension AppleAVPlayerViewController: AVPlayerViewControllerDelegate {
func playerViewControllerShouldDismiss(_: AVPlayerViewController) -> Bool {
2022-08-20 21:05:40 +00:00
true
2022-02-16 20:23:11 +00:00
}
func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_: AVPlayerViewController) -> Bool {
2022-08-20 21:05:40 +00:00
true
2022-02-16 20:23:11 +00:00
}
func playerViewControllerWillBeginDismissalTransition(_: AVPlayerViewController) {
if Defaults[.pauseOnHidingPlayer] {
playerModel.pause()
}
dismiss(animated: false)
}
func playerViewControllerDidEndDismissalTransition(_: AVPlayerViewController) {}
func playerViewController(
_: AVPlayerViewController,
2022-07-09 22:29:13 +00:00
willBeginFullScreenPresentationWithAnimationCoordinator _: UIViewControllerTransitionCoordinator
) {}
2022-02-16 20:23:11 +00:00
func playerViewController(
_: AVPlayerViewController,
2022-07-09 22:29:13 +00:00
willEndFullScreenPresentationWithAnimationCoordinator _: UIViewControllerTransitionCoordinator
) {}
2022-02-16 20:23:11 +00:00
func playerViewController(
_: AVPlayerViewController,
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void
) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.playerModel.show()
2022-05-29 18:26:56 +00:00
self.playerModel.setNeedsDrawing(true)
2022-02-16 20:23:11 +00:00
#if os(tvOS)
if self.playerModel.playingInPictureInPicture {
self.present(self.playerView, animated: false) {
completionHandler(true)
}
}
#else
completionHandler(true)
#endif
}
}
func playerViewControllerWillStartPictureInPicture(_: AVPlayerViewController) {
playerModel.playingInPictureInPicture = true
}
func playerViewControllerWillStopPictureInPicture(_: AVPlayerViewController) {
playerModel.playingInPictureInPicture = false
}
}