Skip to content

Commit c5d59e3

Browse files
committed
feat: support iOS
1 parent 7d8c3a7 commit c5d59e3

9 files changed

Lines changed: 74 additions & 199 deletions

File tree

.github/FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
buy_me_a_coffee: arang

.github/actions/setup/action.yml

Lines changed: 0 additions & 27 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 0 additions & 157 deletions
This file was deleted.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Quickly and easily check if your app is multiwindow
88

99
![image](https://github.com/user-attachments/assets/945aac7c-c5a5-42dd-8e50-5527c0ea77d4)
1010

11+
- iOS, iPad OS
12+
13+
![image](https://github.com/user-attachments/assets/945aac7c-c5a5-42dd-8e50-5527c0ea77d4)
14+
1115
## Installation
1216

1317
```sh
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#import <React/RCTBridgeModule.h>
22
#import <React/RCTViewManager.h>
3+
#import <React/RCTEventEmitter.h>

ios/IsMultiWindow.mm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
#import <Foundation/Foundation.h>
12
#import <React/RCTBridgeModule.h>
3+
#import <React/RCTEventEmitter.h>
24

3-
@interface RCT_EXTERN_MODULE(IsMultiWindow, NSObject)
5+
@interface RCT_EXTERN_MODULE(IsMultiWindow, RCTEventEmitter)
46

5-
RCT_EXTERN_METHOD(multiply:(float)a withB:(float)b
6-
withResolver:(RCTPromiseResolveBlock)resolve
7-
withRejecter:(RCTPromiseRejectBlock)reject)
7+
RCT_EXTERN_METHOD(isMultiWindowMode:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
88

99
+ (BOOL)requiresMainQueueSetup
1010
{

ios/IsMultiWindow.swift

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,60 @@
1-
@objc(IsMultiWindow)
2-
class IsMultiWindow: NSObject {
1+
import UIKit
2+
import React
33

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+
}
759
}
860
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-is-multi-window",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Quickly and easily check if your app is multiwindowed",
55
"source": "./src/index.tsx",
66
"main": "./lib/commonjs/index.js",

src/index.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ const ReactNativeIsMultiWindowModule = NativeModules.IsMultiWindow
1919
);
2020

2121
const isSupportPlatform = () => {
22-
return Platform.OS === 'android';
22+
return Platform.OS === 'android' || Platform.OS === 'ios';
2323
};
2424

25-
const ReactNativeIsMultiWindowEmitter =
26-
Platform.OS === 'android'
27-
? new NativeEventEmitter(ReactNativeIsMultiWindowModule)
28-
: undefined;
25+
const ReactNativeIsMultiWindowEmitter = isSupportPlatform()
26+
? new NativeEventEmitter(ReactNativeIsMultiWindowModule)
27+
: undefined;
2928

3029
const addListener = (callback: (isMultiMode: boolean) => void) => {
3130
if (!isSupportPlatform()) {

0 commit comments

Comments
 (0)