-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGolfer_VC.m
executable file
·321 lines (248 loc) · 12 KB
/
Golfer_VC.m
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// Golfer_VC.m
// OnPar2
//
// Created by Chad Galloway on 2/14/13.
// Copyright (c) 2013 Chad Galloway. All rights reserved.
//
#import "Golfer_VC.h"
@implementation GolferVC{
NSMutableArray *golfers;
MBProgressHUD *HUD;
}
@synthesize golferTableView;
@synthesize holeStepper;
@synthesize addButton;
@synthesize myView;
- (void)viewDidLoad
{
[super viewDidLoad];
//[self showNavBar];
[self stepperInitialization];
self.holeNumberLabel.text = [NSString stringWithFormat:@"%.f", holeStepper.value];
}
- (void)viewWillAppear:(BOOL)animated{
// delay before showing navbar
//[NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(showNavBar) userInfo:nil repeats:NO];
//[self showNavBar];
golfers = [[NSMutableArray alloc] init];
// load golfers that are in database
id appDelegate = (id)[[UIApplication sharedApplication] delegate];
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName: @"User"
inManagedObjectContext: [appDelegate managedObjectContext]];
[fetchRequest setEntity: entity];
NSArray *fetchedObjects = [[appDelegate managedObjectContext] executeFetchRequest: fetchRequest error: &error];
for (User *u in fetchedObjects) {
[golfers addObject: u];
}
[golferTableView reloadData];
if (golfers.count == 0)
{
// disable start button
self.navigationItem.rightBarButtonItem.enabled = NO;
}
else
{
// enable start button
self.navigationItem.rightBarButtonItem.enabled = YES;
}
if (golfers.count == 4)
{
// hide add button
addButton.hidden = YES;
}
}
#pragma mark - TableView methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [golfers count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"golfer";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
User *u = [golfers objectAtIndex: indexPath.row];
// if they have a nickname, display the username
// else display the full name
// for the details, display what tee they are using.
if (u.nickname) {
cell.textLabel.text = u.nickname;
} else {
cell.textLabel.text = u.name;
}
if (u.tee == [NSNumber numberWithInt: AGGIES]) {
cell.detailTextLabel.text = @"Aggies";
} else if (u.tee == [NSNumber numberWithInt: COWBELLS]) {
cell.detailTextLabel.text = @"Cowbells";
} else if (u.tee == [NSNumber numberWithInt: MAROONS]) {
cell.detailTextLabel.text = @"Maroons";
} else {
cell.detailTextLabel.text = @"Bulldogs";
}
return cell;
}
#pragma mark - Custom Header methods
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* myHeaderView = [[UIView alloc] initWithFrame: CGRectMake(0.0f,-50.0f, tableView.contentSize.width,70.0f)];
myHeaderView.backgroundColor = [UIColor clearColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)];
label.text = @"Golfers:";
label.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.75];
label.backgroundColor = [UIColor clearColor];
[myHeaderView addSubview:label];
return myHeaderView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30.0;
}
#pragma mark - Custom Footer methods
- (UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView* myFooterView = [[UIView alloc] initWithFrame: CGRectMake(0.0f,-50.0f, tableView.contentSize.height,70.0f )];
myFooterView.backgroundColor = [UIColor clearColor];
[tableView addSubview:myFooterView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width-22, 18)];
label.textAlignment = NSTextAlignmentCenter;
// change footer text
if (golfers.count == 0)
label.text = @"Add Some Golfers!";
else
label.text = @"Maximum of 4";
label.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.75];
label.backgroundColor = [UIColor clearColor];
[myFooterView addSubview:label];
return myFooterView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 30.0;
}
- (IBAction)startRound:(id)sender {
// create round
// 1. check for internet reachablility
// 1. loop through all the golfers
// 2. start a round with the current time
// 3. call API to get reference points for their selected tee
// check for reachability
if ([[Reachability reachabilityForLocalWiFi] isReachable]) {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo: self.view animated: YES];
hud.labelText = @"Loading...";
int __block numberOfGolfers = golfers.count;
int __block count = 1;
for (User *u in golfers) {
id appDelegate = (id)[[UIApplication sharedApplication] delegate];
NSError *error;
u.stageInfo.holeNumber = [NSNumber numberWithInt:[[NSNumber numberWithDouble: self.holeStepper.value] intValue]];
if (![[appDelegate managedObjectContext] save: &error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
// call the API to obtain the reference points for the holes
[[LRResty authenticatedClientWithUsername: API_USERNAME
password: API_PASSWORD
]
get: [NSString stringWithFormat: @"%@%@%@%@%@", BASE_URL, @"holes/reference/", @1, @"/", u.tee]
parameters: nil
headers: [NSDictionary dictionaryWithObject: @"application/json"
forKey: @"Content-Type"
]
withBlock: ^(LRRestyResponse *response) {
// 1. decode the JSON
// 2. create a hole and fill the reference points
// 3. point the hole to the round
// 4. save the round
if (response.status == 200) {
id appDelegate = (id)[[UIApplication sharedApplication] delegate];
NSError *error;
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
NSDictionary *jsonObject = [jsonParser objectWithData: response.responseData];
NSArray *JSONholes = [jsonObject objectForKey: @"holes"];
Round *r = [NSEntityDescription
insertNewObjectForEntityForName: @"Round"
inManagedObjectContext: [appDelegate managedObjectContext]];
// the course ID can maybe change at a later time
r.courseID = @1;
r.userID = u.userID;
r.teeID = u.tee;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [formatter stringFromDate: [NSDate date]];
r.startTime = dateString;
for (NSDictionary *holeTop in JSONholes) {
NSDictionary *hole = [holeTop objectForKey: @"hole"];
Hole *h = [NSEntityDescription
insertNewObjectForEntityForName: @"Hole"
inManagedObjectContext: [appDelegate managedObjectContext]];
h.holeNumber = [NSNumber numberWithInt: [[hole objectForKey: @"holeNumber"] intValue]];
h.par = [NSNumber numberWithInt: [[hole objectForKey: @"par"] intValue]];
h.distance = [NSNumber numberWithInt: [[hole objectForKey: @"distance"] intValue]];
h.firstRefLat = [NSNumber numberWithDouble: [[hole objectForKey: @"firstRefLat"] doubleValue]];
h.firstRefLong = [NSNumber numberWithDouble: [[hole objectForKey: @"firstRefLong"] doubleValue]];
h.secondRefLat = [NSNumber numberWithDouble: [[hole objectForKey: @"secondRefLat"] doubleValue]];
h.secondRefLong = [NSNumber numberWithDouble: [[hole objectForKey: @"secondRefLong"] doubleValue]];
h.firstRefX = [NSNumber numberWithInt: [[hole objectForKey: @"firstRefX"] intValue]];
h.firstRefY = [NSNumber numberWithInt: [[hole objectForKey: @"firstRefY"] intValue]];
h.secondRefX = [NSNumber numberWithInt: [[hole objectForKey: @"secondRefX"] intValue]];
h.secondRefY = [NSNumber numberWithInt: [[hole objectForKey: @"secondRefY"] intValue]];
h.round = r;
[r addHolesObject: h];
}
// save the round to the DB
if (![[appDelegate managedObjectContext] save: &error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
} else {
AHAlertView *alert = [[AHAlertView alloc] initWithTitle:@"Error" message:@"API error."];
[alert applyCustomAlertAppearance];
__weak AHAlertView *weakAlert = alert;
[alert addButtonWithTitle:@"OK" block:^{
weakAlert.dismissalStyle = AHAlertViewDismissalStyleTumble;
}];
[alert show];
}
//NSLog(@"Count: %d", count);
//NSLog(@"numberOfGolers: %d", numberOfGolfers);
if (count == numberOfGolfers) {
// hide spinner and transition
[MBProgressHUD hideHUDForView: self.view animated: YES];
// transition
[self performSegueWithIdentifier:@"settings2play" sender:self];
} else {
count++;
}
}];
}
} else {
// internet reachablility failed
AHAlertView *alert = [[AHAlertView alloc] initWithTitle:@"Connection Error" message:@"You must be connected to the Wi-Fi at the club house for this action."];
[alert applyCustomAlertAppearance];
__weak AHAlertView *weakAlert = alert;
[alert addButtonWithTitle:@"OK" block:^{
weakAlert.dismissalStyle = AHAlertViewDismissalStyleTumble;
}];
[alert show];
}
}
- (IBAction)valueChanged:(id)sender {
double stepperValue = holeStepper.value;
self.holeNumberLabel.text = [NSString stringWithFormat:@"%.f", stepperValue];
}
- (void)showNavBar
{
self.navigationController.navigationBar.hidden = NO;
}
- (void)stepperInitialization
{
// Hole Stepper Setup
self.holeStepper.minimumValue = 1;
self.holeStepper.maximumValue = 18;
self.holeStepper.stepValue = 1;
self.holeStepper.wraps = YES;
self.holeStepper.autorepeat = YES;
self.holeStepper.continuous = YES;
}
@end