Permalink
Browse files

Update app delegate to start syncing when app comes to foreground

Clean code
  • Loading branch information...
1 parent 0bde684 commit 4ea76ba0def16cc7e4640b561ae6e1ede54a27fd @vtourraine vtourraine committed Dec 2, 2012
@@ -14,9 +14,13 @@
#import "MTESyncManager.h"
#import "ECSlidingViewController.h"
+@interface MTEAppDelegate ()
+
+@property (nonatomic, strong) MTESyncManager * syncManager;
+@end
+
@implementation MTEAppDelegate
-@synthesize window = _window;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
@@ -33,17 +37,17 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"selection-tab"]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor grayColor]];
- MTESyncManager * syncManager = [MTESyncManager new];
+ self.syncManager = [MTESyncManager new];
- [syncManager setupSyncManager];
- [syncManager startSync];
+ [self.syncManager setupSyncManager];
+ [self.syncManager startSync];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
MTETShirtsViewController * tshirtsViewController = (MTETShirtsViewController*)navController.topViewController;
- tshirtsViewController.syncManager = syncManager;
+ tshirtsViewController.syncManager = self.syncManager;
}
else
{
@@ -64,7 +68,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
// if (localNotif)
// tabBarController.selectedIndex = [tabBarController.viewControllers indexOfObject:navController1];
- tshirtsViewController.syncManager = syncManager;
+ tshirtsViewController.syncManager = self.syncManager;
// settingsViewController.syncManager = syncManager;
// todayViewController.managedObjectContext = self.managedObjectContext;
@@ -91,16 +95,13 @@ - (void)applicationDidEnterBackground:(UIApplication *)application
- (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.
- */
+ // 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.
}
- (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.
- */
+ // 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.
+ [self.syncManager startSync];
}
- (void)applicationWillTerminate:(UIApplication *)application
@@ -117,29 +118,20 @@ - (void)saveContext
{
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
{
- /*
- Replace this implementation with code to handle the error appropriately.
-
- abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
- */
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
+#ifdef DEBUG
abort();
+#endif
}
}
}
#pragma mark - Core Data stack
-/**
- Returns the managed object context for the application.
- If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- */
- (NSManagedObjectContext *)managedObjectContext
{
if (__managedObjectContext != nil)
- {
return __managedObjectContext;
- }
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
@@ -150,73 +142,38 @@ - (NSManagedObjectContext *)managedObjectContext
return __managedObjectContext;
}
-/**
- Returns the managed object model for the application.
- If the model doesn't already exist, it is created from the application's model.
- */
- (NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil)
- {
return __managedObjectModel;
- }
+
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"mytee" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return __managedObjectModel;
}
-/**
- Returns the persistent store coordinator for the application.
- If the coordinator doesn't already exist, it is created and the application's store added to it.
- */
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
- {
return __persistentStoreCoordinator;
- }
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"mytee.sqlite"];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
- if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
+ if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{ NSMigratePersistentStoresAutomaticallyOption : @(YES), NSInferMappingModelAutomaticallyOption : @(YES) } error:&error])
{
- /*
- Replace this implementation with code to handle the error appropriately.
-
- abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
-
- Typical reasons for an error here include:
- * The persistent store is not accessible;
- * The schema for the persistent store is incompatible with current managed object model.
- Check the error message to determine what the actual problem was.
-
-
- If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
-
- If you encounter schema incompatibility errors during development, you can reduce their frequency by:
- * Simply deleting the existing store:
- [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
-
- * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
- [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
-
- Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
-
- */
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
+#ifdef DEBUG
abort();
- }
+#endif
+ }
return __persistentStoreCoordinator;
}
#pragma mark - Application's Documents directory
-/**
- Returns the URL to the application's Documents directory.
- */
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
@@ -14,19 +14,14 @@
@implementation MTELoginViewController
-@synthesize emailTextField;
-@synthesize passwordTextField;
-
-@synthesize delegate;
-
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
- emailTextField.text = [MTESyncManager emailFromKeychain];
- passwordTextField.text = [MTESyncManager passwordFromKeychain];
+ self.emailTextField.text = [MTESyncManager emailFromKeychain];
+ self.passwordTextField.text = [MTESyncManager passwordFromKeychain];
}
- (void)viewDidUnload
@@ -41,7 +36,7 @@ - (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
- [emailTextField becomeFirstResponder];
+ [self.emailTextField becomeFirstResponder];
}
#pragma mark - Table view data source
@@ -60,33 +55,33 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
switch (indexPath.row)
{
case 0:
- [emailTextField becomeFirstResponder];
+ [self.emailTextField becomeFirstResponder];
break;
case 1:
- [passwordTextField becomeFirstResponder];
+ [self.passwordTextField becomeFirstResponder];
break;
}
}
if(indexPath.section==1)
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
- if (![emailTextField text] || [[emailTextField text] isEqualToString:@""])
+ if (![self.emailTextField text] || [[self.emailTextField text] isEqualToString:@""])
{
- [emailTextField becomeFirstResponder];
+ [self.emailTextField becomeFirstResponder];
}
- else if (![passwordTextField text] || [[passwordTextField text] isEqualToString:@""])
+ else if (![self.passwordTextField text] || [[self.passwordTextField text] isEqualToString:@""])
{
- [passwordTextField becomeFirstResponder];
+ [self.passwordTextField becomeFirstResponder];
}
else
{
- if ([emailTextField isFirstResponder])
- [emailTextField resignFirstResponder];
- if ([passwordTextField isFirstResponder])
- [passwordTextField resignFirstResponder];
+ if ([self.emailTextField isFirstResponder])
+ [self.emailTextField resignFirstResponder];
+ if ([self.passwordTextField isFirstResponder])
+ [self.passwordTextField resignFirstResponder];
- [self startAuthenticatingWithEmail:[emailTextField text] password:[passwordTextField text]];
+ [self startAuthenticatingWithEmail:[self.emailTextField text] password:[self.passwordTextField text]];
}
}
}
@@ -95,9 +90,9 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
- if (textField==emailTextField)
+ if (textField == self.emailTextField)
{
- [passwordTextField becomeFirstResponder];
+ [self.passwordTextField becomeFirstResponder];
}
else
{
@@ -164,7 +159,7 @@ - (void)hudWasHidden:(MBProgressHUD *)hud
if (authenticationSuccessful)
{
[self dismissViewControllerAnimated:YES completion:nil];
- [delegate loginViewControllerDidLoggedIn:self];
+ [self.delegate loginViewControllerDidLoggedIn:self];
}
}
@@ -35,12 +35,6 @@ - (void)viewDidLoad
}
}
-- (void)viewDidUnload
-{
- [super viewDidUnload];
- self.webView = nil;
-}
-
#pragma mark - Actions
- (IBAction)presentActionSheet:(id)sender
@@ -59,4 +53,5 @@ - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger
[[UIApplication sharedApplication] openURL:URL];
}
}
+
@end
@@ -24,9 +24,6 @@ - (NSString*)storeLocation;
@implementation MTEStoreRetailViewController
-@synthesize geocoder;
-@synthesize mapView;
-
- (void)viewDidLoad
{
[super viewDidLoad];
@@ -37,12 +34,6 @@ - (void)viewDidLoad
self.mapView.layer.cornerRadius = 4;
}
-- (void)viewDidUnload
-{
- [super viewDidUnload];
- self.mapView = nil;
-}
-
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
@@ -51,7 +42,7 @@ - (void)viewDidAppear:(BOOL)animated
if (geocodingAddress)
{
- [geocoder geocodeAddressString:geocodingAddress
+ [self.geocoder geocodeAddressString:geocodingAddress
completionHandler:^(NSArray* placemarks, NSError* error){
if (!error && [placemarks count] > 0)
{
@@ -23,13 +23,6 @@ - (IBAction)presentActionSheet:(id)sender;
@implementation MTEStoreViewController
-@synthesize store;
-
-@synthesize nameLabel;
-@synthesize typeLabel;
-@synthesize addressIconImageView;
-@synthesize addressLabel;
-
#pragma mark - View lifecycle
- (void)viewDidLoad
@@ -41,11 +34,11 @@ - (void)viewDidLoad
if ([self.store.type isEqualToString:@"Web"])
{
- self.addressLabel.text = store.url;
+ self.addressLabel.text = self.store.url;
}
else
{
- self.addressLabel.text = store.address;
+ self.addressLabel.text = self.store.address;
}
((UIScrollView*)self.view).alwaysBounceVertical = YES;
@@ -55,16 +48,6 @@ - (void)viewDidLoad
[self.view setBackgroundColor:woodColor];
}
-- (void)viewDidUnload
-{
- [super viewDidUnload];
-
- self.typeLabel = nil;
- self.nameLabel = nil;
- self.addressIconImageView = nil;
- self.addressLabel = nil;
-}
-
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
@@ -25,8 +25,6 @@
@implementation MTESyncManager
-@synthesize isSyncing;
-
#pragma mark - Keychain
+ (NSString*)pathForResource:(NSString*)resourcePath withEmail:(NSString*)email password:(NSString*)password
@@ -53,7 +51,7 @@ + (BOOL)authenticationResponseIsSuccessful:(NSHTTPURLResponse*)response
{
NSUInteger status = [(NSHTTPURLResponse*)response statusCode];
- if (status==200)
+ if (status == 200)
return YES;
return NO;
Oops, something went wrong.

0 comments on commit 4ea76ba

Please sign in to comment.