1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/Shared/Player/AppleAVPlayerViewController.swift

138 lines
4.6 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
2022-08-21 02:35:40 +05:30
var accountsModel: AccountsModel!
2022-02-17 01:53:11 +05:30
var commentsModel: CommentsModel!
var navigationModel: NavigationModel!
var playerModel: PlayerModel!
2022-08-21 02:35:40 +05:30
var playlistsModel: PlaylistsModel!
2022-02-17 01:53:11 +05:30
var subscriptionsModel: SubscriptionsModel!
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
2022-08-21 02:35:40 +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)
.environmentObject(accountsModel)
.environmentObject(commentsModel)
2022-08-23 21:05:39 +05:30
.environmentObject(navigationModel)
2022-08-21 02:35:40 +05:30
.environmentObject(playerModel)
.environmentObject(playlistsModel)
.environmentObject(subscriptionsModel)
.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) {}
func playerViewController(
_: AVPlayerViewController,
2022-07-10 03:59:13 +05:30
willBeginFullScreenPresentationWithAnimationCoordinator _: UIViewControllerTransitionCoordinator
) {}
2022-02-17 01:53:11 +05:30
func playerViewController(
_: AVPlayerViewController,
2022-07-10 03:59:13 +05:30
willEndFullScreenPresentationWithAnimationCoordinator _: UIViewControllerTransitionCoordinator
) {}
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
}
}