From 5dce892dbc5ea9b7d0410fff82b80cca59131c27 Mon Sep 17 00:00:00 2001 From: Dominic Go Date: Sat, 28 Sep 2024 06:50:40 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=AB=20Update:=20`ViewControllerLifecyc?= =?UTF-8?q?leNotifiable`=20Requirements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ModalSheetPresentationStateMachine.swift | 9 ++++-- .../ViewControllerLifecycleNotifiable.swift | 18 ++++++++---- .../ViewControllerLifecycleNotifier.swift | 29 +++++++++++++++++-- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/ios/Temp/ModalSheetPresentationStateMachine.swift b/ios/Temp/ModalSheetPresentationStateMachine.swift index 52f8b545..3560be8d 100644 --- a/ios/Temp/ModalSheetPresentationStateMachine.swift +++ b/ios/Temp/ModalSheetPresentationStateMachine.swift @@ -147,7 +147,8 @@ extension ModalSheetPresentationStateMachine: ViewControllerLifecycleNotifiable public func notifyOnViewWillAppear( sender: UIViewController, - isAnimated: Bool + isAnimated: Bool, + isFirstAppearance: Bool ) { self.setState(nextState: .presenting); @@ -155,7 +156,8 @@ extension ModalSheetPresentationStateMachine: ViewControllerLifecycleNotifiable public func notifyOnViewIsAppearing( sender: UIViewController, - isAnimated: Bool + isAnimated: Bool, + isFirstAppearance: Bool ) { self.setState(nextState: .presenting); @@ -163,7 +165,8 @@ extension ModalSheetPresentationStateMachine: ViewControllerLifecycleNotifiable public func notifyOnViewDidAppear( sender: UIViewController, - isAnimated: Bool + isAnimated: Bool, + isFirstAppearance: Bool ) { self.setState(nextState: .presented); diff --git a/ios/Temp/ViewControllerLifecycleNotifiable.swift b/ios/Temp/ViewControllerLifecycleNotifiable.swift index 8feacab5..318c0f35 100644 --- a/ios/Temp/ViewControllerLifecycleNotifiable.swift +++ b/ios/Temp/ViewControllerLifecycleNotifiable.swift @@ -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( @@ -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 }; diff --git a/ios/Temp/ViewControllerLifecycleNotifier.swift b/ios/Temp/ViewControllerLifecycleNotifier.swift index eaa06efe..ca97c932 100644 --- a/ios/Temp/ViewControllerLifecycleNotifier.swift +++ b/ios/Temp/ViewControllerLifecycleNotifier.swift @@ -11,6 +11,8 @@ import DGSwiftUtilities open class ViewControllerLifecycleNotifier: UIViewController { + private(set) var isAppearingForTheFirstTime = true; + private(set) public var lifecycleEventDelegates: MulticastDelegate = .init(); @@ -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 @@ -54,6 +60,7 @@ open class ViewControllerLifecycleNotifier: UIViewController { "\n - className:", self.className, "\n - animated:", animated, "\n - isBeingPresented:", self.isBeingPresented, + "\n - isAppearingForTheFirstTime:", self.isAppearingForTheFirstTime, "\n" ); }; @@ -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 @@ -73,6 +84,7 @@ open class ViewControllerLifecycleNotifier: UIViewController { "\n - className:", self.className, "\n - animated:", animated, "\n - isBeingPresented:", self.isBeingPresented, + "\n - isAppearingForTheFirstTime:", self.isAppearingForTheFirstTime, "\n" ); }; @@ -80,8 +92,18 @@ open class ViewControllerLifecycleNotifier: UIViewController { }; 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 @@ -92,6 +114,7 @@ open class ViewControllerLifecycleNotifier: UIViewController { "\n - className:", self.className, "\n - animated:", animated, "\n - isBeingPresented:", self.isPresentedAsModal, + "\n - isAppearingForTheFirstTime:", self.isAppearingForTheFirstTime, "\n" ); };