Skip to content

Commit

Permalink
Background mask caching fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
peyton committed Feb 29, 2012
1 parent 95b11a4 commit e7a7163
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 36 deletions.
7 changes: 5 additions & 2 deletions Demo Project/MOOMaskedIconView Demo/AppDelegate.m
Expand Up @@ -18,15 +18,18 @@ @implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Cache masks listed in "Icons to Cache.plist" in the background
MOOResourceList *iconsToCache = [MOOResourceList resourceListWithPlistNamed:@"Icons to Cache.plist"];
[[MOOResourceRegistry sharedRegistry] registerList:iconsToCache];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Override point for customization after application launch.
self.viewController = [[ViewController alloc] init];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

// Cache masks listed in "Icons.plist" in the background
[[MOOResourceList resourceListWithPlistNamed:@"Icons to Cache.plist"] renderMasksInBackground];
[iconsToCache renderMasksInBackground];

return YES;
}
Expand Down
3 changes: 0 additions & 3 deletions Demo Project/MOOMaskedIconView Demo/ViewController.h
Expand Up @@ -10,8 +10,5 @@
@class MOOMaskedIconView;

@interface ViewController : UIViewController
{
MOOMaskedIconView *_icon4;
}

@end
13 changes: 0 additions & 13 deletions Demo Project/MOOMaskedIconView Demo/ViewController.m
Expand Up @@ -14,12 +14,6 @@

#define kBarHeight 64.0f

@interface ViewController ()

- (void)toggleIcon4Highlighted:(id)sender;

@end

@implementation ViewController

#pragma mark - View lifecycle
Expand Down Expand Up @@ -140,11 +134,4 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
return interfaceOrientation == UIInterfaceOrientationPortrait;
}

#pragma mark -

- (void)toggleIcon4Highlighted:(id)sender;
{
_icon4.highlighted = !_icon4.isHighlighted;
}

@end
15 changes: 14 additions & 1 deletion MOOMaskedIconView/MOOMaskedIconView.h
Expand Up @@ -8,7 +8,6 @@
#import <UIKit/UIKit.h>



typedef void (^DrawingBlock)(CGContextRef context);

typedef enum {
Expand Down Expand Up @@ -280,6 +279,20 @@ typedef enum {

/* @name Traits */

/*
* Returns a trait composed of the icon's current style properties.
*
* Setting overwrites all properties of the icon with those of the passed-in trait.
*
* @see mixInTrait
*/
@property (nonatomic, strong) id<MOOStyleTrait> trait;

/*
* Apply the properties of the passed-in trait in addition to the icon's current properties.
*
* @see setTrait
*/
- (void)mixInTrait:(id<MOOStyleTrait>)trait;


Expand Down
25 changes: 24 additions & 1 deletion MOOMaskedIconView/MOOMaskedIconView.m
Expand Up @@ -12,6 +12,7 @@

#import "AHHelper.h"
#import "MOOCGImageWrapper.h"
#import "MOOResourceList.h"
#import "MOOStyleTrait.h"

// Keys for KVO
Expand Down Expand Up @@ -634,6 +635,28 @@ - (Protocol *)styleProtocol;
return @protocol(MOOMaskedIconViewStyles);
}

- (id<MOOStyleTrait>)trait;
{
MOOStyleTrait *trait = [MOOStyleTrait trait];

for (NSString *propertyName in propertyNamesForStyleProtocol(self.styleProtocol))
[trait setValue:[(NSObject *)self valueForKey:propertyName] forKey:propertyName];

return trait;
}

- (void)setTrait:(id<MOOStyleTrait>)trait;
{
if (![[self class] conformsToProtocol:trait.styleProtocol])
{
NSLog(@"Attempting to mix object %@ of incompatible protocol %@ into object %@ of protocol %@.", trait, NSStringFromProtocol(trait.styleProtocol), self, NSStringFromProtocol(self.styleProtocol));
return;
}

for (NSString *propertyName in propertyNamesForStyleProtocol(self.styleProtocol))
[self setValue:[(NSObject *)trait valueForKey:propertyName] forKey:propertyName];
}

#pragma mark - Caching methods

+ (NSCache *)defaultMaskCache;
Expand All @@ -652,7 +675,7 @@ + (NSCache *)defaultMaskCache;

+ (BOOL)shouldCacheMaskForKey:(NSString *)key;
{
return NO;
return [[MOOResourceRegistry sharedRegistry] shouldCacheResourceWithKey:key];
}

#pragma mark - KVO methods
Expand Down
16 changes: 16 additions & 0 deletions MOOMaskedIconView/MOOResourceList.h
Expand Up @@ -14,6 +14,7 @@
}

