-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⭐️ Impl:
ModalViewControllerEventsNotifiable
- Loading branch information
1 parent
8bfa246
commit 8e76eef
Showing
4 changed files
with
202 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// ModalViewControllerEventsNotifiable.swift | ||
// react-native-ios-modal | ||
// | ||
// Created by Dominic Go on 9/30/24. | ||
// | ||
|
||
import UIKit | ||
|
||
|
||
public protocol ModalViewControllerEventsNotifiable { | ||
|
||
func notifyOnModalWillPresent( | ||
sender: UIViewController, | ||
isAnimated: Bool | ||
); | ||
|
||
func notifyOnModalDidPresent( | ||
sender: UIViewController, | ||
isAnimated: Bool | ||
); | ||
|
||
func notifyOnModalWillDismiss( | ||
sender: UIViewController, | ||
isAnimated: Bool | ||
); | ||
|
||
func notifyOnModalDidDismiss( | ||
sender: UIViewController, | ||
isAnimated: Bool | ||
); | ||
}; | ||
|
||
// MARK: - ModalViewControllerEventsNotifiable | ||
// ------------------------------------------- | ||
|
||
public extension ModalViewControllerEventsNotifiable { | ||
|
||
func notifyOnModalWillPresent( | ||
sender: UIViewController, | ||
isAnimated: Bool | ||
) { | ||
// no-op | ||
}; | ||
|
||
func notifyOnModalDidPresent( | ||
sender: UIViewController, | ||
isAnimated: Bool | ||
) { | ||
// no-op | ||
}; | ||
|
||
func notifyOnModalWillDismiss( | ||
sender: UIViewController, | ||
isAnimated: Bool | ||
) { | ||
// no-op | ||
}; | ||
|
||
func notifyOnModalDidDismiss( | ||
sender: UIViewController, | ||
isAnimated: Bool | ||
) { | ||
// no-op | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// | ||
// ModalViewControllerLifecycleNotifier.swift | ||
// react-native-ios-modal | ||
// | ||
// Created by Dominic Go on 9/30/24. | ||
// | ||
|
||
import UIKit | ||
import DGSwiftUtilities | ||
|
||
|
||
open class ModalViewControllerLifecycleNotifier: ViewControllerLifecycleNotifier { | ||
|
||
public var isExplicitlySomeKindOfModal = false; | ||
public var isExplicitlyBeingDismissed = false; | ||
|
||
private(set) public var modalLifecycleEventDelegates: | ||
MulticastDelegate<ModalViewControllerEventsNotifiable> = .init(); | ||
|
||
// MARK: - View Controller Lifecycle | ||
// --------------------------------- | ||
|
||
public override func viewWillAppear(_ animated: Bool) { | ||
defer { | ||
super.viewWillAppear(animated); | ||
}; | ||
|
||
guard self.isAppearingForTheFirstTime else { | ||
return; | ||
}; | ||
|
||
self.isExplicitlySomeKindOfModal = | ||
self.isBeingPresented | ||
|| self.presentingViewController?.presentedViewController === self; | ||
|
||
guard self.isExplicitlySomeKindOfModal else { | ||
return; | ||
}; | ||
|
||
self.modalLifecycleEventDelegates.invoke { | ||
$0.notifyOnModalWillPresent( | ||
sender: self, | ||
isAnimated: animated | ||
); | ||
}; | ||
}; | ||
|
||
public override func viewDidAppear(_ animated: Bool) { | ||
defer { | ||
super.viewDidAppear(animated); | ||
}; | ||
|
||
guard self.isAppearingForTheFirstTime, | ||
self.isExplicitlySomeKindOfModal | ||
else { | ||
return; | ||
}; | ||
|
||
self.modalLifecycleEventDelegates.invoke { | ||
$0.notifyOnModalDidPresent( | ||
sender: self, | ||
isAnimated: animated | ||
); | ||
}; | ||
}; | ||
|
||
public override func viewWillDisappear(_ animated: Bool) { | ||
defer { | ||
super.viewWillDisappear(animated); | ||
}; | ||
|
||
guard self.isBeingDismissed, | ||
self.isExplicitlySomeKindOfModal | ||
else { | ||
return; | ||
}; | ||
|
||
guard animated, | ||
let transitionCoordinator = self.transitionCoordinator | ||
else { | ||
self.isExplicitlyBeingDismissed = true; | ||
|
||
self.modalLifecycleEventDelegates.invoke { | ||
$0.notifyOnModalWillDismiss( | ||
sender: self, | ||
isAnimated: false | ||
); | ||
}; | ||
return; | ||
}; | ||
|
||
transitionCoordinator.notifyWhenInteractionChanges { context in | ||
guard !context.isCancelled else { | ||
return; | ||
}; | ||
|
||
self.isExplicitlyBeingDismissed = true; | ||
self.modalLifecycleEventDelegates.invoke { | ||
$0.notifyOnModalWillDismiss( | ||
sender: self, | ||
isAnimated: context.isAnimated | ||
); | ||
}; | ||
}; | ||
}; | ||
|
||
public override func viewDidDisappear(_ animated: Bool) { | ||
defer { | ||
super.viewDidDisappear(animated); | ||
}; | ||
|
||
guard self.isBeingDismissed, | ||
self.isExplicitlySomeKindOfModal, | ||
self.isExplicitlyBeingDismissed | ||
else { | ||
return; | ||
}; | ||
|
||
self.modalLifecycleEventDelegates.invoke { | ||
$0.notifyOnModalDidDismiss( | ||
sender: self, | ||
isAnimated: animated | ||
); | ||
}; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters