shlist

share and manage lists between multiple people
Log | Files | Refs

EditItemTableViewController.m (3501B)


      1 #import "EditItemTableViewController.h"
      2 #import "Network.h"
      3 
      4 @interface EditItemTableViewController () {
      5 	Network *network_connection;
      6 }
      7 
      8 @property (weak, nonatomic) IBOutlet UIBarButtonItem *save_button;
      9 
     10 @property (weak, nonatomic) IBOutlet UILabel *item_name;
     11 @property (weak, nonatomic) IBOutlet UILabel *quantity_label;
     12 @property (weak, nonatomic) IBOutlet UISwitch *shared_sw;
     13 
     14 @property (weak, nonatomic) IBOutlet UILabel *owner_label;
     15 @property (weak, nonatomic) IBOutlet UILabel *price_label;
     16 
     17 
     18 @end
     19 
     20 @implementation EditItemTableViewController
     21 
     22 // called when shared switch is toggled
     23 - (IBAction)shared_switch:(id)sender
     24 {
     25 	NSIndexSet *index_set = [NSIndexSet indexSetWithIndex:1];
     26 
     27 	if (_item.shared) {
     28 		_item.shared = false;
     29 		// XXX: send network request with list id, item id, and this device id
     30 		[self.tableView deleteSections:index_set withRowAnimation:UITableViewRowAnimationMiddle];
     31 	} else {
     32 		_item.shared = true;
     33 		// XXX: send item commit network request with list id, item id, and this device id
     34 		[self.tableView insertSections:index_set withRowAnimation:UITableViewRowAnimationMiddle];
     35 	}
     36 }
     37 
     38 - (void) set_item:(ListItem *)item for_list:(SharedList *)list;
     39 {
     40 	_list = list;
     41 	_item = item;
     42 }
     43 
     44 - (void) set_edit_or_new:(NSString *)edit_or_new;
     45 {
     46 	self.title = edit_or_new;
     47 }
     48 
     49 - (void) viewDidLoad
     50 {
     51 	[super viewDidLoad];
     52 	network_connection = [Network shared_network_connection];
     53 
     54 	[_shared_sw setOn:_item.shared animated:YES];
     55 	[_item_name setText:_item.name];
     56 	[_quantity_label setText:[NSString stringWithFormat:@"%i", _item.quantity]];
     57 
     58 	if (_item.committed)
     59 		_owner_label.text = _item.owner;
     60 	else
     61 		_owner_label.text = @"";
     62 
     63 	[_price_label setText:@"$26.99"];
     64 }
     65 
     66 - (void)didReceiveMemoryWarning
     67 {
     68 	[super didReceiveMemoryWarning];
     69 	// Dispose of any resources that can be recreated.
     70 }
     71 
     72 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
     73 {
     74 	if (_item.shared)
     75 		return 2;
     76 	else
     77 		return 1;
     78 }
     79 
     80 // fill in the static table view cells with information
     81 - (UITableViewCell *)tableView:(UITableView *)tableView
     82 	 cellForRowAtIndexPath:(NSIndexPath *)indexPath
     83 {
     84 	UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
     85 
     86 	if ([indexPath section] == 1) {
     87 		/*
     88 		if ([indexPath row] == 0) {
     89 			UILabel *owner = (UILabel *)[cell viewWithTag:1];
     90 
     91 			if (_item.committed)
     92 				owner.text = _item.owner;
     93 			else
     94 				owner.text = @"";
     95 		}
     96 		 */
     97 	}
     98 
     99 	return cell;
    100 }
    101 
    102 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    103 {
    104 	if (sender != self.save_button)
    105 		return;
    106 
    107 	// save item, item_id could be incrementing unique integer
    108 	// device_id:list_id:item_id:name:quantity:owner:committed:complete
    109 	NSMutableArray *string_array = [[NSMutableArray alloc] init];
    110 	[string_array addObject:_item.name];
    111 	[string_array addObject:[NSString stringWithFormat:@"%i", _item.quantity]];
    112 
    113 	if (_item.shared)
    114 		[string_array addObject:_item.owner];
    115 	else
    116 		[string_array addObject:@""];
    117 
    118 	[string_array addObject:[NSString stringWithFormat:@"%i", _item.committed]];
    119 	[string_array addObject:[NSString stringWithFormat:@"%i", _item.completed]];
    120 
    121 	NSMutableData *buffer = [[NSMutableData alloc] init];
    122 	//[buffer appendData:_list.num];
    123 	[buffer appendData:[[string_array componentsJoinedByString:@":"] dataUsingEncoding:NSUTF8StringEncoding]];
    124 
    125 	// the list item that was just edited will be updated when a response comes
    126 	//[network_connection send_message:7 contents:buffer];
    127 
    128 	NSLog(@"debug: %@: %@: saving", _list.name, _item.name);
    129 }
    130 
    131 @end