Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
Merge branch 'multiple_animations'
Browse files Browse the repository at this point in the history
eharrison committed Jun 9, 2023

Verified

This commit was signed with the committer’s verified signature.
GromNaN Jérôme Tamarelle
2 parents 1309cf8 + 18e7bca commit 4821346
Showing 12 changed files with 598 additions and 250 deletions.
53 changes: 53 additions & 0 deletions DotLottieCache.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// LRUDotLottieCache.swift
// Lottie
//
// Created by Evandro Hoffmann on 20/10/22.
//

import Foundation

/// A DotLottie Cache that will store lottie files up to `cacheSize`.
///
/// Once `cacheSize` is reached, the least recently used lottie will be ejected.
/// The default size of the cache is 100.
public class DotLottieCache: DotLottieCacheProvider {

// MARK: Lifecycle

public init() {
cache.countLimit = Self.defaultCacheCountLimit
}

// MARK: Public

/// The global shared Cache.
public static let sharedCache = DotLottieCache()

/// The size of the cache.
public var cacheSize = defaultCacheCountLimit {
didSet {
cache.countLimit = cacheSize
}
}

/// Clears the Cache.
public func clearCache() {
cache.removeAllObjects()
}

public func file(forKey key: String) -> DotLottieFile? {
cache.object(forKey: key as NSString)
}

public func setFile(_ lottie: DotLottieFile, forKey key: String) {
cache.setObject(lottie, forKey: key as NSString)
}

// MARK: Private

private static let defaultCacheCountLimit = 100

private var cache = NSCache<NSString, DotLottieFile>()

}
23 changes: 23 additions & 0 deletions DotLottieCacheProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// DotLottieCacheProvider.swift
// Lottie
//
// Created by Evandro Hoffmann on 20/10/22.
//

import Foundation

/// `DotLottieCacheProvider` is a protocol that describes a DotLottie Cache.
/// DotLottie Cache is used when loading `DotLottie` models. Using a DotLottie Cache
/// can increase performance when loading an animation multiple times.
///
/// Lottie comes with a prebuilt LRU DotLottie Cache.
public protocol DotLottieCacheProvider {

func file(forKey: String) -> DotLottieFile?

func setFile(_ lottie: DotLottieFile, forKey: String)

func clearCache()

}
15 changes: 15 additions & 0 deletions DotLottieConfiguration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// DotLottieSettings.swift
// Lottie
//
// Created by Evandro Hoffmann on 19/10/22.
//

import Foundation

