-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb58033
commit 818cf21
Showing
5 changed files
with
112 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// DefaultAnimationCache.swift | ||
// Lottie | ||
// | ||
// Created by Marcelo Fabri on 10/18/22. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A thread-safe Animation Cache that will store animations up to `cacheSize`. | ||
/// | ||
/// Once `cacheSize` is reached, animations can be ejected. | ||
/// The default size of the cache is 100. | ||
/// | ||
/// This cache implementation also responds to memory pressure, as it's backed by `NSCache`. | ||
public class DefaultAnimationCache: AnimationCacheProvider { | ||
|
||
// MARK: Lifecycle | ||
|
||
public init() { | ||
cache.countLimit = Self.defaultCacheCountLimit | ||
} | ||
|
||
// MARK: Public | ||
|
||
/// The global shared Cache. | ||
public static let sharedCache = DefaultAnimationCache() | ||
|
||
/// The size of the cache. | ||
public var cacheSize = defaultCacheCountLimit { | ||
didSet { | ||
cache.countLimit = cacheSize | ||
} | ||
} | ||
|
||
/// Clears the Cache. | ||
public func clearCache() { | ||
cache.removeAllObjects() | ||
} | ||
|
||
public func animation(forKey key: String) -> LottieAnimation? { | ||
cache.object(forKey: key as NSString) | ||
} | ||
|
||
public func setAnimation(_ animation: LottieAnimation, forKey key: String) { | ||
cache.setObject(animation, forKey: key as NSString) | ||
} | ||
|
||
// MARK: Private | ||
|
||
private static let defaultCacheCountLimit = 100 | ||
|
||
private var cache = NSCache<NSString, LottieAnimation>() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// AnimationCacheProviderTests.swift | ||
// LottieTests | ||
// | ||
// Created by Marcelo Fabri on 10/18/22. | ||
// | ||
|
||
import XCTest | ||
|
||
@testable import Lottie | ||
|
||
final class AnimationCacheProviderTests: XCTestCase { | ||
|
||
func testCaches() throws { | ||
let cache = DefaultAnimationCache() | ||
let animation1 = try XCTUnwrap(Samples.animation(named: "Boat_Loader")) | ||
let animation2 = try XCTUnwrap(Samples.animation(named: "TwitterHeart")) | ||
|
||
XCTAssertNil(cache.animation(forKey: "animation1")) | ||
cache.setAnimation(animation1, forKey: "animation1") | ||
XCTAssertNoDiff(cache.animation(forKey: "animation1"), animation1) | ||
|
||
XCTAssertNil(cache.animation(forKey: "animation2")) | ||
cache.setAnimation(animation2, forKey: "animation2") | ||
XCTAssertNoDiff(cache.animation(forKey: "animation2"), animation2) | ||
XCTAssertNoDiff(cache.animation(forKey: "animation1"), animation1) | ||
} | ||
|
||
func testClearCache() throws { | ||
let cache = DefaultAnimationCache() | ||
let animation = try XCTUnwrap(Samples.animation(named: "Boat_Loader")) | ||
|
||
XCTAssertNil(cache.animation(forKey: "animation")) | ||
cache.setAnimation(animation, forKey: "animation") | ||
XCTAssertNotNil(cache.animation(forKey: "animation")) | ||
|
||
cache.clearCache() | ||
XCTAssertNil(cache.animation(forKey: "animation")) | ||
} | ||
} |