yattee/Shared/Player/AppleAVPlayerViewController.swift

133 lines
4.4 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
var accountsModel: AccountsModel { .shared }
var navigationModel: NavigationModel { .shared }
var playerModel: PlayerModel { .shared }
var playlistsModel: PlaylistsModel { .shared }
2022-12-11 15:15:42 +00:00
var subscriptionsModel: SubscribedChannelsModel { .shared }
2022-02-16 20:23:11 +00:00
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
2023-07-01 16:38:11 +00:00
let queueSections = [NowPlayingView.ViewSection.playingNext]
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)
.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) {
2022-08-26 20:17:21 +00:00
if Defaults[.pauseOnHidingPlayer], !playerModel.playingInPictureInPicture {
2022-02-16 20:23:11 +00:00
playerModel.pause()
}
dismiss(animated: false)
}
func playerViewControllerDidEndDismissalTransition(_: AVPlayerViewController) {}
2023-06-07 21:19:00 +00:00
#if os(iOS)
func playerViewController(
_: AVPlayerViewController,
willBeginFullScreenPresentationWithAnimationCoordinator _: UIViewControllerTransitionCoordinator
) {}
func playerViewController(
_: AVPlayerViewController,
willEndFullScreenPresentationWithAnimationCoordinator _: UIViewControllerTransitionCoordinator
) {}
#endif
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
2022-08-26 20:17:21 +00:00
if self.playerModel.playingInPictureInPicture {
self.present(self.playerView, animated: false) {
completionHandler(true)
2022-02-16 20:23:11 +00:00
}
2022-08-26 20:17:21 +00:00
}
completionHandler(true)
2022-02-16 20:23:11 +00:00
}
}
func playerViewControllerWillStartPictureInPicture(_: AVPlayerViewController) {
playerModel.playingInPictureInPicture = true
}
func playerViewControllerWillStopPictureInPicture(_: AVPlayerViewController) {
playerModel.playingInPictureInPicture = false
}
}