From e3e68cd158dc567034b37d49b28bf12bc552acb0 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Sun, 29 May 2022 22:13:21 +0200 Subject: [PATCH] Minor improvements --- Model/Player/Backends/AVPlayerBackend.swift | 3 +-- Model/Player/PiPDelegate.swift | 22 +++++++++++++------- Shared/Player/VideoPlayerView.swift | 23 ++++++++++----------- Shared/Views/ChannelVideosView.swift | 2 +- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/Model/Player/Backends/AVPlayerBackend.swift b/Model/Player/Backends/AVPlayerBackend.swift index 7e0b31ba..b05d6ae9 100644 --- a/Model/Player/Backends/AVPlayerBackend.swift +++ b/Model/Player/Backends/AVPlayerBackend.swift @@ -169,8 +169,7 @@ final class AVPlayerBackend: PlayerBackend { } #else func closePiP(wasPlaying: Bool) { - controller?.playerView.player = nil - controller?.playerView.player = avPlayer + model.pipController?.stopPictureInPicture() guard wasPlaying else { return diff --git a/Model/Player/PiPDelegate.swift b/Model/Player/PiPDelegate.swift index c34ec797..60d05f8a 100644 --- a/Model/Player/PiPDelegate.swift +++ b/Model/Player/PiPDelegate.swift @@ -19,16 +19,21 @@ final class PiPDelegate: NSObject, AVPictureInPictureControllerDelegate { } func pictureInPictureControllerDidStopPictureInPicture(_: AVPictureInPictureController) { - if player?.avPlayerBackend.switchToMPVOnPipClose ?? false { - DispatchQueue.main.async { [weak player] in - player?.avPlayerBackend.switchToMPVOnPipClose = false - player?.saveTime { [weak player] in - player?.changeActiveBackend(from: .appleAVPlayer, to: .mpv) + guard let player = player else { + return + } + + if player.avPlayerBackend.switchToMPVOnPipClose, + !player.currentItem.isNil { + DispatchQueue.main.async { + player.avPlayerBackend.switchToMPVOnPipClose = false + player.saveTime { + player.changeActiveBackend(from: .appleAVPlayer, to: .mpv) } } } - player?.playingInPictureInPicture = false + player.playingInPictureInPicture = false } func pictureInPictureControllerWillStopPictureInPicture(_: AVPictureInPictureController) {} @@ -37,7 +42,10 @@ final class PiPDelegate: NSObject, AVPictureInPictureControllerDelegate { _: AVPictureInPictureController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void ) { - player?.show() + if !player.currentItem.isNil { + player?.show() + } + DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { completionHandler(true) } diff --git a/Shared/Player/VideoPlayerView.swift b/Shared/Player/VideoPlayerView.swift index 14c9825d..ecffd378 100644 --- a/Shared/Player/VideoPlayerView.swift +++ b/Shared/Player/VideoPlayerView.swift @@ -8,9 +8,7 @@ import SwiftUI struct VideoPlayerView: View { #if os(iOS) - static let hiddenPlayerOffset = max(UIScreen.main.bounds.height, UIScreen.main.bounds.width) + 100 - #else - static let hiddenPlayerOffset = 0.0 + static let hiddenOffset = max(UIScreen.main.bounds.height, UIScreen.main.bounds.width) + 100 #endif static let defaultAspectRatio = 16 / 9.0 @@ -44,7 +42,7 @@ struct VideoPlayerView: View { #endif #if !os(macOS) - @State private var playerOffset = Self.hiddenPlayerOffset + @State private var viewVerticalOffset = Self.hiddenOffset #endif @EnvironmentObject private var accounts @@ -79,7 +77,7 @@ struct VideoPlayerView: View { #endif .onChange(of: player.presentingPlayer) { newValue in if newValue { - playerOffset = 0 + viewVerticalOffset = 0 #if os(iOS) configureOrientationUpdatesBasedOnAccelerometer() #endif @@ -93,13 +91,14 @@ struct VideoPlayerView: View { motionManager?.stopAccelerometerUpdates() motionManager = nil - playerOffset = Self.hiddenPlayerOffset + viewVerticalOffset = Self.hiddenOffset #endif } } } - .offset(y: playerOffset) - .animation(.easeIn(duration: 0.2), value: playerOffset) + .offset(y: viewVerticalOffset) + .opacity(viewVerticalOffset == Self.hiddenOffset ? 0 : 1) + .animation(.easeIn(duration: 0.2), value: viewVerticalOffset) #endif } @@ -168,15 +167,15 @@ struct VideoPlayerView: View { } withAnimation(.easeInOut(duration: 0.2)) { - playerOffset = drag + viewVerticalOffset = drag } } .onEnded { _ in - if playerOffset > 100 { + if viewVerticalOffset > 100 { player.backend.setNeedsDrawing(false) player.hide() } else { - playerOffset = 0 + viewVerticalOffset = 0 player.backend.setNeedsDrawing(true) player.show() } @@ -466,7 +465,7 @@ struct VideoPlayerView: View { } private func handleOrientationDidChangeNotification() { - playerOffset = playerOffset == 0 ? 0 : Self.hiddenPlayerOffset + viewVerticalOffset = viewVerticalOffset == 0 ? 0 : Self.hiddenOffset let newOrientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation if newOrientation?.isLandscape ?? false, player.presentingPlayer, diff --git a/Shared/Views/ChannelVideosView.swift b/Shared/Views/ChannelVideosView.swift index 1d957244..92c2e032 100644 --- a/Shared/Views/ChannelVideosView.swift +++ b/Shared/Views/ChannelVideosView.swift @@ -113,7 +113,7 @@ struct ChannelVideosView: View { ToolbarItem { HStack { HStack(spacing: 3) { - Text("\(store.item?.subscriptionsString ?? "loading")") + Text("\(store.item?.subscriptionsString ?? "")") .fontWeight(.bold) Text(" subscribers") .allowsTightening(true)