Permalink
Browse files

Connect data source to real data

  • Loading branch information...
josecastillo committed Aug 8, 2015
1 parent a71994c commit 923ba79e9c3ca646730de0a3e29f3682e52855ea
@@ -27,9 +27,10 @@ - (void)setDetailItem:(id)newDetailItem {
- (void)configureView {
self.lineImageView.image = [UIImage imageForSubwayLine:self.detailItem[@"line"] withSize:100.0f];
self.lineLabel.text = self.detailItem[@"line"];
self.lineLabel.text = [self.detailItem[@"line"] stringByAppendingString:@" Train"];
self.statusLabel.text = self.detailItem[@"status"];
self.textView.text = self.detailItem[@"detail_short"];
// FIXME: Text view is not scrolled to top when detail view appears.
self.textView.text = self.detailItem[@"detail"];
}
- (void)viewDidLoad {
@@ -39,11 +39,12 @@ - (void)viewDidLoad {
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
];
self.timestampLabel = timestampLabel;
[self refresh:self.navigationItem.rightBarButtonItem];
}
- (void)viewWillAppear:(BOOL)animated {
self.clearsSelectionOnViewWillAppear = YES;
[self refresh:self.navigationItem.rightBarButtonItem];
self.lines = [[[NSUserDefaults standardUserDefaults] arrayForKey:kUserDefaultsKeyLines] mutableCopy];
[super viewWillAppear:animated];
}
@@ -54,9 +55,29 @@ - (void)didReceiveMemoryWarning {
}
- (void)refresh:(UIBarButtonItem *)sender {
self.systemStatus = [[TFLiveDataSource defaultSource] status];
self.timestampLabel.text = [NSString stringWithFormat:@"Last updated: %@", self.systemStatus[kLiveDataSourceKeyTimestamp]];
[self.tableView reloadData];
[[TFLiveDataSource defaultSource] refresh:^(NSError *error) {
if (error) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error Refreshing Status"
message:[error localizedDescription]
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleCancel
handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"Try Again"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
[self refresh:self.navigationItem.rightBarButtonItem];
}]];
[self presentViewController:alert animated:YES completion:nil];
}
self.systemStatus = [[TFLiveDataSource defaultSource] status];
if (self.systemStatus) {
self.timestampLabel.text = [NSString stringWithFormat:@"Last updated: %@", self.systemStatus[kLiveDataSourceKeyTimestamp]];
} else {
self.timestampLabel.text = @"No data! Try refreshing.";
}
[self.tableView reloadData];
}];
}
#pragma mark - Segues
@@ -87,7 +108,7 @@ - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sou
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
return self.systemStatus ? 1 : 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
@@ -8,10 +8,13 @@
#import <Foundation/Foundation.h>
typedef void (^TFLiveDataSourceCompletionHandler)(NSError *error);
@interface TFLiveDataSource : NSObject
+ (instancetype)defaultSource;
- (void)refresh:(TFLiveDataSourceCompletionHandler)completionHandler;
- (NSDictionary *)status;
@end
@@ -7,6 +7,11 @@
//
#import "TFLiveDataSource.h"
#import "Constants.h"
@interface TFLiveDataSource ()
@property (nonatomic, strong) NSDictionary *systemStatus;
@end
@implementation TFLiveDataSource
@@ -19,34 +24,43 @@ + (instancetype)defaultSource {
return instance;
}
- (void)refresh:(TFLiveDataSourceCompletionHandler)completionHandler {
NSURLSession *session = [NSURLSession sharedSession];
NSURL *serviceUrl = [NSURL URLWithString:@"https://www.joeycastillo.com/trainbrain/current.json"];
[[session dataTaskWithRequest:[NSURLRequest requestWithURL:serviceUrl]
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
dispatch_sync(dispatch_get_main_queue(), ^{
completionHandler(error);
});
return;
}
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!result) {
dispatch_sync(dispatch_get_main_queue(), ^{
completionHandler(error);
});
return;
}
// The web service returns an array of line status object.
// For the app, we need the lines stored as key value pairs.
NSMutableDictionary *systemStatus = [NSMutableDictionary dictionary];
NSMutableDictionary *lines = [NSMutableDictionary dictionary];
for (NSDictionary *line in result[kLiveDataSourceKeyLines]) {
NSString *lineName = line[kLiveDataSourceKeyLine];
lines[lineName] = line;
}
systemStatus[kLiveDataSourceKeyTimestamp] = result[kLiveDataSourceKeyTimestamp];
systemStatus[kLiveDataSourceKeyLines] = [NSDictionary dictionaryWithDictionary:lines];
self.systemStatus = systemStatus;
dispatch_sync(dispatch_get_main_queue(), ^{
completionHandler(nil);
});
}] resume];
}
- (NSDictionary *)status {
return @{
@"timestamp": @"8/8/2015 12:52:00 PM",
@"lines":
@{
@"1" : @{@"line" : @"1", @"status": @"GOOD SERVICE", @"alert" : @2},
@"2" : @{@"line" : @"2", @"status": @"SERVICE CHANGE", @"alert" : @4, @"detail_short" : @"Due to NYPD activity at Simpson St, southbound [2] and [5] trains are running express E 180 St to 149 St-Grand Concourse."},
@"3" : @{@"line" : @"3", @"status": @"GOOD SERVICE", @"alert" : @2},
@"4" : @{@"line" : @"4", @"status": @"GOOD SERVICE", @"alert" : @1},
@"5" : @{@"line" : @"5", @"status": @"GOOD SERVICE", @"alert" : @1},
@"6" : @{@"line" : @"6", @"status": @"GOOD SERVICE", @"alert" : @1},
@"7" : @{@"line" : @"7", @"status": @"PLANNED WORK", @"alert" : @3, @"detail_short" : @"[7] No trains between Queensboro Plaza and Times Sq-42 St[E] [F] [N] [S] trains and [SB] free shuttle buses provide alternate service"},
@"A" : @{@"line" : @"A", @"status": @"GOOD SERVICE", @"alert" : @1},
@"C" : @{@"line" : @"C", @"status": @"GOOD SERVICE", @"alert" : @1},
@"E" : @{@"line" : @"E", @"status": @"GOOD SERVICE", @"alert" : @1},
@"B" : @{@"line" : @"B", @"status": @"GOOD SERVICE", @"alert" : @1},
@"D" : @{@"line" : @"D", @"status": @"GOOD SERVICE", @"alert" : @1},
@"F" : @{@"line" : @"F", @"status": @"GOOD SERVICE", @"alert" : @1},
@"M" : @{@"line" : @"M", @"status": @"GOOD SERVICE", @"alert" : @1},
@"G" : @{@"line" : @"G", @"status": @"GOOD SERVICE", @"alert" : @1},
@"J" : @{@"line" : @"J", @"status": @"GOOD SERVICE", @"alert" : @1},
@"Z" : @{@"line" : @"Z", @"status": @"GOOD SERVICE", @"alert" : @1},
@"L" : @{@"line" : @"L", @"status": @"SUSPENDED", @"alert" : @5, @"detail_short" : @"Due to FDNY activity at Broadway Junction, [L] train service is suspended in both directions."},
@"N" : @{@"line" : @"N", @"status": @"GOOD SERVICE", @"alert" : @1},
@"Q" : @{@"line" : @"Q", @"status": @"GOOD SERVICE", @"alert" : @1},
@"R" : @{@"line" : @"R", @"status": @"GOOD SERVICE", @"alert" : @1},
}
};
return self.systemStatus;
}
@end

0 comments on commit 923ba79

Please sign in to comment.