-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParameterViewController.m
More file actions
446 lines (339 loc) · 15.4 KB
/
ParameterViewController.m
File metadata and controls
446 lines (339 loc) · 15.4 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
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
//
// ParameterViewController.m
// Metasome
//
// Created by Omar Metwally on 8/19/13.
// Copyright (c) 2013 Logisome. All rights reserved.
//
#import "ParameterViewController.h"
#import "MetasomeParameterStore.h"
#import "MetasomeParameter.h"
#import "MetasomeDataPointStore.h"
#import "MetasomeDataPoint.h"
#import "DetailViewController.h"
#import "DetailViewController_slider.h"
#import "NewParameterViewController.h"
#import "MetasomeBloodPressureViewController.h"
#import "HomeViewController.h"
#import "LoginScreenViewController.h"
#import "TextFormatter.h"
#import "CustomParameterCell.h"
@implementation ParameterViewController
@synthesize currentSelection, titleString, titleLabel;
- (id)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
-(id)init
{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
UIBarButtonItem *bbi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:@selector(addNewItem:)];
[[self navigationItem] setRightBarButtonItem:bbi];
[[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.75 green:0.90 blue:0.22 alpha:1.0];
titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
}
return self;
}
+(void) initialize
{
NSDictionary *defaults = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:@"launchCount"];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}
-(void)addNewItem:(id)sender
{
NewParameterViewController *npvc = [[NewParameterViewController alloc] init];
[[self navigationController] pushViewController:npvc animated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
// Check if app was previously loaded and recall the last selection
selectionTypeOnLaunch lastSelection = [[NSUserDefaults standardUserDefaults] integerForKey:@"lastSelection"];
[self setCurrentSelection:lastSelection];
[self updateTitle:titleLabel];
// If the current list is empty, set to heart list by default
if ([[self tableView] numberOfRowsInSection:0] == 0) {
[[MetasomeParameterStore sharedStore] setCurrentList:[[MetasomeParameterStore sharedStore] heartList]];
}
[[self tableView] reloadData];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self setEditing:NO animated:YES];
}
- (void)preferredContentSizeChanged:(NSNotification *)aNotification
{
[[self view] setNeedsLayout];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(preferredContentSizeChanged:) name:UIContentSizeCategoryDidChangeNotification object:nil];
// load the custom cell XIB file
UINib *nib = [UINib nibWithNibName:@"CustomParameterCell" bundle:nil];
// register this NIB
[[self tableView] registerNib:nib forCellReuseIdentifier:@"CustomParameterCell"];
switch ([self currentSelection]) {
case heartSelection:
[[MetasomeParameterStore sharedStore] setCurrentList:[[[[MetasomeParameterStore sharedStore] parameterArray] objectAtIndex:0] valueForKey:@"list"]];
break;
case lungSelection:
[[MetasomeParameterStore sharedStore] setCurrentList:[[[[MetasomeParameterStore sharedStore] parameterArray] objectAtIndex:1] valueForKey:@"list"]];
break;
case diabetesSelection:
[[MetasomeParameterStore sharedStore] setCurrentList:[[[[MetasomeParameterStore sharedStore] parameterArray] objectAtIndex:2] valueForKey:@"list"]];
break;
case customSelection:
[[MetasomeParameterStore sharedStore] setCurrentList:[[[[MetasomeParameterStore sharedStore] parameterArray] objectAtIndex:3] valueForKey:@"list"]];
break;
default:
[[MetasomeParameterStore sharedStore] setCurrentList:[[MetasomeParameterStore sharedStore] heartList]];
//[[MetasomeParameterStore sharedStore] setCurrentList:[[[[MetasomeParameterStore sharedStore] parameterArray] objectAtIndex:0] valueForKey:@"list"]];
}
[[self tableView] setDelegate:self];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Index of MetasomeParameterStore.sharedStore.parameterArray
// and Contents
// 0 -- vitalsArrayDict
// 1 -- mindArrayDict
// 2 -- symptomsArrayDict
// The indices above correspond to UITableView section numbers
if ( editingStyle == UITableViewCellEditingStyleDelete ) {
[[[[[MetasomeParameterStore sharedStore] parameterArray] objectAtIndex:[self currentSelection]] valueForKey:@"list"] removeObjectAtIndex:indexPath.row];
//[[[MetasomeParameterStore sharedStore] currentList] removeObjectAtIndex:indexPath.row];
[[MetasomeParameterStore sharedStore] saveChanges];
[[self tableView] reloadData];
//[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
if (sourceIndexPath.row == destinationIndexPath.row)
return;
MetasomeParameter *p = [[[[[MetasomeParameterStore sharedStore] parameterArray] objectAtIndex:[self currentSelection]] valueForKey:@"list"] objectAtIndex:sourceIndexPath.row];
[[[[[MetasomeParameterStore sharedStore] parameterArray] objectAtIndex:[self currentSelection]] valueForKey:@"list"] removeObjectAtIndex:sourceIndexPath.row];
[[[[[MetasomeParameterStore sharedStore] parameterArray] objectAtIndex:[self currentSelection]] valueForKey:@"list"] insertObject:p atIndex:destinationIndexPath.row];
[[MetasomeParameterStore sharedStore] saveChanges];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//return [[[MetasomeParameterStore sharedStore] sections] count];
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *CellIdentifier = @"UITableViewCell";
static NSString *CellIdentifier = @"CustomParameterCell";
CustomParameterCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
MetasomeParameter *p = [[[[[MetasomeParameterStore sharedStore] parameterArray] objectAtIndex:[self currentSelection]] valueForKey:@"list"] objectAtIndex:indexPath.row];
NSString *cellValue = [p parameterName];
//[[cell textLabel] setText:cellValue];
[[cell titleLabel] setText:cellValue];
[[cell titleLabel] setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]];
[[cell gamificationImage] setImage:[self getGamificationImage:p]];
//cell.textLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
// Check if it's time to reset the checkmark each time the cell loads
[p resetCheckmark];
// highlight color when cell is selected
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
if ([p checkedStatus] == YES) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
if ([p checkedStatus] == NO) {
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
// color Fitbit parameters blue
if ([p apiType] == FitbitInput) {
cell.titleLabel.textColor = [UIColor blueColor];
} else if ([p apiType] == WithingsInput) {
//cell.titleLabel.textColor = [UIColor orangeColor];
} else {
cell.titleLabel.textColor = [UIColor blackColor];
}
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//return [[[MetasomeParameterStore sharedStore] currentList] count];
return [[[[[MetasomeParameterStore sharedStore] parameterArray] objectAtIndex:[self currentSelection]] valueForKey:@"list"] count];
}
/*
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath
{
// Don't allow changing parameters' categories
if ( [sourceIndexPath section] != [destinationIndexPath section] )
return;
[[MetasomeParameterStore sharedStore] moveItemAtIndex:[sourceIndexPath row] toIndex:[destinationIndexPath row] inSection:[sourceIndexPath section]];
[[MetasomeParameterStore sharedStore] saveChanges];
}
*/
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
NSInteger row = 0;
if (sourceIndexPath.section < proposedDestinationIndexPath.section) {
row = [tableView numberOfRowsInSection:sourceIndexPath.section] - 1;
}
return [NSIndexPath indexPathForRow:row inSection:sourceIndexPath.section];
}
return proposedDestinationIndexPath;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController;
DetailViewController_slider *detailViewController_slider;
MetasomeParameter *p = [[[[[MetasomeParameterStore sharedStore] parameterArray] objectAtIndex:[self currentSelection]] valueForKey:@"list"] objectAtIndex:indexPath.row];
// Steps and Distance data can only be entered via Fitibit integration
/*
if ([[p parameterName] isEqualToString:@"Distance"] || [[p parameterName] isEqualToString:@"Steps"]) {
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Sync your wearable device to import %@ data.", [p parameterName]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
[tableView reloadData];
return;
}
*/
// Create block to check for presence of at least one data point
BOOL (^block)(void) = ^ {
NSArray *points = [[MetasomeDataPointStore sharedStore] allPoints];
int counter = 0;
for (MetasomeDataPoint *dp in points) {
if ( [[dp parameterName] isEqualToString:[p parameterName]] )
counter +=1;
}
if (counter >0) {
[[MetasomeDataPointStore sharedStore] setParameterToGraph:p];
return YES;
}
else {
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error" message:(@"No data points entered yet!") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
return NO;
}
};
switch ([p inputType]) {
case ParameterInputSlider:
detailViewController_slider = [[DetailViewController_slider alloc] init];
[detailViewController_slider setIsDataPointStoreNonEmpty:block];
[detailViewController_slider setParameter:p];
[[self navigationController] pushViewController:detailViewController_slider animated:YES];
break;
case ParameterBloodPressure:
{
MetasomeBloodPressureViewController *bpvc = [[MetasomeBloodPressureViewController alloc] init];
[bpvc setIsDataPointStoreNonEmpty:block];
[bpvc setParameter:p];
[[self navigationController] pushViewController:bpvc animated:YES];
break;
}
default:
detailViewController = [[DetailViewController alloc] init];
[detailViewController setIsDataPointStoreNonEmpty:block];
[detailViewController setParameter:p];
[[self navigationController] pushViewController:detailViewController animated:YES];
break;
}
}
-(void)updateTitle:(UILabel *)label
{
switch ([self currentSelection]) {
case heartSelection:
[self setTitleString:@"Heart"];
break;
case lungSelection:
[self setTitleString:@"Lung"];
break;
case diabetesSelection:
[self setTitleString:@"Metabolic"];
break;
case customSelection:
[self setTitleString:@"Mind"];
break;
}
[TextFormatter formatTitleLabel:label withTitle:[self titleString]];
[[self navigationItem] setTitleView:label];
}
-(UIImage *)getGamificationImage:(MetasomeParameter *)parameter
{
int numConsecutive = [parameter consecutiveEntries];
UIImage *imageToReturn;
if (numConsecutive == 1) { // first entry
imageToReturn = [UIImage imageNamed:@"single_apple"];
}
else if (numConsecutive == 2) {
imageToReturn = [UIImage imageNamed:@"double_apple"];
}
else if (numConsecutive == 3) {
imageToReturn = [UIImage imageNamed:@"triple_apple"];
}
else if (numConsecutive == 4) {
imageToReturn = [UIImage imageNamed:@"quadruple_apple"];
}
else if (numConsecutive >= 5 && numConsecutive <7 ) {
imageToReturn = [UIImage imageNamed:@"apple_bushel"];
}
else if (numConsecutive >= 7 && numConsecutive < 9) {
imageToReturn = [UIImage imageNamed:@"apple_bin"];
}
else if (numConsecutive >= 9 && numConsecutive < 11) {
imageToReturn = [UIImage imageNamed:@"simple_tree"];
}
else if (numConsecutive >= 11 && numConsecutive < 15) {
imageToReturn = [UIImage imageNamed:@"full_apple_tree"];
}
else if (numConsecutive >= 15 ) {
imageToReturn = [UIImage imageNamed:@"golden_apple"];
}
// When data is pulled from an API, the MetasomeParameter's
// consecutiveEntries field is set to -1
/*
if ( numConsecutive < 0 && [parameter apiType] == FitbitInput ) {
imageToReturn = [UIImage imageNamed:@"fitbit_integrated"];
}
else if ( numConsecutive < 0 && [parameter apiType] == WithingsInput ) {
imageToReturn = [UIImage imageNamed:@"withings_integrated"];
}
*/
if ( [parameter apiType] == FitbitInput ) {
imageToReturn = [UIImage imageNamed:@"fitbit_integrated"];
} else if ( [parameter apiType] == WithingsInput ) {
imageToReturn = [UIImage imageNamed:@"withings_integrated"];
}
return imageToReturn;
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
NSLog(@"setEditing");
if (editing == NO) {
[[self tableView] reloadData];
return;
}
NSArray *visibleCells = [[self tableView] visibleCells];
for (CustomParameterCell *cpc in visibleCells) {
[[cpc gamificationImage] setImage:nil];
}
}
-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"entering edit mode");
}
@end