Merge commit '7621e2f8dec938cf48181c8b10afc9b01f444e68' into beta

This commit is contained in:
Ilya Laktyushin
2025-12-06 02:17:48 +04:00
commit 8344b97e03
28070 changed files with 7995182 additions and 0 deletions
@@ -0,0 +1,22 @@
//
// AnimationCacheProvider.swift
// lottie-swift
//
// Created by Brandon Withrow on 2/5/19.
//
import Foundation
/// `AnimationCacheProvider` is a protocol that describes an Animation Cache.
/// Animation Cache is used when loading `Animation` models. Using an Animation Cache
/// can increase performance when loading an animation multiple times.
///
/// Lottie comes with a prebuilt LRU Animation Cache.
public protocol AnimationCacheProvider {
func animation(forKey: String) -> Animation?
func setAnimation(_ animation: Animation, forKey: String)
func clearCache()
}
@@ -0,0 +1,61 @@
//
// LRUAnimationCache.swift
// lottie-swift
//
// Created by Brandon Withrow on 2/5/19.
//
import Foundation
/// An Animation Cache that will store animations up to `cacheSize`.
///
/// Once `cacheSize` is reached, the least recently used animation will be ejected.
/// The default size of the cache is 100.
public class LRUAnimationCache: AnimationCacheProvider {
// MARK: Lifecycle
public init() { }
// MARK: Public
/// The global shared Cache.
public static let sharedCache = LRUAnimationCache()
/// The size of the cache.
public var cacheSize = 100
/// Clears the Cache.
public func clearCache() {
cacheMap.removeAll()
lruList.removeAll()
}
public func animation(forKey: String) -> Animation? {
guard let animation = cacheMap[forKey] else {
return nil
}
if let index = lruList.firstIndex(of: forKey) {
lruList.remove(at: index)
lruList.append(forKey)
}
return animation
}
public func setAnimation(_ animation: Animation, forKey: String) {
cacheMap[forKey] = animation
lruList.append(forKey)
if lruList.count > cacheSize {
let removed = lruList.remove(at: 0)
if removed != forKey {
cacheMap[removed] = nil
}
}
}
// MARK: Fileprivate
fileprivate var cacheMap: [String: Animation] = [:]
fileprivate var lruList: [String] = []
}