-
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:
ModalSheetPresentationStateMachine
- Loading branch information
1 parent
60424ef
commit 5bb4fe8
Showing
2 changed files
with
115 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,110 @@ | ||
// | ||
// ModalSheetPresentationStateMachine.swift | ||
// react-native-ios-modal | ||
// | ||
// Created by Dominic Go on 9/27/24. | ||
// | ||
|
||
import UIKit | ||
import DGSwiftUtilities | ||
|
||
|
||
public class ModalSheetPresentationStateMachine { | ||
|
||
public var prevState: ModalSheetState?; | ||
public var currentState: ModalSheetState = .dismissed; | ||
|
||
public var didPresent = false; | ||
public var didDismissAfterPresented = false; | ||
|
||
public var eventDelegates: | ||
MulticastDelegate<ModalSheetStateEventNotifiable> = .init(); | ||
|
||
public func setStateExplicit(nextState: ModalSheetState){ | ||
let prevState = self.prevState; | ||
let currentState = self.currentState; | ||
|
||
self.prevState = self.currentState; | ||
self.currentState = nextState; | ||
|
||
switch (prevState, currentState, nextState) { | ||
case (_, let currentState, let nextState) | ||
where !currentState.isPresented && nextState.isPresented: | ||
|
||
self.didPresent = true; | ||
|
||
case (_, let currentState, let nextState) where | ||
!currentState.isPresented && nextState.isPresented: | ||
|
||
self.didDismissAfterPresented = true; | ||
|
||
default: | ||
break; | ||
}; | ||
}; | ||
|
||
public func setState(nextState: ModalSheetState){ | ||
switch (self.prevState, self.currentState, nextState) { | ||
|
||
default: | ||
break; | ||
}; | ||
|
||
self.setStateExplicit(nextState: nextState); | ||
}; | ||
|
||
public func reset(){ | ||
self.prevState = nil; | ||
self.currentState = .dismissed; | ||
|
||
self.didPresent = false; | ||
self.didDismissAfterPresented = false; | ||
}; | ||
}; | ||
|
||
extension ModalSheetPresentationStateMachine: ViewControllerLifecycleNotifiable { | ||
|
||
public func notifyOnViewDidLoad(sender: UIViewController) { | ||
// no-op | ||
}; | ||
|
||
public func notifyOnViewWillAppear( | ||
sender: UIViewController, | ||
isAnimated: Bool | ||
) { | ||
|
||
self.setState(nextState: .presenting); | ||
}; | ||
|
||
public func notifyOnViewIsAppearing( | ||
sender: UIViewController, | ||
isAnimated: Bool | ||
) { | ||
|
||
self.setState(nextState: .presenting); | ||
}; | ||
|
||
public func notifyOnViewDidAppear( | ||
sender: UIViewController, | ||
isAnimated: Bool | ||
) { | ||
|
||
self.setState(nextState: .presented); | ||
}; | ||
|
||
public func notifyOnViewWillDisappear( | ||
sender: UIViewController, | ||
isAnimated: Bool | ||
) { | ||
|
||
self.setState(nextState: .dismissing); | ||
}; | ||
|
||
public func notifyOnViewDidDisappear( | ||
sender: UIViewController, | ||
isAnimated: Bool | ||
) { | ||
|
||
self.setState(nextState: .dismissed); | ||
}; | ||
}; |
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