From ced26f24d39bda24c0dc8e3535747a1485cccc15 Mon Sep 17 00:00:00 2001 From: Dominic Go Date: Thu, 26 Sep 2024 09:32:43 +0800 Subject: [PATCH] =?UTF-8?q?=E2=AD=90=EF=B8=8F=20Impl:=20`ModalSheetView.di?= =?UTF-8?q?smissModal`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RNIModalSheetViewController.swift | 15 +++- .../RNIModalSheetViewDelegate.swift | 85 ++++++------------- .../ModalSheetView/ModalSheetView.tsx | 13 ++- .../ModalSheetView/ModalSheetViewContent.tsx | 3 +- .../ModalSheetView/ModalSheetViewTypes.tsx | 11 ++- .../RNIModalSheetVIew/RNIModalSheetView.tsx | 11 +++ .../RNIModalSheetViewTypes.ts | 4 + 7 files changed, 76 insertions(+), 66 deletions(-) diff --git a/ios/RNIModalSheetView/RNIModalSheetViewController.swift b/ios/RNIModalSheetView/RNIModalSheetViewController.swift index d7c1bd96..5598507c 100644 --- a/ios/RNIModalSheetView/RNIModalSheetViewController.swift +++ b/ios/RNIModalSheetView/RNIModalSheetViewController.swift @@ -41,11 +41,11 @@ open class RNIModalSheetViewController: UIViewController { rootReactView.removeAllAncestorConstraints(); #endif - self.view.addSubview(mainSheetContent); - mainSheetContent.translatesAutoresizingMaskIntoConstraints = false; + self.view.addSubview(mainSheetContentParent); + mainSheetContentParent.translatesAutoresizingMaskIntoConstraints = false; let constraints = self.positionConfigForMainSheetContent.createConstraints( - forView: mainSheetContent, + forView: mainSheetContentParent, attachingTo: self.view, enclosingView: self.view ); @@ -96,6 +96,13 @@ open class RNIModalSheetViewController: UIViewController { extension RNIModalSheetViewController: RNIViewLifecycle { public func notifyOnRequestForCleanup(sender: RNIContentViewParentDelegate) { + guard let mainSheetContentParent = self.mainSheetContentParent else { + return; + }; + + mainSheetContentParent.detachReactTouchHandler(); + self.mainSheetContentParent?.removeFromSuperview(); + guard self.shouldTriggerDefaultCleanup, self.view.window != nil else { @@ -103,7 +110,7 @@ extension RNIModalSheetViewController: RNIViewLifecycle { }; if self.presentingViewController != nil { - self.dismiss(animated: true); + self.dismiss(animated: false); } else if self.parent != nil { self.willMove(toParent: nil); diff --git a/ios/RNIModalSheetView/RNIModalSheetViewDelegate.swift b/ios/RNIModalSheetView/RNIModalSheetViewDelegate.swift index f583e44e..a48eae81 100644 --- a/ios/RNIModalSheetView/RNIModalSheetViewDelegate.swift +++ b/ios/RNIModalSheetView/RNIModalSheetViewDelegate.swift @@ -23,12 +23,11 @@ public final class RNIModalSheetViewDelegate: UIView, RNIContentView { // MARK: Properties // ---------------- - var _didSendEvents = false; - // MARK: - Properties - RNIContentViewDelegate // ------------------------------------------- public weak var parentReactView: RNIContentViewParentDelegate?; + public var modalSheetController: RNIModalSheetViewController?; public var detachedModalContentParentViews: [RNIContentViewParentDelegate] = []; @@ -50,18 +49,9 @@ public final class RNIModalSheetViewDelegate: UIView, RNIContentView { fatalError("init(coder:) has not been implemented"); } - // MARK: Functions + // MARK: - Methods // --------------- - public override func didMoveToWindow() { - guard self.window != nil, - let parentReactView = self.parentReactView - else { return }; - - DispatchQueue.main.asyncAfter(deadline: .now() + 10){ - parentReactView.setSize(.init(width: 300, height: 300)); - }; - }; }; extension RNIModalSheetViewDelegate: RNIContentViewDelegate { @@ -78,21 +68,15 @@ extension RNIModalSheetViewDelegate: RNIContentViewDelegate { superBlock: () -> Void ) { - guard let parentReactView = parentReactView else { - return; - }; - - defer { - parentReactView.requestToRemoveReactSubview(childComponentView); - childComponentView.removeFromSuperview(); - }; - guard let reactView = childComponentView as? RNIContentViewParentDelegate, reactView.contentDelegate is RNIWrapperViewContent else { return; }; + reactView.removeFromSuperview(); + reactView.attachReactTouchHandler(); + self.detachedModalContentParentViews.append(reactView); }; @@ -102,10 +86,7 @@ extension RNIModalSheetViewDelegate: RNIContentViewDelegate { index: NSInteger, superBlock: () -> Void ) { - #if !RCT_NEW_ARCH_ENABLED - superBlock(); - #endif - + // no-op } public func notifyDidSetProps(sender: RNIContentViewParentDelegate) { @@ -155,10 +136,27 @@ extension RNIModalSheetViewDelegate: RNIContentViewDelegate { ); let modalVC = RNIModalSheetViewController(); + self.modalSheetController = modalVC; + modalVC.mainSheetContentParent = mainSheetContentParent; modalVC.view.backgroundColor = .systemBackground; closestVC.present(modalVC, animated: isAnimated); + resolveBlock([:]); + + case "dismissModal": + guard let modalSheetController = self.modalSheetController else { + throw RNIUtilitiesError(errorCode: .unexpectedNilValue); + }; + + let isAnimated: Bool = commandArguments.getValueFromDictionary( + forKey: "isAnimated", + fallbackValue: true + ); + + modalSheetController.dismiss(animated: isAnimated) { + self.modalSheetController = nil; + }; resolveBlock([:]); @@ -173,39 +171,12 @@ extension RNIModalSheetViewDelegate: RNIContentViewDelegate { // MARK: Fabric Only // ----------------- - - #if RCT_NEW_ARCH_ENABLED - public func notifyOnUpdateProps( - sender: RNIContentViewParentDelegate, - oldProps: NSDictionary, - newProps: NSDictionary - ) { - // no-op - }; - - public func notifyOnUpdateState( - sender: RNIContentViewParentDelegate, - oldState: NSDictionary?, - newState: NSDictionary - ) { - // no-op - }; - public func notifyOnFinalizeUpdates( - sender: RNIContentViewParentDelegate, - updateMaskRaw: Int, - updateMask: RNIComponentViewUpdateMask - ) { - // no-op - }; - - public func notifyOnPrepareForReuse(sender: RNIContentViewParentDelegate) { - // no-op + #if RCT_NEW_ARCH_ENABLED + public func shouldRecycleContentDelegate( + sender: RNIContentViewParentDelegate + ) -> Bool { + return false; }; - #else - - // MARK: - Paper Only - // ------------------ - #endif }; diff --git a/src/components/ModalSheetView/ModalSheetView.tsx b/src/components/ModalSheetView/ModalSheetView.tsx index b2b654ff..5775121b 100644 --- a/src/components/ModalSheetView/ModalSheetView.tsx +++ b/src/components/ModalSheetView/ModalSheetView.tsx @@ -31,6 +31,16 @@ export const ModalSheetView = React.forwardRef< ...commandArgs, }); }, + dismissModal: async (commandArgs) => { + if(nativeRef.current == null) { + throw Error("Unable to get ref to native sheet"); + }; + + await nativeRef.current.dismissModal({ + isAnimated: true, + ...commandArgs, + }); + }, })); const shouldEnableDebugBackgroundColors = @@ -58,7 +68,6 @@ export const ModalSheetView = React.forwardRef< const styles = StyleSheet.create({ nativeModalSheet: { position: 'absolute', - pointerEvents: 'none', - opacity: 0, + opacity: 0.01, }, }); \ No newline at end of file diff --git a/src/components/ModalSheetView/ModalSheetViewContent.tsx b/src/components/ModalSheetView/ModalSheetViewContent.tsx index eda56292..0c9fea4c 100644 --- a/src/components/ModalSheetView/ModalSheetViewContent.tsx +++ b/src/components/ModalSheetView/ModalSheetViewContent.tsx @@ -25,7 +25,8 @@ export function ModalSheetViewContent( const didDetach = (props.isParentDetached ?? false) - || detachedSubviewEntry.didDetachFromOriginalParent; + || detachedSubviewEntry.didDetachFromOriginalParent + || true return ( , { + | 'dismissModal' +>; + +export type ModalSheetViewRef = RemapObject Promise; + + dismissModal: (commandArgs?: { + isAnimated?: boolean; + }) => Promise; }>; export type ModalSheetViewInheritedProps = Pick { + if(viewID == null) return; + const module = Helpers.getRNIUtilitiesModule(); + + await module.viewCommandRequest( + /* viewID : */ viewID, + /* commandName: */ 'dismissModal', + /* commandArgs: */ commandArgs, + ); + }, })); const reactChildrenCount = React.Children.count(props.children); @@ -39,6 +49,7 @@ export const RNIModalSheetView = React.forwardRef< {...props} style={[ styles.detachedView, + styles.detachedViewDebug, props.style, ]} reactChildrenCount={reactChildrenCount} diff --git a/src/native_components/RNIModalSheetVIew/RNIModalSheetViewTypes.ts b/src/native_components/RNIModalSheetVIew/RNIModalSheetViewTypes.ts index 0f9a8cb4..06dfdc92 100644 --- a/src/native_components/RNIModalSheetVIew/RNIModalSheetViewTypes.ts +++ b/src/native_components/RNIModalSheetVIew/RNIModalSheetViewTypes.ts @@ -12,6 +12,10 @@ export type RNIModalSheetViewRef = { presentModal: (commandArgs: { isAnimated: boolean; }) => Promise; + + dismissModal: (commandArgs: { + isAnimated: boolean; + }) => Promise; }; export type RNIModalSheetViewInheritedOptionalProps = Partial