@property (strong, readonly) NSArray *names;
@property (strong, readonly) NSArray *keys;

- (id)initWithResourceNames:(NSArray *)resourceNames;
- (id)initWithPlistNamed:(NSString *)propertyListName;
Expand All @@ -27,3 +28,18 @@

@end

@interface MOOResourceRegistry : NSObject
{
NSArray *_resourceLists;
}

@property (nonatomic, strong, readonly) NSArray *resourceLists;

- (void)registerList:(MOOResourceList *)resourceList;
- (void)deregisterList:(MOOResourceList *)resourceList;

- (BOOL)shouldCacheResourceWithKey:(NSString *)key;

+ (MOOResourceRegistry *)sharedRegistry;

@end
85 changes: 83 additions & 2 deletions MOOMaskedIconView/MOOResourceList.m
Expand Up @@ -11,8 +11,12 @@
#import "MOOCGImageWrapper.h"
#import "MOOMaskedIconView.h"

// Queues
static dispatch_queue_t _defaultRenderQueue;

// Shared instances
static MOOResourceRegistry *_sharedRegistry;

@interface MOOResourceList ()

@property (strong) NSArray *names;
Expand All @@ -21,6 +25,7 @@ @interface MOOResourceList ()

@implementation MOOResourceList
@synthesize names = _names;
@dynamic keys;

- (id)initWithResourceNames:(NSArray *)resourceNames;
{
Expand Down Expand Up @@ -81,7 +86,6 @@ - (void)renderMasksInBackground;
if (![maskCache objectForKey:key])
{
CGImageRef mask = CGImageCreateMaskFromResourceNamed(resourceName, CGSizeZero);

MOOCGImageWrapper *imageWrapper = [MOOCGImageWrapper wrapperWithCGImage:mask];
CGImageRelease(mask);

Expand All @@ -93,7 +97,19 @@ - (void)renderMasksInBackground;

}

#pragma mark -
#pragma mark - Getters and setters

- (NSArray *)keys;
{
NSMutableArray *keys = [NSMutableArray arrayWithCapacity:[self.names count]];

for (NSString *name in self.names)
[keys addObject:[name stringByAppendingString:NSStringFromCGSize(CGSizeZero)]];

return keys;
}

#pragma mark - Queues

+ (dispatch_queue_t)defaultRenderQueue;
{
Expand All @@ -113,3 +129,68 @@ + (dispatch_queue_t)defaultRenderQueue;
}

@end

@interface MOOResourceRegistry ()

@property (nonatomic, strong) NSArray *resourceLists;

@end

@implementation MOOResourceRegistry
@synthesize resourceLists = _resourceLists;

- (id)init;
{
if (!(self = [super init]))
return nil;

// Initialize resource lists
self.resourceLists = [NSArray array];

return self;
}

- (void)dealloc;
{
self.resourceLists = nil;

AH_SUPER_DEALLOC;
}

#pragma mark - List marshalling

- (void)registerList:(MOOResourceList *)resourceList;
{
if (![self.resourceLists containsObject:resourceList])
self.resourceLists = [self.resourceLists arrayByAddingObject:resourceList];
}

- (void)deregisterList:(MOOResourceList *)resourceList;
{
self.resourceLists = [self.resourceLists filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
return evaluatedObject != resourceList;
}]];
}

#pragma mark - Resource querying

- (BOOL)shouldCacheResourceWithKey:(NSString *)key;
{
for (MOOResourceList *list in self.resourceLists)
for (NSString *keyToCache in list.keys)
if ([keyToCache isEqualToString:key])
return YES;
return NO;
}

#pragma mark - Shared instances

+ (MOOResourceRegistry *)sharedRegistry;
{
if (!_sharedRegistry)
_sharedRegistry = [[MOOResourceRegistry alloc] init];

return _sharedRegistry;
}

@end
14 changes: 0 additions & 14 deletions MOOMaskedIconView/Support/AHHelper.h

This file was deleted.

0 comments on commit e7a7163

Please sign in to comment.