From c814a7d2834aa32480686e045102329a9d602b57 Mon Sep 17 00:00:00 2001 From: gkueny Date: Sat, 23 Mar 2024 10:58:41 +0100 Subject: [PATCH] fix(ios): fix PiP callback We rename internal call back to XXXEnter & XXXExit for better understanding We do not call StatusChanged twice on PiP exit We do call onRestoreUserInterfaceForPictureInPictureStop on PiP exit should fix #3598 & #3501 --- ios/Video/Features/RCTPictureInPicture.swift | 24 +++++++++++--------- ios/Video/RCTVideo.swift | 10 ++++---- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/ios/Video/Features/RCTPictureInPicture.swift b/ios/Video/Features/RCTPictureInPicture.swift index 0ecc37b37b..cabf6c6ba8 100644 --- a/ios/Video/Features/RCTPictureInPicture.swift +++ b/ios/Video/Features/RCTPictureInPicture.swift @@ -6,37 +6,39 @@ import React #if os(iOS) class RCTPictureInPicture: NSObject, AVPictureInPictureControllerDelegate { - private var _onPictureInPictureStatusChanged: (() -> Void)? + private var _onPictureInPictureEnter: (() -> Void)? + private var _onPictureInPictureExit: (() -> Void)? private var _onRestoreUserInterfaceForPictureInPictureStop: (() -> Void)? private var _restoreUserInterfaceForPIPStopCompletionHandler: ((Bool) -> Void)? private var _pipController: AVPictureInPictureController? private var _isActive = false - init(_ onPictureInPictureStatusChanged: (() -> Void)? = nil, _ onRestoreUserInterfaceForPictureInPictureStop: (() -> Void)? = nil) { - _onPictureInPictureStatusChanged = onPictureInPictureStatusChanged + init(_ onPictureInPictureEnter: (() -> Void)? = nil, _ onPictureInPictureExit: (() -> Void)? = nil , _ onRestoreUserInterfaceForPictureInPictureStop: (() -> Void)? = nil) { + _onPictureInPictureEnter = onPictureInPictureEnter + _onPictureInPictureExit = onPictureInPictureExit _onRestoreUserInterfaceForPictureInPictureStop = onRestoreUserInterfaceForPictureInPictureStop } func pictureInPictureControllerDidStartPictureInPicture(_: AVPictureInPictureController) { - guard let _onPictureInPictureStatusChanged else { return } + guard let _onPictureInPictureEnter else { return } - _onPictureInPictureStatusChanged() + _onPictureInPictureEnter() } func pictureInPictureControllerDidStopPictureInPicture(_: AVPictureInPictureController) { - guard let _onPictureInPictureStatusChanged else { return } + if let onPictureInPictureExit = _onPictureInPictureExit { + onPictureInPictureExit() + } - _onPictureInPictureStatusChanged() + if let onRestoreUserInterfaceForPictureInPictureStop = _onRestoreUserInterfaceForPictureInPictureStop { + onRestoreUserInterfaceForPictureInPictureStop() + } } func pictureInPictureController( _: AVPictureInPictureController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void ) { - guard let _onRestoreUserInterfaceForPictureInPictureStop else { return } - - _onRestoreUserInterfaceForPictureInPictureStop() - _restoreUserInterfaceForPIPStopCompletionHandler = completionHandler } diff --git a/ios/Video/RCTVideo.swift b/ios/Video/RCTVideo.swift index a1d34063fa..102c1a0a2b 100644 --- a/ios/Video/RCTVideo.swift +++ b/ios/Video/RCTVideo.swift @@ -120,12 +120,12 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH @objc var onTextTrackDataChanged: RCTDirectEventBlock? @objc - func _onPictureInPictureStatusChanged() { + func _onPictureInPictureEnter() { onPictureInPictureStatusChanged?(["isActive": NSNumber(value: true)]) } @objc - func _onRestoreUserInterfaceForPictureInPictureStop() { + func _onPictureInPictureExit() { onPictureInPictureStatusChanged?(["isActive": NSNumber(value: false)]) } @@ -143,9 +143,11 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH #if os(iOS) _pip = RCTPictureInPicture({ [weak self] in - self?._onPictureInPictureStatusChanged() + self?._onPictureInPictureEnter() }, { [weak self] in - self?._onRestoreUserInterfaceForPictureInPictureStop() + self?._onPictureInPictureExit() + }, { [weak self] in + self?.onRestoreUserInterfaceForPictureInPictureStop?([:]) }) #endif