Skip to content

Commit

Permalink
⭐️ Impl: ModalSheetView.dismissModal
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Sep 26, 2024
1 parent 1f05e96 commit ced26f2
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 66 deletions.
15 changes: 11 additions & 4 deletions ios/RNIModalSheetView/RNIModalSheetViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down Expand Up @@ -96,14 +96,21 @@ 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 {
return;
};

if self.presentingViewController != nil {
self.dismiss(animated: true);
self.dismiss(animated: false);

} else if self.parent != nil {
self.willMove(toParent: nil);
Expand Down
85 changes: 28 additions & 57 deletions ios/RNIModalSheetView/RNIModalSheetViewDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [];

Expand All @@ -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 {
Expand All @@ -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);
};

Expand All @@ -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) {
Expand Down Expand Up @@ -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([:]);

Expand All @@ -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
};
13 changes: 11 additions & 2 deletions src/components/ModalSheetView/ModalSheetView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -58,7 +68,6 @@ export const ModalSheetView = React.forwardRef<
const styles = StyleSheet.create({
nativeModalSheet: {
position: 'absolute',
pointerEvents: 'none',
opacity: 0,
opacity: 0.01,
},
});
3 changes: 2 additions & 1 deletion src/components/ModalSheetView/ModalSheetViewContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export function ModalSheetViewContent(

const didDetach =
(props.isParentDetached ?? false)
|| detachedSubviewEntry.didDetachFromOriginalParent;
|| detachedSubviewEntry.didDetachFromOriginalParent
|| true

return (
<RNIWrapperView
Expand Down
11 changes: 9 additions & 2 deletions src/components/ModalSheetView/ModalSheetViewTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import type { RemapObject } from "react-native-ios-utilities";
import type { RNIModalSheetViewProps, RNIModalSheetViewRef } from "../../native_components/RNIModalSheetVIew";


export type ModalSheetViewRef = RemapObject<Pick<RNIModalSheetViewRef,
type ModalSheetViewRefInheritedRaw = Pick<RNIModalSheetViewRef,
| 'presentModal'
>, {
| 'dismissModal'
>;

export type ModalSheetViewRef = RemapObject<ModalSheetViewRefInheritedRaw, {
presentModal: (commandArgs?: {
isAnimated?: boolean;
}) => Promise<void>;

dismissModal: (commandArgs?: {
isAnimated?: boolean;
}) => Promise<void>;
}>;

export type ModalSheetViewInheritedProps = Pick<RNIModalSheetViewProps,
Expand Down
11 changes: 11 additions & 0 deletions src/native_components/RNIModalSheetVIew/RNIModalSheetView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ export const RNIModalSheetView = React.forwardRef<
/* commandArgs: */ commandArgs,
);
},
dismissModal: async (commandArgs) => {
if(viewID == null) return;
const module = Helpers.getRNIUtilitiesModule();

await module.viewCommandRequest(
/* viewID : */ viewID,
/* commandName: */ 'dismissModal',
/* commandArgs: */ commandArgs,
);
},
}));

const reactChildrenCount = React.Children.count(props.children);
Expand All @@ -39,6 +49,7 @@ export const RNIModalSheetView = React.forwardRef<
{...props}
style={[
styles.detachedView,
styles.detachedViewDebug,
props.style,
]}
reactChildrenCount={reactChildrenCount}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export type RNIModalSheetViewRef = {
presentModal: (commandArgs: {
isAnimated: boolean;
}) => Promise<void>;

dismissModal: (commandArgs: {
isAnimated: boolean;
}) => Promise<void>;
};

export type RNIModalSheetViewInheritedOptionalProps = Partial<Pick<RNIModalSheetNativeViewProps,
Expand Down

0 comments on commit ced26f2

Please sign in to comment.