Skip to content

Commit

Permalink
⭐️ Impl: RNIModalManager.getWindows
Browse files Browse the repository at this point in the history
Related:
* TODO:2023-03-04-06-34-28 - Library Native Cleanup
* TODO:2023-03-04-15-39-46 - Impl. `RNIModalManager`

Summary:
* Impl. `RNIModalManager.getWindows`.
* Refactor `RNIModalManager.getRootViewController` to use `getWindows`.
  • Loading branch information
dominicstop committed Mar 24, 2023
1 parent 13cf593 commit ea43983
Showing 1 changed file with 50 additions and 22 deletions.
72 changes: 50 additions & 22 deletions ios/src_library/React Native/RNIModal/RNIModalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,75 @@ public class RNIModalManager {
// ------------------------

/// TODO:2023-03-20-21-29-36 - Move to `RNIUtilities`
static func getRootViewController(
for window: UIWindow? = nil
) -> UIViewController? {

if let window = window {
return window.rootViewController;
};
static func getWindows() -> [UIWindow] {
var windows: [UIWindow] = [];

#if swift(>=5.5)
// Version: Swift 5.5 and newer - iOS 15 and newer
guard #available(iOS 13.0, *) else { return nil };

let scenes = UIApplication.shared.connectedScenes;
guard #available(iOS 13.0, *) else { return [] };

guard let windowScene = scenes.first as? UIWindowScene,
let window = windowScene.windows.first
else { return nil };

return window.rootViewController;
for scene in UIApplication.shared.connectedScenes {
guard let windowScene = scene as? UIWindowScene else { continue };
windows += windowScene.windows;
};

#elseif swift(>=5)
// Version: Swift 5.4 and below - iOS 14.5 and below
// Note: 'windows' was deprecated in iOS 15.0+
guard let window = UIApplication.shared.windows.first else { return nil };
return window.rootViewController;

// first element is the "key window"
if let keyWindow =
UIApplication.shared.windows.first(where: { $0.isKeyWindow }) {

windows.append(keyWindow);
};

UIApplication.shared.windows.forEach {
// skip if already added
guard !windows.contains($0) else { return };
windows.append($0);
};

#elseif swift(>=4)
// Version: Swift 4 and below - iOS 12.4 and below
// Note: `keyWindow` was deprecated in iOS 13.0+
guard let window = UIApplication.shared.keyWindow else { return nil };
return window.rootViewController;

// first element is the "key window"
if let keyWindow = UIApplication.shared.keyWindow {
windows.append(keyWindow);
};

UIApplication.shared.windows.forEach {
// skip if already added
guard !windows.contains($0) else { return };
windows.append($0);
};

#else
// Version: Swift 3.1 and below - iOS 10.3 and below
// Note: 'sharedApplication' has been renamed to 'shared'
guard let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate,
guard let appDelegate =
UIApplication.sharedApplication().delegate as? AppDelegate,

let window = appDelegate.window
else { return nil };
else { return [] };

return window.rootViewController;
return windows.append(window);
#endif

return windows;
};

/// TODO:2023-03-20-21-29-36 - Move to `RNIUtilities`
static func getRootViewController(
for window: UIWindow? = nil
) -> UIViewController? {

if let window = window {
return window.rootViewController;
};

return Self.getWindows().first?.rootViewController;
};

/// TODO:2023-03-20-21-29-36 - Move to `RNIUtilities`
Expand Down

0 comments on commit ea43983

Please sign in to comment.