Commit 6100ccb
Changed files (5)
cakeside-ios
cakeside-ios.xcodeproj
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
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
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
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
cakeside-ios.xcodeproj/project.pbxproj
@@ -29,6 +29,7 @@
CDE67AC017934E7D00B4742C /* NSDictionary+Additions.m in Sources */ = {isa = PBXBuildFile; fileRef = CDE67ABF17934E7D00B4742C /* NSDictionary+Additions.m */; };
CDE67AC417935C1C00B4742C /* CreationsTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = CDE67AC217935C1C00B4742C /* CreationsTableViewController.m */; };
CDE67AC517935C1C00B4742C /* CreationsTableViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = CDE67AC317935C1C00B4742C /* CreationsTableViewController.xib */; };
+ CDE67ACB179364A600B4742C /* Cake.m in Sources */ = {isa = PBXBuildFile; fileRef = CDE67ACA179364A600B4742C /* Cake.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -73,6 +74,8 @@
CDE67AC117935C1C00B4742C /* CreationsTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CreationsTableViewController.h; sourceTree = "<group>"; };
CDE67AC217935C1C00B4742C /* CreationsTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CreationsTableViewController.m; sourceTree = "<group>"; };
CDE67AC317935C1C00B4742C /* CreationsTableViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = CreationsTableViewController.xib; path = ../controllers/CreationsTableViewController.xib; sourceTree = "<group>"; };
+ CDE67AC9179364A600B4742C /* Cake.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Cake.h; sourceTree = "<group>"; };
+ CDE67ACA179364A600B4742C /* Cake.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Cake.m; sourceTree = "<group>"; };
CE65D4B28521438085302B5E /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
@@ -185,6 +188,8 @@
CDE67AAB179346E800B4742C /* models */ = {
isa = PBXGroup;
children = (
+ CDE67AC9179364A600B4742C /* Cake.h */,
+ CDE67ACA179364A600B4742C /* Cake.m */,
);
path = models;
sourceTree = "<group>";
@@ -369,6 +374,7 @@
CDE67AB617934E4200B4742C /* NSString+Additions.m in Sources */,
CDE67AC017934E7D00B4742C /* NSDictionary+Additions.m in Sources */,
CDE67AC417935C1C00B4742C /* CreationsTableViewController.m in Sources */,
+ CDE67ACB179364A600B4742C /* Cake.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};