Skip to content

Commit

Permalink
⭐️ Impl: RNIModalViewModule.setModalVisibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Jan 6, 2023
1 parent 723ccd2 commit 979396b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
30 changes: 30 additions & 0 deletions ios/src_library/React Native/RNIModalView/RNIModalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,36 @@ extension RNIModalView {
};
};

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

public func setModalVisibility(
visibility: Bool,
completion: CompletionHandler? = nil
){
var params: Dictionary<AnyHashable, Any> = [
"visibility": visibility,
];

self.createModalNativeEventDict().forEach { (key, value) in
params[key] = value
};

let modalAction = visibility
? self.presentModal
: self.dismissModal;

modalAction() { (success, error) in
params["success"] = success;

if let errorCode = error {
params["errorCode"] = errorCode.rawValue;
params["errorMessage"] = RNIModalViewError.getErrorMessage(for: errorCode);
};

completion?(success, error);
};
};
// --------------------------------------
// MARK: Public Functions for ViewManager
// --------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ @interface RCT_EXTERN_MODULE(RNIModalViewModule, RCTEventEmitter)
callback: (RCTResponseSenderBlock)callback
);

// MARK: - View-Related Functions
// ------------------------------

RCT_EXTERN_METHOD(setModalVisibility: (nonnull NSNumber)node
visibility: (BOOL)visibility
// promise blocks -----------------------
resolve: (RCTPromiseResolveBlock *)resolve
reject : (RCTPromiseRejectBlock *)reject);

@end
38 changes: 38 additions & 0 deletions ios/src_library/React Native/RNIModalView/RNIModalViewModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ class RNIModalViewModule: RCTEventEmitter {
@objc override static func requiresMainQueueSetup() -> Bool {
return false;
};

func getModalViewInstance(for node: NSNumber) -> RNIModalView? {
return RNIUtilities.getView(
forNode: node,
type : RNIModalView.self,
bridge : self.bridge
);
};

// MARK: - Event-Related
// ----------------------
Expand Down Expand Up @@ -137,3 +145,33 @@ extension RNIModalViewModule {
};
};
};

// MARK: - View-Related Functions
// ------------------------------

extension RNIModalViewModule {

@objc func setModalVisibility(
_ node: NSNumber,
visibility: Bool,
// promise blocks ------------------------
resolve: @escaping RCTPromiseResolveBlock,
reject : @escaping RCTPromiseRejectBlock
){
DispatchQueue.main.async {
guard let modalView = self.getModalViewInstance(for: node) else {
reject(nil, "Unable to get the corresponding 'RNIModalView' instance for node: \(node)", nil);
return;
};

modalView.setModalVisibility(visibility: visibility) { isSuccess, error in
if isSuccess {
resolve(modalView.createModalNativeEventDict());

} else {
reject(nil, error?.errorMessage, nil);
};
};
};
};
};
10 changes: 10 additions & 0 deletions src/native_modules/RNIModalViewModule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NativeModules, NativeEventEmitter } from 'react-native';
import type { RNIModalViewInfo } from 'src/native_components/RNIModalView';

const MODULE_NAME = 'RNIModalViewModule';

Expand All @@ -17,6 +18,15 @@ interface RNIModalViewModule {
animated: boolean,
callback: (success: boolean) => void
): void;

// View-Related Functions
// ----------------------

// prettier-ignore
setModalVisibility(
node: number,
visibility: boolean
): Promise<RNIModalViewInfo>;
}

export const RNIModalViewModule: RNIModalViewModule =
Expand Down

0 comments on commit 979396b

Please sign in to comment.