Skip to content

Commit

Permalink
⭐️ Impl: ModalSheetView Presentation Events
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Sep 27, 2024
1 parent adffaf0 commit e533692
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 15 deletions.
99 changes: 95 additions & 4 deletions ios/RNIModalSheetView/RNIModalSheetViewDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ public final class RNIModalSheetViewDelegate: UIView, RNIContentView {
case mainSheetContent;
};

public static var propKeyPathMap: Dictionary<String, PartialKeyPath<RNIModalSheetViewDelegate>> {
return [:];
public enum Events: String, CaseIterable {
case onModalWillPresent;
case onModalDidPresent;
case onModalWillShow;
case onModalDidShow;
case onModalWillHide;
case onModalDidHide;
};

public enum Events: String, CaseIterable {
Expand Down Expand Up @@ -58,8 +63,20 @@ public final class RNIModalSheetViewDelegate: UIView, RNIContentView {
// MARK: - Methods
// ---------------

func discardCurrentModalControllerIfNeeded(){
guard let modalVC = self.modalSheetController,
!modalVC.isPresentedAsModal
else {
return;
};

self.modalSheetController = nil;
};
};

// MARK: - RNIModalSheetViewDelegate+RNIContentViewDelegate
// --------------------------------------------------------

extension RNIModalSheetViewDelegate: RNIContentViewDelegate {

public typealias KeyPathRoot = RNIModalSheetViewDelegate;
Expand Down Expand Up @@ -154,8 +171,24 @@ extension RNIModalSheetViewDelegate: RNIContentViewDelegate {

modalVC.mainSheetContentParent = mainSheetContentParent;
modalVC.view.backgroundColor = .systemBackground;
modalVC.lifecycleEventDelegates.add(self);

self.dispatchEvent(
for: .onModalWillPresent,
withPayload: [
"isAnimated": isAnimated,
]
);

closestVC.present(modalVC, animated: isAnimated) {
self.dispatchEvent(
for: .onModalDidPresent,
withPayload: [
"isAnimated": isAnimated,
]
);
};

closestVC.present(modalVC, animated: isAnimated);
resolveBlock([:]);

case "dismissModal":
Expand All @@ -169,7 +202,7 @@ extension RNIModalSheetViewDelegate: RNIContentViewDelegate {
);

modalSheetController.dismiss(animated: isAnimated) {
self.modalSheetController = nil;
self.discardCurrentModalControllerIfNeeded();
};

resolveBlock([:]);
Expand Down Expand Up @@ -201,3 +234,61 @@ extension RNIModalSheetViewDelegate: RNIContentViewDelegate {
};
#endif
};

// MARK: - RNIModalSheetViewDelegate+ViewControllerLifecycleNotifiable
// -------------------------------------------------------------------

extension RNIModalSheetViewDelegate: ViewControllerLifecycleNotifiable {

public func notifyOnViewWillAppear(
sender: UIViewController,
isAnimated: Bool,
isFirstAppearance: Bool
) {
self.dispatchEvent(
for: .onModalWillShow,
withPayload: [
"isAnimated": isAnimated,
"isFirstAppearance": isFirstAppearance,
]
);
};

public func notifyOnViewDidAppear(
sender: UIViewController,
isAnimated: Bool,
isFirstAppearance: Bool
) {
self.dispatchEvent(
for: .onModalDidShow,
withPayload: [
"isAnimated": isAnimated,
"isFirstAppearance": isFirstAppearance,
]
);
};

public func notifyOnViewWillDisappear(
sender: UIViewController,
isAnimated: Bool
) {
self.dispatchEvent(
for: .onModalWillHide,
withPayload: [
"isAnimated": isAnimated,
]
);
};

public func notifyOnViewDidDisappear(
sender: UIViewController,
isAnimated: Bool
) {
self.dispatchEvent(
for: .onModalDidHide,
withPayload: [
"isAnimated": isAnimated,
]
);
};
};
12 changes: 12 additions & 0 deletions ios/RNIModalSheetView/RNIModalSheetViewManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ - (UIView *)view
{
return [[RNIModalSheetView new] initWithBridge:self.bridge];
}

RNI_EXPORT_VIEW_EVENT(onDidSetViewID, RCTBubblingEventBlock)

RNI_EXPORT_VIEW_EVENT(onModalWillPresent, RCTBubblingEventBlock);
RNI_EXPORT_VIEW_EVENT(onModalDidPresent, RCTBubblingEventBlock);

RNI_EXPORT_VIEW_EVENT(onModalWillShow, RCTBubblingEventBlock);
RNI_EXPORT_VIEW_EVENT(onModalDidShow, RCTBubblingEventBlock);

RNI_EXPORT_VIEW_EVENT(onModalWillHide, RCTBubblingEventBlock);
RNI_EXPORT_VIEW_EVENT(onModalDidHide, RCTBubblingEventBlock);

#endif

@end
Expand Down
36 changes: 25 additions & 11 deletions src/types/CommonModalEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,31 @@ import type { BubblingEventHandler } from 'react-native/Libraries/Types/CodegenT
// MARK: Event Objects
// -------------------

export type OnModalWillPresentPayload = Readonly<{}>;

export type OnModalDidPresentEventPayload = Readonly<{}>;

export type OnModalWillShowEventPayload = Readonly<{}>;

export type OnModalDidShowEventPayload = Readonly<{}>;

export type OnModalWillHideEventPayload = Readonly<{}>;

export type OnModalDidHideEventPayload = Readonly<{}>;
export type OnModalWillPresentPayload = Readonly<{
isAnimated: boolean;
}>;

export type OnModalDidPresentEventPayload = Readonly<{
isAnimated: boolean;
}>;

export type OnModalWillShowEventPayload = Readonly<{
isAnimated: boolean;
isFirstAppearance: boolean;
}>;

export type OnModalDidShowEventPayload = Readonly<{
isAnimated: boolean;
isFirstAppearance: boolean;
}>;

export type OnModalWillHideEventPayload = Readonly<{
isAnimated: boolean;
}>;

export type OnModalDidHideEventPayload = Readonly<{
isAnimated: boolean;
}>;

// MARK: Events
// ------------
Expand Down

0 comments on commit e533692

Please sign in to comment.