Skip to content

Commit

Permalink
Defensive fixes for a UIViewPropertyAnimator crash (#1385)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcapkovic committed Nov 21, 2022
1 parent 3222188 commit c92abb7
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions ios/FluentUI/Bottom Sheet/BottomSheetController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,7 @@ public class BottomSheetController: UIViewController {
animator = stateChangeAnimator(to: targetState)

currentStateChangeAnimator = animator
animator?.addCompletion { [weak self] finalPosition in
self?.currentStateChangeAnimator = nil
animator?.addCompletion { finalPosition in
completion?(finalPosition)
}
}
Expand Down Expand Up @@ -733,9 +732,6 @@ public class BottomSheetController: UIViewController {

if animated {
currentStateChangeAnimator = animator
animator.addCompletion { [weak self] _ in
self?.currentStateChangeAnimator = nil
}
animator.startAnimation()
} else {
animator.startAnimation() // moves the animator into active state so it can be stopped
Expand Down Expand Up @@ -790,6 +786,13 @@ public class BottomSheetController: UIViewController {
guard let strongSelf = self else {
return
}

// It's important we drop the reference to the animator as early as possible.
// Otherwise we could accidentally try modifying the animator while it's calling out to its completion handler, which can lead to a crash.
if let animator = strongSelf.currentStateChangeAnimator, animator == translationAnimator {
strongSelf.currentStateChangeAnimator = nil
}

strongSelf.targetExpansionState = nil
strongSelf.panGestureRecognizer.isEnabled = strongSelf.isExpandable
strongSelf.handleCompletedStateChange(to: finalPosition == .start ? originalExpansionState : targetExpansionState,
Expand Down Expand Up @@ -849,7 +852,7 @@ public class BottomSheetController: UIViewController {
}

private func completeAnimationsIfNeeded(skipToEnd: Bool = false) {
if let currentAnimator = currentStateChangeAnimator, currentAnimator.isRunning {
if let currentAnimator = currentStateChangeAnimator, currentAnimator.isRunning, currentAnimator.state == .active {
let endPosition: UIViewAnimatingPosition = currentAnimator.isReversed ? .start : .end
currentAnimator.stopAnimation(false)
currentAnimator.finishAnimation(at: skipToEnd ? endPosition : .current)
Expand Down

0 comments on commit c92abb7

Please sign in to comment.