master
  1#import "CreationsTableViewController.h"
  2#import "Cake.h"
  3
  4@interface CreationsTableViewController ()
  5@property (strong, nonatomic) NSMutableArray *data;
  6@end
  7
  8@implementation CreationsTableViewController
  9
 10- (id)initWithStyle:(UITableViewStyle)style
 11{
 12    self = [super initWithStyle:style];
 13    if (self) {
 14    }
 15    return self;
 16}
 17
 18- (void)viewDidLoad
 19{
 20  [super viewDidLoad];
 21  [self.navigationItem setHidesBackButton:YES];
 22  self.title = @"CakeSide";
 23
 24  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatedDataNotification) name:NOTIFICATION_CAKES_UPDATED object:nil];
 25  // start loading data
 26  [self updateData];
 27}
 28
 29- (void)didReceiveMemoryWarning
 30{
 31    [super didReceiveMemoryWarning];
 32}
 33
 34- (void)updatedDataNotification
 35{
 36  [self.tableView reloadData];
 37}
 38
 39- (void)updateData
 40{
 41  self.data = nil;
 42  [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
 43
 44  NSError *error;
 45  NSString *token = [SSKeychain passwordForService:KEYCHAIN_API_TOKEN account:KEYCHAIN_ACCOUNT error:&error];
 46
 47  // load data
 48  AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:HOST]];
 49  [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
 50  [httpClient setDefaultHeader:@"Accept" value:@"application/json"];
 51  [httpClient setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Token token=%@", token]];
 52  [httpClient setParameterEncoding:AFJSONParameterEncoding];
 53
 54  NSMutableURLRequest *request;
 55  request = [httpClient requestWithMethod:@"GET" path:URL_CAKES parameters:nil];
 56
 57  NSLog(@"GET: %@", request);
 58
 59  AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
 60   [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
 61   if (!self.data)
 62   {
 63     self.data = [[NSMutableArray alloc] init];
 64   }
 65   for (NSDictionary *data in JSON)
 66   {
 67     Cake *cake = [Cake initFromJSON:data];
 68     [self.data addObject:cake];
 69   }
 70   [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_CAKES_UPDATED object:nil];
 71  }
 72  failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
 73   NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
 74   [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
 75   [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_CAKES_UPDATED object:nil];
 76
 77   BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Oops..." message:@"Something is broken in the kitchen. Please try again later."];
 78   [alert setCancelButtonWithTitle:@"Got it" block:nil];
 79   [alert show];
 80  }];
 81  [operation start];
 82}
 83
 84#pragma mark - Table view data source
 85
 86- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 87{
 88  return 1;
 89}
 90
 91- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 92{
 93  if (!self.data || self.data.count == 0)
 94  {
 95    return 1;
 96  }
 97  else
 98  {
 99    return self.data.count;
100  }
101}
102
103- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
104{
105  if(!self.data || self.data.count == 0)
106  {
107    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoadingCell"];
108    if (cell == nil)
109    {
110      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LoadingCell"];
111    }
112    cell.textLabel.text = @"Loading...";
113    return cell;
114  }
115
116  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CakeCell"];
117  if (cell == nil)
118  {
119    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CakeCell"];
120  }
121
122  Cake *cake = [self.data objectAtIndex:indexPath.row];
123  [cell.imageView setImageWithURL:[NSURL URLWithString:cake.photo] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
124  cell.textLabel.text = cake.name;
125  return cell;
126}
127
128- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
129{
130    return NO;
131}
132
133#pragma mark - Table view delegate
134
135- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
136{
137    // Navigation logic may go here. Create and push another view controller.
138    /*
139     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
140     // ...
141     // Pass the selected object to the new view controller.
142     [self.navigationController pushViewController:detailViewController animated:YES];
143     */
144}
145
146@end