ListTableViewController.m (8006B)
1 #import "EditItemTableViewController.h" 2 #import "ListTableViewController.h" 3 4 #import "DataStructures.h" 5 #import "Network.h" 6 7 @interface ListTableViewController () { 8 Network *network_connection; 9 } 10 11 - (void)load_initial_data; 12 13 @end 14 15 @implementation ListTableViewController 16 17 - (void) load_initial_data 18 { 19 ListItem *item = [[ListItem alloc] init]; 20 item.name = @"Cheese Pizza"; 21 item.owner = @"Dave"; 22 item.committed = 1; 23 item.shared = 1; 24 [self.list_items addObject:item]; 25 26 item = [[ListItem alloc] init]; 27 item.modifier = 0; 28 item.name = @"Camp stove"; 29 item.owner = @"Steve"; 30 item.committed = 1; 31 item.shared = 1; 32 [self.list_items addObject:item]; 33 34 item = [[ListItem alloc] init]; 35 item.name = @"Ear Plugs"; 36 item.quantity = 10; 37 item.owner = @""; 38 item.committed = 0; 39 item.shared = 1; 40 [self.list_items addObject:item]; 41 42 item = [[ListItem alloc] init]; 43 item.name = @"Fruit by the Foot"; 44 item.quantity = 1; 45 item.owner = @"You"; 46 item.committed = 1; 47 item.shared = 1; 48 [self.list_items addObject:item]; 49 50 item = [[ListItem alloc] init]; 51 item.name = @"Well used matress"; 52 item.quantity = 1; 53 item.owner = @""; 54 item.committed = 0; 55 item.shared = 1; 56 [self.list_items addObject:item]; 57 58 item = [[ListItem alloc] init]; 59 item.name = @"Rifle and Ammo"; 60 item.quantity = 1; 61 item.owner = @"Greg"; 62 item.committed = 1; 63 item.shared = 1; 64 [self.list_items addObject:item]; 65 66 67 item = [[ListItem alloc] init]; 68 item.name = @"Deoderant"; 69 item.shared = 0; 70 [self.private_items addObject:item]; 71 72 item = [[ListItem alloc] init]; 73 item.name = @"Toothbrush"; 74 item.shared = 0; 75 [self.private_items addObject:item]; 76 77 item = [[ListItem alloc] init]; 78 item.name = @"Pillow"; 79 [self.private_items addObject:item]; 80 81 item = [[ListItem alloc] init]; 82 item.name = @"Brass knuckles"; 83 [self.private_items addObject:item]; 84 85 item = [[ListItem alloc] init]; 86 item.name = @"Soldering Iron"; 87 [self.private_items addObject:item]; 88 89 item = [[ListItem alloc] init]; 90 item.name = @"8mm wrench"; 91 [self.private_items addObject:item]; 92 93 item = [[ListItem alloc] init]; 94 item.name = @"Fuzzy Dice"; 95 [self.private_items addObject:item]; 96 97 item = [[ListItem alloc] init]; 98 item.name = @"Jerry Can"; 99 [self.private_items addObject:item]; 100 } 101 102 - (void) viewDidLoad 103 { 104 [super viewDidLoad]; 105 106 // Uncomment the following line to preserve selection between 107 // presentations. 108 // self.clearsSelectionOnViewWillAppear = NO; 109 110 // Uncomment the following line to display an Edit button in the 111 // navigation bar for this view controller. 112 // self.navigationItem.leftBarButtonItem = self.editButtonItem; 113 114 _list_items = [[NSMutableArray alloc] init]; 115 _private_items = [[NSMutableArray alloc] init]; 116 117 network_connection = [Network shared_network_connection]; 118 [self load_initial_data]; 119 } 120 121 - (void) didReceiveMemoryWarning 122 { 123 [super didReceiveMemoryWarning]; 124 // Dispose of any resources that can be recreated. 125 } 126 127 // called when commit switch is toggled, section 0 only 128 - (IBAction)commit_toggled:(id)sender 129 { 130 // XXX: This is an iOS 7+ hack only! 131 // http://stackoverflow.com/questions/13014592/how-to-get-indexpath-over-touched-button 132 UITableViewCell *cell = (UITableViewCell *)[[[sender superview] superview] superview]; 133 NSIndexPath *path = [self.tableView indexPathForCell:cell]; 134 135 ListItem *item = [_list_items objectAtIndex:[path row]]; 136 137 // UISwitch *shared_sw = (UISwitch *)sender; 138 NSLog(@"debug: %@: commit toggled", item.name); 139 140 // send an update list item message 141 // device_id:list_id:item_id:item_name:item_shared:item_owner:... 142 // [network_connection send_message:7 contents:]; 143 } 144 145 // called when previous segue's are unwinding 146 - (IBAction)unwindToList:(UIStoryboardSegue *)segue 147 { 148 } 149 150 - (void) setMetadata:(SharedList *)metadata 151 { 152 _list_metadata = metadata; 153 self.title = _list_metadata.name; 154 } 155 156 - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView 157 { 158 return 2; 159 } 160 161 - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 162 { 163 if (section == 0) 164 return [_list_items count]; 165 else if (section == 1) 166 return [_private_items count]; 167 return 0; 168 } 169 170 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 171 { 172 if (section == 0) 173 return [NSString stringWithFormat:@"Shared Items (%lu)", (unsigned long)[_list_items count]]; 174 else if (section == 1) 175 return [NSString stringWithFormat:@"Private Items (%lu)", (unsigned long)[_private_items count]]; 176 return @""; 177 } 178 179 - (UITableViewCell *) tableView:(UITableView *)tableView 180 cellForRowAtIndexPath:(NSIndexPath *)indexPath 181 { 182 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListDetailPrototypeCell" 183 forIndexPath:indexPath]; 184 185 // Tags: 186 // 1) modifier -- ie $, info, etc 187 // 2) item name 188 // 3) quantity of item, in parenthesis 189 // 4) owners name 190 // 5) completion/packing of item 191 192 UILabel *item_name = (UILabel *)[cell viewWithTag:2]; 193 UILabel *quantity = (UILabel *)[cell viewWithTag:3]; 194 UILabel *owner = (UILabel *)[cell viewWithTag:4]; 195 UISwitch *commit_switch = (UISwitch *)[cell viewWithTag:5]; 196 197 /* 198 if (item.modifier == 1) { 199 UIImageView *image_view; 200 image_view = (UIImageView *)[cell viewWithTag:1]; 201 image_view.image = [UIImage imageNamed: @"dollar103-2.png"]; 202 } 203 else if (item.modifier == 2) { 204 UIImageView *image_view; 205 image_view = (UIImageView *)[cell viewWithTag:1]; 206 image_view.image = [UIImage imageNamed: @"information15-3.png"]; 207 } 208 */ 209 210 ListItem *item; 211 if ([indexPath section] == 0) { 212 // "shared items" section 213 item = [self.list_items objectAtIndex:indexPath.row]; 214 215 owner.text = @""; 216 [commit_switch setOn:item.committed animated:YES]; 217 218 if (item.committed) { 219 if ([item.owner compare:@"You"] == NSOrderedSame) 220 owner.text = @""; 221 else { 222 owner.text = item.owner; 223 [commit_switch setEnabled:NO]; 224 } 225 } 226 227 commit_switch.hidden = false; 228 owner.hidden = false; 229 } 230 else if ([indexPath section] == 1) { 231 // "private items" section 232 item = [self.private_items objectAtIndex:indexPath.row]; 233 234 // no owner or commit fields here 235 owner.hidden = true; 236 commit_switch.hidden = true; 237 } 238 else 239 return nil; 240 241 item_name.text = item.name; 242 243 if (item.quantity > 1) 244 quantity.text = [NSString stringWithFormat:@"x%d", item.quantity]; 245 else 246 quantity.text = @""; 247 248 return cell; 249 } 250 251 // Override to support editing the table view. 252 - (void) tableView:(UITableView *)tableView 253 commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 254 forRowAtIndexPath:(NSIndexPath *)indexPath 255 { 256 if (editingStyle == UITableViewCellEditingStyleDelete) { 257 // Delete the row from the data source 258 // [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 259 } 260 } 261 262 // In a storyboard-based application, you will often want to do a little preparation before navigation 263 - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 264 { 265 // This guys answer here saved my ass once 266 // http://stackoverflow.com/questions/9727549/unrecognized-selector-sent-to-instance-using-storyboards 267 268 // I like the two line version from above but it gives warnings :( 269 EditItemTableViewController *tvc = (EditItemTableViewController *)[[segue destinationViewController] topViewController]; 270 271 if ([[segue identifier] isEqualToString:@"edit item segue"]) { 272 // NSIndexPath *path = [self.tableView indexPathForSelectedRow]; 273 274 NSIndexPath *path = [self.tableView indexPathForCell:sender]; 275 ListItem *item; 276 if ([path section] == 0) 277 item = [self.list_items objectAtIndex:[path row]]; 278 else if ([path section] == 1) 279 item = [self.private_items objectAtIndex:[path row]]; 280 else 281 // segue from unknown section 282 return; 283 284 // make sure incoming view controller knows about itself 285 [tvc set_item:item for_list:_list_metadata]; 286 [tvc set_edit_or_new:@"Edit Item"]; 287 288 NSLog(@"debug: %@: edit item segue", _list_metadata.name); 289 } 290 else if ([[segue identifier] isEqualToString:@"add item segue"]) { 291 [tvc set_edit_or_new:@"Add Item"]; 292 NSLog(@"debug: %@: add item segue", _list_metadata.name); 293 } 294 } 295 296 @end