LottieAnimationSource.swift 1.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
// Created by Cal Stephens on 7/26/23.
// Copyright © 2023 Airbnb Inc. All rights reserved.

// MARK: - LottieAnimationSource

/// A data source for a Lottie animation.
/// Either a `LottieAnimation` loaded from a `.json` file,
/// or a `DotLottieFile` loaded from a `.lottie` file.
public enum LottieAnimationSource: Sendable {
  /// A `LottieAnimation` loaded from a `.json` file
  case lottieAnimation(LottieAnimation)

  /// A `DotLottieFile` loaded from a `.lottie` file
  case dotLottieFile(DotLottieFile)
}

extension LottieAnimationSource {
  /// The default animation displayed by this data source
  var animation: LottieAnimation? {
    switch self {
    case .lottieAnimation(let animation):
      animation
    case .dotLottieFile:
      dotLottieAnimation?.animation
    }
  }

  /// The `DotLottieFile.Animation`, if this is a dotLottie animation
  var dotLottieAnimation: DotLottieFile.Animation? {
    switch self {
    case .lottieAnimation:
      nil
    case .dotLottieFile(let dotLottieFile):
      dotLottieFile.animation()
    }
  }
}

extension LottieAnimation {
  /// This animation represented as a `LottieAnimationSource`
  public var animationSource: LottieAnimationSource {
    .lottieAnimation(self)
  }
}

extension DotLottieFile {
  /// This animation represented as a `LottieAnimationSource`
  public var animationSource: LottieAnimationSource {
    .dotLottieFile(self)
  }
}