Skip to content

Commit

Permalink
ZDSStreamJSONParser and start of sample project
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus S. Zarra committed Nov 3, 2011
1 parent 01c95cd commit 6b4db4b
Show file tree
Hide file tree
Showing 29 changed files with 2,720 additions and 0 deletions.
38 changes: 38 additions & 0 deletions FJImporter/Classes/CGFAppDelegate.h
@@ -0,0 +1,38 @@
//
// CGFAppDelegate.h
// FJImporter
//
// Created by Marcus Zarra on 10/28/11.
// Copyright (c) 2011 Cocoa Is My Girlfriend. 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 CGFAppDelegate : UIResponder <UIApplicationDelegate>

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) UINavigationController *navController;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;

- (void)saveContext;

@end
116 changes: 116 additions & 0 deletions FJImporter/Classes/CGFAppDelegate.m
@@ -0,0 +1,116 @@

//
// CGFAppDelegate.m
// FJImporter
//
// Created by Marcus Zarra on 10/28/11.
// Copyright (c) 2011 Cocoa Is My Girlfriend. 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 "CGFAppDelegate.h"

#import "CGFUsernameViewController.h"

@implementation CGFAppDelegate

@synthesize window;
@synthesize managedObjectContext;
@synthesize navController;

- (void)dealloc
{
[window release];
[managedObjectContext release];

[super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self setWindow:[[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]];

CGFUsernameViewController *controller = [[CGFUsernameViewController alloc] initWithNibName:@"CGFUsernameView" bundle:nil];
[controller setManagedObjectContext:[self managedObjectContext]];

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
[self setNavController:nav];
MCRelease(controller);
MCRelease(nav);

[[self window] addSubview:[[self navController] view]];

[[self window] makeKeyAndVisible];

return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
[self saveContext];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self saveContext];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
[self saveContext];
}

- (void)saveContext
{
NSError *error = nil;

if (![self managedObjectContext]) return;
if (![[self managedObjectContext] hasChanges]) return;

ZAssert([[self managedObjectContext] save:&error], @"Unresolved error %@, %@", error, [error userInfo]);
}

#pragma mark - Core Data stack

- (NSManagedObjectContext *)managedObjectContext
{
if (managedObjectContext) return managedObjectContext;

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"FJImporter" withExtension:@"momd"];
NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

NSURL *storeURL = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:@"FJImporter.sqlite"];

NSError *error = nil;
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
MCRelease(mom);

ZAssert([psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error], @"Unresolved error %@, %@", error, [error userInfo]);

managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[managedObjectContext setPersistentStoreCoordinator:psc];
MCRelease(psc);

return managedObjectContext;
}

@end
14 changes: 14 additions & 0 deletions FJImporter/Classes/CGFTimelineViewController.h
@@ -0,0 +1,14 @@
//
// CGFTimelineViewController.h
// FJImporter
//
// Created by Marcus Zarra on 11/3/11.
// Copyright (c) 2011 Cocoa Is My Girlfriend. All rights reserved.
//

@interface CGFTimelineViewController : UITableViewController

@property (nonatomic, retain) NSManagedObject *user;
@property (nonatomic, assign) NSManagedObjectContext *managedObjectContext;

@end
125 changes: 125 additions & 0 deletions FJImporter/Classes/CGFTimelineViewController.m
@@ -0,0 +1,125 @@
//
// CGFTimelineViewController.m
// FJImporter
//
// Created by Marcus Zarra on 11/3/11.
// Copyright (c) 2011 Cocoa Is My Girlfriend. 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 "CGFTimelineViewController.h"

@interface CGFTimelineViewController() <NSFetchedResultsControllerDelegate>

@property (nonatomic, retain) NSFetchedResultsController *fetchController;

@end

@implementation CGFTimelineViewController

- (void)viewDidLoad
{
[super viewDidLoad];

[self setTitle:[[self user] valueForKey:@"name"]];

if ([self fetchController]) return;

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Tweet"];
[request setPredicate:[NSPredicate predicateWithFormat:@"ANY timeline == %@", [self user]]];

NSSortDescriptor *sort = [[[NSSortDescriptor alloc] initWithKey:@"createdAt" ascending:NO] autorelease];
[request setSortDescriptors:[NSArray arrayWithObject:sort]];

NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:[[self user] valueForKey:@"name"]];
[controller setDelegate:self];

NSError *error = nil;
ZAssert([controller performFetch:&error], @"Error fetching tweets: %@", error);

[self setFetchController:controller];
MCRelease(controller);
}

#pragma mark - Table view data source

- (void)configureCell:(id)cell atIndexPath:(NSIndexPath*)indexPath
{
NSManagedObject *tweet = [[self fetchController] objectAtIndexPath:indexPath];

[[cell textLabel] setText:[tweet valueForKey:@"text"]];
[[cell detailTextLabel] setText:[tweet valueForKeyPath:@"author.name"]];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[[self fetchController] sections] objectAtIndex:section] numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
id cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}

[self configureCell:cell atIndexPath:indexPath];

return cell;
}

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[[self tableView] beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
switch(type) {
case NSFetchedResultsChangeInsert:
[[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[[self tableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[[self tableView] cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[[self tableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[[self tableView] endUpdates];
}

@synthesize user;
@synthesize managedObjectContext;
@synthesize fetchController;

@end
41 changes: 41 additions & 0 deletions FJImporter/Classes/CGFURLDelegateOperation.h
@@ -0,0 +1,41 @@
//
// CGFURLDelegateOperation.h
//
// Created by Marcus Zarra on 10/28/11.
// Copyright (c) 2011 Cocoa Is My Girlfriend. 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.

@class YAJLParser;

@interface CGFURLDelegateOperation : NSOperation

@property (nonatomic, retain) NSURL *requestURL;
@property (nonatomic, retain) YAJLParser *parser;
@property (nonatomic, retain) NSHTTPURLResponse *response;

@property (nonatomic, retain) NSManagedObject *user;
@property (nonatomic, assign) NSManagedObjectContext *managedObjectContext;

- (id)initWithRequestURL:(NSURL*)aURL andContext:(NSManagedObjectContext*)aContext;

@end

0 comments on commit 6b4db4b

Please sign in to comment.