Commit ca3d1923 by Daniel Dahan

updated animators, plugins, and preprocessors in MotionController to no longer…

updated animators, plugins, and preprocessors in MotionController to no longer use unwrapped optionals
parent 9fa456da
...@@ -610,7 +610,11 @@ fileprivate extension Motion { ...@@ -610,7 +610,11 @@ fileprivate extension Motion {
insertToViewFirst = true insertToViewFirst = true
} }
for v in animators { guard let a = animators else {
return
}
for v in a {
(v as? MotionHasInsertOrder)?.insertToViewFirst = insertToViewFirst (v as? MotionHasInsertOrder)?.insertToViewFirst = insertToViewFirst
} }
} }
......
...@@ -107,16 +107,16 @@ public class MotionController: NSObject { ...@@ -107,16 +107,16 @@ public class MotionController: NSObject {
internal var isFinished = true internal var isFinished = true
/// An Array of MotionPreprocessors used during a transition. /// An Array of MotionPreprocessors used during a transition.
internal var preprocessors: [MotionPreprocessor]! internal var preprocessors: [MotionPreprocessor]?
/// An Array of MotionAnimators used during a transition. /// An Array of MotionAnimators used during a transition.
internal var animators: [MotionAnimator]! internal var animators: [MotionAnimator]?
/// An Array of MotionPlugins used during a transition. /// An Array of MotionPlugins used during a transition.
internal var plugins: [MotionPlugin]! internal var plugins: [MotionPlugin]?
/// The matching fromViews to toViews based on the motionIdentifier value. /// The matching fromViews to toViews based on the motionIdentifier value.
internal var transitionPairs: [(fromViews: [UIView], toViews: [UIView])]! internal var transitionPairs: [(fromViews: [UIView], toViews: [UIView])]?
/// Plugins that are enabled during the transition. /// Plugins that are enabled during the transition.
internal static var enabledPlugins = [MotionPlugin.Type]() internal static var enabledPlugins = [MotionPlugin.Type]()
...@@ -155,16 +155,22 @@ fileprivate extension MotionController { ...@@ -155,16 +155,22 @@ fileprivate extension MotionController {
/// Updates the animators. /// Updates the animators.
func updateAnimators() { func updateAnimators() {
let v = elapsedTime * totalDuration let v = elapsedTime * totalDuration
for a in animators {
a.seek(to: v) animators?.forEach {
$0.seek(to: v)
} }
} }
/// Updates the plugins. /// Updates the plugins.
func updatePlugins() { func updatePlugins() {
guard let p = plugins else {
return
}
let v = elapsedTime * totalDuration let v = elapsedTime * totalDuration
for p in plugins where p.requirePerFrameCallback {
p.seek(to: v) for plugin in p where plugin.requirePerFrameCallback {
plugin.seek(to: v)
} }
} }
} }
...@@ -240,8 +246,8 @@ public extension MotionController { ...@@ -240,8 +246,8 @@ public extension MotionController {
} }
var v: TimeInterval = 0 var v: TimeInterval = 0
for a in animators { animators?.forEach {
v = max(v, a.resume(at: elapsedTime * totalDuration, isReversed: false)) v = max(v, $0.resume(at: elapsedTime * totalDuration, isReversed: false))
} }
complete(after: v, isFinished: true) complete(after: v, isFinished: true)
...@@ -264,13 +270,16 @@ public extension MotionController { ...@@ -264,13 +270,16 @@ public extension MotionController {
} }
var v: TimeInterval = 0 var v: TimeInterval = 0
for a in animators { let et = elapsedTime
var t = elapsedTime
animators?.forEach {
var t = et
if t < 0 { if t < 0 {
t = -t t = -t
} }
v = max(v, a.resume(at: t * totalDuration, isReversed: true)) v = max(v, $0.resume(at: t * totalDuration, isReversed: true))
} }
complete(after: v, isFinished: false) complete(after: v, isFinished: false)
...@@ -295,8 +304,8 @@ public extension MotionController { ...@@ -295,8 +304,8 @@ public extension MotionController {
let s = MotionTransitionState(transitions: transitions) let s = MotionTransitionState(transitions: transitions)
let v = context.transitionPairedView(for: view) ?? view let v = context.transitionPairedView(for: view) ?? view
for a in animators { animators?.forEach {
a.apply(state: s, to: v) $0.apply(state: s, to: v)
} }
} }
} }
...@@ -328,7 +337,11 @@ internal extension MotionController { ...@@ -328,7 +337,11 @@ internal extension MotionController {
transitionPairs = [([UIView], [UIView])]() transitionPairs = [([UIView], [UIView])]()
for a in animators { guard let v = animators else {
return
}
for a in v {
let fv = context.fromViews.filter { (view: UIView) -> Bool in let fv = context.fromViews.filter { (view: UIView) -> Bool in
return a.canAnimate(view: view, isAppearing: false) return a.canAnimate(view: view, isAppearing: false)
} }
...@@ -337,7 +350,7 @@ internal extension MotionController { ...@@ -337,7 +350,7 @@ internal extension MotionController {
return a.canAnimate(view: view, isAppearing: true) return a.canAnimate(view: view, isAppearing: true)
} }
transitionPairs.append((fv, tv)) transitionPairs?.append((fv, tv))
} }
} }
} }
...@@ -349,8 +362,12 @@ internal extension MotionController { ...@@ -349,8 +362,12 @@ internal extension MotionController {
fatalError() fatalError()
} }
for v in preprocessors { preprocessors?.forEach { [weak self] in
v.process(fromViews: context.fromViews, toViews: context.toViews) guard let s = self else {
return
}
$0.process(fromViews: s.context.fromViews, toViews: s.context.toViews)
} }
} }
...@@ -363,26 +380,34 @@ internal extension MotionController { ...@@ -363,26 +380,34 @@ internal extension MotionController {
fatalError() fatalError()
} }
for (fv, tv) in transitionPairs { transitionPairs?.forEach { [weak self] in
for view in fv { guard let s = self else {
context.hide(view: view) return
}
for view in $0.fromViews {
s.context.hide(view: view)
} }
for view in tv { for view in $0.toViews {
context.hide(view: view) s.context.hide(view: view)
} }
} }
var t: TimeInterval = 0 var t: TimeInterval = 0
var v = false var v = false
for (i, a) in animators.enumerated() { if let a = animators, let tp = transitionPairs {
let d = a.animate(fromViews: transitionPairs[i].0, toViews: transitionPairs[i].1) for (i, x) in a.enumerated() {
if d == .infinity {
v = true let d = x.animate(fromViews: tp[i].0, toViews: tp[i].1)
} else {
t = max(t, d) if d == .infinity {
v = true
} else {
t = max(t, d)
}
} }
} }
...@@ -423,32 +448,33 @@ internal extension MotionController { ...@@ -423,32 +448,33 @@ internal extension MotionController {
has completed. has completed.
*/ */
func complete(isFinished: Bool) { func complete(isFinished: Bool) {
guard isTransitioning else { defer {
fatalError()
animators?.forEach {
$0.clean()
}
transitionContainer?.isUserInteractionEnabled = true
transitionPairs = nil
transitionObservers = nil
transitionContainer = nil
completionCallback = nil
container = nil
preprocessors = nil
animators = nil
plugins = nil
context = nil
beginTime = nil
elapsedTime = 0
totalDuration = 0
} }
for a in animators { guard isTransitioning else {
a.clean() return
} }
transitionContainer?.isUserInteractionEnabled = true completionCallback?(isFinished)
let completion = completionCallback
transitionPairs = nil
transitionObservers = nil
transitionContainer = nil
completionCallback = nil
container = nil
preprocessors = nil
animators = nil
plugins = nil
context = nil
beginTime = nil
elapsedTime = 0
totalDuration = 0
completion?(isFinished)
} }
} }
...@@ -485,7 +511,7 @@ fileprivate extension MotionController { ...@@ -485,7 +511,7 @@ fileprivate extension MotionController {
DurationPreprocessor() DurationPreprocessor()
] ]
for v in preprocessors { for v in preprocessors! {
v.context = context v.context = context
} }
} }
...@@ -497,10 +523,10 @@ fileprivate extension MotionController { ...@@ -497,10 +523,10 @@ fileprivate extension MotionController {
] ]
if #available(iOS 10, tvOS 10, *) { if #available(iOS 10, tvOS 10, *) {
animators.append(MotionDefaultAnimator<MotionViewPropertyViewContext>()) animators?.append(MotionDefaultAnimator<MotionViewPropertyViewContext>())
} }
for v in animators { for v in animators! {
v.context = context v.context = context
} }
} }
...@@ -511,9 +537,9 @@ fileprivate extension MotionController { ...@@ -511,9 +537,9 @@ fileprivate extension MotionController {
return $0.init() return $0.init()
}) })
for plugin in plugins { for plugin in plugins! {
preprocessors.append(plugin) preprocessors?.append(plugin)
animators.append(plugin) animators?.append(plugin)
} }
} }
} }
...@@ -553,8 +579,12 @@ internal extension MotionController { ...@@ -553,8 +579,12 @@ internal extension MotionController {
internal extension MotionController { internal extension MotionController {
// should call this after `prepareTransitionPairs` & before `processContext` // should call this after `prepareTransitionPairs` & before `processContext`
func insert<T>(preprocessor: MotionPreprocessor, before: T.Type) { func insert<T>(preprocessor: MotionPreprocessor, before: T.Type) {
let v = preprocessors.index { $0 is T } ?? preprocessors.count guard var v = preprocessors else {
return
}
let i = v.index { $0 is T } ?? v.count
preprocessor.context = context preprocessor.context = context
preprocessors.insert(preprocessor, at: v) v.insert(preprocessor, at: i)
} }
} }
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