Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LottieView.animationDidLoad API #2172

Merged
merged 2 commits into from
Sep 7, 2023
Merged

Add LottieView.animationDidLoad API #2172

merged 2 commits into from
Sep 7, 2023

Conversation

calda
Copy link
Member

@calda calda commented Sep 7, 2023

This PR adds a new animationDidLoad API to the SwiftUI LottieView.

LottieAnimationView has an animationLoaded API, but its design makes it hard to use from SwiftUI. To support some specific implementation details of supporting both LottieAnimation and DotLottieFile in LottieAnimationView, the closure is called immediately when set if animation isn't nil:

/// A closure that is called when `self.animation` is loaded. When setting this closure,
/// it is called immediately if `self.animation` is non-nil.
///
/// When initializing a `LottieAnimationView`, the animation will either be loaded
/// synchronously (when loading a `LottieAnimation` from a .json file on disk)
/// or asynchronously (when loading a `DotLottieFile` from disk, or downloading
/// an animation from a URL). This closure is called in both cases once the
/// animation is loaded and applied, so can be a useful way to configure this
/// `LottieAnimationView` regardless of which initializer was used. For example:
///
/// ```
/// let animationView: LottieAnimationView
///
/// if loadDotLottieFile {
///   // Loads the .lottie file asynchronously
///   animationView = LottieAnimationView(dotLottieName: "animation")
/// } else {
///   // Loads the .json file synchronously
///   animationView = LottieAnimationView(name: "animation")
/// }
///
/// animationView.animationLoaded = { animationView, animation in
///   // If using a .lottie file, this is called once the file finishes loading.
///   // If using a .json file, this is called immediately (since the animation is loaded synchronously).
///   animationView.play()
/// }
/// ```
public var animationLoaded: ((_ animationView: LottieAnimationView, _ animation: LottieAnimation) -> Void)? {
  didSet {
    if let animation = animation {
      animationLoaded?(self, animation)
    }
  }
}

This doesn't work well when paired with .configure, because a call like this will invoke animationLoaded every time configure is called (which is arbitrarily often):

.configure { animationView in
  animationView.animationLoaded = {  _, _ in
    // handle the animation being loaded
    // (whoops, this is called way too often)
  }
}

In retrospect this was a bad API choice (imo) and we would have been better off with an API more similar to how LottieView works with LottieAnimationSource. Unfortunately we're stuck with this design on LottieAnimationView due to backwards compatibility.

To make this more obvious when using LottieView, lets just add a new animationDidLoad API with the expected semantics (only called after calling animationView.loadAnimation(animationSource)):

.animationDidLoad { _ in
  // handle the animation being loaded
}

@@ -6,7 +6,6 @@ import SwiftUI

// MARK: - AnimationPreviewView

/// TODO: Implement functionality from UIKit `AnimationPreviewViewController`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this since it's out of date (AnimationPreviewViewController no longer exists)

@@ -15,7 +15,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "080DEF612A95707B00BE2D96"
BuildableName = "Lottie_visionOS.framework"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Xcode did this automatically 🤷🏻

… called if you manually updated the animation in the underlying LottieAnimationView somehow
Copy link
Contributor

@thedrick thedrick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM @calda

@calda calda enabled auto-merge (squash) September 7, 2023 20:57
@calda calda merged commit 97cea95 into master Sep 7, 2023
17 checks passed
@calda calda deleted the cal--animation-loaded branch September 7, 2023 22:33
iago849 pushed a commit to atteamapps/lottie-ios that referenced this pull request Feb 8, 2024
MoroziOS pushed a commit to MoroziOS/tmg-lottie-ios that referenced this pull request May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants