commit c8a158d096c7a9b1ca16593735d8c45846a2f336
parent 4e8325b1da447ce8fa2c6cfed68b96d989097440
Author: Kyle Milz <kyle@green.krwm.net>
Date: Sun, 14 Feb 2016 14:18:50 -0700
ios: switch from using callbacks to NSNotificationCenter
Diffstat:
10 files changed, 279 insertions(+), 181 deletions(-)
diff --git a/ios/shlist/AppDelegate.m b/ios/shlist/AppDelegate.m
@@ -31,7 +31,7 @@
NSMutableString *hex_token = [NSMutableString stringWithCapacity:(token_length * 2)];
for (int i = 0; i < token_length; i++) {
- [hex_token appendFormat:@"%02X", (NSUInteger)token_data[i]];
+ [hex_token appendFormat:@"%02lX", (unsigned long)token_data[i]];
}
NSLog(@"apn: device token is 0x%@", hex_token);
@@ -48,7 +48,7 @@
for (id key in userInfo) {
NSLog(@"notify: '%@' => '%@'", key, userInfo[key]);
}
- [[NSNotificationCenter defaultCenter] postNotificationName:@"PushNotificationMessageReceivedNotification" object:nil userInfo:userInfo];
+ [[NSNotificationCenter defaultCenter] postNotificationName:@"MessageReceivedNotification" object:nil userInfo:userInfo];
}
- (void) applicationWillResignActive:(UIApplication *)application
diff --git a/ios/shlist/Base.lproj/Main.storyboard b/ios/shlist/Base.lproj/Main.storyboard
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7706" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="KKJ-Fr-5sX">
+<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7706" systemVersion="14F1605" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="KKJ-Fr-5sX">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
@@ -259,7 +259,7 @@
</objects>
<point key="canvasLocation" x="3828" y="-638"/>
</scene>
- <!--Shlist-->
+ <!--Shared Lists-->
<scene sceneID="hc1-Lv-WtP">
<objects>
<tableViewController id="x0J-ua-E8Q" customClass="MainTableViewController" sceneMemberID="viewController">
@@ -321,7 +321,7 @@
</connections>
</tableView>
<toolbarItems/>
- <navigationItem key="navigationItem" title="Shlist" id="uhM-V6-aB7">
+ <navigationItem key="navigationItem" title="Shared Lists" id="uhM-V6-aB7">
<barButtonItem key="leftBarButtonItem" systemItem="edit" id="XHh-J3-UVD"/>
<barButtonItem key="rightBarButtonItem" systemItem="add" id="hNc-gt-QbU">
<connections>
@@ -335,7 +335,7 @@
</objects>
<point key="canvasLocation" x="432" y="202"/>
</scene>
- <!--Add Shared List-->
+ <!--New List-->
<scene sceneID="tne-QT-ifu">
<objects>
<tableViewController id="BYZ-38-t0r" customClass="NewListTableViewController" sceneMemberID="viewController">
@@ -442,7 +442,7 @@
</tableViewSection>
</sections>
</tableView>
- <navigationItem key="navigationItem" title="Add Shared List" id="5ys-ck-wLj">
+ <navigationItem key="navigationItem" title="New List" id="5ys-ck-wLj">
<barButtonItem key="leftBarButtonItem" systemItem="cancel" id="WaR-Ud-Hmo">
<connections>
<segue destination="C3w-Ab-gX6" kind="unwind" unwindAction="unwindToList:" id="7wZ-HT-2Br"/>
@@ -485,6 +485,9 @@
<rect key="frame" x="16" y="13" width="576" height="17"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
+ <connections>
+ <action selector="name_editing_ended:" destination="uyw-yd-pcJ" eventType="editingDidEnd" id="7XN-3a-wjb"/>
+ </connections>
</textField>
</subviews>
<constraints>
@@ -505,11 +508,9 @@
<navigationItem key="navigationItem" title="Edit Name" id="fd1-sm-vtP"/>
<connections>
<outlet property="list_name" destination="zf2-sM-1jM" id="w6j-kT-Mxt"/>
- <segue destination="w7g-ds-BFe" kind="unwind" unwindAction="unwindToAddList:" id="Gsp-mJ-yJ9"/>
</connections>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="K6g-mo-23k" userLabel="First Responder" sceneMemberID="firstResponder"/>
- <exit id="w7g-ds-BFe" userLabel="Exit" sceneMemberID="exit"/>
</objects>
<point key="canvasLocation" x="3024" y="202"/>
</scene>
@@ -649,8 +650,8 @@
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
- <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Good" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="15d-vN-62f">
- <rect key="frame" x="541" y="11" width="43" height="21"/>
+ <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="15d-vN-62f">
+ <rect key="frame" x="584" y="22" width="0.0" height="0.0"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
@@ -669,7 +670,7 @@
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="pLe-rJ-ITY" id="RsW-FA-jWb">
<autoresizingMask key="autoresizingMask"/>
<subviews>
- <label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Device Id" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="0kV-we-bvq">
+ <label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Fingerprint" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="0kV-we-bvq">
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
diff --git a/ios/shlist/EditTableViewController.m b/ios/shlist/EditTableViewController.m
@@ -13,32 +13,43 @@
}
-- (void)didReceiveMemoryWarning {
+- (void)didReceiveMemoryWarning
+{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
-- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
+- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
+{
// Return the number of sections.
return 1;
}
+- (IBAction)name_editing_ended:(id)sender
+{
+
+ NSIndexPath *name_path = [NSIndexPath indexPathForRow:0 inSection:0];
+ UITableViewCell *name_cell = [self.tableView cellForRowAtIndexPath:name_path];
+
+ UITextField *name = (UITextField *)[name_cell viewWithTag:1];
+
+ NSLog(@"name was '%@'", name.text);
+}
+
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 1;
}
-/*
-- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
-
- // Configure the cell...
-
- return cell;
+- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
+{
+ // Configure the cell...
+ UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
+
+ return cell;
}
-*/
/*
// Override to support conditional editing of the table view.
diff --git a/ios/shlist/MainTableViewController.h b/ios/shlist/MainTableViewController.h
@@ -6,12 +6,6 @@
- (void) update_address_book;
-- (void) lists_get_finished:(NSArray *)lists;
-- (void) lists_get_other_finished:(NSArray *)other_lists;
-- (void) finished_new_list_request:(SharedList *) shlist;
-- (void) finished_join_list_request:(NSDictionary *) shlist;
-- (void) finished_leave_list_request:(NSDictionary *) shlist;
-
- (IBAction)unwindToList:(UIStoryboardSegue *)segue;
@end
\ No newline at end of file
diff --git a/ios/shlist/MainTableViewController.m b/ios/shlist/MainTableViewController.m
@@ -24,16 +24,55 @@
@implementation MainTableViewController
+- (void) dealloc
+{
+ // If you don't remove yourself as an observer, the Notification Center
+ // will continue to try and send notification objects to the deallocated
+ // object.
+ [[NSNotificationCenter defaultCenter] removeObserver:self];
+}
+
- (void) viewDidLoad
{
[super viewDidLoad];
+ // Listen for push notifications
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(receiveNotification:)
+ name:@"MessageReceivedNotification"
+ object:nil];
+
+ // Hook up generic message handlers
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(lists_get_finished:)
+ name:[NSString stringWithFormat:@"NetworkResponseForMsgType%i", lists_get]
+ object:nil];
+
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(lists_get_other_finished:)
+ name:[NSString stringWithFormat:@"NetworkResponseForMsgType%i", lists_get_other]
+ object:nil];
+
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(finished_new_list_request:)
+ name:[NSString stringWithFormat:@"NetworkResponseForMsgType%i", list_add]
+ object:nil];
+
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(finished_join_list_request:)
+ name:[NSString stringWithFormat:@"NetworkResponseForMsgType%i", list_join]
+ object:nil];
+
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(finished_leave_list_request:)
+ name:[NSString stringWithFormat:@"NetworkResponseForMsgType%i", list_leave]
+ object:nil];
+
// display an Edit button in the navigation bar for this view controller
self.navigationItem.leftBarButtonItem = self.editButtonItem;
// there's a race here when assigning self
network_connection = [Network shared_network_connection];
- network_connection->shlist_tvc = self;
_lists = [[NSMutableArray alloc] init];
[_lists addObject:[[NSMutableArray alloc] init]];
@@ -68,17 +107,50 @@
_address_book.main_tvc = self;
}
+- (void) receiveNotification:(NSNotification *) notification
+{
+ NSDictionary *userinfo = notification.userInfo;
+
+ // [notification name] should always be @"TestNotification"
+ // unless you use this method for observation of other notifications
+ // as well.
+
+ if ([[notification name] isEqualToString:@"MessageReceivedNotification"])
+ NSLog (@"Successfully received the test notification!");
+
+ NSMutableArray *other_lists = [_lists objectAtIndex:1];
+
+ SharedList *tmp = [[SharedList alloc] init];
+ // tmp.num = list[@"num"];
+ tmp.num = [NSNumber numberWithInt:99];
+
+ // NSData *name_data = [list[@"name"] dataUsingEncoding:NSISOLatin1StringEncoding];
+ // tmp.name = [[NSString alloc] initWithData:name_data encoding:NSUTF8StringEncoding];
+ tmp.name = @"Some new list from the outthere";
+
+ // tmp.members_phone_nums = list[@"members"];
+ [other_lists addObject:tmp];
+
+ NSLog(@"notify: adding other list '%@', num '%@'", tmp.name, tmp.num);
+
+ NSIndexPath *new_path = [NSIndexPath indexPathForRow:[other_lists count] - 1 inSection:1];
+ [self.tableView insertRowsAtIndexPaths:@[new_path] withRowAnimation:UITableViewRowAnimationAutomatic];
+
+ [self update_section_headers];
+}
+
- (bool) load_phone_number
{
if ([[NSFileManager defaultManager] fileExistsAtPath:phone_num_file]) {
// file exists, read what it has
// XXX: validate length of file too
- phone_number = [NSString stringWithContentsOfFile:phone_num_file];
+ NSError *error = nil;
+ phone_number = [NSString stringWithContentsOfFile:phone_num_file encoding:NSASCIIStringEncoding error:&error];
return true;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Important"
- message:@"Your phone number is needed for us to calculate your mutual contacts. Expect severe functionality loss."
+ message:@"We need this phone's number to find your mutual friends."
delegate:self cancelButtonTitle:@"Nope" otherButtonTitles:@"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
@@ -170,8 +242,13 @@ clickedButtonAtIndex:(NSInteger)buttonIndex
{
}
-- (void) lists_get_finished:(NSArray *)json_lists;
+- (void) lists_get_finished:(NSNotification *)notification;
{
+ NSDictionary *response = notification.userInfo;
+
+ NSArray *json_lists = [response objectForKey:@"lists"];
+ NSLog(@"lists_get: got %lu lists from server", (unsigned long)[json_lists count]);
+
NSMutableArray *lists = [_lists objectAtIndex:0];
[lists removeAllObjects];
@@ -179,11 +256,16 @@ clickedButtonAtIndex:(NSInteger)buttonIndex
SharedList *tmp = [[SharedList alloc] init];
tmp.num = list[@"num"];
+ // Convert incoming encoded UTF-8 into real UTF-8
NSData *name_data = [list[@"name"] dataUsingEncoding:NSISOLatin1StringEncoding];
tmp.name = [[NSString alloc] initWithData:name_data encoding:NSUTF8StringEncoding];
NSNumber *date = list[@"date"];
- tmp.date = [NSDate dateWithTimeIntervalSince1970:[date floatValue]];
+ if ([date intValue] != 0)
+ tmp.date = [NSDate dateWithTimeIntervalSince1970:[date floatValue]];
+ else
+ tmp.date = nil;
+
tmp.members_phone_nums = list[@"members"];
tmp.items_ready = list[@"items_complete"];
tmp.items_total = list[@"items_total"];
@@ -192,11 +274,16 @@ clickedButtonAtIndex:(NSInteger)buttonIndex
NSLog(@"adding list '%@', num '%@'", tmp.name, tmp.num);
}
- [self.tableView reloadData];
+ NSIndexSet *section = [NSIndexSet indexSetWithIndex:0];
+ [self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationNone];
}
-- (void) lists_get_other_finished:(NSArray *)other_json_lists;
+- (void) lists_get_other_finished:(NSNotification *)notification;
{
+ NSDictionary *response = notification.userInfo;
+ NSArray *other_json_lists = [response objectForKey:@"other_lists"];
+ NSLog(@"lists_get_other: got %i other lists from server", [other_json_lists count]);
+
NSMutableArray *other_lists = [_lists objectAtIndex:1];
[other_lists removeAllObjects];
@@ -213,18 +300,33 @@ clickedButtonAtIndex:(NSInteger)buttonIndex
NSLog(@"adding other list '%@', num '%@'", tmp.name, tmp.num);
}
- [self.tableView reloadData];
+ NSIndexSet *section = [NSIndexSet indexSetWithIndex:1];
+ [self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationNone];
}
-- (void) finished_new_list_request:(SharedList *) shlist
+- (void) finished_new_list_request:(NSNotification *) notification
{
- [[_lists objectAtIndex:0] addObject:shlist];
- // [self.shared_lists addObject:shlist];
+ NSDictionary *response = notification.userInfo;
+ NSDictionary *list = [response objectForKey:@"list"];
+
+ SharedList *shlist = [[SharedList alloc] init];
+ shlist.num = [list objectForKey:@"num"];
+ shlist.name = [list objectForKey:@"name"];
+
+ NSMutableArray *members = [list objectForKey:@"members"];
+ shlist.members_phone_nums = members;
+ shlist.items_ready = [list objectForKey:@"items_complete"];
+ shlist.items_total = [list objectForKey:@"items_total"];
+
+ NSMutableArray *lists = [_lists objectAtIndex:0];
+ [lists addObject:shlist];
// response looks good, insert the new list
- int section_0_rows = [[_lists objectAtIndex:0] count];
- NSIndexPath *index_path = [NSIndexPath indexPathForRow:section_0_rows - 1 inSection:0];
+ NSUInteger new_row_pos = [lists count] - 1;
+ NSIndexPath *index_path = [NSIndexPath indexPathForRow:new_row_pos inSection:0];
[self.tableView insertRowsAtIndexPaths:@[index_path] withRowAnimation:UITableViewRowAnimationFade];
+
+ [self update_section_headers];
}
// major thing here is join list requests
@@ -247,8 +349,12 @@ clickedButtonAtIndex:(NSInteger)buttonIndex
[network_connection send_message:list_join contents:request];
}
-- (void) finished_join_list_request:(NSDictionary *) shlist
+- (void) finished_join_list_request:(NSNotification *) notification
{
+ NSDictionary *response = notification.userInfo;
+ NSDictionary *shlist = response[@"list"];
+ NSLog(@"network: joined list %@", shlist[@"num"]);
+
NSMutableArray *lists = [_lists objectAtIndex:0];
NSMutableArray *other_lists = [_lists objectAtIndex:1];
@@ -274,14 +380,24 @@ clickedButtonAtIndex:(NSInteger)buttonIndex
// Compute new position and start moving row as soon as possible
// XXX: sorting
- int new_row_pos = [lists count] - 1;
+ NSUInteger new_row_pos = [lists count] - 1;
NSIndexPath *new_index_path = [NSIndexPath indexPathForRow:new_row_pos inSection:0];
[self.tableView moveRowAtIndexPath:orig_index_path toIndexPath:new_index_path];
+ [self update_section_headers];
+
// Put any new values into data structs
NSData *name_data = [shlist[@"name"] dataUsingEncoding:NSISOLatin1StringEncoding];
needle.name = [[NSString alloc] initWithData:name_data encoding:NSUTF8StringEncoding];
- // needle.date =
+
+ NSNumber *date = shlist[@"date"];
+ if ([date intValue] != 0) {
+ needle.date = [NSDate dateWithTimeIntervalSince1970:[date floatValue]];
+ }
+ else {
+ needle.date = nil;
+ }
+
needle.items_ready = shlist[@"items_complete"];
needle.items_total = shlist[@"items_total"];
needle.num_members = shlist[@"num_members"];
@@ -296,13 +412,56 @@ clickedButtonAtIndex:(NSInteger)buttonIndex
UILabel *fraction = (UILabel *)[needle.cell viewWithTag:4];
fraction.text = [self fraction:needle.items_ready denominator:needle.items_total];
fraction.hidden = NO;
+
+ // Show date label if date has been set to something
+ if (needle.date != nil) {
+ UILabel *deadline_label = (UILabel *)[needle.cell viewWithTag:3];
+ deadline_label.hidden = NO;
+ }
}
-- (void) finished_leave_list_request:(NSDictionary *) response
+
+// section header titles
+- (NSString *)tableView:(UITableView *)tableView
+titleForHeaderInSection:(NSInteger)section
+{
+ if (section > 1)
+ // should not happen
+ return @"";
+
+ unsigned long total = [[_lists objectAtIndex:section] count];
+ if (section == 0)
+ return [NSString stringWithFormat:@"Your Lists (%lu)", total];
+ else if (section == 1)
+ return [NSString stringWithFormat:@"Other Lists (%lu)", total];
+ return @"";
+}
+
+- (void) update_section_headers
{
NSMutableArray *lists = [_lists objectAtIndex:0];
NSMutableArray *other_lists = [_lists objectAtIndex:1];
+ UITableViewHeaderFooterView *sectionZeroHeader = [self.tableView headerViewForSection:0];
+ UITableViewHeaderFooterView *sectionOneHeader = [self.tableView headerViewForSection:1];
+ NSString *sectionZeroLabel = [NSString stringWithFormat:@"Your Lists (%lu)", (unsigned long)[lists count]];
+ NSString *sectionOneLabel = [NSString stringWithFormat:@"Other Lists (%lu)", (unsigned long)[other_lists count]];
+
+ [sectionZeroHeader.textLabel setText:sectionZeroLabel];
+ [sectionZeroHeader setNeedsLayout];
+ [sectionOneHeader.textLabel setText:sectionOneLabel];
+ [sectionOneHeader setNeedsLayout];
+}
+
+- (void) finished_leave_list_request:(NSNotification *) notification
+{
+ NSDictionary *response = notification.userInfo;
+ NSNumber *list_num = response[@"list_num"];
+ NSLog(@"network: left list %@", list_num);
+
+ NSMutableArray *lists = [_lists objectAtIndex:0];
+ NSMutableArray *other_lists = [_lists objectAtIndex:1];
+
SharedList *list = nil;
for (SharedList *temp in lists) {
if (temp.num == response[@"list_num"]) {
@@ -321,6 +480,7 @@ clickedButtonAtIndex:(NSInteger)buttonIndex
NSIndexPath *old_path = [self.tableView indexPathForCell:list.cell];
[self.tableView deleteRowsAtIndexPaths:@[old_path] withRowAnimation:UITableViewRowAnimationAutomatic];
+ [self update_section_headers];
return;
}
@@ -333,11 +493,28 @@ clickedButtonAtIndex:(NSInteger)buttonIndex
NSIndexPath *new_path = [NSIndexPath indexPathForRow:0 inSection:1];
[self.tableView moveRowAtIndexPath:old_path toIndexPath:new_path];
- // Remove > accessory and hide the completion fraction
+ // Make sure section headers are accurate
+ [self update_section_headers];
+
+ // Remove yourself from the members array
+ NSMutableArray *members = [list.members_phone_nums mutableCopy];
+ NSUInteger index = [members indexOfObject:@"4037082094"];
+ if (index != NSNotFound) {
+ [members removeObjectAtIndex:index];
+ }
+ [self process_members_array:members cell:list.cell];
+
+ // Remove > accessory
list.cell.accessoryType = UITableViewCellAccessoryNone;
+
+ // Hide completion fraction
UILabel *fraction = (UILabel *)[list.cell viewWithTag:4];
fraction.hidden = YES;
+ // Hide date
+ UILabel *deadline_label = (UILabel *)[list.cell viewWithTag:3];
+ deadline_label.hidden = YES;
+
// XXX: update members array to disclude yourself (maybe send it back in response?)
// XXX: Maybe clear out list data that's no longer needed
// XXX: give some visual feedback here what's happening
@@ -358,20 +535,24 @@ clickedButtonAtIndex:(NSInteger)buttonIndex
UILabel *fraction_label = (UILabel *)[cell viewWithTag:4];
if ([indexPath section] == 0) {
- // "lists you're in" section
-
- // XXX: needs to be stored on/sent from the server
- deadline_label.text = @"in 3 days";
+ // your lists section
+
+ if (shared_list.date == nil) {
+ deadline_label.hidden = YES;
+ } else {
+ // XXX: calculate how long until deadline
+ // NSDate *date = shared_list.date;
+ deadline_label.text = @"date";
+ }
- // float frac = shared_list.items_ready / shared_list.items_total;
- float frac = 0.0f;
+ float frac = [shared_list.items_ready floatValue] / [shared_list.items_total floatValue];
if (frac > 0.80f)
fraction_label.textColor = [UIColor greenColor];
-
fraction_label.hidden = NO;
fraction_label.text = [self fraction:shared_list.items_ready
denominator:shared_list.items_total];
+ // Add ">" accessory to indicate you can "enter" this list
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
@@ -452,21 +633,6 @@ clickedButtonAtIndex:(NSInteger)buttonIndex
members_label.text = members_str;
}
-// section header titles
-- (NSString *)tableView:(UITableView *)tableView
- titleForHeaderInSection:(NSInteger)section
-{
- if (section > 1)
- // should not happen
- return @"";
-
- NSUInteger total = [[_lists objectAtIndex:section] count];
- if (section == 0)
- return [NSString stringWithFormat:@"Lists you're in (%lu)", (unsigned long)total];
- else if (section == 1)
- return [NSString stringWithFormat:@"Other lists (%lu)", (unsigned long)total];
- return @"";
-}
// only section 0 lists can be edited
- (BOOL) tableView:(UITableView *)tableView
diff --git a/ios/shlist/Network.h b/ios/shlist/Network.h
@@ -11,7 +11,6 @@
int *bytesRead;
@public
- MainTableViewController *shlist_tvc;
ListTableViewController *shlist_ldvc;
SettingsTableViewController *settings_tvc;
}
@@ -21,11 +20,11 @@
// only networking really cares about the device id
- (bool) load_device_id:(NSString *)phone_number;
-- (NSData *) get_device_id;
+- (NSString *) get_device_id;
+- (bool) connected;
- (bool) send_message:(uint16_t)msg_type contents:(NSMutableDictionary *)data;
-
// returns singleton instance
+ (id) shared_network_connection;
diff --git a/ios/shlist/Network.m b/ios/shlist/Network.m
@@ -39,6 +39,11 @@
return self;
}
+- (bool) connected
+{
+ return connected;
+}
+
- (NSString *) get_device_id
{
return device_id;
@@ -48,6 +53,7 @@
{
NSLog(@"network: connect()");
connected = 1;
+ [settings_tvc update_network_text:@"Connected"];
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
@@ -76,6 +82,7 @@
{
NSLog(@"network: disconnect()");
connected = 0;
+ [settings_tvc update_network_text:@"Disconnected"];
[inputShlistStream close];
[outputShlistStream close];
@@ -296,102 +303,22 @@
return;
}
+ // device_add responses don't trigger any gui updates
if (msg_type == device_add) {
- [self device_add:response];
- } else if (msg_type == list_add) {
- [self list_add:response];
- } else if (msg_type == lists_get) {
- [self lists_get:response];
- } else if (msg_type == list_join) {
- [self list_join:response];
- } else if (msg_type == list_leave) {
- [self list_leave:response];
- } else if (msg_type == lists_get_other) {
- [self lists_get_other:response];
- }
-}
-
-- (void) device_add:(NSDictionary *)response
-{
- device_id = [response objectForKey:@"device_id"];
-
- NSLog(@"device_add: writing new key '%@' to file", device_id);
- NSError *error = nil;
- [device_id writeToFile:device_id_file atomically:YES encoding:NSUTF8StringEncoding error:&error];
-
- if (error != nil)
- NSLog(@"%@", [error userInfo]);
-}
-
-- (void) list_add:(NSDictionary *)response
-{
- NSDictionary *list = [response objectForKey:@"list"];
-
- SharedList *shlist = [[SharedList alloc] init];
- shlist.num = [list objectForKey:@"num"];
- shlist.name = [list objectForKey:@"name"];
-
- NSArray *members = [list objectForKey:@"members"];
- shlist.members_phone_nums = members;
- shlist.items_ready = [list objectForKey:@"items_complete"];
- shlist.items_total = [list objectForKey:@"items_total"];
-
- if ([self check_tvc:shlist_tvc])
- [shlist_tvc finished_new_list_request:shlist];
+ device_id = [response objectForKey:@"device_id"];
- NSLog(@"list_add: successfully added new list '%@'", shlist.name);
-}
-
-- (void) lists_get:(NSDictionary *)response
-{
- NSArray *lists = [response objectForKey:@"lists"];
- NSLog(@"lists_get: got %i lists from server", [lists count]);
-
- // Don't attempt to update a view controller that isn't there yet
- if (![self check_tvc:shlist_tvc])
- return;
-
- if (shlist_tvc)
- [shlist_tvc lists_get_finished:lists];
-}
-
-- (void) lists_get_other:(NSDictionary *)response
-{
- NSArray *other_lists = [response objectForKey:@"other_lists"];
- NSLog(@"lists_get_other: got %i other lists from server", [other_lists count]);
+ NSLog(@"device_add: writing new key '%@' to file", device_id);
+ NSError *error = nil;
+ [device_id writeToFile:device_id_file atomically:YES encoding:NSUTF8StringEncoding error:&error];
- // Don't attempt to update a view controller that isn't there yet
- if (![self check_tvc:shlist_tvc])
+ if (error != nil)
+ NSLog(@"%@", [error userInfo]);
return;
+ }
- if (shlist_tvc)
- [shlist_tvc lists_get_other_finished:other_lists];
-}
-
-- (void) list_join:(NSDictionary *)response
-{
- NSDictionary *list = response[@"list"];
- NSLog(@"network: joined list %@", list[@"num"]);
-
- if ([self check_tvc:shlist_tvc])
- [shlist_tvc finished_join_list_request:list];
-}
-
-- (void) list_leave:(NSDictionary *)response
-{
- NSNumber *list_num = response[@"list_num"];
- NSLog(@"network: left list %@", list_num);
-
- if ([self check_tvc:shlist_tvc])
- [shlist_tvc finished_leave_list_request:response];
-}
-
-- (bool) check_tvc:(MainTableViewController *) tvc
-{
- if (tvc)
- return true;
- NSLog(@"network: trying to update main_tvc before it's ready, ignoring!");
- return false;
+ // Send a generic notification, these have to be hooked up to work
+ NSString *notification_name = [NSString stringWithFormat:@"NetworkResponseForMsgType%i", msg_type];
+ [[NSNotificationCenter defaultCenter] postNotificationName:notification_name object:nil userInfo:response];
}
- (void) dealloc
diff --git a/ios/shlist/NewListTableViewController.m b/ios/shlist/NewListTableViewController.m
@@ -10,7 +10,6 @@
@property (weak, nonatomic) IBOutlet UISwitch *deadline_switch;
@property (weak, nonatomic) IBOutlet UILabel *list_name;
-// @property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
@end
@@ -37,7 +36,10 @@
[super viewDidLoad];
// Do any additional setup after loading the view.
- _list_name.text = @"New List";
+ //_edit_text_field = [[UITextField alloc] init];
+ //_edit_text_field.text = @"New Shlist";
+
+ _list_name.text = @"New Shlist";
network_connection = [Network shared_network_connection];
}
@@ -58,16 +60,17 @@
// preparation before navigation
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
- if ([[segue identifier] isEqualToString:@"edit name segue"]) {
+ if ([[segue identifier] isEqualToString:@"add shared list name edit"]) {
// segue forwards to name editor
NSLog(@"debug: %@: editing name", _list_name.text);
- // EditTableViewController *edit = [segue destinationViewController];
- // edit.list_name.text = @"New List";
+ EditTableViewController *edit = [segue destinationViewController];
+ edit.list_name = (UITextField *)_list_name;
+
return;
}
- // jump backwards to previous view controller
+ // User hit cancel, throw away any changes
if (sender != self.saveButton)
return;
@@ -76,7 +79,6 @@
// saving, copy form fields into shared list object
shared_list.name = _list_name.text;
shared_list.deadline = _deadline_switch.isOn;
- // _shared_list.filters = ???
NSLog(@"new_list: sending list_add request...");
diff --git a/ios/shlist/SettingsTableViewController.h b/ios/shlist/SettingsTableViewController.h
@@ -2,6 +2,6 @@
@interface SettingsTableViewController : UITableViewController
-- (void) finish_ok_request;
+- (void) update_network_text:(NSString *)new_text;
@end
diff --git a/ios/shlist/SettingsTableViewController.m b/ios/shlist/SettingsTableViewController.m
@@ -19,16 +19,19 @@
[super viewDidLoad];
netconn = [Network shared_network_connection];
- NSString *device_id = [[NSString alloc] initWithData:[netconn get_device_id] encoding:NSASCIIStringEncoding];
+ NSString *device_id = [netconn get_device_id];
_device_id_label.text = [device_id substringToIndex:8];
+
+ if ([netconn connected])
+ _network_label.text = @"Connected";
+ else
+ _network_label.text = @"Disconnected";
+ netconn->settings_tvc = self;
}
-- (void) viewWillAppear:(BOOL)animated
+- (void) update_network_text:(NSString *)new_text
{
- // check every time this view is selected
- _network_label.text = @"Checking...";
- netconn->settings_tvc = self;
- [netconn send_message:8 contents:nil];
+ _network_label.text = new_text;
}
- (void)didReceiveMemoryWarning
@@ -37,11 +40,6 @@
// Dispose of any resources that can be recreated.
}
-- (void) finish_ok_request
-{
- _network_label.text = @"All good";
-}
-
/*
#pragma mark - Navigation