You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.0 KiB

2 years ago
  1. // Created by Cal Stephens on 12/13/21.
  2. // Copyright © 2021 Airbnb Inc. All rights reserved.
  3. // MARK: - LottieConfiguration
  4. /// Global configuration options for Lottie animations
  5. public struct LottieConfiguration: Hashable {
  6. public init(
  7. renderingEngine: RenderingEngineOption = .mainThread,
  8. decodingStrategy: DecodingStrategy = .codable)
  9. {
  10. self.renderingEngine = renderingEngine
  11. self.decodingStrategy = decodingStrategy
  12. }
  13. /// The global configuration of Lottie,
  14. /// which applies to all `AnimationView`s by default.
  15. public static var shared = LottieConfiguration()
  16. /// The rendering engine implementation to use when displaying an animation
  17. public var renderingEngine: RenderingEngineOption
  18. /// The decoding implementation to use when parsing an animation JSON file
  19. public var decodingStrategy: DecodingStrategy
  20. }
  21. // MARK: - RenderingEngineOption
  22. public enum RenderingEngineOption: Hashable {
  23. /// Uses the Core Animation engine for supported animations, and falls back to using
  24. /// the Main Thread engine for animations that use features not supported by the
  25. /// Core Animation engine.
  26. case automatic
  27. /// Uses the specified rendering engine
  28. case specific(RenderingEngine)
  29. /// The Main Thread rendering engine, which supports all Lottie features
  30. /// but runs on the main thread, which comes with some CPU overhead and
  31. /// can cause the animation to play at a low framerate when the CPU is busy.
  32. public static var mainThread: RenderingEngineOption { .specific(.mainThread) }
  33. /// The Core Animation rendering engine, that animates using Core Animation
  34. /// and has better performance characteristics than the Main Thread engine,
  35. /// but doesn't support all Lottie features.
  36. public static var coreAnimation: RenderingEngineOption { .specific(.coreAnimation) }
  37. }
  38. // MARK: - RenderingEngine
  39. /// The rendering engine implementation to use when displaying an animation
  40. public enum RenderingEngine: Hashable {
  41. /// The Main Thread rendering engine, which supports all Lottie features
  42. /// but runs on the main thread, which comes with some CPU overhead and
  43. /// can cause the animation to play at a low framerate when the CPU is busy.
  44. case mainThread
  45. /// The Core Animation rendering engine, that animates using Core Animation
  46. /// and has better performance characteristics than the Main Thread engine,
  47. /// but doesn't support all Lottie features.
  48. case coreAnimation
  49. }
  50. // MARK: - RenderingEngineOption + RawRepresentable, CustomStringConvertible
  51. extension RenderingEngineOption: RawRepresentable, CustomStringConvertible {
  52. // MARK: Lifecycle
  53. public init?(rawValue: String) {
  54. if rawValue == "Automatic" {
  55. self = .automatic
  56. } else if let engine = RenderingEngine(rawValue: rawValue) {
  57. self = .specific(engine)
  58. } else {
  59. return nil
  60. }
  61. }
  62. // MARK: Public
  63. public var rawValue: String {
  64. switch self {
  65. case .automatic:
  66. return "Automatic"
  67. case .specific(let engine):
  68. return engine.rawValue
  69. }
  70. }
  71. public var description: String {
  72. rawValue
  73. }
  74. }
  75. // MARK: - RenderingEngine + RawRepresentable, CustomStringConvertible
  76. extension RenderingEngine: RawRepresentable, CustomStringConvertible {
  77. // MARK: Lifecycle
  78. public init?(rawValue: String) {
  79. switch rawValue {
  80. case "Main Thread":
  81. self = .mainThread
  82. case "Core Animation":
  83. self = .coreAnimation
  84. default:
  85. return nil
  86. }
  87. }
  88. // MARK: Public
  89. public var rawValue: String {
  90. switch self {
  91. case .mainThread:
  92. return "Main Thread"
  93. case .coreAnimation:
  94. return "Core Animation"
  95. }
  96. }
  97. public var description: String {
  98. rawValue
  99. }
  100. }
  101. // MARK: - DecodingStrategy
  102. /// How animation files should be decoded
  103. public enum DecodingStrategy: Hashable {
  104. /// Use Codable. This is the default strategy introduced on Lottie 3.
  105. case codable
  106. /// Manually deserialize a dictionary into an Animation.
  107. /// This should be at least 2-3x faster than using Codable,
  108. /// but since it's manually implemented, there might be issues while it's experimental.
  109. case dictionaryBased
  110. }