github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/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 18 @implementation LAViewController 19 20 - (void)viewDidLoad 21 { 22 [super viewDidLoad]; 23 24 self.operations = [NSMutableArray array]; 25 26 self.navigationItem.title = @"camlistore"; 27 28 UIBarButtonItem *settingsItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(showSettings)]; 29 30 [self.navigationItem setRightBarButtonItem:settingsItem]; 31 32 NSURL *serverURL = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] stringForKey:CamliServerKey]]; 33 NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:CamliUsernameKey]; 34 35 NSString *password = nil; 36 if (username) { 37 password = [LACamliUtil passwordForUsername:username]; 38 } 39 40 if (!serverURL || !username || !password) { 41 [self showSettings]; 42 } 43 44 [[NSNotificationCenter defaultCenter] addObserverForName:@"statusText" object:nil queue:nil usingBlock:^(NSNotification *note) { 45 UploadStatusCell *cell = (UploadStatusCell *)[_table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 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 UploadStatusCell *cell = (UploadStatusCell *)[_table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 54 55 dispatch_async(dispatch_get_main_queue(), ^{ 56 cell.error.text = note.object[@"text"]; 57 }); 58 }]; 59 } 60 61 - (void)showSettings 62 { 63 SettingsViewController *settings = [self.storyboard instantiateViewControllerWithIdentifier:@"settings"]; 64 [settings setParent:self]; 65 66 [self presentViewController:settings animated:YES completion:nil]; 67 } 68 69 - (void)dismissSettings 70 { 71 [self dismissViewControllerAnimated:YES completion:nil]; 72 73 [(LAAppDelegate *)[[UIApplication sharedApplication] delegate] loadCredentials]; 74 } 75 76 #pragma mark - client delegate methods 77 78 - (void)addedUploadOperation:(LACamliUploadOperation *)op 79 { 80 NSIndexPath *path = [NSIndexPath indexPathForRow:[_operations count] inSection:1]; 81 82 @synchronized(_operations){ 83 [_operations addObject:op]; 84 [_table insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic]; 85 } 86 } 87 88 - (void)finishedUploadOperation:(LACamliUploadOperation *)op 89 { 90 NSIndexPath *path = [NSIndexPath indexPathForRow:[_operations indexOfObject:op] inSection:1]; 91 92 @synchronized(_operations){ 93 [_operations removeObject:op]; 94 [_table deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic]; 95 } 96 } 97 98 - (void)uploadProgress:(float)pct forOperation:(LACamliUploadOperation *)op 99 { 100 NSIndexPath *path = [NSIndexPath indexPathForRow:[_operations indexOfObject:op] inSection:1]; 101 UploadTaskCell *cell = (UploadTaskCell *)[_table cellForRowAtIndexPath:path]; 102 103 cell.progress.progress = pct; 104 } 105 106 #pragma mark - table view methods 107 108 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 109 { 110 UITableViewCell *cell; 111 112 if (indexPath.section == 0) { 113 cell = [tableView dequeueReusableCellWithIdentifier:@"statusCell" forIndexPath:indexPath]; 114 } else { 115 cell = [tableView dequeueReusableCellWithIdentifier:@"taskCell" forIndexPath:indexPath]; 116 117 LACamliUploadOperation *op = [_operations objectAtIndex:indexPath.row]; 118 119 [[(UploadTaskCell *)cell displayText] setText:[NSString stringWithFormat:@"%@",[op name]]]; 120 } 121 122 return cell; 123 } 124 125 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 126 { 127 NSString *title = @""; 128 129 if (section == 0) { 130 title = @"status"; 131 } else { 132 title = @"uploads"; 133 } 134 135 return title; 136 } 137 138 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 139 { 140 return 2; 141 } 142 143 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 144 { 145 if (section == 0) { 146 return 1; 147 } else { 148 return [self.operations count]; 149 } 150 } 151 152 #pragma mark - other 153 154 - (void)didReceiveMemoryWarning 155 { 156 [super didReceiveMemoryWarning]; 157 // Dispose of any resources that can be recreated. 158 } 159 160 - (void)dealloc 161 { 162 [[NSNotificationCenter defaultCenter] removeObserver:self]; 163 } 164 165 @end