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

     1  //
     2  //  LAViewController.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 "LAViewController.h"
    10  #import "LACamliClient.h"
    11  #import "LAAppDelegate.h"
    12  #import "LACamliUtil.h"
    13  #import "SettingsViewController.h"
    14  #import "LACamliUploadOperation.h"
    15  #import "UploadStatusCell.h"
    16  #import "UploadTaskCell.h"
    17  #import "LACamliFile.h"
    18  #import <BugshotKit.h>
    19  
    20  @implementation LAViewController
    21  
    22  - (void)viewDidLoad
    23  {
    24      [super viewDidLoad];
    25  
    26      _operations = [NSMutableArray array];
    27  
    28      self.navigationItem.title = @"camlistore";
    29  
    30      UIBarButtonItem* reportItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
    31                                                                                  target:self
    32                                                                                  action:@selector(reportBug)];
    33  
    34      [self.navigationItem setLeftBarButtonItem:reportItem];
    35  
    36      UIBarButtonItem* settingsItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
    37                                                                                    target:self
    38                                                                                    action:@selector(showSettings)];
    39  
    40      [self.navigationItem setRightBarButtonItem:settingsItem];
    41  
    42      [[NSNotificationCenter defaultCenter] addObserverForName:@"statusText" object:nil queue:nil usingBlock:^(NSNotification *note)
    43      {
    44          UploadStatusCell* cell = (UploadStatusCell*)[_table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0
    45                                                                                                       inSection:0]];
    46  
    47          dispatch_async(dispatch_get_main_queue(), ^{
    48              cell.status.text = note.object[@"text"];
    49          });
    50      }];
    51  
    52      [[NSNotificationCenter defaultCenter] addObserverForName:@"errorText" object:nil queue:nil usingBlock:^(NSNotification *note)
    53      {
    54          UploadStatusCell* cell = (UploadStatusCell*)[_table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0
    55                                                                                                       inSection:0]];
    56  
    57          dispatch_async(dispatch_get_main_queue(), ^{
    58              cell.error.text = note.object[@"text"];
    59          });
    60      }];
    61  }
    62  
    63  - (void)viewDidAppear:(BOOL)animated
    64  {
    65      NSURL* serverURL = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] stringForKey:CamliServerKey]];
    66      NSString* username = [[NSUserDefaults standardUserDefaults] stringForKey:CamliUsernameKey];
    67  
    68      NSString* password = nil;
    69      if (username) {
    70          password = [LACamliUtil passwordForUsername:username];
    71      }
    72  
    73      if (!serverURL || !username || !password) {
    74          [self showSettings];
    75      }
    76  }
    77  
    78  - (void)reportBug
    79  {
    80      [BugshotKit show];
    81  }
    82  
    83  - (void)showSettings
    84  {
    85      SettingsViewController* settings = [self.storyboard instantiateViewControllerWithIdentifier:@"settings"];
    86      [settings setParent:self];
    87  
    88      [self presentViewController:settings
    89                         animated:YES
    90                       completion:nil];
    91  }
    92  
    93  - (void)dismissSettings
    94  {
    95      [self dismissViewControllerAnimated:YES
    96                               completion:nil];
    97  
    98      [(LAAppDelegate*)[[UIApplication sharedApplication] delegate] loadCredentials];
    99  }
   100  
   101  #pragma mark - client delegate methods
   102  
   103  - (void)addedUploadOperation:(LACamliUploadOperation*)op
   104  {
   105      @synchronized(_operations)
   106      {
   107          NSIndexPath* path = [NSIndexPath indexPathForRow:[_operations count]
   108                                                 inSection:1];
   109  
   110          [_operations addObject:op];
   111          [_table insertRowsAtIndexPaths:@[
   112                                             path
   113                                         ]
   114                        withRowAnimation:UITableViewRowAnimationAutomatic];
   115      }
   116  }
   117  
   118  - (void)finishedUploadOperation:(LACamliUploadOperation*)op
   119  {
   120      NSIndexPath* path = [NSIndexPath indexPathForRow:[_operations indexOfObject:op]
   121                                             inSection:1];
   122  
   123      @synchronized(_operations)
   124      {
   125          [_operations removeObject:op];
   126          [_table deleteRowsAtIndexPaths:@[
   127                                             path
   128                                         ]
   129                        withRowAnimation:UITableViewRowAnimationAutomatic];
   130      }
   131  }
   132  
   133  - (void)uploadProgress:(float)pct forOperation:(LACamliUploadOperation*)op
   134  {
   135      NSIndexPath* path = [NSIndexPath indexPathForRow:[_operations indexOfObject:op]
   136                                             inSection:1];
   137      UploadTaskCell* cell = (UploadTaskCell*)[_table cellForRowAtIndexPath:path];
   138  
   139      cell.progress.progress = pct;
   140  }
   141  
   142  #pragma mark - table view methods
   143  
   144  - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
   145  {
   146      if (indexPath.section == 0) {
   147          UploadStatusCell* cell = [tableView dequeueReusableCellWithIdentifier:@"statusCell"
   148                                                                   forIndexPath:indexPath];
   149  
   150          return cell;
   151      } else {
   152          UploadTaskCell* cell = [tableView dequeueReusableCellWithIdentifier:@"taskCell"
   153                                                                 forIndexPath:indexPath];
   154  
   155          cell.progress.progress = 0.0;
   156  
   157          LACamliUploadOperation* op = [_operations objectAtIndex:indexPath.row];
   158          [cell.displayText setText:[NSString stringWithFormat:@"%@", [op name]]];
   159          [cell.preview setImage:[op.file thumbnail]];
   160  
   161          return cell;
   162      }
   163  
   164      return nil;
   165  }
   166  
   167  - (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
   168  {
   169      NSString* title = @"";
   170  
   171      if (section == 0) {
   172          title = @"status";
   173      } else {
   174          title = @"uploads";
   175      }
   176  
   177      return title;
   178  }
   179  
   180  - (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
   181  {
   182      return 2;
   183  }
   184  
   185  - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
   186  {
   187      if (section == 0) {
   188          return 1;
   189      } else {
   190          return [_operations count];
   191      }
   192  }
   193  
   194  #pragma mark - other
   195  
   196  - (void)didReceiveMemoryWarning
   197  {
   198      [super didReceiveMemoryWarning];
   199      // Dispose of any resources that can be recreated.
   200  }
   201  
   202  - (void)dealloc
   203  {
   204      [[NSNotificationCenter defaultCenter] removeObserver:self];
   205  }
   206  
   207  @end