Commit 969344f3 by Daniel Dahan

reworked Motion class delegation handling

parent adc55d38
...@@ -269,14 +269,14 @@ internal extension Motion { ...@@ -269,14 +269,14 @@ internal extension Motion {
fullScreenSnapshot?.removeFromSuperview() fullScreenSnapshot?.removeFromSuperview()
} }
override func complete(isFinished finished: Bool) { override func complete(isFinished: Bool) {
guard isTransitioning else { guard isTransitioning else {
return return
} }
context.clean() context.clean()
if finished && isPresenting && toOverFullScreen { if isFinished && isPresenting && toOverFullScreen {
// finished presenting a overFullScreen VC // finished presenting a overFullScreen VC
context.unhide(rootView: toView) context.unhide(rootView: toView)
context.removeSnapshots(rootView: toView) context.removeSnapshots(rootView: toView)
...@@ -285,7 +285,7 @@ internal extension Motion { ...@@ -285,7 +285,7 @@ internal extension Motion {
fromViewController!.motionStoredSnapshot = container fromViewController!.motionStoredSnapshot = container
fromView.removeFromSuperview() fromView.removeFromSuperview()
fromView.addSubview(container) fromView.addSubview(container)
} else if !finished && !isPresenting && fromOverFullScreen { } else if !isFinished && !isPresenting && fromOverFullScreen {
// cancelled dismissing a overFullScreen VC // cancelled dismissing a overFullScreen VC
context.unhide(rootView: fromView) context.unhide(rootView: fromView)
context.removeSnapshots(rootView: fromView) context.removeSnapshots(rootView: fromView)
...@@ -301,7 +301,7 @@ internal extension Motion { ...@@ -301,7 +301,7 @@ internal extension Motion {
} }
// move fromView & toView back from our container back to the one supplied by UIKit // move fromView & toView back from our container back to the one supplied by UIKit
if (toOverFullScreen && isFinished) || (fromOverFullScreen && !finished) { if (toOverFullScreen && isFinished) || (fromOverFullScreen && !isFinished) {
transitionContainer.addSubview(isFinished ? fromView : toView) transitionContainer.addSubview(isFinished ? fromView : toView)
} }
...@@ -320,6 +320,23 @@ internal extension Motion { ...@@ -320,6 +320,23 @@ internal extension Motion {
let fvc = fromViewController let fvc = fromViewController
let tvc = toViewController let tvc = toViewController
resetTransition()
super.complete(isFinished: isFinished)
if isFinished {
processEndTransitionDelegation(transitionContext: tContext, fromViewController: fvc, toViewController: tvc)
} else {
processCancelTransitionDelegation(transitionContext: tContext, fromViewController: fvc, toViewController: tvc)
}
tContext?.completeTransition(isFinished)
}
}
fileprivate extension Motion {
/// Resets the transition values.
func resetTransition() {
transitionContext = nil transitionContext = nil
fromViewController = nil fromViewController = nil
toViewController = nil toViewController = nil
...@@ -329,83 +346,13 @@ internal extension Motion { ...@@ -329,83 +346,13 @@ internal extension Motion {
forceNonInteractive = false forceNonInteractive = false
insertToViewFirst = false insertToViewFirst = false
defaultAnimation = .auto defaultAnimation = .auto
super.complete(isFinished: isFinished)
if finished {
if let fvc = fvc, let tvc = tvc {
processForMotionDelegate(viewController: fvc) { [weak self] in
guard let s = self else {
return
}
$0.motion?(motion: s, didEndTransitionTo: tvc)
$0.motionDidEndTransition?(motion: s)
}
processForMotionDelegate(viewController: tvc) { [weak self] in
guard let s = self else {
return
}
$0.motion?(motion: s, didEndTransitionFrom: fvc)
$0.motionDidEndTransition?(motion: s)
}
}
tContext?.finishInteractiveTransition()
} else {
if let fvc = fvc, let tvc = tvc {
processForMotionDelegate(viewController: fvc) { [weak self] in
guard let s = self else {
return
}
$0.motion?(motion: s, didCancelTransitionTo: tvc)
$0.motionDidCancelTransition?(motion: s)
}
processForMotionDelegate(viewController: tvc) { [weak self] in
guard let s = self else {
return
}
$0.motion?(motion: s, didCancelTransitionFrom: fvc)
$0.motionDidCancelTransition?(motion: s)
}
}
tContext?.cancelInteractiveTransition()
}
tContext?.completeTransition(finished)
} }
} }
fileprivate extension Motion { fileprivate extension Motion {
/// Prepares the from and to view controllers. /// Prepares the from and to view controllers.
func prepareViewControllers() { func prepareViewControllers() {
guard let fvc = fromViewController, let tvc = toViewController else { processStartTransitionDelegation(fromViewController: fromViewController, toViewController: toViewController)
return
}
processForMotionDelegate(viewController: fvc) { [weak self] in
guard let s = self else {
return
}
$0.motionWillStartTransition?(motion: s)
$0.motion?(motion: s, willStartTransitionTo: tvc)
}
processForMotionDelegate(viewController: tvc) { [weak self] in
guard let s = self else {
return
}
$0.motionWillStartTransition?(motion: s)
$0.motion?(motion: s, willStartTransitionFrom: fvc)
}
} }
/// Prepares the snapshot view, which hides any flashing that may occur. /// Prepares the snapshot view, which hides any flashing that may occur.
...@@ -475,6 +422,111 @@ fileprivate extension Motion { ...@@ -475,6 +422,111 @@ fileprivate extension Motion {
} }
#endif #endif
} }
/**
Processes the start transition delegation methods.
- Parameter fromViewController: An optional UIViewController.
- Parameter toViewController: An optional UIViewController.
*/
func processStartTransitionDelegation(fromViewController: UIViewController?, toViewController: UIViewController?) {
guard let fvc = fromViewController else {
return
}
guard let tvc = toViewController else {
return
}
processForMotionDelegate(viewController: fvc) { [weak self] in
guard let s = self else {
return
}
$0.motion?(motion: s, willStartTransitionTo: tvc)
$0.motionWillStartTransition?(motion: s)
}
processForMotionDelegate(viewController: tvc) { [weak self] in
guard let s = self else {
return
}
$0.motion?(motion: s, willStartTransitionFrom: fvc)
$0.motionWillStartTransition?(motion: s)
}
}
/**
Processes the end transition delegation methods.
- Parameter transitionContext: An optional UIViewControllerContextTransitioning.
- Parameter fromViewController: An optional UIViewController.
- Parameter toViewController: An optional UIViewController.
*/
func processEndTransitionDelegation(transitionContext: UIViewControllerContextTransitioning?, fromViewController: UIViewController?, toViewController: UIViewController?) {
guard let fvc = fromViewController else {
return
}
guard let tvc = toViewController else {
return
}
processForMotionDelegate(viewController: fvc) { [weak self] in
guard let s = self else {
return
}
$0.motion?(motion: s, didEndTransitionTo: tvc)
$0.motionDidEndTransition?(motion: s)
}
processForMotionDelegate(viewController: tvc) { [weak self] in
guard let s = self else {
return
}
$0.motion?(motion: s, didEndTransitionFrom: fvc)
$0.motionDidEndTransition?(motion: s)
}
transitionContext?.finishInteractiveTransition()
}
/**
Processes the cancel transition delegation methods.
- Parameter transitionContext: An optional UIViewControllerContextTransitioning.
- Parameter fromViewController: An optional UIViewController.
- Parameter toViewController: An optional UIViewController.
*/
func processCancelTransitionDelegation(transitionContext: UIViewControllerContextTransitioning?, fromViewController: UIViewController?, toViewController: UIViewController?) {
guard let fvc = fromViewController else {
return
}
guard let tvc = toViewController else {
return
}
processForMotionDelegate(viewController: fvc) { [weak self] in
guard let s = self else {
return
}
$0.motion?(motion: s, didCancelTransitionTo: tvc)
$0.motionDidCancelTransition?(motion: s)
}
processForMotionDelegate(viewController: tvc) { [weak self] in
guard let s = self else {
return
}
$0.motion?(motion: s, didCancelTransitionFrom: fvc)
$0.motionDidCancelTransition?(motion: s)
}
transitionContext?.finishInteractiveTransition()
}
} }
fileprivate extension Motion { fileprivate extension Motion {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment