-
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:
ViewControllerLifecycleNotifier
- Loading branch information
1 parent
8a61ff7
commit 5c48449
Showing
1 changed file
with
182 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
// | ||
// ViewControllerLifecycleNotifier.swift | ||
// react-native-ios-modal | ||
// | ||
// Created by Dominic Go on 9/27/24. | ||
// | ||
|
||
import UIKit | ||
import DGSwiftUtilities | ||
|
||
|
||
open class ViewControllerLifecycleNotifier: UIViewController { | ||
|
||
#if DEBUG | ||
public static var shouldLog = true; | ||
#endif | ||
|
||
private(set) public var lifecycleEventDelegates: | ||
MulticastDelegate<ViewControllerLifecycleNotifiable> = .init(); | ||
|
||
// MARK: - Init | ||
// ------------ | ||
|
||
public convenience init() { | ||
self.init(nibName: nil, bundle: nil); | ||
}; | ||
|
||
// MARK: - View Controller Lifecycle | ||
// --------------------------------- | ||
|
||
public override func viewDidLoad() { | ||
self.lifecycleEventDelegates.invoke { | ||
$0.notifyOnViewDidLoad(sender: self); | ||
}; | ||
|
||
#if DEBUG | ||
if Self.shouldLog { | ||
print( | ||
"ViewControllerLifecycleNotifier.\(#function)", | ||
"\n - instance:", Unmanaged.passUnretained(self).toOpaque(), | ||
"\n - className:", self.className, | ||
"\n" | ||
); | ||
}; | ||
#endif | ||
}; | ||
|
||
public override func viewWillAppear(_ animated: Bool) { | ||
self.lifecycleEventDelegates.invoke { | ||
$0.notifyOnViewWillAppear(sender: self, isAnimated: animated); | ||
}; | ||
|
||
#if DEBUG | ||
if Self.shouldLog { | ||
print( | ||
"ViewControllerLifecycleNotifier.\(#function)", | ||
"\n - instance:", Unmanaged.passUnretained(self).toOpaque(), | ||
"\n - className:", self.className, | ||
"\n - animated:", animated, | ||
"\n" | ||
); | ||
}; | ||
#endif | ||
}; | ||
|
||
public override func viewIsAppearing(_ animated: Bool) { | ||
self.lifecycleEventDelegates.invoke { | ||
$0.notifyOnViewIsAppearing(sender: self, isAnimated: animated); | ||
}; | ||
|
||
#if DEBUG | ||
if Self.shouldLog { | ||
print( | ||
"ViewControllerLifecycleNotifier.\(#function)", | ||
"\n - instance:", Unmanaged.passUnretained(self).toOpaque(), | ||
"\n - className:", self.className, | ||
"\n - animated:", animated, | ||
"\n" | ||
); | ||
}; | ||
#endif | ||
}; | ||
|
||
public override func viewDidAppear(_ animated: Bool) { | ||
self.lifecycleEventDelegates.invoke { | ||
$0.notifyOnViewDidAppear(sender: self, isAnimated: animated); | ||
}; | ||
|
||
#if DEBUG | ||
if Self.shouldLog { | ||
print( | ||
"ViewControllerLifecycleNotifier.\(#function)", | ||
"\n - instance:", Unmanaged.passUnretained(self).toOpaque(), | ||
"\n - className:", self.className, | ||
"\n - animated:", animated, | ||
"\n" | ||
); | ||
}; | ||
#endif | ||
}; | ||
|
||
public override func viewWillDisappear(_ animated: Bool) { | ||
self.lifecycleEventDelegates.invoke { | ||
$0.notifyOnViewWillDisappear(sender: self, isAnimated: animated); | ||
}; | ||
|
||
#if DEBUG | ||
if Self.shouldLog { | ||
print( | ||
"ViewControllerLifecycleNotifier.\(#function)", | ||
"\n - instance:", Unmanaged.passUnretained(self).toOpaque(), | ||
"\n - className:", self.className, | ||
"\n - animated:", animated, | ||
"\n" | ||
); | ||
}; | ||
#endif | ||
}; | ||
|
||
public override func viewDidDisappear(_ animated: Bool) { | ||
self.lifecycleEventDelegates.invoke { | ||
$0.notifyOnViewDidDisappear(sender: self, isAnimated: animated); | ||
}; | ||
|
||
#if DEBUG | ||
if Self.shouldLog { | ||
print( | ||
"ViewControllerLifecycleNotifier.\(#function)", | ||
"\n - instance:", Unmanaged.passUnretained(self).toOpaque(), | ||
"\n - className:", self.className, | ||
"\n - animated:", animated, | ||
"\n" | ||
); | ||
}; | ||
#endif | ||
}; | ||
|
||
public override func viewWillLayoutSubviews() { | ||
self.lifecycleEventDelegates.invoke { | ||
$0.notifyOnViewWillLayoutSubviews(sender: self); | ||
}; | ||
|
||
#if DEBUG | ||
if Self.shouldLog { | ||
print( | ||
"ViewControllerLifecycleNotifier.\(#function)", | ||
"\n - instance:", Unmanaged.passUnretained(self).toOpaque(), | ||
"\n - className:", self.className, | ||
"\n" | ||
); | ||
}; | ||
#endif | ||
}; | ||
|
||
public override func viewDidLayoutSubviews() { | ||
self.lifecycleEventDelegates.invoke { | ||
$0.notifyOnViewWillLayoutSubviews(sender: self); | ||
}; | ||
|
||
#if DEBUG | ||
if Self.shouldLog { | ||
print( | ||
"ViewControllerLifecycleNotifier.\(#function)", | ||
"\n - instance:", Unmanaged.passUnretained(self).toOpaque(), | ||
"\n - className:", self.className, | ||
"\n" | ||
); | ||
}; | ||
#endif | ||
}; | ||
|
||
// MARK: - Methods | ||
// --------------- | ||
|
||
public func addEventDelegate(_ delegate: AnyObject){ | ||
guard let delegate = delegate as? ViewControllerLifecycleNotifiable else { | ||
return; | ||
}; | ||
|
||
self.lifecycleEventDelegates.add(delegate); | ||
}; | ||
}; |