summaryrefslogtreecommitdiff
path: root/cakeside-ios
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2013-07-14 17:27:04 -0600
committermo khan <mo@mokhan.ca>2013-07-14 17:27:04 -0600
commit6100ccb8f66b9e1880fb1ac8b3028c44156e20f1 (patch)
treea14acd37e6a509d1d21ba309331e773466522a41 /cakeside-ios
parent8ac2d897ae30b79bce9c9485a15fb98470401594 (diff)
fix json parsing code
Diffstat (limited to 'cakeside-ios')
-rw-r--r--cakeside-ios/cakeside-ios-Prefix.pch6
-rw-r--r--cakeside-ios/controllers/CreationsTableViewController.m64
-rw-r--r--cakeside-ios/models/Cake.h15
-rw-r--r--cakeside-ios/models/Cake.m21
4 files changed, 104 insertions, 2 deletions
diff --git a/cakeside-ios/cakeside-ios-Prefix.pch b/cakeside-ios/cakeside-ios-Prefix.pch
index dcf7e28..33553ef 100644
--- a/cakeside-ios/cakeside-ios-Prefix.pch
+++ b/cakeside-ios/cakeside-ios-Prefix.pch
@@ -26,6 +26,7 @@
// URLs
#define HOST @"http://localhost:3000"
#define URL_LOGIN @"/api/v1/logins"
+#define URL_CAKES @"/api/v1/creations"
// KeyChain defs
#define KEYCHAIN_ACCOUNT @"cakeside"
@@ -37,4 +38,7 @@
#define HUD_ANIMATION_DURATION 0.4
#define HUD_ANIMATION_DURATION_LONG 1.0
#define HUD_ANIMATION_DURATION_EXTRALONG 1.6
-#define OVERLAY_MESSAGE_DURATION 3 \ No newline at end of file
+#define OVERLAY_MESSAGE_DURATION 3
+
+// Notifications
+#define NOTIFICATION_UPDATED_STATS_DATA @"NOTIFICATION_UPDATED_STATS_DATA" \ No newline at end of file
diff --git a/cakeside-ios/controllers/CreationsTableViewController.m b/cakeside-ios/controllers/CreationsTableViewController.m
index 0299f05..e5d6f8d 100644
--- a/cakeside-ios/controllers/CreationsTableViewController.m
+++ b/cakeside-ios/controllers/CreationsTableViewController.m
@@ -7,9 +7,10 @@
//
#import "CreationsTableViewController.h"
+#import "Cake.h"
@interface CreationsTableViewController ()
-
+@property (strong, nonatomic) NSMutableArray *data;
@end
@implementation CreationsTableViewController
@@ -32,6 +33,8 @@
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
+ // start loading data...
+ [self updateData];
}
- (void)didReceiveMemoryWarning
@@ -40,6 +43,65 @@
// Dispose of any resources that can be recreated.
}
+- (void)updateData
+{
+ self.data = nil;
+ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
+
+ NSError *error;
+ NSString *token = [SSKeychain passwordForService:KEYCHAIN_API_TOKEN account:KEYCHAIN_ACCOUNT error:&error];
+
+ // load data
+ AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:HOST]];
+ [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
+ [httpClient setDefaultHeader:@"Accept" value:@"application/json"];
+ [httpClient setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Token token=%@", token]];
+// Authorization: Token token=
+ [httpClient setParameterEncoding:AFJSONParameterEncoding];
+
+ NSMutableURLRequest *request;
+ request = [httpClient requestWithMethod:@"GET" path:URL_CAKES parameters:nil];
+
+ NSLog(@"GET: %@", request);
+
+ AFJSONRequestOperation *operation = [AFJSONRequestOperation
+ JSONRequestOperationWithRequest:request
+ success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
+ {
+ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
+ //NSLog(@"%@", JSON);
+
+ if (!self.data)
+ {
+ self.data = [[NSMutableArray alloc] init];
+ }
+
+ for (NSDictionary *data in JSON)
+ {
+ Cake *cake = [Cake initFromJSON:data];
+ [self.data addObject:cake];
+ }
+
+// [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_UPDATED_STATS_DATA object:nil];
+ }
+ failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
+ {
+ NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
+ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
+ [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_UPDATED_STATS_DATA object:nil];
+
+ BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Error" message:@"Failed to retrieve reading data. Please try again later."];
+ [alert setCancelButtonWithTitle:@"Ok" block:nil];
+ [alert show];
+ }];
+ [operation start];
+
+
+}
+
+
+
+
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
diff --git a/cakeside-ios/models/Cake.h b/cakeside-ios/models/Cake.h
new file mode 100644
index 0000000..da71cc9
--- /dev/null
+++ b/cakeside-ios/models/Cake.h
@@ -0,0 +1,15 @@
+//
+// Cake.h
+// cakeside-ios
+//
+// Created by mo khan on 2013-07-14.
+// Copyright (c) 2013 mo khan. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface Cake : NSObject
++ (Cake *)initFromJSON:(NSDictionary *)jsonData;
+@property (nonatomic) NSInteger id;
+@property (nonatomic) NSString *name;
+@end
diff --git a/cakeside-ios/models/Cake.m b/cakeside-ios/models/Cake.m
new file mode 100644
index 0000000..f484881
--- /dev/null
+++ b/cakeside-ios/models/Cake.m
@@ -0,0 +1,21 @@
+//
+// Cake.m
+// cakeside-ios
+//
+// Created by mo khan on 2013-07-14.
+// Copyright (c) 2013 mo khan. All rights reserved.
+//
+
+#import "Cake.h"
+
+@implementation Cake
++ (Cake *)initFromJSON:(NSDictionary *)jsonData;
+{
+ NSLog(@"%@", jsonData);
+ Cake *result = [[Cake alloc] init];
+ result.id = [[jsonData objectForKey:@"id"] integerValue];
+ result.name = [jsonData objectForKey:@"name"];
+ return result;
+}
+
+@end