Skip to content

Commit

Permalink
fix(ios): fix PiP callback
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gkueny committed Mar 23, 2024
1 parent d5c8b51 commit c814a7d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
24 changes: 13 additions & 11 deletions ios/Video/Features/RCTPictureInPicture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Check failure on line 16 in ios/Video/Features/RCTPictureInPicture.swift

View workflow job for this annotation

GitHub Actions / Swift-Lint

Comma Spacing Violation: There should be no space before and one after any comma (comma)

Check failure on line 16 in ios/Video/Features/RCTPictureInPicture.swift

View workflow job for this annotation

GitHub Actions / Swift-Lint

Line Length Violation: Line should be 160 characters or less; currently it has 180 characters (line_length)
_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
}

Expand Down
10 changes: 6 additions & 4 deletions ios/Video/RCTVideo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
}

Expand All @@ -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

Expand Down

0 comments on commit c814a7d

Please sign in to comment.