github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/clients/ios-objc/photobackup/LAAppDelegate.m (about)

     1  //
     2  //  LAAppDelegate.m
     3  //  photobackup
     4  //
     5  //  Created by Nick O'Neill on 10/20/13.
     6  //  Copyright (c) 2013 The Camlistore Authors. All rights reserved.
     7  //
     8  
     9  #import "LAAppDelegate.h"
    10  #import "LACamliUtil.h"
    11  #import "LACamliFile.h"
    12  #import "LAViewController.h"
    13  #import <BugshotKit.h>
    14  #import <AssetsLibrary/AssetsLibrary.h>
    15  #import <HockeySDK/HockeySDK.h>
    16  
    17  @implementation LAAppDelegate
    18  
    19  - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
    20  {
    21      [[BITHockeyManager sharedHockeyManager] configureWithIdentifier:@"de94cf9f0f0ad2ea0b19b2ad18ebe11f"
    22                                                             delegate:self];
    23      [[BITHockeyManager sharedHockeyManager] startManager];
    24      [[BITHockeyManager sharedHockeyManager].updateManager setDelegate:self];
    25      [[BITHockeyManager sharedHockeyManager].updateManager checkForUpdate];
    26  
    27      [BugshotKit enableWithNumberOfTouches:1
    28                         performingGestures:BSKInvocationGestureNone
    29                       feedbackEmailAddress:@"nick.oneill@gmail.com"];
    30  
    31      self.locationManager = [[CLLocationManager alloc] init];
    32      self.locationManager.delegate = self;
    33      [self.locationManager startMonitoringSignificantLocationChanges];
    34  
    35      [self loadCredentials];
    36  
    37      self.library = [[ALAssetsLibrary alloc] init];
    38  
    39      return YES;
    40  }
    41  
    42  - (void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations
    43  {
    44      [self checkForUploads];
    45  }
    46  
    47  - (void)loadCredentials
    48  {
    49      NSURL* serverURL = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] stringForKey:CamliServerKey]];
    50      NSString* username = [[NSUserDefaults standardUserDefaults] stringForKey:CamliUsernameKey];
    51  
    52      NSString* password = nil;
    53      if (username) {
    54          password = [LACamliUtil passwordForUsername:username];
    55      }
    56  
    57      if (serverURL && username && password) {
    58          [LACamliUtil statusText:@[
    59                                      @"found credentials"
    60                                  ]];
    61          [LACamliUtil logText:@[
    62                                   @"found credentials"
    63                               ]];
    64          self.client = [[LACamliClient alloc] initWithServer:serverURL
    65                                                     username:username
    66                                                  andPassword:password];
    67  
    68          // TODO there must be a better way to get the current instance of this
    69          LAViewController* mainView = (LAViewController*)[(UINavigationController*)self.window.rootViewController topViewController];
    70          [self.client setDelegate:mainView];
    71      } else {
    72          [LACamliUtil statusText:@[
    73                                      @"credentials or server not found"
    74                                  ]];
    75      }
    76  
    77      [self checkForUploads];
    78  }
    79  
    80  - (void)checkForUploads
    81  {
    82      if (self.client && [self.client readyToUpload]) {
    83          NSInteger __block filesToUpload = 0;
    84  
    85          [LACamliUtil statusText:@[
    86                                      @"looking for new files..."
    87                                  ]];
    88  
    89          // checking all assets can take some time
    90          dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    91              [self.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    92  
    93                  [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
    94                      if (result && [result valueForProperty:ALAssetPropertyType] != ALAssetTypeVideo) { // enumerate returns null after the last item
    95  
    96                          NSString *filename = [[result defaultRepresentation] filename];
    97  
    98                          @synchronized(self.client){
    99                              if (![self.client fileAlreadyUploaded:filename]) {
   100                                  filesToUpload++;
   101  
   102                                  [LACamliUtil logText:@[[NSString stringWithFormat:@"found %ld files",(long)filesToUpload]]];
   103  
   104                                  __block LACamliClient *weakClient = self.client;
   105  
   106                                  LACamliFile *file = [[LACamliFile alloc] initWithAsset:result];
   107                                  [self.client addFile:file withCompletion:^{
   108                                      [UIApplication sharedApplication].applicationIconBadgeNumber = [weakClient.uploadQueue operationCount];
   109                                  }];
   110                              }
   111                          }
   112                      }
   113                  }];
   114  
   115                  if (filesToUpload == 0) {
   116                      [LACamliUtil statusText:@[@"no new files to upload"]];
   117                  }
   118  
   119                  [UIApplication sharedApplication].applicationIconBadgeNumber = filesToUpload;
   120  
   121              } failureBlock:^(NSError *error) {
   122                  [LACamliUtil errorText:@[@"failed enumerate: ",[error description]]];
   123              }];
   124          });
   125      }
   126  }
   127  
   128  - (void)applicationWillResignActive:(UIApplication*)application
   129  {
   130      // 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.
   131      // 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.
   132  }
   133  
   134  - (void)applicationDidEnterBackground:(UIApplication*)application
   135  {
   136      // 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.
   137      // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
   138  }
   139  
   140  - (void)applicationWillEnterForeground:(UIApplication*)application
   141  {
   142      // 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.
   143  }
   144  
   145  - (void)applicationDidBecomeActive:(UIApplication*)application
   146  {
   147      // 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.
   148      [self checkForUploads];
   149  }
   150  
   151  - (void)applicationWillTerminate:(UIApplication*)application
   152  {
   153      // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
   154  }
   155  
   156  @end