Skip to content

Commit

Permalink
⭐️ Impl: ViewControllerLifecycleNotifier
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Sep 26, 2024
1 parent 8a61ff7 commit 5c48449
Showing 1 changed file with 182 additions and 0 deletions.
182 changes: 182 additions & 0 deletions ios/Temp/ViewControllerLifecycleNotifier.swift
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);
};
};

0 comments on commit 5c48449

Please sign in to comment.