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

133 lines
4.4 KiB
Swift
Raw Normal View History

2022-02-17 01:53:11 +05:30
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 20:45:42 +05:30
var subscriptionsModel: SubscribedChannelsModel { .shared }
2022-02-17 01:53:11 +05:30
var playerView = AVPlayerViewController()
let persistenceController = PersistenceController.shared
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
loadPlayer()
2022-08-21 02:35:40 +05:30
if playerModel.presentingPlayer, !playerView.isBeingPresented, !playerView.isBeingDismissed {
present(playerView, animated: false)
}
2022-02-17 01:53:11 +05:30
}
2022-08-21 02:35:40 +05:30
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
2022-02-17 01:53:11 +05:30
2022-08-21 02:35:40 +05:30
if !playerModel.presentingPlayer, !Defaults[.pauseOnHidingPlayer], !playerModel.isPlaying {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
self?.playerModel.play()
2022-02-17 01:53:11 +05:30
}
}
2022-08-21 02:35:40 +05:30
}
2022-02-17 01:53:11 +05:30
func loadPlayer() {
guard !playerLoaded else {
return
}
playerView.player = playerModel.avPlayerBackend.avPlayer
playerView.allowsPictureInPicturePlayback = true
2022-08-21 02:35:40 +05:30
playerView.showsPlaybackControls = true
2022-02-17 01:53:11 +05:30
playerView.delegate = self
2022-08-21 02:35:40 +05:30
var infoViewControllers = [UIHostingController<AnyView>]()
infoViewControllers.append(infoViewController([.chapters], title: "Chapters"))
infoViewControllers.append(infoViewController([.comments], title: "Comments"))
2022-02-17 01:53:11 +05:30
var queueSections = [NowPlayingView.ViewSection.playingNext]
2022-02-17 01:53:11 +05:30
2022-08-21 02:35:40 +05:30
infoViewControllers.append(contentsOf: [
infoViewController([.related], title: "Related"),
infoViewController(queueSections, title: "Queue")
])
2022-02-17 01:53:11 +05:30
2022-08-21 02:35:40 +05:30
playerView.customInfoViewControllers = infoViewControllers
2022-02-17 01:53:11 +05:30
}
2022-08-21 02:35:40 +05:30
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-17 01:53:11 +05:30
)
2022-08-21 02:35:40 +05:30
)
2022-02-17 01:53:11 +05:30
2022-08-21 02:35:40 +05:30
controller.title = title
2022-02-17 01:53:11 +05:30
2022-08-21 02:35:40 +05:30
return controller
}
2022-02-17 01:53:11 +05:30
}
extension AppleAVPlayerViewController: AVPlayerViewControllerDelegate {
func playerViewControllerShouldDismiss(_: AVPlayerViewController) -> Bool {
2022-08-21 02:35:40 +05:30
true
2022-02-17 01:53:11 +05:30
}
func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_: AVPlayerViewController) -> Bool {
2022-08-21 02:35:40 +05:30
true
2022-02-17 01:53:11 +05:30
}
func playerViewControllerWillBeginDismissalTransition(_: AVPlayerViewController) {
2022-08-27 01:47:21 +05:30
if Defaults[.pauseOnHidingPlayer], !playerModel.playingInPictureInPicture {
2022-02-17 01:53:11 +05:30
playerModel.pause()
}
dismiss(animated: false)
}
func playerViewControllerDidEndDismissalTransition(_: AVPlayerViewController) {}
2023-06-08 02:49:00 +05:30
#if os(iOS)
func playerViewController(
_: AVPlayerViewController,
willBeginFullScreenPresentationWithAnimationCoordinator _: UIViewControllerTransitionCoordinator
) {}
func playerViewController(
_: AVPlayerViewController,
willEndFullScreenPresentationWithAnimationCoordinator _: UIViewControllerTransitionCoordinator
) {}
#endif
2022-02-17 01:53:11 +05:30
func playerViewController(
_: AVPlayerViewController,
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void
) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.playerModel.show()
2022-05-29 23:56:56 +05:30
self.playerModel.setNeedsDrawing(true)
2022-02-17 01:53:11 +05:30
2022-08-27 01:47:21 +05:30
if self.playerModel.playingInPictureInPicture {
self.present(self.playerView, animated: false) {
completionHandler(true)
2022-02-17 01:53:11 +05:30
}
2022-08-27 01:47:21 +05:30
}
completionHandler(true)
2022-02-17 01:53:11 +05:30
}
}
func playerViewControllerWillStartPictureInPicture(_: AVPlayerViewController) {
playerModel.playingInPictureInPicture = true
}
func playerViewControllerWillStopPictureInPicture(_: AVPlayerViewController) {
playerModel.playingInPictureInPicture = false
}
}