Skip to content

Instantly share code, notes, and snippets.

@pnegri
Created November 4, 2012 19:17
Show Gist options
  • Save pnegri/4013162 to your computer and use it in GitHub Desktop.
Save pnegri/4013162 to your computer and use it in GitHub Desktop.
//
// AppDelegate.m
// PayWithIugu
//
// Created by Patrick Negri on 09/08/12.
// Copyright (c) 2012 Iugu. All rights reserved.
//
#import "AppDelegate.h"
#import "LockViewController.h"
@implementation AppDelegate
@synthesize managedObjectContext = _managedObjectContext;
LockViewController *lockView;
UIImageView *lockViewHeader;
- (NSManagedObjectContext *) managedObjectContext {
if(managedObjectContext == nil)
{
NSLog(@"Started Managed Object...");
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *storeDatabasedUrl = [url URLByAppendingPathComponent:@"datastore.storage"];
NSError *error = nil;
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:nil]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeDatabasedUrl options:nil error:&error])
{
NSLog(@"Error while loading persistent store...");
}
managedObjectContext = [[NSManagedObjectContext alloc]init];
[managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];
}
return managedObjectContext;
}
- (NSUInteger)totalCards {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity: [NSEntityDescription entityForName:@"Card" inManagedObjectContext: self.managedObjectContext]];
NSError *error = nil;
NSUInteger count = [self.managedObjectContext countForFetchRequest: request error: &error];
return count;
}
- (Card *)getCardPosition:(NSInteger)cardID
{
NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Card" inManagedObjectContext:self.managedObjectContext]];
[request setPredicate: [NSPredicate predicateWithFormat: @"(id=%d)", cardID]];
NSError *error = nil;
[self.managedObjectContext countForFetchRequest:request error:&error];
NSArray *queryResult = [self.managedObjectContext executeFetchRequest:request error:&error];
if ([queryResult count] > 0)
{
Card *card = (Card *)[queryResult objectAtIndex:0];
if (card) return card;
}
NSLog(@"NEW CARD");
Card *newCard = (Card *) [NSEntityDescription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:self.managedObjectContext];
newCard.id = [[NSNumber alloc] initWithInt:cardID];
newCard.data = @"{}";
[self.managedObjectContext save:nil];
return newCard;
}
- (void)saveCardData:(NSInteger)cardID data:(NSString *)cardData
{
Card *card = [self getCardPosition:cardID];
card.id = [[NSNumber alloc] initWithInt:cardID];
card.data = cardData;
[self.managedObjectContext save:nil];
}
- (void) deleteAllObjects: (NSString *) entityDescription
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityDescription inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *items = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *managedObject in items) {
[self.managedObjectContext deleteObject:managedObject];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"ENTER BACKGROUND");
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
NSLog(@"ENTERING ACTIVE");
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
NSLog(@"ENTERING ACTIVE 2");
if (lockView == nil) {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
lockView = [mainStoryboard instantiateViewControllerWithIdentifier:@"LockScreen"];
[lockView setTarget:self withAction:@selector(codeEntered:)];
//[vc.view.backgroundColor ]
lockViewHeader = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LockControllerUpper"]];
lockViewHeader.frame = CGRectMake(0, 0, lockViewHeader.image.size.width, lockViewHeader.image.size.height);
lockViewHeader.hidden = NO;
lockView.view.frame = CGRectMake(0,460 - (303),320,303);
[self.window.rootViewController.view addSubview:lockViewHeader];
[self.window.rootViewController addChildViewController:lockView];
[self.window.rootViewController.view addSubview:lockView.view];
[self.window.rootViewController.view bringSubviewToFront:lockView.view];
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, 320/2, 460+(303/2));
CGPathAddLineToPoint(curvedPath, NULL, 320/2, 460 - (303/2) );
//CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, vc.view.frame.origin.y, endPoint.x, vc.view.frame.origin.y, endPoint.x, endPoint.y);
pathAnimation.path = curvedPath;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseIn];
CGPathRelease(curvedPath);
CAAnimationGroup *group = [CAAnimationGroup animation];
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
[group setAnimations:[NSArray arrayWithObjects:pathAnimation, nil]];
group.duration = 0.3f;
group.delegate = self;
[lockView.view.layer addAnimation:group forKey:@"lockScreenAnimationBottom"];
CAKeyframeAnimation *pathAnimationB = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimationB.calculationMode = kCAAnimationPaced;
pathAnimationB.fillMode = kCAFillModeForwards;
pathAnimationB.removedOnCompletion = NO;
CGMutablePathRef curvedPathB = CGPathCreateMutable();
CGPathMoveToPoint(curvedPathB, NULL, 320/2, -lockViewHeader.image.size.height);
CGPathAddLineToPoint(curvedPathB, NULL, 320/2, (lockViewHeader.image.size.height/2) );
//CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, vc.view.frame.origin.y, endPoint.x, vc.view.frame.origin.y, endPoint.x, endPoint.y);
pathAnimationB.path = curvedPathB;
pathAnimationB.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseIn];
CGPathRelease(curvedPathB);
CAAnimationGroup *groupB = [CAAnimationGroup animation];
groupB.fillMode = kCAFillModeForwards;
groupB.removedOnCompletion = NO;
[groupB setAnimations:[NSArray arrayWithObjects:pathAnimationB, nil]];
groupB.duration = 0.3f;
groupB.delegate = self;
[lockViewHeader.layer addAnimation:groupB forKey:@"lockScreenAnimationHeader"];
}
//[self.window.rootViewController.view addSubview:vc.view];
/*
[self.window.rootViewController transitionFromViewController:self.window.rootViewController
toViewController:vc
duration:0.3f
options:UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionCurveEaseInOut
animations:^(void){
//oldView.view.center = CGPointMake( cardSwipper.bounds.size.width + (cardSwipper.bounds.size.width/2), cardSwipper.bounds.size.height / 2);
//currentCardView.view.center = CGPointMake( cardSwipper.bounds.size.width/2, cardSwipper.bounds.size.height / 2);
//firstView.view.alpha = 0.0f;
//secondView.view.alpha = 1.0f;
}
completion:^(BOOL finished)
{
//[oldView.view removeFromSuperview];
[self.window.rootViewController.view addSubview:vc.view];
}
];
*/
/*
[self.window.rootViewController presentViewController:vc animated:TRUE completion:^(void){
NSLog(@"END");
}];
*/
}
- (void)codeEntered:(NSString*)key {
NSLog(@"key: %@", key);
if (![key isEqualToString:@"01040302081216"]) {
/*
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Wrong pattern!"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[alertView show];
*/
}
else
[self dismissLockScreen];
}
- (void) dismissLockScreen
{
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, 320/2, 460-(303/2));
CGPathAddLineToPoint(curvedPath, NULL, 320/2, 460 + (303/2) );
//CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, vc.view.frame.origin.y, endPoint.x, vc.view.frame.origin.y, endPoint.x, endPoint.y);
pathAnimation.path = curvedPath;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseIn];
CGPathRelease(curvedPath);
CAAnimationGroup *group = [CAAnimationGroup animation];
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
[group setAnimations:[NSArray arrayWithObjects:pathAnimation, nil]];
group.duration = 0.3f;
group.delegate = self;
[group setValue:@"lockScreenBaseDismiss" forKey:@"name"];
[lockView.view.layer addAnimation:group forKey:@"lockScreenAnimationBottom"];
CAKeyframeAnimation *pathAnimationB = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimationB.calculationMode = kCAAnimationPaced;
pathAnimationB.fillMode = kCAFillModeForwards;
pathAnimationB.removedOnCompletion = NO;
CGMutablePathRef curvedPathB = CGPathCreateMutable();
CGPathMoveToPoint(curvedPathB, NULL, 320/2, (lockViewHeader.image.size.height/2) );
CGPathAddLineToPoint(curvedPathB, NULL, 320/2, -lockViewHeader.image.size.height );
//CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, vc.view.frame.origin.y, endPoint.x, vc.view.frame.origin.y, endPoint.x, endPoint.y);
pathAnimationB.path = curvedPathB;
pathAnimationB.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseIn];
CGPathRelease(curvedPathB);
CAAnimationGroup *groupB = [CAAnimationGroup animation];
groupB.fillMode = kCAFillModeForwards;
groupB.removedOnCompletion = NO;
[groupB setAnimations:[NSArray arrayWithObjects:pathAnimationB, nil]];
groupB.duration = 0.3f;
groupB.delegate = self;
[groupB setValue:@"lockScreeHeaderDismiss" forKey:@"name"];
[lockViewHeader.layer addAnimation:groupB forKey:@"lockScreenAnimationHeader"];
}
-(void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)finished
{
if (finished) {
if([[theAnimation valueForKey:@"name"] isEqual: @"lockScreenHeaderDismiss"])
{
if (lockViewHeader) {
[lockViewHeader removeFromSuperview];
lockViewHeader = nil;
}
}
if([[theAnimation valueForKey:@"name"] isEqual: @"lockScreenBaseDismiss"])
{
if (lockView) {
[lockView removeFromParentViewController];
[lockView.view removeFromSuperview];
lockView = nil;
NSLog(@"Setou Base == nil");
}
}
}
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment