diff --git a/ios/Common/ModalSheetState+Helpers.swift b/ios/Common/ModalSheetState+Helpers.swift index 3c9bf9e2..3166b35c 100644 --- a/ios/Common/ModalSheetState+Helpers.swift +++ b/ios/Common/ModalSheetState+Helpers.swift @@ -19,6 +19,6 @@ public extension ModalSheetState { }; var asDictionary: [String : Any] { - self.asMetrics.synthesizedDictionary; + self.asMetrics.synthesizedDictionaryJSON; }; }; diff --git a/ios/RNIModalSheetView/RNIModalSheetViewDelegate.swift b/ios/RNIModalSheetView/RNIModalSheetViewDelegate.swift index 8f8c6f41..abb1ee44 100644 --- a/ios/RNIModalSheetView/RNIModalSheetViewDelegate.swift +++ b/ios/RNIModalSheetView/RNIModalSheetViewDelegate.swift @@ -9,6 +9,7 @@ import UIKit import DGSwiftUtilities import react_native_ios_utilities + @objc(RNIModalSheetViewDelegate) public final class RNIModalSheetViewDelegate: UIView, RNIContentView { @@ -27,7 +28,7 @@ public final class RNIModalSheetViewDelegate: UIView, RNIContentView { case onModalDidHide; case onModalSheetStateWillChange; - case onModalSheetStateDidChange; + case onModalSheetStateDidChange; }; public static var propKeyPathMap: PropKeyPathMap { @@ -68,6 +69,23 @@ public final class RNIModalSheetViewDelegate: UIView, RNIContentView { // MARK: - Methods // --------------- + func createModalController() throws -> RNIModalSheetViewController { + guard let mainSheetContentParent = self.sheetMainContentParentView else { + throw RNIUtilitiesError(errorCode: .unexpectedNilValue); + }; + + let modalVC = RNIModalSheetViewController(); + self.modalSheetController = modalVC; + + modalVC.mainSheetContentParent = mainSheetContentParent; + modalVC.view.backgroundColor = .systemBackground; + + modalVC.lifecycleEventDelegates.add(self); + modalVC.sheetPresentationStateMachine.eventDelegates.add(self); + + return modalVC; + }; + func discardCurrentModalControllerIfNeeded(){ guard let modalVC = self.modalSheetController, !modalVC.isPresentedAsModal @@ -150,21 +168,12 @@ extension RNIModalSheetViewDelegate: RNIContentViewDelegate { throw RNIUtilitiesError(errorCode: .unexpectedNilValue); }; - guard let mainSheetContentParent = self.sheetMainContentParentView else { - throw RNIUtilitiesError(errorCode: .unexpectedNilValue); - }; - let isAnimated: Bool = commandArguments.getValueFromDictionary( forKey: "isAnimated", fallbackValue: true ); - let modalVC = RNIModalSheetViewController(); - self.modalSheetController = modalVC; - - modalVC.mainSheetContentParent = mainSheetContentParent; - modalVC.view.backgroundColor = .systemBackground; - modalVC.lifecycleEventDelegates.add(self); + let modalVC = try self.createModalController(); self.dispatchEvent( for: .onModalWillPresent, @@ -285,3 +294,46 @@ extension RNIModalSheetViewDelegate: ViewControllerLifecycleNotifiable { ); }; }; + +// MARK: - RNIModalSheetViewDelegate+ViewControllerLifecycleNotifiable +// ------------------------------------------------------------------- + +extension RNIModalSheetViewDelegate: ModalSheetPresentationStateEventsNotifiable { + + public func onModalSheetStateWillChange( + sender: ModalSheetPresentationStateMachine, + prevState: ModalSheetState?, + currentState: ModalSheetState, + nextState: ModalSheetState + ) { + var payload: Dictionary = [ + "currentState": currentState.asDictionary, + "nextState": nextState.asDictionary + ]; + + payload.unwrapAndMerge(withOther: [ + "prevState": prevState?.asDictionary + ]); + + self.dispatchEvent( + for: .onModalSheetStateWillChange, + withPayload: payload + ); + }; + + public func onModalSheetStateDidChange( + sender: ModalSheetPresentationStateMachine, + prevState: ModalSheetState?, + currentState: ModalSheetState + ) { + let payload: Dictionary = [ + "prevState": currentState.asDictionary, + "currentState": currentState.asDictionary + ]; + + self.dispatchEvent( + for: .onModalSheetStateWillChange, + withPayload: payload + ); + }; +}; diff --git a/src/native_components/RNIModalSheetVIew/RNIModalSheetViewEvents.ts b/src/native_components/RNIModalSheetVIew/RNIModalSheetViewEvents.ts index 5bc2655c..5e6f2107 100644 --- a/src/native_components/RNIModalSheetVIew/RNIModalSheetViewEvents.ts +++ b/src/native_components/RNIModalSheetVIew/RNIModalSheetViewEvents.ts @@ -1,14 +1,20 @@ import type { BubblingEventHandler } from 'react-native/Libraries/Types/CodegenTypes'; +import type { RNIModalSheetStateMetrics } from '../../types/RNIModalSheetStateMetrics'; // MARK: Event Objects // ------------------- export type OnModalSheetStateWillChangeEventPayload = Readonly<{ + prevState?: RNIModalSheetStateMetrics; + currentState: RNIModalSheetStateMetrics; + nextState: RNIModalSheetStateMetrics; }>; export type OnModalSheetStateDidChangeEventPayload = Readonly<{ + prevState: RNIModalSheetStateMetrics; + currentState: RNIModalSheetStateMetrics; }>; // MARK: Events diff --git a/src/native_components/RNIModalSheetVIew/RNIModalSheetViewTypes.ts b/src/native_components/RNIModalSheetVIew/RNIModalSheetViewTypes.ts index 09001300..ddcbc783 100644 --- a/src/native_components/RNIModalSheetVIew/RNIModalSheetViewTypes.ts +++ b/src/native_components/RNIModalSheetVIew/RNIModalSheetViewTypes.ts @@ -39,6 +39,10 @@ export type RNIModalSheetViewInheritedOptionalProps = Partial>; export type RNIModalSheetViewBaseProps = {