Skip to content

Commit

Permalink
Initial commit of common code
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus S. Zarra committed Sep 24, 2010
0 parents commit 572b86a
Show file tree
Hide file tree
Showing 6 changed files with 668 additions and 0 deletions.
48 changes: 48 additions & 0 deletions ZSContextWatcher.h
@@ -0,0 +1,48 @@
//
// ZSContextWatcher.h
//
// Copyright 2010 Zarra Studios, LLC All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

@interface ZSContextWatcher : NSObject
{
NSPersistentStoreCoordinator *persistentStoreCoordinator;
NSPredicate *masterPredicate;
NSString *reference;

__weak id delegate;
SEL action;
}

@property (nonatomic, assign) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (nonatomic, assign) id delegate;
@property (nonatomic, assign) SEL action;

@property (nonatomic, retain) NSPredicate *masterPredicate;
@property (nonatomic, retain) NSString *reference;

- (id)initWithManagedObjectContext:(NSManagedObjectContext*)context;

- (void)addEntityToWatch:(NSEntityDescription*)description withPredicate:(NSPredicate*)predicate;

@end
119 changes: 119 additions & 0 deletions ZSContextWatcher.m
@@ -0,0 +1,119 @@
//
// ZSContextWatcher.m
//
// Copyright 2010 Zarra Studios, LLC All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#import "ZSContextWatcher.h"

@implementation ZSContextWatcher

- (id)initWithManagedObjectContext:(NSManagedObjectContext*)context;
{
ZAssert(context, @"Context is nil!");
[super init];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextUpdated:) name:NSManagedObjectContextDidSaveNotification object:nil];

persistentStoreCoordinator = [context persistentStoreCoordinator];

return self;
}

- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
delegate = nil;
[masterPredicate release], masterPredicate = nil;
[super dealloc];
}

- (void)addEntityToWatch:(NSEntityDescription*)description withPredicate:(NSPredicate*)predicate;
{
NSPredicate *entityPredicate = [NSPredicate predicateWithFormat:@"entity.name == %@", [description name]];
NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:entityPredicate, predicate, nil]];

if (![self masterPredicate]) {
[self setMasterPredicate:finalPredicate];
return;
}

NSArray *array = [[NSArray alloc] initWithObjects:[self masterPredicate], finalPredicate, nil];
finalPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:array];
[array release], array = nil;
[self setMasterPredicate:finalPredicate];
}

- (void)contextUpdated:(NSNotification*)notification
{
NSManagedObjectContext *incomingContext = [notification object];
if ([incomingContext persistentStoreCoordinator] != [self persistentStoreCoordinator]) return;
if ([self reference]) {
DLog(@"%@ entered", [self reference]);
}
NSMutableSet *inserted = [[[notification userInfo] objectForKey:NSInsertedObjectsKey] mutableCopy];
[inserted filterUsingPredicate:[self masterPredicate]];
NSMutableSet *deleted = [[[notification userInfo] objectForKey:NSDeletedObjectsKey] mutableCopy];
[deleted filterUsingPredicate:[self masterPredicate]];
NSMutableSet *updated = [[[notification userInfo] objectForKey:NSUpdatedObjectsKey] mutableCopy];
[updated filterUsingPredicate:[self masterPredicate]];

NSInteger totalCount = [inserted count] + [deleted count] + [updated count];
if (totalCount == 0) {
[inserted release], inserted = nil;
[deleted release], deleted = nil;
[updated release], updated = nil;
if ([self reference]) {
DLog(@"%@----------fail on count", [self reference]);
}
return;
}

NSMutableDictionary *results = [[NSMutableDictionary alloc] init];
if (inserted) [results setObject:inserted forKey:NSInsertedObjectsKey];
if (deleted) [results setObject:deleted forKey:NSDeletedObjectsKey];
if (updated) [results setObject:updated forKey:NSUpdatedObjectsKey];

if ([[self delegate] respondsToSelector:[self action]]) {
if ([self reference]) {
DLog(@"%@++++++++++firing action", [self reference]);
}
[[self delegate] performSelector:[self action] withObject:self withObject:results];
} else {
if ([self reference]) {
DLog(@"%@----------delegate doesn't respond", [self reference]);
}
}
[results release], results = nil;
[inserted release], inserted = nil;
[deleted release], deleted = nil;
[updated release], updated = nil;
}

@synthesize persistentStoreCoordinator;
@synthesize delegate;
@synthesize action;
@synthesize masterPredicate;
@synthesize reference;

@end
59 changes: 59 additions & 0 deletions ZSImageCacheHandler.h
@@ -0,0 +1,59 @@
/*
* ZSImageCacheHandler.h
*
* Created by Marcus S. Zarra
* Copyright Zarra Studos LLC 2010. All rights reserved.
*
* Implementation of an image cache that stores downloaded images
* based on a URL key. The cache is not persistent (OS makes no
* guarantees) and is not backed-up when the device is sync'd.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/

#define kImageItemDownloadedKey @"kImageItemDownloadedKey"
#define kImageItem @"kImageItem"
#define kImageDownloadComplete @"kImageDownloadComplete"
#define kCachePath @"imageCache"

#import "ZSURLConnectionDelegate.h";

@interface ZSImageCacheHandler : NSObject
{
NSMutableDictionary *currentRequests;
NSMutableDictionary *imageCache;
NSOperationQueue *operationQueue;
NSManagedObjectContext *managedObjectContext;

NSString *headshotBaseURLString;
}

@property (nonatomic, retain) NSString *headshotBaseURLString;

- (id)initWithManagedObjectContext:(NSManagedObjectContext*)moc;

- (UIImage*)imageForURL:(NSString*)url;

- (NSString*)URLForPlayer:(NSString*)playerID withSize:(CGSize)size;

@end

0 comments on commit 572b86a

Please sign in to comment.