forked from jsonmodel/jsonmodel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKivaViewController.m
More file actions
108 lines (84 loc) · 3.75 KB
/
KivaViewController.m
File metadata and controls
108 lines (84 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// KivaViewController.m
// JSONModelDemo
//
// Created by Marin Todorov on 02/12/2012.
// Copyright (c) 2012 Underplot ltd. All rights reserved.
//
#import "KivaViewController.h"
#import "KivaFeed.h"
#import "HUD.h"
#import "JSONModel+networking.h"
@interface KivaViewController () <UITableViewDataSource, UITableViewDelegate>
{
IBOutlet UITableView* table;
KivaFeed* feed;
double benchStart;
double benchObj;
double benchEnd;
}
@end
@implementation KivaViewController
-(void)viewDidAppear:(BOOL)animated
{
self.title = @"Kiva.org latest loans";
[HUD showUIBlockingIndicatorWithText:@"Fetching JSON"];
[JSONHTTPClient getJSONFromURLWithString:@"http://api.kivaws.org/v1/loans/search.json"
params:@{@"status":@"fundraising"}
completion:^(NSDictionary *json, JSONModelError *err) {
benchStart = CFAbsoluteTimeGetCurrent();
feed = [[KivaFeed alloc] initWithDictionary: json error:nil];
benchEnd = CFAbsoluteTimeGetCurrent();
[HUD hideUIBlockingIndicator];
if (feed) {
[table reloadData];
[self logBenchmark];
} else {
//show error
[[[UIAlertView alloc] initWithTitle:@"Error"
message:[err localizedDescription]
delegate:nil
cancelButtonTitle:@"Close"
otherButtonTitles:nil] show];
}
}];
}
-(void)logBenchmark
{
NSLog(@"start: %f", benchStart);
NSLog(@"model: %f", benchEnd);
NSLog(@"-------------------------");
NSLog(@"json -> model: %.4f", benchEnd - benchStart);
}
#pragma mark - table methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return feed.loans.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KivaCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"KivaCell"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
LoanModel* loan = feed.loans[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@ from %@ (%@)",
loan.name, loan.location.country, loan.location.countryCode
];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[table deselectRowAtIndexPath:indexPath animated:YES];
LoanModel* loan = feed.loans[indexPath.row];
NSString* message = [NSString stringWithFormat:@"%@ from %@(%@) needs a loan %@",
loan.name, loan.location.country, loan.location.countryCode, loan.use
];
[HUD showAlertWithTitle:@"Loan details" text:message];
}
@end