-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeightViewController.m
515 lines (349 loc) · 16.3 KB
/
WeightViewController.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//
// WeightViewController.m
// BWSimulatorOS
//
// Created by Carson Chow on 1/4/13.
// Copyright (c) 2013 NIDDK, NIH. All rights reserved.
//
#import "WeightViewController.h"
#import "BaseViewController.h"
#import "NewPALViewController.h"
#import "HelpView2Controller.h"
#import "GraphViewController.h"
#import "PersonStore.h"
#import "Person.h"
// NSUserDefault strings
NSString * const firstOpening = @"firstWVCOpening";
NSString * const weightUnitWVCPrefKey = @"weightUnitWVCPrefKey";
NSString * const intakeUnitWVCPrefKey = @"intakeUnitWVCPrefKey";
NSString * const intakeSignWVCPrefKey = @"intakeSignWVCPrefKey";
NSString * const endDayPrefKey = @"endDayPrefKey";
NSString * const ageWVCKey = @"ageKey";
NSString * const weightInitWVCKey = @"weightInitKey";
NSString * const heightWVCKey = @"heightKey";
NSString * const intakeInitWVCKey = @"intakeInitKey";
NSString * const palInitWVCKey = @"palInitKey";
NSString * const palWVCKey = @"palKey";
NSString * const activitySignWVCPrefKey = @"activitySignWVCPrefKey";
@implementation WeightViewController
- (id)init
{
self = [super init];
if (self) {
UINavigationItem *n = [self navigationItem];
[n setTitle:@"BW Simulator"];
// Create bar button
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithTitle:@"Help" style:UIBarButtonItemStyleBordered target:self action:@selector(showHelp:)];
[[self navigationItem] setRightBarButtonItem:bbi];
for (int i = 0; i < 1; i++) {
[[PersonStore sharedStore] createPerson];
}
}
return self;
}
/*
- (id)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
*/
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewWillAppear:(BOOL)animated
{
NSArray *persons = [[PersonStore sharedStore] allPersons];
Person *p = [persons objectAtIndex:0];
double activityChange = 100*fabs([[NSUserDefaults standardUserDefaults] doubleForKey:palWVCKey]-[p palInitial])/[p palInitial];
[activityChangeField setText:[NSString stringWithFormat:@"%g", activityChange ]];
[endDayField setText:[NSString stringWithFormat:@"%d",[[NSUserDefaults standardUserDefaults] integerForKey:endDayPrefKey]]];
NSLog(@"dPA = %g",[[NSUserDefaults standardUserDefaults] doubleForKey:palWVCKey]-[p palInitial]);
}
- (void) viewWillDisappear:(BOOL)animated
{
NSArray *persons = [[PersonStore sharedStore] allPersons];
Person *p = [persons objectAtIndex:0];
[[NSUserDefaults standardUserDefaults] setDouble:[p pal] forKey:palWVCKey];
[[NSUserDefaults standardUserDefaults] setInteger:[[endDayField text] integerValue] forKey:endDayPrefKey];
NSLog(@"pal %g",[p pal]);
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSArray *persons = [[PersonStore sharedStore] allPersons];
Person *p = [persons objectAtIndex:0];
[activityIndicator stopAnimating];
// On first opening, go directly to Help
if (![[NSUserDefaults standardUserDefaults] boolForKey:firstOpening])
{
// Acknowledge first opening
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:firstOpening];
// Go to Help View through Baseline View
BaseViewController *baseViewController = [[BaseViewController alloc] init];
NSArray *persons = [[PersonStore sharedStore] allPersons];
Person *p = [persons objectAtIndex:0];
[baseViewController setPerson:p];
[[self navigationController] pushViewController:baseViewController animated:YES];
// Back button in Help View will be Baseline Information
HelpView2Controller *helpViewController = [[HelpView2Controller alloc] init];
[[self navigationController] pushViewController:helpViewController animated:YES];
//store initial activity value
[[NSUserDefaults standardUserDefaults] setDouble:[p pal] forKey:palWVCKey];
[[NSUserDefaults standardUserDefaults] setInteger:180 forKey:endDayPrefKey];
[endDayField setText:@"180"];
}
else
{
// Set to default values
NSInteger weightUnitValue = [[NSUserDefaults standardUserDefaults]
integerForKey:weightUnitWVCPrefKey];
[weightUnitSwitch setSelectedSegmentIndex:weightUnitValue];
NSInteger intakeUnitValue = [[NSUserDefaults standardUserDefaults]
integerForKey:intakeUnitWVCPrefKey];
[intakeUnitSwitch setSelectedSegmentIndex:intakeUnitValue];
NSInteger intakeSignValue = [[NSUserDefaults standardUserDefaults] integerForKey:intakeSignWVCPrefKey];
[intakeSignSwitch setSelectedSegmentIndex:intakeSignValue];
NSInteger activitySignValue = [[NSUserDefaults standardUserDefaults] integerForKey:activitySignWVCPrefKey];
[activityChangeSwitch setSelectedSegmentIndex:activitySignValue];
[p setAge:[[NSUserDefaults standardUserDefaults] integerForKey:ageWVCKey]];
[p setHeight:[[NSUserDefaults standardUserDefaults] doubleForKey:heightWVCKey]];
[p setWeightInitial:[[NSUserDefaults standardUserDefaults] doubleForKey:weightInitWVCKey]];
[p setIntakeInitial:[[NSUserDefaults standardUserDefaults] doubleForKey:intakeInitWVCKey]];
[p setPal:[[NSUserDefaults standardUserDefaults] doubleForKey:palWVCKey]];
[p setPalInitial:[[NSUserDefaults standardUserDefaults] doubleForKey:palInitWVCKey]];
}
// Set activity field to current physical activity level
[activityChangeField setText:[NSString stringWithFormat:@"%g", 100*abs([p pal]-[p palInitial])/[p palInitial]]];
[endDayField setText:[NSString stringWithFormat:@"%d",[[NSUserDefaults standardUserDefaults] integerForKey:endDayPrefKey]]];
}
- (void)viewDidUnload
{
endDayField = nil;
newIntakeField = nil;
newWeightField = nil;;
intakeUnitSwitch = nil;
maintenanceIntakeField = nil;
weightUnitSwitch = nil;
maintenanceLabel = nil;
activityIndicator = nil;
intakeSignSwitch = nil;
intakeSignSwitch = nil;
intakeChangeField = nil;
activityChangeField = nil;
activityChangeSwitch = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)showBaselineView:(id)sender
{
BaseViewController *baseViewController = [[BaseViewController alloc] init];
NSArray *persons = [[PersonStore sharedStore] allPersons];
Person *p = [persons objectAtIndex:0];
[baseViewController setPerson:p];
[[self navigationController] pushViewController:baseViewController animated:YES];
}
- (IBAction)reset:(id)sender
{
NSArray *persons = [[PersonStore sharedStore] allPersons];
Person *p = [persons objectAtIndex:0];
[p setIntake:0];
[maintenanceIntakeField setText:@""];
[maintenanceLabel setText:@""];
[newWeightField setText:@""];
[newIntakeField setText:@""];
[intakeChangeField setText:@""];
NSLog(@"reset");
}
#pragma mark - Run
- (IBAction)run:(id)sender
{
NSArray *persons = [[PersonStore sharedStore] allPersons];
Person *p = [persons objectAtIndex:0];
NSInteger weightUnitValue = [[NSUserDefaults standardUserDefaults]
integerForKey:weightUnitWVCPrefKey];
NSInteger intakeUnitValue = [[NSUserDefaults standardUserDefaults]
integerForKey:intakeUnitWVCPrefKey];
// NSInteger activitySign = [[NSUserDefaults standardUserDefaults] integerForKey:activitySignWVCPrefKey];
double totalTime = 1.0*[[endDayField text] intValue ];
if (totalTime > 0) {
double intake =[[newIntakeField text] doubleValue]*(1+intakeUnitValue*(1/4.184-1));
double goalWeight = [[newWeightField text] doubleValue]*(1+weightUnitValue*(1/2.2-1));
[p setPal:[p palInitial]*(1+(2*[activityChangeSwitch selectedSegmentIndex]-1)*[[activityChangeField text] doubleValue]/100)];
// [activityIndicator startAnimating];
// NSLog(@"F=%f, L=%f, BW=%f, bmi=%f, therm=%f, intake=%f, te=%f",[p fatInitial], [p lean],[p weightInitial],[p bmi],[p therm], [p intakeInitial], [p energyExpenditure]);
if (intake > 0) {
[p setIntake:intake];
[p stepper:totalTime];
[newWeightField setText:[NSString stringWithFormat:@"%ld", lroundf([p weight]*(1+weightUnitValue*(2.2-1)))]];
[maintenanceLabel setText:@"Maintenance Diet"];
[maintenanceIntakeField setText:[NSString stringWithFormat:@"%ld", lroundf([p findMaintenanceIntake:[p weight]]*(1+intakeUnitValue*(4.184-1)))]];
}
else if (goalWeight > 0) {
[p findIntake:goalWeight:totalTime];
intake = [p intake];
[newIntakeField setText:[NSString stringWithFormat:@"%ld", lroundf([p intake]*(1+intakeUnitValue*(4.184-1)))]];
double intakeChange = ([p intake]-[p intakeInitial])*(1+intakeUnitValue*(4.184-1));
if (intakeChange > 0) {
[intakeSignSwitch setSelectedSegmentIndex:1];
} else
[intakeSignSwitch setSelectedSegmentIndex:0];
[intakeChangeField setText:[NSString stringWithFormat:@"%ld",lroundf(abs(intakeChange)*(1+intakeUnitValue*(4.184-1)))]];
[maintenanceLabel setText:@"Maintenance Diet"];
[maintenanceIntakeField setText:[NSString stringWithFormat:@"%ld", lroundf([p findMaintenanceIntake:goalWeight]*(1+intakeUnitValue*(4.184-1)))]];
}
[activityIndicator stopAnimating];
// [p setIntake:intake];
// NSLog(@"Intake is %g, Weight is %g, PAL is %g",[p intake],[p weight],[p pal]);
NSLog(@"F is now %f, L is %f, BW is %f, g is %f, decw is %f, bmi is %f, therm is %f, fatfrac is %f, EE is %f, pal is %f",[p fat], [p lean],[p weight],[p glycogen],[p deltaExtraCellularWater],[p bmi],[p therm], [p fatFrac], [p energyExpenditure], [p pal]);
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField != newWeightField & textField != endDayField)
[self setAttributes];
[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)changeIntakeSign:(id)sender
{
[[NSUserDefaults standardUserDefaults]
setInteger:[sender selectedSegmentIndex] forKey:intakeSignWVCPrefKey];
if ([[intakeChangeField text] doubleValue]>0) {
[self setAttributes];
}
// NSLog(@"%d",-[sender selectedSegmentIndex]);
}
- (IBAction)changeIntakeUnit:(id)sender
{
// NSLog(@"segment is %d",[sender selectedSegmentIndex]);
[[NSUserDefaults standardUserDefaults]
setInteger:[sender selectedSegmentIndex] forKey:intakeUnitWVCPrefKey];
double newIntake = [[newIntakeField text] doubleValue];
if (newIntake > 0)
{
switch ([sender selectedSegmentIndex]) {
case 1: {
[newIntakeField setText:[NSString stringWithFormat:@"%ld", lroundf(newIntake*4.184)]];
[maintenanceIntakeField setText:[NSString stringWithFormat:@"%ld", lroundf([[maintenanceIntakeField text] doubleValue]*4.184)]];
NSLog(@"KJ");
} break;
default:
[newIntakeField setText:[NSString stringWithFormat:@"%ld", lroundf(newIntake/4.184)]];
[maintenanceIntakeField setText:[NSString stringWithFormat:@"%ld", lroundf([[maintenanceIntakeField text] doubleValue]/4.184)]];
// [intakeField setText:[NSString stringWithFormat:@"%g", [person teeInitial]]];
NSLog(@"Cal");
break;
}
}
}
- (IBAction)changeWeightUnit:(id)sender
{
[[NSUserDefaults standardUserDefaults]
setInteger:[sender selectedSegmentIndex] forKey:weightUnitWVCPrefKey];
double newWeight = [[newWeightField text]
doubleValue];
if (newWeight > 0){
switch ([sender selectedSegmentIndex]) {
case 1: {
[newWeightField setText:[NSString stringWithFormat:@"%g", newWeight*2.2]];
// [weightField setText:[NSString stringWithFormat:@"%g", [person weightInitial]*2.2]];
NSLog(@"lbs");
} break;
default:
[newWeightField setText:[NSString stringWithFormat:@"%g", newWeight/2.2]];
// [weightField setText:[NSString stringWithFormat:@"%g", [person weightInitial]]];
NSLog(@"Kg");
break;
}
}
}
- (void)setAttributes
{
double intake;
NSArray *persons = [[PersonStore sharedStore] allPersons];
Person *p = [persons objectAtIndex:0];
NSInteger intakeUnitValue = [[NSUserDefaults standardUserDefaults]
integerForKey:intakeUnitWVCPrefKey];
int intakeSign = 2*[[NSUserDefaults standardUserDefaults] integerForKey:intakeSignWVCPrefKey]-1;
double intakeChange=[[intakeChangeField text] doubleValue]*(1+intakeUnitValue*(1/4.184-1));
if ([[newIntakeField text] doubleValue]>0)
{
intake =[[newIntakeField text] doubleValue]*(1+intakeUnitValue*(1/4.184-1));
intakeChange = [p intakeInitial] - intake;
[intakeChangeField setText:[NSString stringWithFormat:@"%ld", lroundf(intakeChange*(1+intakeUnitValue*(4.184-1)))]];
} else {
intake = [p intakeInitial]+ intakeSign*intakeChange;
[p setIntake:intake];
[newIntakeField setText:[NSString stringWithFormat:@"%ld", lroundf(intake*(1+intakeUnitValue*(4.184-1)))]];
}
[p setPal:[p palInitial]*(1+(2*[activityChangeSwitch selectedSegmentIndex]-1)*[[activityChangeField text] doubleValue]/100)];
}
- (IBAction)showHelp:(id)sender
{
HelpView2Controller *helpViewController = [[HelpView2Controller alloc] init];
[[self navigationController] pushViewController:helpViewController animated:YES];
}
- (IBAction)showGraph:(id)sender
{
// [activityIndicator startAnimating];
NSArray *persons = [[PersonStore sharedStore] allPersons];
Person *p = [persons objectAtIndex:0];
GraphViewController *graphViewController = [[GraphViewController alloc] init];
[graphViewController setPerson:p];
[[self navigationController] pushViewController:graphViewController animated:YES];
}
@end