This repository was archived by the owner on Jun 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassDetailViewController.m
More file actions
496 lines (399 loc) · 19.3 KB
/
ClassDetailViewController.m
File metadata and controls
496 lines (399 loc) · 19.3 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
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
//
// ClassDetailViewController.m
// TeachNotes With Parse
//
// Created by Davis Carlson on 2014-06-08.
// Copyright (c) 2014 Davis Carlson. All rights reserved.
//
#import "ClassDetailViewController.h"
@interface ClassDetailViewController () {
BOOL _bannerIsVisible;
}
@end
@implementation ClassDetailViewController
@synthesize popoverController;
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.jpg"]];
// Do any additional setup after loading the view.
[timesTable setDelegate:self];
[timesTable setDataSource:self];
notes.layer.borderColor = [[UIColor blackColor] CGColor];
notes.layer.borderWidth = 2.0f;
lessonView.layer.borderColor = [[UIColor blackColor] CGColor];
lessonView.layer.borderWidth = 1.0f;
self.canDisplayBannerAds = YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
notes.delegate = self;
lessonPlansAvailable.hidden = YES;
self.title = self.detailClass;
notes.hidden = YES;
notesHereLabel.hidden = YES;
save.hidden = YES;
printClass.hidden = YES;
lessonViewTitle.hidden = YES;
lessonView.hidden = YES;
addNotesButton.hidden = YES;
[self getAllTimes];
startEnd.text = [NSString stringWithFormat:@"%@ - %@", self.detailStart, self.detailEnd];
teacherName.text = self.detailTeacherName;
//----------------------------------------------------------------------------------------
PFUser *user = [PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:@"Classes"];
[query getObjectInBackgroundWithId:self.detailObjectID block:^(PFObject *object, NSError *error) {
if (![object[@"primaryTeacher"] isEqualToString:user.objectId]) {
shareClass.hidden = YES;
}
}];
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
CGPoint scrollPoint = CGPointMake(0.0, 300.0);
[scrollView setContentOffset:scrollPoint animated:YES];
}
- (void)textViewDidEndEditing:(UITextView *)textView {
[scrollView setContentOffset:CGPointZero animated:YES];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
-(void)getAllTimes {
queryTimes = [PFQuery queryWithClassName:@"ClassNotes"];
[queryTimes whereKey:@"classID" equalTo:self.detailObjectID];
[queryTimes whereKey:@"deleted" equalTo:@NO];
[queryTimes findObjectsInBackgroundWithBlock:^(NSArray *objectsT, NSError *error) {
if (!error) {
objectTimesArray = [[NSMutableArray alloc] initWithArray:objectsT];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:TRUE];
[objectTimesArray sortUsingDescriptors:[NSMutableArray arrayWithObject:sortDescriptor]];
[timesTable reloadData];
if (indexOfSelectedRow) {
[timesTable selectRowAtIndexPath:indexOfSelectedRow animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
}];
}
#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 [objectTimesArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Times";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFObject *post = [objectTimesArray objectAtIndex:indexPath.row];
[cell.textLabel setText:[post objectForKey:@"date"]];
return cell;
}
-(IBAction)addNewNotes:(id)sender {
ActionSheetDatePicker *datePicker = [[ActionSheetDatePicker alloc] initWithTitle:@"Set The Class Date" datePickerMode:UIDatePickerModeDate selectedDate:[NSDate date] target:self action:@selector(addTheNewNote:) origin:sender];
[datePicker showActionSheetPicker];
}
-(void)addTheNewNote:(NSDate *)newDate {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM dd, yyyy"];
NSString *dateString = [NSString stringWithString:[dateFormat stringFromDate:newDate]];
PFObject *newNote = [PFObject objectWithClassName:@"ClassNotes"];
newNote[@"date"] = dateString;
newNote[@"userID"] = self.detailUserID;
newNote[@"classID"] = self.detailObjectID;
newNote[@"deleted"] = @NO;
[newNote saveInBackground];
[self performSelector:@selector(getTheData) withObject:self afterDelay:1.0];
[self performSelector:@selector(reloadTheData) withObject:self afterDelay:1.0];
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Hi");
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self userSelectedRow:indexPath];
PFObject *post = [objectTimesArray objectAtIndex:indexPath.row];
NSString *Objectid = [post objectId];
indexOfSelectedRow =[NSIndexPath indexPathForRow:indexPath.row inSection:0];
notes.hidden = NO;
notesHereLabel.hidden = NO;
save.hidden = NO;
//printClass.hidden = NO;
PFUser *user = [PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:@"Classes"];
[query getObjectInBackgroundWithId:self.detailObjectID block:^(PFObject *object, NSError *error) {
if ([object[@"primaryTeacher"] isEqualToString:user.objectId]) {
lessonViewTitle.hidden = NO;
lessonView.hidden = NO;
lessonViewTitle.text = @"Type Your Lesson Plans Below";
lessonView.editable = YES;
lessonView.backgroundColor = [UIColor whiteColor];
} else {
PFQuery *query = [PFQuery queryWithClassName:@"ClassNotes"];
[query getObjectInBackgroundWithId:Objectid block:^(PFObject *object, NSError *error) {
if ([object[@"lessonPlan"] isEqualToString:@""] || ![object objectForKey:@"lessonPlan"]) {
lessonView.hidden = YES;
lessonPlansAvailable.hidden = NO;
addLessonPlanButton.hidden = YES;
} else {
lessonViewTitle.hidden = NO;
lessonView.hidden = NO;
lessonViewTitle.text = @"Here Are Todays Lesson Plans";
lessonView.editable = NO;
}
}];
}
}];
save.hidden = NO;
}
- (void)userSelectedRow:(NSIndexPath *)indexPath {
PFObject *post2 = [objectTimesArray objectAtIndex:indexPath.row];
notes.text = [post2 objectForKey:@"notes"];
lessonView.text = [post2 objectForKey:@"lessonPlan"];
}
-(IBAction)saveAndSend:(id)sender {
PFQuery *query = [PFQuery queryWithClassName:@"Classes"];
[query getObjectInBackgroundWithId:self.detailObjectID block:^(PFObject *object, NSError *error) {
PFQuery *querySave = [PFQuery queryWithClassName:@"ClassNotes"];
NSIndexPath *index = timesTable.indexPathForSelectedRow;
PFObject *post = [objectTimesArray objectAtIndex:index.row];
NSString *Objectid = [post objectId];
[querySave getObjectInBackgroundWithId:Objectid block:^(PFObject *notesObject, NSError *error) {
notesObject[@"notes"] = notes.text;
notesObject[@"lessonPlan"] = lessonView.text;
[notesObject saveInBackground];
}];
[self.view endEditing:YES];
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.transform = CGAffineTransformMakeScale(1.5, 1.5);
activityIndicator.frame = CGRectMake(0.0, 0.0, 150.0, 100.0);
activityIndicator.center = self.view.center;
[activityIndicator.layer setCornerRadius:8];
activityIndicator.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8f];
[self.view addSubview:activityIndicator];
[activityIndicator startAnimating];
[self performSelector:@selector(reloadTheTable) withObject:self afterDelay:1.0];
}];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"Save"]) {
PFQuery *querySave = [PFQuery queryWithClassName:@"ClassNotes"];
NSIndexPath *index = timesTable.indexPathForSelectedRow;
PFObject *post = [objectTimesArray objectAtIndex:index.row];
NSString *Objectid = [post objectId];
[querySave getObjectInBackgroundWithId:Objectid block:^(PFObject *notesObject, NSError *error) {
notesObject[@"notes"] = notes.text;
notesObject[@"lessonPlan"] = lessonView.text;
[notesObject saveInBackground];
}];
[self.view endEditing:YES];
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.transform = CGAffineTransformMakeScale(1.5, 1.5);
activityIndicator.frame = CGRectMake(0.0, 0.0, 150.0, 100.0);
activityIndicator.center = self.view.center;
[activityIndicator.layer setCornerRadius:8];
activityIndicator.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8f];
[self.view addSubview:activityIndicator];
[activityIndicator startAnimating];
[self performSelector:@selector(reloadTheTable) withObject:self afterDelay:1.0];
}
//---------
if ([buttonTitle isEqualToString:@"Save and Send"]) {
PFQuery *querySave = [PFQuery queryWithClassName:@"ClassNotes"];
NSIndexPath *index = timesTable.indexPathForSelectedRow;
PFObject *post = [objectTimesArray objectAtIndex:index.row];
NSString *dateString = [post objectForKey:@"date"];
NSString *Objectid = [post objectId];
[querySave getObjectInBackgroundWithId:Objectid block:^(PFObject *notesObject, NSError *error) {
notesObject[@"notes"] = notes.text;
[notesObject saveInBackground];
}];
[self.view endEditing:YES];
NSString *emailTitle = [NSString stringWithFormat:@"Class Notes For %@",dateString];
// Email Content
NSString *messageBody = [NSString stringWithFormat:@"%@,\n Here was the Lesson Plan from the %@ class on %@:\n\n %@. \n\n Here are the notes for this class. \n\n %@ \n\nThank You For Using TeachNotes",self.detailTeacherName,self.detailClass,dateString,lessonView.text, notes.text];
// To address
NSArray *toRecipents = [NSArray arrayWithObject:self.detailTeacherEmail];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
[self performSelector:@selector(reloadTheTable) withObject:self afterDelay:1.0];
}
//-------------
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(void)reloadTheTable {
[self getAllTimes];
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
}
-(void)reselectRow {
[timesTable selectRowAtIndexPath:indexOfSelectedRow animated:YES scrollPosition:UITableViewScrollPositionNone];
}
-(void)reloadTheData {
[timesTable reloadData];
}
-(void)getTheData {
[self getAllTimes];
}
- (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) {
PFObject *post = [objectTimesArray objectAtIndex:indexPath.row];
NSString *Objectid = [post objectId];
PFQuery *query = [PFQuery queryWithClassName:@"ClassNotes"];
[query getObjectInBackgroundWithId:Objectid block:^(PFObject *object, NSError *error) {
object[@"deleted"] = @YES;
[object saveInBackground];
}];
[objectTimesArray removeObjectAtIndex:indexPath.row];
[timesTable deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];
[self performSelector:@selector(getTheData) withObject:self afterDelay:1.0];
}
}
- (CIImage *)createQRForString:(NSString *)qrString
{
// Need to convert the string to a UTF-8 encoded NSData object
NSData *stringData = [qrString dataUsingEncoding:NSUTF8StringEncoding];
// Create the filter
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// Set the message content and error-correction level
[qrFilter setValue:stringData forKey:@"inputMessage"];
[qrFilter setValue:@"H" forKey:@"inputCorrectionLevel"];
// Send the image back
return qrFilter.outputImage;
}
- (UIImage *)createNonInterpolatedUIImageFromCIImage:(CIImage *)image withScale:(CGFloat)scale
{
// Render the CIImage into a CGImage
CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:image fromRect:image.extent];
// Now we'll rescale using CoreGraphics
UIGraphicsBeginImageContext(CGSizeMake(image.extent.size.width * scale, image.extent.size.width * scale));
CGContextRef context = UIGraphicsGetCurrentContext();
// We don't want to interpolate (since we've got a pixel-correct image)
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
// Get the image out
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// Tidy up
UIGraphicsEndImageContext();
CGImageRelease(cgImage);
return scaledImage;
}
- (IBAction)shareTheClass:(id)sender {
NSString *message = [NSString stringWithFormat:@"Class Code:\n %@ \n\nHave the person you want to share this class with, enter this code in the \"Add A Class\" Menu or have them scan a QR Code",self.detailObjectID];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Share This Class" message:message delegate:self cancelButtonTitle:@"Done" otherButtonTitles:@"QR Code",nil];
// optional - add more buttons:
alert.tag = 1;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 1) {
if (buttonIndex == 1) {
// Disable the UI
// Get the string
NSString *stringToEncode = self.detailObjectID;
// Generate the image
CIImage *qrCode = [self createQRForString:stringToEncode];
// Convert to an UIImage
UIImage *qrCodeImg = [self createNonInterpolatedUIImageFromCIImage:qrCode withScale:4*[[UIScreen mainScreen] scale]];
qrCodeImg = [UIImage imageWithData:UIImageJPEGRepresentation(qrCodeImg, 1.0)];
CGImageRef rawImageRef=qrCodeImg.CGImage;
//RGB color range to mask (make transparent) R-Low, R-High, G-Low, G-High, B-Low, B-High
const CGFloat colorMasking[6] = {222, 255, 222, 255, 222, 255};
UIGraphicsBeginImageContext(qrCodeImg.size);
CGImageRef maskedImageRef=CGImageCreateWithMaskingColors(rawImageRef, colorMasking);
//iPhone translation
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, qrCodeImg.size.height);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, qrCodeImg.size.width, qrCodeImg.size.height), maskedImageRef);
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
CGImageRelease(maskedImageRef);
UIGraphicsEndImageContext();
UIImageView* qrcodeImageView = [[UIImageView alloc] initWithImage:result];
CustomIOS7AlertView *alertViewCustom = [[CustomIOS7AlertView alloc] init];
qrcodeImageView.backgroundColor = [UIColor clearColor];
[alertViewCustom setContainerView:qrcodeImageView];
[alertViewCustom show];
}
}
}
-(IBAction)printDetail:(id)sender {
NSIndexPath *index = timesTable.indexPathForSelectedRow;
PFObject *post = [objectTimesArray objectAtIndex:index.row];
NSString *Objectid = [post objectId];
PFQuery *query = [PFQuery queryWithClassName:@"ClassNotes"];
[query getObjectInBackgroundWithId:Objectid block:^(PFObject *object, NSError *error) {
NSString *lessonPlan = object[@"lessonPlan"];
NSString *notesText = object[@"notes"];
NSMutableString *printBody = [NSMutableString stringWithFormat:@"Lesson Plan: %@\n\nClass Notes: %@",lessonPlan, notesText];
[printBody appendFormat:@"\n\n\n\n\n\n\n\nPrinted From TeachNotes"];
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = object[@"date"];
pic.printInfo = printInfo;
UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printBody];
textFormatter.startPage = 0;
textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
textFormatter.maximumContentWidth = 6 * 72.0;
pic.printFormatter = textFormatter;
pic.showsPageRange = YES;
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if (!completed && error) {
NSLog(@"Printing could not complete because of error: %@", error);
}
};
[pic presentAnimated:YES completionHandler:completionHandler];
}];
}
@end