|
1 | | -@objc(IsMultiWindow) |
2 | | -class IsMultiWindow: NSObject { |
| 1 | +import UIKit |
| 2 | +import React |
3 | 3 |
|
4 | | - @objc(multiply:withB:withResolver:withRejecter:) |
5 | | - func multiply(a: Float, b: Float, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void { |
6 | | - resolve(a*b) |
| 4 | +@objc(IsMultiWindow) |
| 5 | +class IsMultiWindow: RCTEventEmitter { |
| 6 | + public static var emitter: RCTEventEmitter! |
| 7 | + |
| 8 | + override init() { |
| 9 | + super.init() |
| 10 | + IsMultiWindow.emitter = self |
| 11 | + addObserver() |
| 12 | + } |
| 13 | + |
| 14 | + override func supportedEvents() -> [String]! { |
| 15 | + return ["onMultiWindowModeChanged"] |
| 16 | + } |
| 17 | + |
| 18 | + @objc func addObserver() { |
| 19 | + NotificationCenter.default.addObserver(self, |
| 20 | + selector: #selector(didRCTWindowFrameDidChange), |
| 21 | + name: NSNotification.Name.RCTWindowFrameDidChange, |
| 22 | + object: nil) |
| 23 | + } |
| 24 | + |
| 25 | + @objc func removeObserver() { |
| 26 | + NotificationCenter.default.removeObserver(self, |
| 27 | + name: NSNotification.Name.RCTWindowFrameDidChange, |
| 28 | + object: nil) |
| 29 | + } |
| 30 | + |
| 31 | + @objc func didRCTWindowFrameDidChange(notification: Notification) { |
| 32 | + if let screen = notification.object as? UIApplicationDelegate { |
| 33 | + let window = (screen.window) as? UIWindow |
| 34 | + let updatedBounds = window?.bounds |
| 35 | + let fullScreenSize = UIScreen.main.bounds |
| 36 | + |
| 37 | + IsMultiWindow.emitter.sendEvent( |
| 38 | + withName: "onMultiWindowModeChanged", |
| 39 | + body: !(updatedBounds!.width == fullScreenSize.width && updatedBounds!.height == fullScreenSize.height) |
| 40 | + ) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @objc(isMultiWindowMode:rejecter:) |
| 45 | + func isMultiWindowMode(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) { |
| 46 | + DispatchQueue.main.async { |
| 47 | + let fullScreenSize = UIScreen.main.bounds |
| 48 | + guard let keyWindow = UIApplication.shared.connectedScenes |
| 49 | + .filter({$0.activationState == .foregroundActive}) |
| 50 | + .compactMap({$0 as? UIWindowScene}) |
| 51 | + .first?.windows |
| 52 | + .filter({$0.isKeyWindow}).first else { |
| 53 | + resolve(false) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + resolve(!(keyWindow.bounds.width == fullScreenSize.width && keyWindow.bounds.height == fullScreenSize.height)) |
| 58 | + } |
7 | 59 | } |
8 | 60 | } |
0 commit comments