Skip to content

Commit

Permalink
⭐️ Impl: ModalSheetView State Change Events
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Sep 29, 2024
1 parent ce1abce commit e430a5e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ios/Common/ModalSheetState+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public extension ModalSheetState {
};

var asDictionary: [String : Any] {
self.asMetrics.synthesizedDictionary;
self.asMetrics.synthesizedDictionaryJSON;
};
};
74 changes: 63 additions & 11 deletions ios/RNIModalSheetView/RNIModalSheetViewDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import UIKit
import DGSwiftUtilities
import react_native_ios_utilities


@objc(RNIModalSheetViewDelegate)
public final class RNIModalSheetViewDelegate: UIView, RNIContentView {

Expand All @@ -27,7 +28,7 @@ public final class RNIModalSheetViewDelegate: UIView, RNIContentView {
case onModalDidHide;

case onModalSheetStateWillChange;
case onModalSheetStateDidChange;
case onModalSheetStateDidChange;
};

public static var propKeyPathMap: PropKeyPathMap {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<String, Any> = [
"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<String, Any> = [
"prevState": currentState.asDictionary,
"currentState": currentState.asDictionary
];

self.dispatchEvent(
for: .onModalSheetStateWillChange,
withPayload: payload
);
};
};
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export type RNIModalSheetViewInheritedOptionalProps = Partial<Pick<RNIModalSheet
| 'onModalDidShow'
| 'onModalWillHide'
| 'onModalDidHide'

// modal sheet events
| 'onModalSheetStateWillChange'
| 'onModalSheetStateDidChange'
>>;

export type RNIModalSheetViewBaseProps = {
Expand Down

0 comments on commit e430a5e

Please sign in to comment.