Created
April 14, 2015 02:32
-
-
Save parrots/673507d1ca206c0aea9f to your computer and use it in GitHub Desktop.
CachingInterfaceController
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
@import WatchKit; | |
@interface CachingInterfaceController : WKInterfaceController | |
- (void)updateLabel:(WKInterfaceLabel *)label withString:(NSString *)string; | |
- (void)updateLabel:(WKInterfaceLabel *)label asHidden:(BOOL)hidden; | |
- (void)updateImage:(WKInterfaceImage *)image withImageNamed:(NSString *)imageName; | |
- (void)updateImage:(WKInterfaceImage *)image withBaseNameForAnimation:(NSString *)baseName withRange:(NSRange)range duration:(NSTimeInterval)duration repeatCount:(NSInteger)repeatCount; | |
- (NSString *)currentImageNameForImage:(WKInterfaceImage *)image; | |
+ (NSNumber *)frameNumberForImageNamed:(NSString *)imageName; | |
@end |
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
#import "CachingInterfaceController.h" | |
@interface CachingInterfaceController() | |
@property (nonatomic) NSMutableDictionary *cachedValues; | |
@property BOOL isActive; | |
@end | |
@implementation CachingInterfaceController | |
#pragma mark - Lifecycle | |
- (instancetype)init | |
{ | |
if (self = [super init]) { | |
self.cachedValues = [[NSMutableDictionary alloc] init]; | |
} | |
return self; | |
} | |
- (void)willActivate | |
{ | |
[super willActivate]; | |
self.isActive = YES; | |
} | |
- (void)didDeactivate | |
{ | |
self.isActive = NO; | |
[super didDeactivate]; | |
} | |
# pragma mark - Cache helpers | |
- (void)updateLabel:(WKInterfaceLabel *)label withString:(NSString *)string | |
{ | |
NSString *cacheName = [NSString stringWithFormat:@"text%p", label]; | |
if (![self.cachedValues[cacheName] isEqualToString:string] && self.isActive) { | |
[label setText:string]; | |
self.cachedValues[cacheName] = string; | |
} | |
} | |
- (void)updateLabel:(WKInterfaceLabel *)label asHidden:(BOOL)hidden | |
{ | |
NSString *cacheName = [NSString stringWithFormat:@"hidden%p", label]; | |
if ((self.cachedValues[cacheName] == nil || [self.cachedValues[cacheName] boolValue] != hidden) && self.isActive) { | |
[label setHidden:hidden]; | |
self.cachedValues[cacheName] = @(hidden); | |
} | |
} | |
- (void)updateImage:(WKInterfaceImage *)image withImageNamed:(NSString *)imageName | |
{ | |
NSString *cacheName = [NSString stringWithFormat:@"imagenamed%p", image]; | |
if (![self.cachedValues[cacheName] isEqualToString:imageName] && self.isActive) { | |
[image setImageNamed:imageName]; | |
self.cachedValues[cacheName] = imageName; | |
} | |
} | |
- (void)updateImage:(WKInterfaceImage *)image withBaseNameForAnimation:(NSString *)baseName withRange:(NSRange)range duration:(NSTimeInterval)duration repeatCount:(NSInteger)repeatCount | |
{ | |
NSString *cacheName = [NSString stringWithFormat:@"imagenamed%p", image]; | |
if (self.isActive) { | |
[image setImageNamed:baseName]; | |
[image startAnimatingWithImagesInRange:range duration:duration repeatCount:repeatCount]; | |
NSNumber *endingImage = @(duration >= 0.0 ? range.location + range.length : range.location); | |
self.cachedValues[cacheName] = [NSString stringWithFormat:@"%@%@", baseName, endingImage]; | |
} | |
} | |
- (NSString *)currentImageNameForImage:(WKInterfaceImage *)image | |
{ | |
NSString *cacheName = [NSString stringWithFormat:@"imagenamed%p", image]; | |
return self.cachedValues[cacheName]; | |
} | |
+ (NSNumber *)frameNumberForImageNamed:(NSString *)imageName | |
{ | |
NSString *strippedNumber = [imageName stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [imageName length])]; | |
return strippedNumber != nil ? @([strippedNumber integerValue]) : nil; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment