Skip to content

Commit 816e52f

Browse files
authored
Adds support for .lottie v2 (#2564)
1 parent d6ddd82 commit 816e52f

14 files changed

+56
-10
lines changed

Example/Example/AnimationListView.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,22 @@ struct AnimationListView: View {
2020
var body: some View {
2121
List {
2222
ForEach(items, id: \.self) { item in
23-
NavigationLink(value: item) {
23+
NavigationLink {
24+
switch item {
25+
case .animation(_, let animationPath):
26+
AnimationPreviewView(animationSource: .local(animationPath: animationPath))
27+
case .remoteAnimations(let name, let urls):
28+
AnimationPreviewView(animationSource: .remote(urls: urls, name: name))
29+
case .animationList(let listContent):
30+
AnimationListView(content: listContent)
31+
case .controlsDemo:
32+
ControlsDemoView()
33+
case .swiftUIInteroperability:
34+
SwiftUIInteroperabilityDemoView()
35+
case .lottieViewLayoutDemo:
36+
LottieViewLayoutDemoView()
37+
}
38+
} label: {
2439
switch item {
2540
case .animation, .remoteAnimations:
2641
HStack {

Sources/Public/DotLottie/DotLottieFile.swift

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,46 @@ public final class DotLottieFile {
4242
private(set) var imageProvider: DotLottieImageProvider?
4343

4444
/// Animations folder url
45-
lazy var animationsUrl: URL = fileUrl.appendingPathComponent("\(DotLottieFile.animationsFolderName)")
45+
///
46+
/// - Parameters:
47+
/// - version: version of .lottie file
48+
func animationsUrl(for version: String?) -> URL {
49+
switch Int(version ?? "1") ?? 1 {
50+
case 2...:
51+
fileUrl.appendingPathComponent("a")
52+
default:
53+
fileUrl.appendingPathComponent("animations")
54+
}
55+
}
4656

4757
/// All files in animations folder
48-
lazy var animationUrls: [URL] = FileManager.default.urls(for: animationsUrl) ?? []
58+
///
59+
/// - Parameters:
60+
/// - version: version of .lottie file
61+
func animationUrls(for version: String?) -> [URL] {
62+
FileManager.default.urls(for: animationsUrl(for: version)) ?? []
63+
}
4964

5065
/// Images folder url
51-
lazy var imagesUrl: URL = fileUrl.appendingPathComponent("\(DotLottieFile.imagesFolderName)")
66+
///
67+
/// - Parameters:
68+
/// - version: version of .lottie file
69+
func imagesUrl(for version: String?) -> URL {
70+
switch Int(version ?? "1") ?? 1 {
71+
case 2...:
72+
fileUrl.appendingPathComponent("i")
73+
default:
74+
fileUrl.appendingPathComponent("images")
75+
}
76+
}
5277

5378
/// All images in images folder
54-
lazy var imageUrls: [URL] = FileManager.default.urls(for: imagesUrl) ?? []
79+
///
80+
/// - Parameters:
81+
/// - version: version of .lottie file
82+
func imageUrls(for version: String?) -> [URL] {
83+
FileManager.default.urls(for: imagesUrl(for: version)) ?? []
84+
}
5585

5686
/// The `LottieAnimation` and `DotLottieConfiguration` for the given animation ID in this file
5787
func animation(for id: String? = nil) -> DotLottieFile.Animation? {
@@ -71,8 +101,6 @@ public final class DotLottieFile {
71101
// MARK: Private
72102

73103
private static let manifestFileName = "manifest.json"
74-
private static let animationsFolderName = "animations"
75-
private static let imagesFolderName = "images"
76104

77105
private let fileUrl: URL
78106

@@ -104,10 +132,12 @@ public final class DotLottieFile {
104132

105133
/// Loads file content to memory
106134
private func loadContent() throws {
107-
imageProvider = DotLottieImageProvider(filepath: imagesUrl)
135+
let manifest = try loadManifest()
136+
137+
imageProvider = DotLottieImageProvider(filepath: imagesUrl(for: manifest.version))
108138

109-
animations = try loadManifest().animations.map { dotLottieAnimation in
110-
let animation = try dotLottieAnimation.animation(url: animationsUrl)
139+
animations = try manifest.animations.map { dotLottieAnimation in
140+
let animation = try dotLottieAnimation.animation(url: animationsUrl(for: manifest.version))
111141
let configuration = DotLottieConfiguration(
112142
id: dotLottieAnimation.id,
113143
loopMode: dotLottieAnimation.loopMode,
6.03 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Supports Core Animation engine
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)