Skip to content

Commit

Permalink
⭐️ Impl: Add RNIModalWindowMap
Browse files Browse the repository at this point in the history
Related:
* `TODO:2023-03-31-18-33-34` - Unify/streamline/consolidate logic for invoking modal focus/blur.
  • Loading branch information
dominicstop committed Apr 8, 2023
1 parent 6b89ad5 commit 80d315a
Showing 1 changed file with 140 additions and 0 deletions.
140 changes: 140 additions & 0 deletions ios/src_library/React Native/RNIModal/RNIModalWindowMap.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//
// RNIModalWindowMap.swift
// react-native-ios-modal
//
// Created by Dominic Go on 4/8/23.
//

import Foundation


internal class RNIWindowMapData {

// MARK: - Properties
// ------------------

weak var window: UIWindow?;

private(set) var modalIndexCurrent: Int = -1;
private(set) var modalIndexPrev: Int = -1;
private(set) var modalIndexNext: Int?;

private(set) weak var nextModalToFocus: (any RNIModal)?;
private(set) weak var nextModalToBlur: (any RNIModal)?;

#if DEBUG
var modalIndexNext_: String {
self.modalIndexNext == nil ? "N/A" : "\(self.modalIndexNext!)"
};
#endif

// MARK: - Functions
// -----------------

init(window: UIWindow){
self.window = window;
};

func set(nextModalToFocus modalToFocus: any RNIModal) {
self.nextModalToFocus = modalToFocus;

let modalIndexCurrent =
RNIModalManager.computeModalIndex(forWindow: modalToFocus.window);

let modalIndexPrev = self.modalIndexCurrent;

self.modalIndexCurrent = modalIndexCurrent;
self.modalIndexNext = modalIndexCurrent + 1;
self.modalIndexPrev = modalIndexPrev;
};

func apply(forFocusedModal focusedModal: any RNIModal) {
self.nextModalToFocus = nil;

let modalIndexCurrent =
RNIModalManager.computeModalIndex(forWindow: focusedModal.window);

let modalIndexPrev = self.modalIndexCurrent;

self.modalIndexCurrent = modalIndexCurrent;
self.modalIndexNext = nil;
self.modalIndexPrev = modalIndexPrev;

focusedModal.modalIndexPrev = modalIndexPrev;
focusedModal.modalIndex = modalIndexCurrent;
};

func set(nextModalToBlur modalToBlur: any RNIModal) {
self.nextModalToBlur = modalToBlur;

let modalIndexCurrent =
RNIModalManager.computeModalIndex(forWindow: modalToBlur.window);

let modalIndexPrev = self.modalIndexCurrent;

self.modalIndexCurrent = modalIndexCurrent;
self.modalIndexNext = modalIndexCurrent - 1;
self.modalIndexPrev = modalIndexPrev;
};

func apply(forBlurredModal blurredModal: any RNIModal) {
self.nextModalToBlur = nil;

let modalIndexCurrent =
RNIModalManager.computeModalIndex(forWindow: blurredModal.window);

let modalIndexPrev = self.modalIndexCurrent;

self.modalIndexCurrent = modalIndexCurrent;
self.modalIndexNext = nil;
self.modalIndexPrev = modalIndexPrev;

blurredModal.modalIndexPrev = modalIndexPrev;
blurredModal.modalIndex = modalIndexCurrent;
};

// MARK: - Properties - Computed
// -----------------------------

var windowID: String? {
self.window?.synthesizedStringID;
};
};

// MARK: - RNIModalWindowMap
// -------------------------

internal let RNIModalWindowMapShared = RNIModalWindowMap.sharedInstance;

internal class RNIModalWindowMap {

// MARK: - Properties - Static
// ---------------------------

static var sharedInstance = RNIModalWindowMap();

// MARK: - Properties
// ------------------

private(set) var windowDataMap: Dictionary<String, RNIWindowMapData> = [:];

// MARK: - Functions
// -----------------

func set(forWindow window: UIWindow, data: RNIWindowMapData){
self.windowDataMap[window.synthesizedStringID] = data;
};

func get(forWindow window: UIWindow) -> RNIWindowMapData {
guard let windowData = self.windowDataMap[window.synthesizedStringID] else {
// No corresponding "modal index" for window yet, so initialize
// with value
let windowDataNew = RNIWindowMapData(window: window);
self.set(forWindow: window, data: windowDataNew);

return windowDataNew;
};

return windowData;
};
};

0 comments on commit 80d315a

Please sign in to comment.