-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseViewController.m
326 lines (236 loc) · 10.3 KB
/
BaseViewController.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
322
323
324
325
326
//
// BaseViewController.m
// BWSimulatorOS
//
// Created by Carson Chow on 1/2/13.
// Copyright (c) 2013 NIDDK, NIH. All rights reserved.
//
#import "BaseViewController.h"
#import "PALViewController.h"
#import "HelpView2Controller.h"
#import "Person.h"
NSString * const sexPrefKey = @"sexPrefKey";
NSString * const weightUnitPrefKey = @"weightUnitPrefKey";
NSString * const intakeUnitPrefKey = @"intakeUnitPrefKey";
NSString * const heightUnitPrefKey = @"heightUnitPrefKey";
NSString * const ageKey = @"ageKey";
NSString * const weightInitKey = @"weightInitKey";
NSString * const heightKey = @"heightKey";
NSString * const intakeInitKey = @"intakeInitKey";
NSString * const palInitKey = @"palInitKey";
NSString * const firstBVCOpening = @"firstBVOpening";
@implementation BaseViewController
@synthesize person;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
UINavigationItem *tbi = [self navigationItem];
[tbi setTitle:@"Baseline Info"];
// Create bar button
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithTitle:@"Help" style:UIBarButtonItemStyleBordered target:self action:@selector(showHelp:)];
[[self navigationItem] setRightBarButtonItem:bbi];
}
return self;
}
- (void) viewDidLoad
{
NSInteger sexValue = [[NSUserDefaults standardUserDefaults]
integerForKey:sexPrefKey];
[sexSwitch setSelectedSegmentIndex:sexValue];
[self changeSex:sexSwitch];
NSInteger heightUnitValue = [[NSUserDefaults standardUserDefaults]
integerForKey:heightUnitPrefKey];
[heightUnitSwitch setSelectedSegmentIndex:heightUnitValue];
[self changeHeightUnit:heightUnitSwitch];
NSInteger weightUnitValue = [[NSUserDefaults standardUserDefaults]
integerForKey:weightUnitPrefKey];
[weightUnitSwitch setSelectedSegmentIndex:weightUnitValue];
[self changeWeightUnit:weightUnitSwitch];
NSInteger intakeUnitValue = [[NSUserDefaults standardUserDefaults]
integerForKey:intakeUnitPrefKey];
[intakeUnitSwitch setSelectedSegmentIndex:intakeUnitValue];
[self changeIntakeUnit:intakeUnitSwitch];
if ([[NSUserDefaults standardUserDefaults] boolForKey:firstBVCOpening])
{
[person setAge:[[NSUserDefaults standardUserDefaults] integerForKey:ageKey]];
[person setHeight:[[NSUserDefaults standardUserDefaults] doubleForKey:heightKey]];
[person setWeightInitial:[[NSUserDefaults standardUserDefaults] doubleForKey:weightInitKey]];
[person setPalInitial:[[NSUserDefaults standardUserDefaults] doubleForKey:palInitKey]];
NSLog(@"not first time");
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:firstBVCOpening];
}
}
- (void)viewDidUnload {
ageField = nil;
sexSwitch = nil;
palField = nil;
heightUnitSwitch = nil;
intakeUnitSwitch = nil;
weightUnitSwitch = nil;
intakeField = nil;
[super viewDidUnload];
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSInteger heightUnitValue = [[NSUserDefaults standardUserDefaults]
integerForKey:heightUnitPrefKey];
NSInteger weightUnitValue = [[NSUserDefaults standardUserDefaults]
integerForKey:weightUnitPrefKey];
NSInteger intakeUnitValue = [[NSUserDefaults standardUserDefaults]
integerForKey:intakeUnitPrefKey];
[ageField setText:[NSString stringWithFormat:@"%d", [person age]]];
[heightField setText:[NSString stringWithFormat:@"%.00f", [person height]*(1+heightUnitValue*(.3937 -1))*100]];
[palField setText:[NSString stringWithFormat:@"%g", [person palInitial]]];
[intakeField setText:[NSString stringWithFormat:@"%ld", lroundf([person teeInitial]*(1+intakeUnitValue*(4.184-1)))]];
[weightField setText:[NSString stringWithFormat:@"%g", [person weightInitial]*(1+weightUnitValue*(2.2-1))]];
}
- (void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[self view] endEditing:YES];
NSInteger heightUnitValue = [[NSUserDefaults standardUserDefaults]
integerForKey:heightUnitPrefKey];
NSInteger weightUnitValue = [[NSUserDefaults standardUserDefaults]
integerForKey:weightUnitPrefKey];
[person setAge:[[ageField text] intValue]];
[person setHeight:[[heightField text] doubleValue]*(1+heightUnitValue*(1/.3937 -1))/100];
[person setPalInitial:[[palField text] doubleValue]];
[person setWeightInitial:[[weightField text] doubleValue]*(1+weightUnitValue*(1/2.2-1))];
[person setIntakeInitial:[[intakeField text] doubleValue]];
[[NSUserDefaults standardUserDefaults] setInteger:[person age] forKey:ageKey];
[[NSUserDefaults standardUserDefaults] setDouble:[person height] forKey:heightKey];
[[NSUserDefaults standardUserDefaults] setDouble:[person weightInitial] forKey:weightInitKey];
[[NSUserDefaults standardUserDefaults] setDouble:[person intakeInitial] forKey:intakeInitKey];
[[NSUserDefaults standardUserDefaults] setDouble:[person palInitial] forKey:palInitKey];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSInteger heightUnitValue = [[NSUserDefaults standardUserDefaults]
integerForKey:heightUnitPrefKey];
NSInteger weightUnitValue = [[NSUserDefaults standardUserDefaults]
integerForKey:weightUnitPrefKey];
[person setHeight:[[heightField text] doubleValue]*(1+heightUnitValue*(1/.3937 -1))/100];
[person setPalInitial:[[palField text] doubleValue]];
[person setWeightInitial:[[weightField text] doubleValue]*(1+weightUnitValue*(1/2.2-1))];
[intakeField setText:[NSString stringWithFormat:@"%g", [person teeInitial]]];
[[NSUserDefaults standardUserDefaults] setInteger:[person age] forKey:ageKey];
[[NSUserDefaults standardUserDefaults] setDouble:[person height] forKey:heightKey];
[[NSUserDefaults standardUserDefaults] setDouble:[person weightInitial] forKey:weightInitKey];
[[NSUserDefaults standardUserDefaults] setDouble:[person palInitial] forKey:palInitKey];
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField: textField: YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField: textField: NO];
}
- (void) animateTextField: (UITextField*) textField: (BOOL) up
{
int animatedDistance;
int moveUpValue = textField.frame.origin.y+ textField.frame.size.height;
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = 320-(480-moveUpValue-5);
}
else
{
animatedDistance = 162-(320-moveUpValue-5);
}
if(animatedDistance>0)
{
const int movementDistance = animatedDistance;
const float movementDuration = 0.3;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: nil context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
}
- (IBAction)changeSex:(id)sender
{
[[NSUserDefaults standardUserDefaults]
setInteger:[sender selectedSegmentIndex] forKey:sexPrefKey];
switch ([sender selectedSegmentIndex]) {
case 0: {
[person setSex:1];
// NSLog(@"female");
} break;
case 1: {
[person setSex:0];
// NSLog(@"male");
} break;
}
}
- (IBAction)changeIntakeUnit:(id)sender
{
// NSLog(@"segment is %d",[sender selectedSegmentIndex]);
[[NSUserDefaults standardUserDefaults]
setInteger:[sender selectedSegmentIndex] forKey:intakeUnitPrefKey];
switch ([sender selectedSegmentIndex]) {
case 1: {
[intakeField setText:[NSString stringWithFormat:@"%ld", lround([person teeInitial]*4.184)]];
// NSLog(@"KJ");
} break;
default:
[intakeField setText:[NSString stringWithFormat:@"%ld", lround([person teeInitial])]];
// NSLog(@"Cal");
break;
}
}
- (IBAction)changeWeightUnit:(id)sender
{
[[NSUserDefaults standardUserDefaults]
setInteger:[sender selectedSegmentIndex] forKey:weightUnitPrefKey];
switch ([sender selectedSegmentIndex]) {
case 1: {
[weightField setText:[NSString stringWithFormat:@"%g", [person weightInitial]*2.2]];
// NSLog(@"lbs");
} break;
default:
[weightField setText:[NSString stringWithFormat:@"%g", [person weightInitial]]];
// NSLog(@"Kg");
break;
}
}
- (IBAction)changeHeightUnit:(id)sender
{
[[NSUserDefaults standardUserDefaults]
setInteger:[sender selectedSegmentIndex] forKey:heightUnitPrefKey];
switch ([sender selectedSegmentIndex]) {
case 1: {
[heightField setText:[NSString stringWithFormat:@"%.01f", [person height]*39.37]];
// NSLog(@"inches");
} break;
default:
[heightField setText:[NSString stringWithFormat:@"%.00f", [person height]*100]];
// NSLog(@"m");
break;
}
}
- (IBAction)showEstimatePAL:(id)sender
{
PALViewController *palViewController = [[PALViewController alloc] init];
[[self navigationController] pushViewController:palViewController animated:YES];
[palViewController setPerson:person];
NSLog(@"estimatePAL");
}
- (IBAction)showHelp:(id)sender
{
HelpView2Controller *helpViewController = [[HelpView2Controller alloc] init];
[[self navigationController] pushViewController:helpViewController animated:YES];
}
@end