feat(ios): update the way to get keyWindow (#3448)

* feat(ios): update the way to get `keyWindow`

* lint
This commit is contained in:
Krzysztof Moch 2023-12-28 12:58:25 +01:00 committed by GitHub
parent 1a9ea827c0
commit f35727f30e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 5 deletions

View File

@ -348,4 +348,16 @@ enum RCTVideoUtils {
return nil return nil
} }
static func getCurrentWindow() -> UIWindow? {
if #available(iOS 13.0, tvOS 13, *) {
return UIApplication.shared.connectedScenes
.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
.last { $0.isKeyWindow }
} else {
#if !os(visionOS)
return UIApplication.shared.keyWindow
#endif
}
}
} }

View File

@ -792,7 +792,8 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
// Find the nearest view controller // Find the nearest view controller
var viewController: UIViewController! = self.firstAvailableUIViewController() var viewController: UIViewController! = self.firstAvailableUIViewController()
if viewController == nil { if viewController == nil {
let keyWindow: UIWindow! = UIApplication.shared.keyWindow guard let keyWindow = RCTVideoUtils.getCurrentWindow() else { return }
viewController = keyWindow.rootViewController viewController = keyWindow.rootViewController
if !viewController.children.isEmpty { if !viewController.children.isEmpty {
viewController = viewController.children.last viewController = viewController.children.last
@ -1291,9 +1292,12 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
func handleViewControllerOverlayViewFrameChange(overlayView _: UIView, change: NSKeyValueObservedChange<CGRect>) { func handleViewControllerOverlayViewFrameChange(overlayView _: UIView, change: NSKeyValueObservedChange<CGRect>) {
let oldRect = change.oldValue let oldRect = change.oldValue
let newRect = change.newValue let newRect = change.newValue
guard let bounds = RCTVideoUtils.getCurrentWindow()?.bounds else { return }
if !oldRect!.equalTo(newRect!) { if !oldRect!.equalTo(newRect!) {
// https://github.com/react-native-video/react-native-video/issues/3085#issuecomment-1557293391 // https://github.com/react-native-video/react-native-video/issues/3085#issuecomment-1557293391
if newRect!.equalTo(UIScreen.main.bounds) { if newRect!.equalTo(bounds) {
RCTLog("in fullscreen") RCTLog("in fullscreen")
if !_fullscreenUncontrolPlayerPresented { if !_fullscreenUncontrolPlayerPresented {
_fullscreenUncontrolPlayerPresented = true _fullscreenUncontrolPlayerPresented = true
@ -1311,7 +1315,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
} }
} }
self.reactViewController().view.frame = UIScreen.main.bounds self.reactViewController().view.frame = bounds
self.reactViewController().view.setNeedsLayout() self.reactViewController().view.setNeedsLayout()
} }
} }

View File

@ -35,8 +35,13 @@ class RCTVideoPlayerViewController: AVPlayerViewController {
return .portrait return .portrait
} else { } else {
// default case // default case
let orientation = UIApplication.shared.statusBarOrientation if #available(iOS 13, tvOS 13, *) {
return orientation return RCTVideoUtils.getCurrentWindow()?.windowScene?.interfaceOrientation ?? .unknown
} else {
#if !os(visionOS)
return UIApplication.shared.statusBarOrientation
#endif
}
} }
} }