LottieAnimationViewInitializers.swift 9.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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
//
//  AnimationViewInitializers.swift
//  lottie-swift-iOS
//
//  Created by Brandon Withrow on 2/6/19.
//

import Foundation

extension LottieAnimationView {

  // MARK: Lifecycle

  /// Loads a Lottie animation from a JSON file in the supplied bundle.
  ///
  /// - Parameter name: The string name of the lottie animation with no file extension provided.
  /// - Parameter bundle: The bundle in which the animation is located. Defaults to the Main bundle.
  /// - Parameter subdirectory: A subdirectory in the bundle in which the animation is located. Optional.
  /// - Parameter imageProvider: An image provider for the animation's image data.
  /// If none is supplied Lottie will search in the supplied bundle for images.
  public convenience init(
    name: String,
    bundle: Bundle = Bundle.main,
    subdirectory: String? = nil,
    imageProvider: AnimationImageProvider? = nil,
    animationCache: AnimationCacheProvider? = LottieAnimationCache.shared,
    configuration: LottieConfiguration = .shared)
  {
    let animation = LottieAnimation.named(name, bundle: bundle, subdirectory: subdirectory, animationCache: animationCache)
    let provider = imageProvider ?? BundleImageProvider(bundle: bundle, searchPath: nil)
    self.init(animation: animation, imageProvider: provider, configuration: configuration)
  }

  /// Loads a Lottie animation from a JSON file in a specific path on disk.
  ///
  /// - Parameter name: The absolute path of the Lottie Animation.
  /// - Parameter imageProvider: An image provider for the animation's image data.
  /// If none is supplied Lottie will search in the supplied filepath for images.
  public convenience init(
    filePath: String,
    imageProvider: AnimationImageProvider? = nil,
    animationCache: AnimationCacheProvider? = LottieAnimationCache.shared,
    configuration: LottieConfiguration = .shared)
  {
    let animation = LottieAnimation.filepath(filePath, animationCache: animationCache)
    let provider = imageProvider ??
      FilepathImageProvider(filepath: URL(fileURLWithPath: filePath).deletingLastPathComponent().path)
    self.init(animation: animation, imageProvider: provider, configuration: configuration)
  }

  /// Loads a Lottie animation asynchronously from the URL
  ///
  /// - Parameter url: The url to load the animation from.
  /// - Parameter imageProvider: An image provider for the animation's image data.
  /// If none is supplied Lottie will search in the main bundle for images.
  /// - Parameter closure: A closure to be called when the animation has loaded.
  public convenience init(
    url: URL,
    imageProvider: AnimationImageProvider? = nil,
    session: URLSession = .shared,
    closure: @escaping LottieAnimationView.DownloadClosure,
    animationCache: AnimationCacheProvider? = LottieAnimationCache.shared,
    configuration: LottieConfiguration = .shared)
  {
    if let animationCache, let animation = animationCache.animation(forKey: url.absoluteString) {
      self.init(animation: animation, imageProvider: imageProvider, configuration: configuration)
      closure(nil)
    } else {
      self.init(animation: nil, imageProvider: imageProvider, configuration: configuration)

      LottieAnimation.loadedFrom(url: url, session: session, closure: { animation in
        if let animation {
          self.animation = animation
          closure(nil)
        } else {
          closure(LottieDownloadError.downloadFailed)
        }
      }, animationCache: animationCache)
    }
  }

  /// Loads a Lottie animation from a JSON file located in the Asset catalog of the supplied bundle.
  /// - Parameter name: The string name of the lottie animation in the asset catalog.
  /// - Parameter bundle: The bundle in which the animation is located.
  /// Defaults to the Main bundle.
  /// - Parameter imageProvider: An image provider for the animation's image data.
  /// If none is supplied Lottie will search in the supplied bundle for images.
  public convenience init(
    asset name: String,
    bundle: Bundle = Bundle.main,
    imageProvider: AnimationImageProvider? = nil,
    animationCache: AnimationCacheProvider? = LottieAnimationCache.shared,
    configuration: LottieConfiguration = .shared)
  {
    let animation = LottieAnimation.asset(name, bundle: bundle, animationCache: animationCache)
    let provider = imageProvider ?? BundleImageProvider(bundle: bundle, searchPath: nil)
    self.init(animation: animation, imageProvider: provider, configuration: configuration)
  }

  // MARK: DotLottie

