-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainTableViewController.m
530 lines (401 loc) · 17 KB
/
MainTableViewController.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//
// MainTableViewController.m
// TrainingMonitoring
//
// Created by Nicole Lehrer on 3/25/13.
// Copyright (c) 2013 Nicole Lehrer. All rights reserved.
//
#import "MainTableViewController.h"
#import "DataController.h"
#import "CaptureTaskViewController.h"
#import "SurveyViewController.h"
#import "StoryViewController.h"
#import "SegmentTableViewController.h"
#import "StorySet.h"
#import "SurveyAnswer.h"
#import "Trial.h"
@interface MainTableViewController ()
@property (nonatomic, retain) DataController * dataController;
@property (nonatomic, retain) NSArray * sessionSteps;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *readyToEditStoryBarButton;
@end
@implementation MainTableViewController
@synthesize sessionID = _sessionID;
@synthesize subjectID = _subjectID;
@synthesize dataController = _dataController;
@synthesize sessionSteps = _sessionSteps;
@synthesize managedObjectContext = _managedObjectContext;
@synthesize readyToEditStoryBarButton = _readyToEditStoryBarButton;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkIfTaskMonitoringDone:)
name:@"taskMonitoringDone"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkIfSurveyDone:)
name:@"surveyDone"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkIfStoryDone:)
name:@"storyDone"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkIfSegmentDone:)
name:@"segmentDone"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkIfSharedStoryDone:)
name:@"sharedStoryDone"
object:nil];
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
self.dataController = [[DataController alloc] init];
self.dataController.managedObjectContext = self.managedObjectContext;
//if first session, no video story yet
if (self.sessionID==1) {
self.sessionSteps = [NSArray arrayWithObjects:
@"1. Administer training monitoring tasks",
@"2. Administer system experience questionnaire",
@"3. Segment Videos",
@"4. Create story for next week", nil];
}
else{
self.sessionSteps = [NSArray arrayWithObjects:
@"1. Administer training monitoring tasks",
@"2. Administer system experience questionnaire",
@"3. Share videos from previous sessions",
@"4. Segment videos from this session",
@"5. Edit shared videos for next week", nil];
}
[self makeCustomToolBarTitleWithString:self.title];
}
-(void)makeCustomToolBarTitleWithString:(NSString*)titleString
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 600, 44)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:28];
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentCenter;
label.text = titleString;
self.navigationItem.titleView = label;
}
- (void) checkIfTaskMonitoringDone:(NSNotification *) notification
{
[self.tableView reloadData];
}
- (void) checkIfSurveyDone:(NSNotification *) notification
{
[self.tableView reloadData];
}
- (void) checkIfSegmentDone:(NSNotification *) notification
{
[self.tableView reloadData];
}
- (void) checkIfStoryDone:(NSNotification *) notification
{
[self.tableView reloadData];
}
- (void) checkIfSharedStoryDone:(NSNotification *) notification
{
[self.tableView reloadData];
}
-(BOOL)areAllTrialsSegmentedForTaskWithDescriptor:(NSString*)taskDescriptor forSession:(int)sessionID
{
TaskType * task = [self.dataController fetchTaskWithDescriptor:taskDescriptor forSubject:self.subjectID fromSession:sessionID];
int numComplete = 0;
int i;
for (i=0; i<[task.numTrials intValue]; i++){
Trial * trial = [self.dataController fetchTrialWithIndex:i+1 forTaskType:taskDescriptor forSubject:self.subjectID fromSession:sessionID];
if ([trial.fineStartTime floatValue] != -1 && [trial.fineStopTime floatValue] != -1) {
numComplete++;
}
}
// NSLog(@"numComplete for %@ = %i", taskDescriptor, numComplete);
if (numComplete==[task.numTrials intValue]) {
return YES;
}
else{
return NO;
}
}
-(void)updateImageForCell:(int)indexRowPath
{
NSLog(@" suvey bool is true");
NSArray *visible = [self.tableView indexPathsForVisibleRows];
NSIndexPath *indexpath = (NSIndexPath*)[visible objectAtIndex:indexRowPath];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexpath];
cell.imageView.image = [UIImage imageNamed:@"Checkmark.png"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.sessionSteps count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = [self.sessionSteps objectAtIndex:indexPath.row];
int segmentingRow;
int storyEditRow;
BOOL thereIsStoryToShow = NO;
if (self.sessionID==1) {
segmentingRow=2;
storyEditRow=3;
}
else{
segmentingRow=3;
storyEditRow=4;
thereIsStoryToShow = YES;
}
if ((indexPath.row == 0 && [self checkIfTaskMonitoringComplete]) ||
(indexPath.row == 1 && [self checkIfAllSurveyComplete]) ||
(indexPath.row == segmentingRow && [self checkIfSegmentIsCompleteForSession:self.sessionID]) ||
(indexPath.row == storyEditRow && [self checkIfStoryEditComplete]) ||
(indexPath.row == 2 && thereIsStoryToShow && [self checkIfStorySharedComplete])) {
cell.imageView.image = [UIImage imageNamed:@"Checkmark.png"];
NSLog(@"index path is %i", indexPath.row);
}
else{
cell.imageView.image = nil;
}
if (indexPath.row == [self.sessionSteps count]-1){
if ([self checkIfSegmentIsCompleteForSession:self.sessionID]){
cell.userInteractionEnabled = YES;
cell.textLabel.textColor = [UIColor blackColor];
}else{
cell.userInteractionEnabled = NO;
cell.textLabel.textColor = [UIColor grayColor];
}
}
if (indexPath.row == [self.sessionSteps count]-2){
if ([self checkIfTaskMonitoringComplete]){
cell.userInteractionEnabled = YES;
cell.textLabel.textColor = [UIColor blackColor];
}else{
cell.userInteractionEnabled = NO;
cell.textLabel.textColor = [UIColor grayColor];
}
}
if (self.sessionID > 1 && indexPath.row == [self.sessionSteps count]-3){
if ([self checkIfSegmentIsCompleteForSession:self.sessionID-1]){
cell.userInteractionEnabled = YES;
cell.textLabel.textColor = [UIColor blackColor];
}else{
cell.userInteractionEnabled = NO;
cell.textLabel.textColor = [UIColor grayColor];
}
}
return cell;
}
-(BOOL)checkIfAllSurveyComplete
{
BOOL allMCAnswered = NO;
BOOL lastQuestionAnswered = NO;
Session* session = [self.dataController fetchSessionNumber:self.sessionID forSubject:self.subjectID];
int i;
for (i=0; i<[session.surveyAnswer count]; i++) {
SurveyAnswer * sa = [self.dataController fetchSurveyAnswerWithIndex:i+1 forSubject:self.subjectID fromSession:self.sessionID];
if (i<[session.surveyAnswer count]-1) {
// NSLog(@"question %i answer is %i", i+1, [sa.answerValue intValue]);
if ([sa.answerValue intValue]== -1) {
allMCAnswered = NO;
}
else{
allMCAnswered = YES;
}
}
if(i==[session.surveyAnswer count]-1){
if ([sa.followUp isEqualToString:@"Type annotation text here"] || [sa.followUp isEqualToString:@""]) {
// NSLog(@"no follow up on last question");
lastQuestionAnswered = NO;
}
else{
lastQuestionAnswered = YES;
}
}
}
if (lastQuestionAnswered && allMCAnswered) {
NSLog(@"yes");
return YES;
}
else{
NSLog(@"no");
return NO;
}
}
-(BOOL)checkIfStoryEditComplete
{
Session* session = [self.dataController fetchSessionNumber:self.sessionID forSubject:self.subjectID];
return [session.finishedStory boolValue];
}
-(BOOL)checkIfStorySharedComplete
{
Session* session = [self.dataController fetchSessionNumber:self.sessionID forSubject:self.subjectID];
return [session.sharedStory boolValue];
}
-(BOOL)checkIfTaskMonitoringComplete
{
TaskType * task1 = [self.dataController fetchTaskWithOrder:1 forSubject:self.subjectID fromSession:self.sessionID];
TaskType * task2 = [self.dataController fetchTaskWithOrder:2 forSubject:self.subjectID fromSession:self.sessionID];
TaskType * task3 = [self.dataController fetchTaskWithOrder:3 forSubject:self.subjectID fromSession:self.sessionID];
if (task1.movieURLString && task2.movieURLString && task3.movieURLString){
return YES;
}
else{
return NO;
}
}
-(BOOL)checkIfSegmentIsCompleteForSession:(int)sessionID
{
if ([self areAllTrialsSegmentedForTaskWithDescriptor:@"graspCone" forSession:sessionID]
&& [self areAllTrialsSegmentedForTaskWithDescriptor:@"elevatedTouch" forSession:sessionID]
&& [self areAllTrialsSegmentedForTaskWithDescriptor:@"transport" forSession:sessionID]){
return YES;
}
else{
return NO;
}
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//not passing self.managedObjectContext; getting directly from appDelegate
if ([[segue identifier] isEqualToString:@"CaptureTask"]) {
CaptureTaskViewController * captureVC = segue.destinationViewController;
captureVC.subjectID = self.subjectID;
captureVC.sessionID = self.sessionID;
captureVC.currentTaskType = 1;
}
if ([[segue identifier] isEqualToString:@"Survey"]) {
SurveyViewController * surveyVC = segue.destinationViewController;
surveyVC.subjectID = self.subjectID;
surveyVC.sessionID = self.sessionID;
}
if ([[segue identifier] isEqualToString:@"Segment"]) {
SegmentTableViewController * segVC = segue.destinationViewController;
segVC.subjectID = self.subjectID;
segVC.sessionID = self.sessionID;
// [self.dataController resetSegmentingTimesFor:1 forTaskType:@"graspCone" inSession:2];
}
if ([[segue identifier] isEqualToString:@"CreateStory"]) {
StoryViewController * storyVC = segue.destinationViewController;
storyVC.subjectID = self.subjectID;
storyVC.sessionID = self.sessionID;
if (![self.dataController checkIfDataExistsForSubjectID:self.subjectID ForCurrentSessionID:self.sessionID]) {
NSLog(@"saveInitialStorySetForSession is called");
storyVC.updateFromPlist = NO;
[self.dataController writeInitialBookmarkedDescriptorsToPlistForSession:self.sessionID ForSubject:self.subjectID];
}//also calling this in viewWillDisapper inside segmentTableViewController so that if they go back and edit videos it will resave plist - this will in effect delete any edits that were made in storyViewTable
}
if ([[segue identifier] isEqualToString:@"Progress"]) {
StoryViewController * storyVC = segue.destinationViewController;
storyVC.subjectID = self.subjectID;
storyVC.sessionID = self.sessionID;
if (![self.dataController checkIfDataExistsForSubjectID:self.subjectID ForCurrentSessionID:self.sessionID]) {
NSLog(@"saveInitialStorySetForSession is called");
storyVC.updateFromPlist = NO;
// [self.dataController writeInitialBookmarkedDescriptorsToPlistForSession:self.sessionID ForSubject:self.subjectID];
}//also calling this in viewWillDisapper inside segmentTableViewController so that if they go back and edit videos it will resave plist - this will in effect delete any edits that were made in storyViewTable
}
}
-(BOOL)returnStoryRecordForSubject:(int)subjectID forSession:(int)sessionID
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"StoryEditRecord.plist"]; //3
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSString * subjectKey = [@"Subject" stringByAppendingFormat:@"%i", subjectID];
NSNumber* boolNumber = [[data objectForKey:subjectKey] objectAtIndex:sessionID-1];
// NSLog(@"from returnStoryRecord %@", [boolNumber boolValue] ? @"YES":@"NO");
return [boolNumber boolValue];
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
[self performSegueWithIdentifier:@"CaptureTask" sender:indexPath];
}
if (indexPath.row == 1) {
[self performSegueWithIdentifier:@"Survey" sender:indexPath];
}
if (self.sessionID==1) {
if (indexPath.row == 2) {
[self performSegueWithIdentifier:@"Segment" sender:indexPath];
}
if (indexPath.row == 3) {
[self performSegueWithIdentifier:@"CreateStory" sender:indexPath];
}
}
else{
if (indexPath.row == 2) {
[self performSegueWithIdentifier:@"Progress" sender:indexPath];
}
if (indexPath.row == 3) {
[self performSegueWithIdentifier:@"Segment" sender:indexPath];
}
if (indexPath.row == 4) {
[self performSegueWithIdentifier:@"CreateStory" sender:indexPath];
}
}
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] postNotificationName:@"percentDone" object:nil];
}
@end