Skip to content

Commit

Permalink
⭐️ Impl: ModalSheetView.getModalMetrics
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Sep 27, 2024
1 parent 397a4f7 commit 4cbfe5f
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 1 deletion.
17 changes: 17 additions & 0 deletions ios/RNIModalSheetView/RNIModalSheetViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ open class RNIModalSheetViewController: UIViewController {

public var positionConfigForMainSheetContent: AlignmentPositionConfig = .default;

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

public var baseEventObject: [String: Any] {
var eventDict: [String: Any] = [:];

eventDict["modalViewControllerMetrics"] =
self.modalMetrics.synthesizedDictionaryJSON;

if let presentationControllerMetrics = self.presentationControllerMetrics {
eventDict["presentationControllerMetrics"] =
presentationControllerMetrics.synthesizedDictionaryJSON;
};

return eventDict;
};

// MARK: - View Controller Lifecycle
// ---------------------------------

Expand Down
7 changes: 7 additions & 0 deletions ios/RNIModalSheetView/RNIModalSheetViewDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ extension RNIModalSheetViewDelegate: RNIContentViewDelegate {
};

resolveBlock([:]);

case "getModalMetrics":
guard let modalSheetController = self.modalSheetController else {
throw RNIUtilitiesError(errorCode: .unexpectedNilValue);
};

resolveBlock(modalSheetController.baseEventObject as NSDictionary);

default:
throw RNIUtilitiesError(errorCode: .invalidValue);
Expand Down
7 changes: 7 additions & 0 deletions src/components/ModalSheetView/ModalSheetView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export const ModalSheetView = React.forwardRef<
...commandArgs,
});
},
getModalMetrics: async () => {
if(nativeRef.current == null) {
throw Error("Unable to get ref to native sheet");
};

return await nativeRef.current.getModalMetrics();
},
}));

const shouldEnableDebugBackgroundColors =
Expand Down
11 changes: 10 additions & 1 deletion src/components/ModalSheetView/ModalSheetViewTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import type { RemapObject } from "react-native-ios-utilities";
import type { RNIModalSheetViewProps, RNIModalSheetViewRef } from "../../native_components/RNIModalSheetVIew";


type ModalSheetViewRefInherited = Pick<RNIModalSheetViewRef,
| 'getModalMetrics'
>;

type ModalSheetViewRefInheritedRaw = Pick<RNIModalSheetViewRef,
| 'presentModal'
| 'dismissModal'
>;

export type ModalSheetViewRef = RemapObject<ModalSheetViewRefInheritedRaw, {
type ModalSheetViewRefInheritedRemapped = RemapObject<ModalSheetViewRefInheritedRaw, {
presentModal: (commandArgs?: {
isAnimated?: boolean;
}) => Promise<void>;
Expand All @@ -20,6 +24,11 @@ export type ModalSheetViewRef = RemapObject<ModalSheetViewRefInheritedRaw, {
}) => Promise<void>;
}>;


export type ModalSheetViewRef =
ModalSheetViewRefInherited
& ModalSheetViewRefInheritedRemapped;

export type ModalSheetViewInheritedProps = Pick<RNIModalSheetViewProps,
| 'onDidSetViewID'
| 'shouldEnableDebugBackgroundColors'
Expand Down
13 changes: 13 additions & 0 deletions src/native_components/RNIModalSheetVIew/RNIModalSheetView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Helpers, type StateViewID, type StateReactTag } from 'react-native-ios-

import { RNIModalSheetNativeView } from './RNIModalSheetNativeView';
import type { RNIModalSheetViewProps, RNIModalSheetViewRef, } from './RNIModalSheetViewTypes';
import type { ModalMetrics } from '../../types/ModalMetrics';


export const RNIModalSheetView = React.forwardRef<
Expand Down Expand Up @@ -40,6 +41,18 @@ export const RNIModalSheetView = React.forwardRef<
/* commandArgs: */ commandArgs,
);
},
getModalMetrics: async () => {
if(viewID == null) return;
const module = Helpers.getRNIUtilitiesModule();

const result = await module.viewCommandRequest(
/* viewID : */ viewID,
/* commandName: */ 'getModalMetrics',
/* commandArgs: */ {},
);

return result as any;
},
}));

const reactChildrenCount = React.Children.count(props.children);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import type { StateReactTag, StateViewID } from "react-native-ios-utilities";

import type { RNIModalSheetNativeViewProps } from "./RNIModalSheetNativeView";

import type { ModalMetrics } from "../../types/ModalMetrics";


export type RNIModalSheetViewRef = {
getViewID: () => StateViewID;
Expand All @@ -16,6 +18,8 @@ export type RNIModalSheetViewRef = {
dismissModal: (commandArgs: {
isAnimated: boolean;
}) => Promise<void>;

getModalMetrics: () => Promise<ModalMetrics>;
};

export type RNIModalSheetViewInheritedOptionalProps = Partial<Pick<RNIModalSheetNativeViewProps,
Expand Down
8 changes: 8 additions & 0 deletions src/types/ModalMetrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { ModalViewControllerMetrics } from "./ModalViewControllerMetrics";
import type { PresentationControllerMetrics } from "./PresentationControllerMetrics";


export type ModalMetrics = {
modalViewControllerMetrics: ModalViewControllerMetrics;
presentationControllerMetrics?: PresentationControllerMetrics;
}
14 changes: 14 additions & 0 deletions src/types/ModalViewControllerMetrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

export type ModalViewControllerMetrics = {
instanceID: string;
isBeingDismissed: boolean;
isBeingPresented: boolean;
isPresentedAsModal: boolean;
modalLevel: number;
topmostModalLevel: boolean;
isTopMostModal: boolean;
hasPanGesture: boolean;
isPanGestureEnabled: boolean;
modalPresentationStyle: string;
isUsingSheetPresentationController: boolean;
};
13 changes: 13 additions & 0 deletions src/types/PresentationControllerMetrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { CGRectNative, CGSize } from "react-native-ios-utilities";


export type PresentationControllerMetrics = {
instanceID: string;
frameOfPresentedViewInContainerView: CGRectNative;
preferredContentSize: CGSize;
presentedViewFrame?: CGRectNative;
containerViewFrame?: CGRectNative;
adaptivePresentationStyle: string;
shouldPresentInFullscreen: boolean;
shouldRemovePresentersView: boolean;
};

0 comments on commit 4cbfe5f

Please sign in to comment.