Skip to content

Commit

Permalink
🛠 Refactor: Replace RNIModalIdentity
Browse files Browse the repository at this point in the history
Summary: Remove `RNIModalIdentity` and replace w/ `RNIIdentifiable`.
  • Loading branch information
dominicstop committed Mar 30, 2023
1 parent 3819540 commit ed4839d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 91 deletions.
6 changes: 3 additions & 3 deletions ios/src_library/React Native/RNIModal/RNIModal+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension RNIModalState where Self: RNIModalPresentation {
};
};

extension RNIModalIdentity where Self: RNIModalPresentation {
extension RNIModalState where Self: RNIModalPresentation {

/// Programmatically get the "modal index"
var synthesizedModalIndex: Int {
Expand All @@ -48,11 +48,11 @@ extension RNIModalIdentity where Self: RNIModalPresentation {
};
};

extension RNIModalIdentity where Self: RNIModal {
extension RNIModalState where Self: RNIModal {

var synthesizedModalData: RNIModalData {
RNIModalData(
modalNativeID: self.modalNativeID,
modalNativeID: self.synthesizedStringID,
modalIndex: self.modalIndex,
currentModalIndex: RNIModalManagerShared.currentModalIndex,
isModalPresented: self.isModalPresented,
Expand Down
37 changes: 10 additions & 27 deletions ios/src_library/React Native/RNIModal/RNIModal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,13 @@ import Foundation
/// be considered a "modal".
///
public typealias RNIModal =
RNIModalIdentity
RNIIdentifiable
& RNIModalState
& RNIModalRequestable
& RNIModalFocusNotifiable
& RNIModalFocusNotifying
& RNIModalPresentation;


/// Contains modal-related properties that are used to uniquely identify a modal
/// instance.
///
/// Specifies that the "adoptee/delegate" that conforms to this protocol must
/// have the specified modal-related properties so that it can be uniquely
/// identified amongst different modal instances.
///
/// The "implementor/delegator" updates these properties; The delegate
/// should treat the properties declared in this protocol as read-only.
///
public protocol RNIModalIdentity: AnyObject {

var modalIndexPrev: Int! { set get };

var modalNativeID: String! { set get };

};

/// Contains modal-related properties for keeping track of the state of the
/// modal.
///
Expand All @@ -48,6 +29,8 @@ public protocol RNIModalIdentity: AnyObject {
///
public protocol RNIModalState: AnyObject {

var modalIndexPrev: Int! { set get };

var modalIndex: Int! { set get };

var isModalPresented: Bool { set get };
Expand All @@ -64,13 +47,13 @@ public protocol RNIModalState: AnyObject {
public protocol RNIModalRequestable: AnyObject {

func requestModalToShow(
sender: RNIModal,
sender: any RNIModal,
onRequestApprovedBlock: () -> Void,
onRequestDeniedBlock: (_ reason: String) -> Void
);

func requestModalToHide(
sender: RNIModal,
sender: any RNIModal,
onRequestApprovedBlock: () -> Void,
onRequestDeniedBlock: (_ reason: String) -> Void
);
Expand All @@ -84,14 +67,14 @@ public protocol RNIModalRequestable: AnyObject {
/// An interface for the "adoptee/delegate" to receive and handle incoming
/// modal "focus/blur"-related notifications.
///
public protocol RNIModalFocusNotifiable {
func onModalWillFocusNotification(sender modal: RNIModal);
public protocol RNIModalFocusNotifiable: AnyObject {
func onModalWillFocusNotification(sender modal: any RNIModal);

func onModalDidFocusNotification(sender modal: RNIModal);
func onModalDidFocusNotification(sender modal: any RNIModal);

func onModalWillBlurNotification(sender modal: RNIModal);
func onModalWillBlurNotification(sender modal: any RNIModal);

func onModalDidBlurNotification(sender modal: RNIModal);
func onModalDidBlurNotification(sender modal: any RNIModal);

};

Expand Down
33 changes: 10 additions & 23 deletions ios/src_library/React Native/RNIModal/RNIModalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import Foundation

public let RNIModalManagerShared = RNIModalManager.sharedInstance;



/// Note:2023-03-30-19-36-33
///
/// * This assumes that the app is using a single window, and all the modals are
Expand Down Expand Up @@ -161,7 +159,8 @@ public class RNIModalManager {

private(set) public var currentModalIndex = -1;

private(set) public var modalInstanceDict = RNIWeakDictionary<String, RNIModal>();
private(set) public var modalInstanceDict =
RNIWeakDictionary<UUID, any RNIModal>();

// MARK: - Properties - Computed
// -----------------------------
Expand All @@ -170,13 +169,13 @@ public class RNIModalManager {
self.currentModalIndex >= 0;
};

public var modalInstances: [RNIModal] {
public var modalInstances: [any RNIModal] {
self.modalInstanceDict.dict.compactMap {
$0.value.synthesizedRef;
};
};

public var presentedModals: [RNIModal] {
public var presentedModals: [any RNIModal] {
self.modalInstances.compactMap {
$0.isModalPresented ? $0 : nil;
};
Expand All @@ -185,27 +184,15 @@ public class RNIModalManager {
// MARK: - Methods
// ---------------

private func createModalNativeID() -> String {
let modalNativeID = self.counterModalNativeID;
self.counterModalNativeID += 1;

return "modal-native-id:\(modalNativeID)";
};

public func register(modal: RNIModal) {
let key = self.createModalNativeID();

modal.modalNativeID = key;

public func register(modal: any RNIModal) {
modal.modalIndex = -1;
modal.modalIndexPrev = -1;

modal.isModalPresented = false;
modal.isModalInFocus = false;

modal.modalFocusDelegate = self;

self.modalInstanceDict[key] = modal;
self.modalInstanceDict[modal.synthesizedUUID] = modal;
};
};

Expand All @@ -218,7 +205,7 @@ public class RNIModalManager {
extension RNIModalManager: RNIModalFocusNotifiable {

public func onModalWillFocusNotification(
sender modal: RNIModal
sender modal: any RNIModal
) {
let nextModalIndex = self.currentModalIndex + 1;
self.currentModalIndex = nextModalIndex;
Expand All @@ -239,7 +226,7 @@ extension RNIModalManager: RNIModalFocusNotifiable {
};
};

public func onModalDidFocusNotification(sender modal: RNIModal) {
public func onModalDidFocusNotification(sender modal: any RNIModal) {
modal.isModalInFocus = true;
modal.isModalPresented = true;

Expand All @@ -257,7 +244,7 @@ extension RNIModalManager: RNIModalFocusNotifiable {
};
};

public func onModalWillBlurNotification(sender modal: RNIModal) {
public func onModalWillBlurNotification(sender modal: any RNIModal) {
self.currentModalIndex -= 1;

modal.modalIndexPrev = modal.modalIndex;
Expand All @@ -276,7 +263,7 @@ extension RNIModalManager: RNIModalFocusNotifiable {
};
};

public func onModalDidBlurNotification(sender modal: RNIModal) {
public func onModalDidBlurNotification(sender modal: any RNIModal) {
modal.isModalInFocus = false;
modal.isModalPresented = false;

Expand Down
Loading

0 comments on commit ed4839d

Please sign in to comment.