PiP and UI improvements

This commit is contained in:
Arkadiusz Fal
2021-10-28 19:14:55 +02:00
parent c387454d9a
commit 24f7c566bf
18 changed files with 169 additions and 55 deletions

View File

@@ -2,6 +2,7 @@ import Defaults
import SwiftUI
struct Player: UIViewControllerRepresentable {
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<PlayerModel> private var player
var controller: PlayerViewController?
@@ -17,6 +18,7 @@ struct Player: UIViewControllerRepresentable {
let controller = PlayerViewController()
controller.navigationModel = navigation
controller.playerModel = player
player.controller = controller

View File

@@ -4,6 +4,7 @@ import SwiftUI
final class PlayerViewController: UIViewController {
var playerLoaded = false
var navigationModel: NavigationModel!
var playerModel: PlayerModel!
var playerViewController = AVPlayerViewController()
@@ -11,6 +12,12 @@ final class PlayerViewController: UIViewController {
super.viewWillAppear(animated)
loadPlayer()
#if os(tvOS)
if !playerViewController.isBeingPresented, !playerViewController.isBeingDismissed {
present(playerViewController, animated: false)
}
#endif
}
func loadPlayer() {
@@ -26,12 +33,9 @@ final class PlayerViewController: UIViewController {
#if os(tvOS)
playerModel.avPlayerViewController = playerViewController
playerViewController.customInfoViewControllers = [playerQueueInfoViewController]
present(playerViewController, animated: false)
#else
embedViewController()
#endif
playerLoaded = true
}
#if os(tvOS)
@@ -66,7 +70,7 @@ extension PlayerViewController: AVPlayerViewControllerDelegate {
}
func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_: AVPlayerViewController) -> Bool {
false
true
}
func playerViewControllerWillBeginDismissalTransition(_: AVPlayerViewController) {}
@@ -95,7 +99,35 @@ extension PlayerViewController: AVPlayerViewControllerDelegate {
}
}
func playerViewControllerWillStartPictureInPicture(_: AVPlayerViewController) {}
func playerViewController(
_ playerViewController: AVPlayerViewController,
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void
) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
if self.navigationModel.presentingChannel {
self.playerModel.playerNavigationLinkActive = true
} else {
self.playerModel.presentPlayer()
}
func playerViewControllerWillStopPictureInPicture(_: AVPlayerViewController) {}
#if os(tvOS)
if self.playerModel.playingInPictureInPicture {
self.present(playerViewController, animated: false) {
completionHandler(true)
}
}
#else
completionHandler(true)
#endif
}
}
func playerViewControllerWillStartPictureInPicture(_: AVPlayerViewController) {
playerModel.playingInPictureInPicture = true
playerModel.playerNavigationLinkActive = false
}
func playerViewControllerWillStopPictureInPicture(_: AVPlayerViewController) {
playerModel.playingInPictureInPicture = false
}
}

View File

@@ -17,6 +17,7 @@ struct VideoDetails: View {
@State private var currentPage = Page.details
@Environment(\.dismiss) private var dismiss
@Environment(\.inNavigationView) private var inNavigationView
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<PlayerModel> private var player
@@ -89,6 +90,7 @@ struct VideoDetails: View {
.edgesIgnoringSafeArea(.horizontal)
}
}
.padding(.top, inNavigationView && fullScreen ? 10 : 0)
.onAppear {
#if !os(macOS)
if video.isNil {
@@ -298,9 +300,9 @@ struct VideoDetails: View {
}
#if os(iOS)
.sheet(isPresented: $presentingShareSheet) {
ShareSheet(activityItems: [
accounts.api.shareURL(contentItem)
])
if let url = accounts.api.shareURL(contentItem) {
ShareSheet(activityItems: [url])
}
}
#endif
}

View File

@@ -42,9 +42,9 @@ struct VideoPlayerView: View {
var content: some View {
Group {
VStack(alignment: .leading, spacing: 0) {
Group {
#if os(tvOS)
player()
player.playerView
#else
GeometryReader { geometry in
VStack(spacing: 0) {
@@ -59,7 +59,8 @@ struct VideoPlayerView: View {
if player.currentItem.isNil {
playerPlaceholder(geometry: geometry)
} else {
player(geometry: geometry)
player.playerView
.modifier(VideoPlayerSizeModifier(geometry: geometry))
}
}
#if os(iOS)
@@ -131,13 +132,6 @@ struct VideoPlayerView: View {
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: geometry.size.width / VideoPlayerView.defaultAspectRatio)
}
func player(geometry: GeometryProxy? = nil) -> some View {
Player()
#if !os(tvOS)
.modifier(VideoPlayerSizeModifier(geometry: geometry))
#endif
}
#if os(iOS)
var sidebarQueue: Bool {
horizontalSizeClass == .regular && playerSize.width > 750