Skip to content

Commit

Permalink
⭐️ Impl: ModalSheetPresentationStateMachine.eventDelegates
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Sep 27, 2024
1 parent 33c5c99 commit 2ae97ff
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
47 changes: 47 additions & 0 deletions ios/Temp/ModalSheetPresentationStateEventsNotifiable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// ModalSheetPresentationStateEventsNotifiable.swift
// react-native-ios-modal
//
// Created by Dominic Go on 9/27/24.
//

import Foundation

public protocol ModalSheetPresentationStateEventsNotifiable: AnyObject {

func onModalSheetStateWillChange(
sender: ModalSheetPresentationStateMachine,
prevState: ModalSheetState?,
currentState: ModalSheetState,
nextState: ModalSheetState
);

func onModalSheetStateDidChange(
sender: ModalSheetPresentationStateMachine,
prevState: ModalSheetState?,
currentState: ModalSheetState
);
};

// MARK: - ModalSheetPresentationStateEventsNotifiable+Default
// -----------------------------------------------------------

public extension ModalSheetPresentationStateEventsNotifiable {

func onModalSheetStateWillChange(
sender: ModalSheetPresentationStateMachine,
prevState: ModalSheetState?,
currentState: ModalSheetState,
nextState: ModalSheetState
) {
// no-op
};

func onModalSheetStateDidChange(
sender: ModalSheetPresentationStateMachine,
prevState: ModalSheetState?,
currentState: ModalSheetState
) {
// no-op
};
};
19 changes: 19 additions & 0 deletions ios/Temp/ModalSheetPresentationStateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class ModalSheetPresentationStateMachine {
public var didPresent = false;
public var didDismissAfterPresented = false;

public var eventDelegates:
MulticastDelegate<ModalSheetPresentationStateEventsNotifiable> = .init();

// MARK: - Methods
// ---------------
Expand All @@ -39,6 +41,15 @@ public class ModalSheetPresentationStateMachine {
);
};
#endif

self.eventDelegates.invoke {
$0.onModalSheetStateWillChange(
sender: self,
prevState: prevState,
currentState: currentState,
nextState: nextState
);
};

self.prevState = self.currentState;
self.currentState = nextState;
Expand Down Expand Up @@ -71,6 +82,14 @@ public class ModalSheetPresentationStateMachine {
);
};
#endif

self.eventDelegates.invoke {
$0.onModalSheetStateDidChange(
sender: self,
prevState: self.prevState,
currentState: self.currentState
);
};
};

public func setState(nextState: ModalSheetState) {
Expand Down

0 comments on commit 2ae97ff

Please sign in to comment.