shlist

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

commit bea6a98935626ea51bf35956e454cd3d3d723491
parent c8d185611a80494c73be5d4acb3f122fb0e360d0
Author: Kyle Milz <kyle@getaddrinfo.net>
Date:   Sat, 21 May 2016 13:09:23 -0600

ios: start fixing broken network api

Diffstat:
Mios/ContactsTableViewController.m | 11+++--------
Mios/shlist/AppDelegate.m | 5+----
Mios/shlist/MainTableViewController.m | 13++++++++-----
Mios/shlist/Network.h | 2+-
Mios/shlist/Network.m | 16+++++++++++++---
Mios/shlist/NewListTableViewController.m | 5+----
6 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/ios/ContactsTableViewController.m b/ios/ContactsTableViewController.m @@ -125,20 +125,15 @@ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; Contact *contact = [[_cells objectAtIndex:section] objectAtIndex:row]; - NSMutableDictionary *request = [[NSMutableDictionary alloc] init]; + NSString *friend_phnum = [contact.phone_numbers objectAtIndex:0]; if ([cell accessoryType] == UITableViewCellAccessoryNone) { // Toggling the contact on, add friend - - [request setObject:[contact.phone_numbers objectAtIndex:0] forKey:@"friend_phnum"]; - [network_connection send_message:friend_add contents:request]; - + [network_connection send_message:friend_add contents:friend_phnum]; [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; } else { // Toggling contact off, delete friend - [request setObject:[contact.phone_numbers objectAtIndex:0] forKey:@"friend_phnum"]; - [network_connection send_message:friend_delete contents:request]; - + [network_connection send_message:friend_delete contents:friend_phnum]; [cell setAccessoryType:UITableViewCellAccessoryNone]; } diff --git a/ios/shlist/AppDelegate.m b/ios/shlist/AppDelegate.m @@ -40,11 +40,8 @@ } NSLog(@"apn: device token is 0x%@", hex_token); - - NSMutableDictionary *request = [[NSMutableDictionary alloc] init]; - [request setObject:hex_token forKey:@"pushtoken_hex"]; if ([network_connection get_device_id] != nil) { - [network_connection send_message:device_update contents:request]; + [network_connection send_message:device_update contents:hex_token]; } } diff --git a/ios/shlist/MainTableViewController.m b/ios/shlist/MainTableViewController.m @@ -224,9 +224,10 @@ clickedButtonAtIndex:(NSInteger)buttonIndex - (void) lists_get_finished:(NSNotification *)notification { - NSDictionary *response = notification.userInfo; + NSDictionary *response_dict = notification.userInfo; + // Already checked for existence before this function was called + NSArray *json_lists = response_dict[@"data"]; - NSArray *json_lists = [response objectForKey:@"lists"]; NSLog(@"lists_get: got %lu lists from server", (unsigned long)[json_lists count]); NSMutableArray *lists = [_lists objectAtIndex:0]; @@ -245,8 +246,10 @@ clickedButtonAtIndex:(NSInteger)buttonIndex - (void) lists_get_other_finished:(NSNotification *)notification; { - NSDictionary *response = notification.userInfo; - NSArray *other_json_lists = [response objectForKey:@"other_lists"]; + NSDictionary *response_dict = notification.userInfo; + // Already checked for existence before this function was called + NSArray *other_json_lists = response_dict[@"data"]; + NSLog(@"lists_get_other: got %lu other lists from server", (unsigned long)[other_json_lists count]); NSMutableArray *other_lists = [_lists objectAtIndex:1]; @@ -266,7 +269,7 @@ clickedButtonAtIndex:(NSInteger)buttonIndex - (void) finished_new_list_request:(NSNotification *) notification { NSDictionary *response = notification.userInfo; - NSDictionary *list = [response objectForKey:@"list"]; + NSDictionary *list = [response objectForKey:@"data"]; SharedList *shlist = [self deserialize_full_list:list]; diff --git a/ios/shlist/Network.h b/ios/shlist/Network.h @@ -14,7 +14,7 @@ - (bool) load_device_id:(NSString *)phone_number; - (NSString *) get_device_id; - (bool) connected; -- (bool) send_message:(uint16_t)msg_type contents:(NSMutableDictionary *)data; +- (bool) send_message:(uint16_t)msg_type contents:(NSObject *)data; // returns singleton instance + (id) shared_network_connection; diff --git a/ios/shlist/Network.m b/ios/shlist/Network.m @@ -55,7 +55,7 @@ CFWriteStreamRef writeStream; CFStringRef host_name = CFSTR("absentmindedproductions.ca"); - CFStreamCreatePairWithSocketToHost(NULL, host_name, 9999, &readStream, &writeStream); + CFStreamCreatePairWithSocketToHost(NULL, host_name, 5437, &readStream, &writeStream); input_stream = (__bridge NSInputStream *)readStream; output_stream = (__bridge NSOutputStream *)writeStream; @@ -118,8 +118,11 @@ return false; } -- (bool) send_message:(uint16_t)send_msg_type contents:(NSMutableDictionary *)request +- (bool) send_message:(uint16_t)send_msg_type contents:(NSObject *)data { + NSMutableDictionary *request = [[NSMutableDictionary alloc] init]; + [request setObject:data forKey:@"data"]; + if (send_msg_type != device_add) { // Append 'device_id' to all message types except device_add [request setObject:device_id forKey:@"device_id"]; @@ -272,9 +275,16 @@ return; } + // 'data' key is always sent back when "status" is "ok" + NSObject *response_data = response[@"data"]; + if (response_data == nil) { + NSLog(@"read: response did not contain 'data' key"); + return; + } + if (msg_type == device_add) { // device_add responses don't trigger any gui updates - device_id = [response objectForKey:@"device_id"]; + device_id = (NSString *)response_data; NSLog(@"device_add: writing new key '%@' to file", device_id); NSError *error = nil; diff --git a/ios/shlist/NewListTableViewController.m b/ios/shlist/NewListTableViewController.m @@ -87,10 +87,7 @@ [list setObject:shared_list.name forKey:@"name"]; [list setObject:[NSNumber numberWithInt:0] forKey:@"date"]; - NSMutableDictionary *request = [[NSMutableDictionary alloc] init]; - [request setObject:list forKey:@"list"]; - - [network_connection send_message:list_add contents:request]; + [network_connection send_message:list_add contents:list]; } @end \ No newline at end of file