Update Caching Layer

This commit is contained in:
brandon_withrow 2017-08-02 14:22:48 -07:00
parent ad9303e689
commit 6fd17cfbe6
5 changed files with 42 additions and 4 deletions

View File

@ -59,5 +59,9 @@ const NSInteger kLOTCacheSize = 50;
[lruOrderArray_ removeAllObjects]; [lruOrderArray_ removeAllObjects];
} }
- (void)removeAnimationForKey:(NSString *)key {
[lruOrderArray_ removeObject:key];
[animationsCache_ removeObjectForKey:key];
}
@end @end

View File

@ -43,7 +43,9 @@
if (JSONObject && !error) { if (JSONObject && !error) {
LOTComposition *laScene = [[LOTComposition alloc] initWithJSON:JSONObject withAssetBundle:bundle]; LOTComposition *laScene = [[LOTComposition alloc] initWithJSON:JSONObject withAssetBundle:bundle];
[[LOTAnimationCache sharedCache] addAnimation:laScene forKey:animationName]; [[LOTAnimationCache sharedCache] addAnimation:laScene forKey:animationName];
return [[LOTAnimationView alloc] initWithModel:laScene inBundle:bundle]; LOTAnimationView *animationView = [[LOTAnimationView alloc] initWithModel:laScene inBundle:bundle];
animationView.cacheKey = animationName;
return animationView;
} }
NSLog(@"%s: Animation Not Found", __PRETTY_FUNCTION__); NSLog(@"%s: Animation Not Found", __PRETTY_FUNCTION__);
return [[LOTAnimationView alloc] initWithModel:nil inBundle:nil]; return [[LOTAnimationView alloc] initWithModel:nil inBundle:nil];
@ -74,7 +76,9 @@
LOTComposition *laScene = [[LOTComposition alloc] initWithJSON:JSONObject withAssetBundle:[NSBundle mainBundle]]; LOTComposition *laScene = [[LOTComposition alloc] initWithJSON:JSONObject withAssetBundle:[NSBundle mainBundle]];
laScene.rootDirectory = [filePath stringByDeletingLastPathComponent]; laScene.rootDirectory = [filePath stringByDeletingLastPathComponent];
[[LOTAnimationCache sharedCache] addAnimation:laScene forKey:animationName]; [[LOTAnimationCache sharedCache] addAnimation:laScene forKey:animationName];
return [[LOTAnimationView alloc] initWithModel:laScene inBundle:[NSBundle mainBundle]]; LOTAnimationView *animationView = [[LOTAnimationView alloc] initWithModel:laScene inBundle:[NSBundle mainBundle]];
animationView.cacheKey = animationName;
return animationView;
} }
NSLog(@"%s: Animation Not Found", __PRETTY_FUNCTION__); NSLog(@"%s: Animation Not Found", __PRETTY_FUNCTION__);
@ -86,6 +90,7 @@
if (self) { if (self) {
LOTComposition *laScene = [[LOTAnimationCache sharedCache] animationForKey:url.absoluteString]; LOTComposition *laScene = [[LOTAnimationCache sharedCache] animationForKey:url.absoluteString];
if (laScene) { if (laScene) {
self.cacheKey = url.absoluteString;
[self _initializeAnimationContainer]; [self _initializeAnimationContainer];
[self _setupWithSceneModel:laScene]; [self _setupWithSceneModel:laScene];
} else { } else {
@ -104,6 +109,7 @@
LOTComposition *laScene = [[LOTComposition alloc] initWithJSON:animationJSON withAssetBundle:[NSBundle mainBundle]]; LOTComposition *laScene = [[LOTComposition alloc] initWithJSON:animationJSON withAssetBundle:[NSBundle mainBundle]];
dispatch_async(dispatch_get_main_queue(), ^(void){ dispatch_async(dispatch_get_main_queue(), ^(void){
[[LOTAnimationCache sharedCache] addAnimation:laScene forKey:url.absoluteString]; [[LOTAnimationCache sharedCache] addAnimation:laScene forKey:url.absoluteString];
self.cacheKey = url.absoluteString;
[self _initializeAnimationContainer]; [self _initializeAnimationContainer];
[self _setupWithSceneModel:laScene]; [self _setupWithSceneModel:laScene];
}); });
@ -140,6 +146,7 @@
#endif #endif
- (void)_setupWithSceneModel:(LOTComposition *)model { - (void)_setupWithSceneModel:(LOTComposition *)model {
_cacheEnable = YES;
_animationSpeed = 1; _animationSpeed = 1;
_sceneModel = model; _sceneModel = model;
[CATransaction begin]; [CATransaction begin];
@ -230,7 +237,6 @@
- (void)setProgressWithFrame:(nonnull NSNumber *)currentFrame { - (void)setProgressWithFrame:(nonnull NSNumber *)currentFrame {
[self _removeCurrentAnimationIfNecessary]; [self _removeCurrentAnimationIfNecessary];
[self _callCompletionIfNecessary:NO]; [self _callCompletionIfNecessary:NO];
CGFloat duration = _sceneModel.endFrame.floatValue - _sceneModel.startFrame.floatValue;
_animationProgress = currentFrame.floatValue / (_sceneModel.endFrame.floatValue - _sceneModel.startFrame.floatValue); _animationProgress = currentFrame.floatValue / (_sceneModel.endFrame.floatValue - _sceneModel.startFrame.floatValue);
[CATransaction begin]; [CATransaction begin];
[CATransaction setDisableActions:YES]; [CATransaction setDisableActions:YES];
@ -239,6 +245,18 @@
[CATransaction commit]; [CATransaction commit];
} }
- (void)setCacheEnable:(BOOL)cacheEnable{
_cacheEnable = cacheEnable;
if (!self.cacheKey) {
return;
}
if (cacheEnable) {
[[LOTAnimationCache sharedCache] addAnimation:_sceneModel forKey:self.cacheKey];
}else {
[[LOTAnimationCache sharedCache] removeAnimationForKey:self.cacheKey];
}
}
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR #if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
- (void)addSubview:(nonnull LOTView *)view - (void)addSubview:(nonnull LOTView *)view

View File

@ -18,5 +18,6 @@ typedef enum : NSUInteger {
@property (nonatomic, readonly) LOTComposition * _Nonnull sceneModel; @property (nonatomic, readonly) LOTComposition * _Nonnull sceneModel;
@property (nonatomic, copy, nullable) LOTAnimationCompletionBlock completionBlock; @property (nonatomic, copy, nullable) LOTAnimationCompletionBlock completionBlock;
@property (nonatomic, copy, nullable) NSString *cacheKey;
@end @end

View File

@ -8,15 +8,27 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@class LOTComposition; @class LOTComposition;
@interface LOTAnimationCache : NSObject @interface LOTAnimationCache : NSObject
/// Global Cache
+ (instancetype)sharedCache; + (instancetype)sharedCache;
/// Adds animation to the cache
- (void)addAnimation:(LOTComposition *)animation forKey:(NSString *)key; - (void)addAnimation:(LOTComposition *)animation forKey:(NSString *)key;
- (LOTComposition *)animationForKey:(NSString *)key;
/// Returns animation from cache.
- (LOTComposition * _Nullable)animationForKey:(NSString *)key;
/// Removes a specific animation from the cache
- (void)removeAnimationForKey:(NSString *)key;
/// Clears Everything from the Cache
- (void)clearCache; - (void)clearCache;
@end @end
NS_ASSUME_NONNULL_END

View File

@ -54,6 +54,9 @@ typedef void (^LOTAnimationCompletionBlock)(BOOL animationFinished);
/// Read only of the duration in seconds of the animation at speed of 1 /// Read only of the duration in seconds of the animation at speed of 1
@property (nonatomic, readonly) CGFloat animationDuration; @property (nonatomic, readonly) CGFloat animationDuration;
/// Enables or disables caching of the backing animation model. Defaults to YES
@property (nonatomic, assign) BOOL cacheEnable;
/* /*
* Plays the animation from its current position to a specific progress. * Plays the animation from its current position to a specific progress.
* The animation will start from its current position. * The animation will start from its current position.