  /// Loads a Lottie animation from a .lottie file in the supplied bundle.
  ///
  /// - Parameter dotLottieName: The name of the lottie file without the lottie extension. EG "StarAnimation"
  /// - Parameter bundle: The bundle in which the lottie is located. Defaults to `Bundle.main`
  /// - Parameter subdirectory: A subdirectory in the bundle in which the lottie is located. Optional.
  /// - Parameter animationId: Animation id to play. Optional
  /// - Parameter completion: A closure that is called when the .lottie file is finished loading
  /// Defaults to first animation in file
  public convenience init(
    dotLottieName name: String,
    bundle: Bundle = Bundle.main,
    subdirectory: String? = nil,
    animationId: String? = nil,
    dotLottieCache: DotLottieCacheProvider? = DotLottieCache.sharedCache,
    configuration: LottieConfiguration = .shared,
    completion: ((LottieAnimationView, Error?) -> Void)? = nil)
  {
    self.init(dotLottie: nil, animationId: animationId, configuration: configuration)
    DotLottieFile.named(name, bundle: bundle, subdirectory: subdirectory, dotLottieCache: dotLottieCache) { result in
      switch result {
      case .success(let dotLottieFile):
        self.loadAnimation(animationId, from: dotLottieFile)
        completion?(self, nil)

      case .failure(let error):
        completion?(self, error)
      }
    }
  }

  /// Loads a Lottie from a .lottie file in a specific path on disk.
  ///
  /// - Parameter dotLottieFilePath: The absolute path of the Lottie file.
  /// - Parameter animationId: Animation id to play. Optional
  /// - Parameter completion: A closure that is called when the .lottie file is finished loading
  /// Defaults to first animation in file
  public convenience init(
    dotLottieFilePath filePath: String,
    animationId: String? = nil,
    dotLottieCache: DotLottieCacheProvider? = DotLottieCache.sharedCache,
    configuration: LottieConfiguration = .shared,
    completion: ((LottieAnimationView, Error?) -> Void)? = nil)
  {
    self.init(dotLottie: nil, animationId: animationId, configuration: configuration)
    DotLottieFile.loadedFrom(filepath: filePath, dotLottieCache: dotLottieCache) { result in
      switch result {
      case .success(let dotLottieFile):
        self.loadAnimation(animationId, from: dotLottieFile)
        completion?(self, nil)

      case .failure(let error):
        completion?(self, error)
      }
    }
  }

  /// Loads a Lottie file asynchronously from the URL
  ///
  /// - Parameter dotLottieUrl: The url to load the lottie file from.
  /// - Parameter animationId: Animation id to play. Optional. Defaults to first animation in file.
  /// - Parameter completion: A closure to be called when the animation has loaded.
  public convenience init(
    dotLottieUrl url: URL,
    animationId: String? = nil,
    dotLottieCache: DotLottieCacheProvider? = DotLottieCache.sharedCache,
    configuration: LottieConfiguration = .shared,
    session: URLSession = .shared,
    completion: ((LottieAnimationView, Error?) -> Void)? = nil)
  {
    if let dotLottieCache, let lottie = dotLottieCache.file(forKey: url.absoluteString) {
      self.init(dotLottie: lottie, animationId: animationId, configuration: configuration)
      completion?(self, nil)
    } else {
      self.init(dotLottie: nil, configuration: configuration)
      DotLottieFile.loadedFrom(url: url, session: session, dotLottieCache: dotLottieCache) { result in
        switch result {
        case .success(let lottie):
          self.loadAnimation(animationId, from: lottie)
          completion?(self, nil)

        case .failure(let error):
          completion?(self, error)
        }
      }
    }
  }

  /// Loads a Lottie from a .lottie file located in the Asset catalog of the supplied bundle.
  /// - Parameter name: The string name of the lottie file in the asset catalog.
  /// - Parameter bundle: The bundle in which the file is located. Defaults to the Main bundle.
  /// - Parameter animationId: Animation id to play. Optional
  /// - Parameter completion: A closure that is called when the .lottie file is finished loading
  /// Defaults to first animation in file
  public convenience init(
    dotLottieAsset name: String,
    bundle: Bundle = Bundle.main,
    animationId: String? = nil,
    dotLottieCache: DotLottieCacheProvider? = DotLottieCache.sharedCache,
    configuration: LottieConfiguration = .shared,
    completion: ((LottieAnimationView, Error?) -> Void)? = nil)
  {
    self.init(dotLottie: nil, animationId: animationId, configuration: configuration)
    DotLottieFile.asset(named: name, bundle: bundle, dotLottieCache: dotLottieCache) { result in
      switch result {
      case .success(let dotLottieFile):
        self.loadAnimation(animationId, from: dotLottieFile)
        completion?(self, nil)

      case .failure(let error):
        completion?(self, error)
      }
    }
  }

  // MARK: Public

  public typealias DownloadClosure = (Error?) -> Void

}

// MARK: - LottieDownloadError

enum LottieDownloadError: Error {
  case downloadFailed
}