Repository: airbnb/lottie-ios
File: Sources/Public/Animation/AnimationView.swift (Swift)

Code before:
  }

  // MARK: Public

  /// The configuration that this `AnimationView` uses when playing its animation
  public let configuration: LottieConfiguration

  /**
   Describes the behavior of an AnimationView when the app is moved to the background.

   The default for the Main Thread animation engine is `pause`,
   which pauses the animation when the application moves to
   the background. This prevents the animation from consuming CPU
   resources when not on-screen. The completion block is called with
   `false` for completed.

   The default for the Core Animation engine is `doNothing`, since
   the Core Animation engine does not have any CPU overhead.
   */
  public lazy var backgroundBehavior: LottieBackgroundBehavior = {
    switch configuration.renderingEngine {
    case .mainThread:
      return .pause

    case .coreAnimation:
      return .doNothing
    }
  }()

  /// Value Providers that have been registered using `setValueProvider(_:keypath:)`
  public private(set) var valueProviders = [AnimationKeypath: AnyValueProvider]()

  /**
   Sets the animation backing the animation view. Setting this will clear the
   view's contents, completion blocks and current state. The new animation will
   be loaded up and set to the beginning of its timeline.
   */
  public var animation: Animation? {
    didSet {
      makeAnimationLayer()
    }
  }

  /**
   Sets the image provider for the animation view. An image provider provides the
   animation with its required image data.

   Setting this will cause the animation to reload its image contents.
   */
  public var imageProvider: AnimationImageProvider {
    didSet {

Review comment (suggestion):
We don't need to pause the Core Animation engine when it moves into the background, since it doesn't introduce any CPU overhead. This prevents us from having to `stop()` and then `start()` the animation, which avoids unnecessary animation setup passes.

Code after:
    animation = nil
    imageProvider = BundleImageProvider(bundle: Bundle.main, searchPath: nil)
    textProvider = DefaultTextProvider()
    fontProvider = DefaultFontProvider()
    self.configuration = configuration
    super.init(frame: .zero)
    commonInit()
  }

  public override init(frame _: CGRect) {
    animation = nil
    imageProvider = BundleImageProvider(bundle: Bundle.main, searchPath: nil)
    textProvider = DefaultTextProvider()
    fontProvider = DefaultFontProvider()
    configuration = .shared
    super.init(frame: .zero)
    commonInit()
  }

  required public init?(coder aDecoder: NSCoder) {
    imageProvider = BundleImageProvider(bundle: Bundle.main, searchPath: nil)
    textProvider = DefaultTextProvider()
    fontProvider = DefaultFontProvider()
    configuration = .shared
    super.init(coder: aDecoder)
    commonInit()
  }

  // MARK: Public

  /// The configuration that this `AnimationView` uses when playing its animation
  public let configuration: LottieConfiguration

  /// Value Providers that have been registered using `setValueProvider(_:keypath:)`
  public private(set) var valueProviders = [AnimationKeypath: AnyValueProvider]()

  /**
   Describes the behavior of an AnimationView when the app is moved to the background.

   The default for the Main Thread animation engine is `pause`,
   which pauses the animation when the application moves to
   the background. This prevents the animation from consuming CPU
   resources when not on-screen. The completion block is called with
   `false` for completed.

   The default for the Core Animation engine is `continuePlaying`,
   since the Core Animation engine does not have any CPU overhead.
   */
  public lazy var backgroundBehavior: LottieBackgroundBehavior = .default(for: configuration.renderingEngine) {
    didSet {
      if