public struct DotLottieConfiguration {
public var id: String
public var imagesUrl: URL?
public var loop: Bool
public var speed: Double
}
30 changes: 18 additions & 12 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 27 additions & 30 deletions Example/Tests/Tests.swift
Original file line number Diff line number Diff line change
@@ -6,18 +6,15 @@ class Tests: XCTestCase {
func testLoadDotLottie() {
let expectation = XCTestExpectation(description: "Load dotlottie file")

DotLottieLoader.load(from: URL(string: "https://dotlottie.io/sample_files/animation.lottie")!) { (lottie) in
XCTAssertNotNil(lottie)
XCTAssertNotNil(lottie?.manifest)

XCTAssertEqual(lottie?.manifest?.animations.count, lottie?.animations.count)
XCTAssertEqual("\(lottie?.manifest?.animations.first?.id ?? "").json", lottie?.animations.first?.lastPathComponent)
XCTAssertEqual(lottie?.animationUrl, lottie?.animations.first)

XCTAssertEqual(lottie?.animations.count, 1)
XCTAssertEqual(lottie?.animations.first?.lastPathComponent, "lf20_gOmta2.json")

XCTAssertEqual(lottie?.images.count, 0)
DotLottieLoader.loadedFrom(url: URL(string: "https://dotlottie.io/sample_files/animation.lottie")!) { result in
switch result {
case .success(let lottie):
XCTAssertEqual(lottie.animations.count, 1)
XCTAssertEqual(lottie.animations.first?.animationUrl.lastPathComponent, "lf20_gOmta2.json")

case .failure(let failure):
XCTFail("Invalid file: \(failure.localizedDescription)")
}

expectation.fulfill()
}
@@ -28,15 +25,15 @@ class Tests: XCTestCase {
func testLoadDotLottieWithExternalImage() {
let expectation = XCTestExpectation(description: "Load dotlottie file with external image")

DotLottieLoader.load(from: URL(string: "https://dotlottie.io/sample_files/animation-external-image.lottie")!) { (lottie) in
XCTAssertNotNil(lottie)
XCTAssertNotNil(lottie?.manifest)

XCTAssertEqual(lottie?.animations.count, 1)
XCTAssertEqual(lottie?.animations.first?.lastPathComponent, "with_image.json")

XCTAssertEqual(lottie?.images.count, 1)
XCTAssertEqual(lottie?.images.first?.lastPathComponent, "img_0.png")
DotLottieLoader.loadedFrom(url: URL(string: "https://dotlottie.io/sample_files/animation-external-image.lottie")!) { result in
switch result {
case .success(let lottie):
XCTAssertEqual(lottie.animations.count, 1)
XCTAssertEqual(lottie.animations.first?.animationUrl.lastPathComponent, "with_image.json")
case .failure(let failure):
XCTFail("Invalid file: \(failure.localizedDescription)")
}

expectation.fulfill()
}
@@ -47,15 +44,15 @@ class Tests: XCTestCase {
func testLoadDotLottieWithInlineImage() {
let expectation = XCTestExpectation(description: "Load dotlottie file with inline image")

DotLottieLoader.load(from: URL(string: "https://dotlottie.io/sample_files/animation-inline-image.lottie")!) { (lottie) in
XCTAssertNotNil(lottie)
XCTAssertNotNil(lottie?.manifest)

XCTAssertEqual(lottie?.animations.count, 1)
XCTAssertEqual(lottie?.animations.first?.lastPathComponent, "lf20_I2I090.json")

XCTAssertEqual(lottie?.images.count, 1)
XCTAssertEqual(lottie?.images.first?.lastPathComponent, "image_0.png")
DotLottieLoader.loadedFrom(url: URL(string: "https://dotlottie.io/sample_files/animation-inline-image.lottie")!) { result in
switch result {
case .success(let lottie):
XCTAssertEqual(lottie.animations.count, 1)
XCTAssertEqual(lottie.animations.first?.animationUrl.lastPathComponent, "lf20_I2I090.json")
case .failure(let failure):
XCTFail("Invalid file: \(failure.localizedDescription)")
}

expectation.fulfill()
}
21 changes: 10 additions & 11 deletions Example/dotLottieLoader/ViewController.swift
Original file line number Diff line number Diff line change
@@ -21,19 +21,18 @@ class ViewController: UIViewController {

creator.create { url in
guard let url = url else { return }
DotLottieLoader.load(from: url) { dotLottieFile in
// file decompressed from dotLottie
guard let dotLottieFile = dotLottieFile else {
print("invalid dotLottie file")
return
DotLottieLoader.loadedFrom(url: url) { result in
switch result {
case .success(let success):
print("""
dotLottieFile decompressed successfuly with:
- \(success.animations.count) animations
- Default animation: \(success.animations.first?.animationUrl.absoluteString ?? "not defined")
""")
case .failure(let failure):
print("invalid dotLottie file: \(failure.localizedDescription)")
}

print("""
dotLottieFile decompressed successfuly with:
- \(dotLottieFile.animations.count) animations
- \(dotLottieFile.images.count) images
- Default animation: \(dotLottieFile.animationUrl?.absoluteString ?? "not defined")
""")
}
}
}
40 changes: 0 additions & 40 deletions Sources/dotLottieLoader/DotLottieCache.swift

This file was deleted.

Loading

0 comments on commit 4821346

Please sign in to comment.