Skip to content

Commit

Permalink
💫 Update: ViewControllerLifecycleNotifiable Requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Sep 27, 2024
1 parent ae4054d commit 5dce892
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
9 changes: 6 additions & 3 deletions ios/Temp/ModalSheetPresentationStateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,26 @@ extension ModalSheetPresentationStateMachine: ViewControllerLifecycleNotifiable

public func notifyOnViewWillAppear(
sender: UIViewController,
isAnimated: Bool
isAnimated: Bool,
isFirstAppearance: Bool
) {

self.setState(nextState: .presenting);
};

public func notifyOnViewIsAppearing(
sender: UIViewController,
isAnimated: Bool
isAnimated: Bool,
isFirstAppearance: Bool
) {

self.setState(nextState: .presenting);
};

public func notifyOnViewDidAppear(
sender: UIViewController,
isAnimated: Bool
isAnimated: Bool,
isFirstAppearance: Bool
) {

self.setState(nextState: .presented);
Expand Down
18 changes: 12 additions & 6 deletions ios/Temp/ViewControllerLifecycleNotifiable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ public protocol ViewControllerLifecycleNotifiable: AnyObject {

func notifyOnViewWillAppear(
sender: UIViewController,
isAnimated: Bool
isAnimated: Bool,
isFirstAppearance: Bool
);

func notifyOnViewIsAppearing(
sender: UIViewController,
isAnimated: Bool
isAnimated: Bool,
isFirstAppearance: Bool
);

func notifyOnViewDidAppear(
sender: UIViewController,
isAnimated: Bool
isAnimated: Bool,
isFirstAppearance: Bool
);

func notifyOnViewWillDisappear(
Expand Down Expand Up @@ -57,21 +60,24 @@ public extension ViewControllerLifecycleNotifiable {

func notifyOnViewWillAppear(
sender: UIViewController,
isAnimated: Bool
isAnimated: Bool,
isFirstAppearance: Bool
) {
// no-op
};

func notifyOnViewIsAppearing(
sender: UIViewController,
isAnimated: Bool
isAnimated: Bool,
isFirstAppearance: Bool
) {
// no-op
};

func notifyOnViewDidAppear(
sender: UIViewController,
isAnimated: Bool
isAnimated: Bool,
isFirstAppearance: Bool
) {
// no-op
};
Expand Down
29 changes: 26 additions & 3 deletions ios/Temp/ViewControllerLifecycleNotifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import DGSwiftUtilities

open class ViewControllerLifecycleNotifier: UIViewController {

private(set) var isAppearingForTheFirstTime = true;

private(set) public var lifecycleEventDelegates:
MulticastDelegate<ViewControllerLifecycleNotifiable> = .init();

Expand Down Expand Up @@ -43,7 +45,11 @@ open class ViewControllerLifecycleNotifier: UIViewController {

public override func viewWillAppear(_ animated: Bool) {
self.lifecycleEventDelegates.invoke {
$0.notifyOnViewWillAppear(sender: self, isAnimated: animated);
$0.notifyOnViewWillAppear(
sender: self,
isAnimated: animated,
isFirstAppearance: self.isAppearingForTheFirstTime
);
};

#if DEBUG
Expand All @@ -54,6 +60,7 @@ open class ViewControllerLifecycleNotifier: UIViewController {
"\n - className:", self.className,
"\n - animated:", animated,
"\n - isBeingPresented:", self.isBeingPresented,
"\n - isAppearingForTheFirstTime:", self.isAppearingForTheFirstTime,
"\n"
);
};
Expand All @@ -62,7 +69,11 @@ open class ViewControllerLifecycleNotifier: UIViewController {

public override func viewIsAppearing(_ animated: Bool) {
self.lifecycleEventDelegates.invoke {
$0.notifyOnViewIsAppearing(sender: self, isAnimated: animated);
$0.notifyOnViewIsAppearing(
sender: self,
isAnimated: animated,
isFirstAppearance: self.isAppearingForTheFirstTime
);
};

#if DEBUG
Expand All @@ -73,15 +84,26 @@ open class ViewControllerLifecycleNotifier: UIViewController {
"\n - className:", self.className,
"\n - animated:", animated,
"\n - isBeingPresented:", self.isBeingPresented,
"\n - isAppearingForTheFirstTime:", self.isAppearingForTheFirstTime,
"\n"
);
};
#endif
};

public override func viewDidAppear(_ animated: Bool) {
defer {
if self.isAppearingForTheFirstTime {
self.isAppearingForTheFirstTime = false;
};
};

self.lifecycleEventDelegates.invoke {
$0.notifyOnViewDidAppear(sender: self, isAnimated: animated);
$0.notifyOnViewDidAppear(
sender: self,
isAnimated: animated,
isFirstAppearance: self.isAppearingForTheFirstTime
);
};

#if DEBUG
Expand All @@ -92,6 +114,7 @@ open class ViewControllerLifecycleNotifier: UIViewController {
"\n - className:", self.className,
"\n - animated:", animated,
"\n - isBeingPresented:", self.isPresentedAsModal,
"\n - isAppearingForTheFirstTime:", self.isAppearingForTheFirstTime,
"\n"
);
};
Expand Down

0 comments on commit 5dce892

Please sign in to comment.