Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // AuditCommentsController.m
- // Roma
- //
- // Created by Brian Seelig on 6/20/11.
- // Copyright 2011 RW3 Inc. All rights reserved.
- //
- #import <QuartzCore/QuartzCore.h>
- #import "CallCommentsController.h"
- #import "CarbonAppDelegate.h"
- #import "PHCommentCell.h"
- #import "MBProgressHUD.h"
- @implementation CallCommentsController
- {
- UIFont *_defaultViewFont;
- Store *_store;
- }
- @synthesize call;
- @synthesize store;
- @synthesize textViewComments;
- @synthesize commentsHistory;
- @synthesize tableViewCommentsHistory;
- @synthesize commentsCellHeight;
- @synthesize gestureRecognizer;
- @synthesize navController;
- @synthesize labelCallComments;
- @synthesize captureAuditCommentButton;
- const NSInteger CommentMaxLength = 500;
- const CGFloat _defaultFontSize = 14.0f;
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self)
- {
- _defaultViewFont = [UIFont systemFontOfSize:_defaultFontSize];
- }
- return self;
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- LogMemory();
- [[NSNotificationCenter defaultCenter] removeObserver: self];
- [Logger reportMemory];
- }
- - (bool)hasData {
- if(call.comment.length > 0 ||
- [[DataManager sharedInstance] getNumberOfCallCommentImagesWithStoreID:call.storeID auditID:call.auditID callID:nil] > 0 ||
- [[DataManager sharedInstance] getAllCallCommentsHistoryForStoreID:call.storeID auditID:call.auditID].count > 0) {
- return YES;
- }
- return NO;
- }
- #pragma mark - View lifecycle
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- LogMemory();
- if([ClientType isEqualToString:ClientTypeMA])
- {
- labelCallComments.text = NSLocalizedString(@"Audit Comments:", nil);
- }
- else
- {
- labelCallComments.text = NSLocalizedString(@"Call Comments:", nil);
- }
- [textViewComments setText: call.comment];
- [textViewComments.layer setBackgroundColor: [[UIColor whiteColor] CGColor]];
- [textViewComments.layer setBorderColor: [[UIColor grayColor] CGColor]];
- [textViewComments.layer setBorderWidth: 1.0];
- [textViewComments.layer setCornerRadius:8.0f];
- [textViewComments.layer setMasksToBounds:YES];
- //iPhone 4S
- if (IS_IPHONE && (self.navController.view.frame.size.height == 480)) {
- textViewComments.autocorrectionType = UITextAutocorrectionTypeNo;
- }
- gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)];
- [self.view addGestureRecognizer:gestureRecognizer];
- self.commentsHistory = [[DataManager sharedInstance] getAllCallCommentsHistoryForStoreID:call.storeID auditID:call.auditID];
- if (IS_IPHONE) {
- self.tableViewCommentsHistory.rowHeight = UITableViewAutomaticDimension;
- self.tableViewCommentsHistory.estimatedRowHeight = 44;
- }
- }
- - (void)viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- NSInteger numberOfImages = [[DataManager sharedInstance] getNumberOfCallCommentImagesWithStoreID:call.storeID auditID:call.auditID callID:nil];
- if (IS_IPAD) {
- [captureAuditCommentButton setContentMode: UIViewContentModeLeft];
- [captureAuditCommentButton setImage:[UIImage imageNamed:@"Camera_Button"] forState:UIControlStateNormal];
- [captureAuditCommentButton addTarget:self action:@selector(tappedAuditCommentCaptureButton:)
- forControlEvents:UIControlEventTouchDown];
- captureAuditCommentButton.tag = -1;
- if(numberOfImages != 0)
- {
- CustomBadge *customBudge = auditCommentCustomBadge;
- if (customBudge == nil) {
- customBudge = [CustomBadge customBadgeWithString:[NSString stringWithFormat:@"%zd", numberOfImages]
- withStringColor:[UIColor whiteColor]
- withInsetColor:[UIColor blueColor]
- withBadgeFrame:NO
- withBadgeFrameColor:[UIColor grayColor]
- withScale:0.9
- withShining:YES];
- CGSize badgeSize = customBudge.frame.size;
- [customBudge setFrame:CGRectMake(44-badgeSize.width, MAX(badgeSize.height, 44.0f) / 2 - 22, badgeSize.width, badgeSize.height)];
- [captureAuditCommentButton addSubview:customBudge];
- [customBudge setTag: -1];
- [customBudge setDelegate:self];
- auditCommentCustomBadge = customBudge;
- } else {
- [customBudge autoBadgeSizeWithString:[NSString stringWithFormat:@"%zd", numberOfImages]];
- CGSize badgeSize = customBudge.frame.size;
- [customBudge setFrame:CGRectMake(44-badgeSize.width, MAX(badgeSize.height, 44.0f) / 2 - 22, badgeSize.width, badgeSize.height)];
- }
- }
- } else { // Phone
- if (numberOfImages != 0) {
- self.auditCommentBadgeLabel.text = [NSString stringWithFormat:@"%zd", numberOfImages];
- self.auditCommentBadgeLabel.hidden = NO;
- } else {
- self.auditCommentBadgeLabel.hidden = YES;
- }
- [tableViewCommentsHistory reloadData];
- }
- [Logger reportMemory];
- [tableViewCommentsHistory performSelector:@selector(flashScrollIndicators) withObject:nil afterDelay:0.75f];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(keyboardWillShown:)
- name:UIKeyboardWillShowNotification object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(keyboardWillBeHidden:)
- name:UIKeyboardWillHideNotification object:nil];
- }
- - (void)viewDidDisappear:(BOOL)animated
- {
- [super viewDidDisappear:animated];
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- #pragma mark Keyboard and TextView
- - (void)dismissKeyboard:(UIGestureRecognizer *)sender
- {
- [textViewComments resignFirstResponder];
- CGPoint tappedPoint = [sender locationInView:self.view];
- UIView *tappedView = [self.view hitTest:tappedPoint withEvent:nil];
- if([tappedView isKindOfClass:UIButton.class] && ((UIButton*)tappedView).tag != -1) {
- [((UIButton*)tappedView) sendActionsForControlEvents:UIControlEventTouchUpInside];
- } else if([tappedView isKindOfClass:CustomBadge.class] && ((CustomBadge*)tappedView).tag != -1) {
- [self loadAuditCommentHistoryCapture:self indexRow:((CustomBadge*)tappedView).tag];
- }
- }
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
- {
- if([text length] == 0)
- {
- if([textView.text length] != 0)
- {
- return YES;
- }
- else
- {
- return NO;
- }
- }
- else if([[textView text] length] > CommentMaxLength)
- {
- return NO;
- }
- return YES;
- }
- - (BOOL)textViewShouldEndEditing:(UITextView *)textView {
- if([CarbonAppDelegate sharedInstance].isDiscardDataInProgress == NO) {
- [self saveAuditComments];
- }
- return YES;
- }
- - (void)saveAuditComments {
- call.comment = textViewComments.text;
- [[DataManager sharedInstance] save];
- }
- #pragma mark Take Photo (view mode)
- - (void)loadAuditCommentHistoryCapture:(id)sender indexRow:(NSInteger)indexRow
- {
- [MBProgressHUD showHUDAddedTo:self.navController.view animated:YES]; // wil be removed at TakePhotoController
- CallComment *auditComment = (CallComment *) [self.commentsHistory objectAtIndex:indexRow];
- StoreTakePhotoViewController *imageCapture = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"StoreTakePhotoViewController"];
- [imageCapture setImageType:AuditCommentHistoryImages];
- [imageCapture setCall:call];
- [imageCapture setAuditCallId:auditComment.callID];
- [imageCapture setAuditId:auditComment.call.auditID];
- [imageCapture setStore:store];
- [imageCapture setStoreQuestion:nil];
- [imageCapture setCategory:nil];
- [imageCapture setSubcategory:nil];
- dispatch_async(dispatch_get_main_queue(), ^{ // async is needed to show spinner
- [self->navController pushViewController:imageCapture animated:YES];
- });
- }
- - (IBAction)tappedAuditCommentCaptureButton:(id)sender
- {
- [textViewComments resignFirstResponder];
- [MBProgressHUD showHUDAddedTo:self.navController.view animated:YES]; // wil be removed at TakePhotoController
- StoreTakePhotoViewController *imageCapture = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"StoreTakePhotoViewController"];
- [imageCapture setImageType:AuditCommentImages];
- [imageCapture setCall:call];
- [imageCapture setAuditId:call.auditID];
- [imageCapture setStore:store];
- [imageCapture setAuditCallId:nil];
- [imageCapture setStoreQuestion:nil];
- [imageCapture setCategory:nil];
- [imageCapture setSubcategory:nil];
- dispatch_async(dispatch_get_main_queue(), ^{ // async is needed to show spinner
- [self->navController pushViewController:imageCapture animated:YES];
- });
- }
- - (void)didTapWithTagValue:(NSInteger)tagValue
- {
- [self loadAuditCommentHistoryCapture:self indexRow:tagValue];
- }
- - (void)didTap
- {
- [self tappedAuditCommentCaptureButton:self];
- }
- -(IBAction)tappedAuditCommentHistoryButton:(id)sender {
- LogNormal();
- if (IS_IPAD) {
- NSInteger tagValue = ((UIButton*)sender).tag;
- [self loadAuditCommentHistoryCapture:self indexRow:tagValue];
- } else { // Phone
- CGPoint buttonPoint = [sender convertPoint:CGPointZero toView:tableViewCommentsHistory];
- NSIndexPath *indexPath = [tableViewCommentsHistory indexPathForRowAtPoint:buttonPoint];
- [self loadAuditCommentHistoryCapture:self indexRow:indexPath.row];
- }
- }
- #pragma mark Table View
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
- UILabel *label = [UILabel new];
- if([ClientType isEqualToString:ClientTypeMA]) {
- label.text = NSLocalizedString(@" Audit Comments History:", nil);
- } else {
- label.text = NSLocalizedString(@" Call Comments History:", nil);
- }
- label.font = [UIFont boldSystemFontOfSize:14];
- label.backgroundColor = [UIColor whiteColor];
- return label;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return [self.commentsHistory count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
- [formatter setDateFormat: DateFormat];
- CallComment *callComment = (CallComment*)[self.commentsHistory objectAtIndex:[indexPath row]];
- NSInteger numberOfImages = [[DataManager sharedInstance] getNumberOfCallCommentImagesWithStoreID:call.storeID auditID:call.auditID callID:callComment.callID];
- NSString *callDate = [formatter stringFromDate: callComment.callDate];
- if (IS_IPAD) {
- NSString *commentHistoryCellId = @"CallCommentHistoryCell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:commentHistoryCellId];
- if (cell == nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:commentHistoryCellId];
- }
- cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
- cell.textLabel.numberOfLines = 0;
- cell.textLabel.font = _defaultViewFont;
- NSString *text = [NSString stringWithFormat: @"%@ (%@): \n%@", callDate, callComment.person, callComment.comment];
- if(numberOfImages != 0) {
- UIButton *captureButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [captureButton setFrame:CGRectMake(0, 0, 44.0f, 44.0f)];
- [captureButton setContentMode: UIViewContentModeLeft];
- [captureButton setImage:[UIImage imageNamed:@"Camera_Button"] forState:UIControlStateNormal];
- [captureButton setUserInteractionEnabled:YES];
- [captureButton addTarget:self action:@selector(tappedAuditCommentHistoryButton:) forControlEvents:UIControlEventTouchUpInside];
- [captureButton setTag:[indexPath row]];
- CustomBadge *customBadge = [CustomBadge customBadgeWithString:@""
- withStringColor:[UIColor whiteColor]
- withInsetColor:[UIColor blueColor]
- withBadgeFrame:NO
- withBadgeFrameColor:[UIColor grayColor]
- withScale:0.9
- withShining:YES];
- [customBadge autoBadgeSizeWithString:[NSString stringWithFormat:@"%zd", numberOfImages]];
- CGRect badgeRect = customBadge.frame;
- [customBadge setFrame:CGRectMake(44-badgeRect.size.width, MAX(badgeRect.size.height, 44.0f) / 2 - 22, badgeRect.size.width, badgeRect.size.height)];
- [captureButton addSubview:customBadge];
- cell.accessoryView = captureButton;
- [customBadge setTag:[indexPath row]];
- [customBadge setDelegate:self];
- } else {
- cell.accessoryView = nil;
- }
- cell.textLabel.text = text;
- [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
- return cell;
- } else { // Phone
- PHCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PHCommentCell" forIndexPath:indexPath];
- cell.personLabel.text = callComment.person;
- cell.dateLabel.text = callDate;
- cell.commentLabel.text = callComment.comment;
- if (numberOfImages != 0) {
- cell.numberOfImagesLabel.hidden = NO;
- cell.numberOfImagesLabel.text = [NSString stringWithFormat:@"%zd", numberOfImages];
- cell.captureButton.hidden = NO;
- cell.bottomCommentConstraint.constant = 30;
- } else {
- cell.numberOfImagesLabel.hidden = YES;
- cell.numberOfImagesLabel.text = @"";
- cell.captureButton.hidden = YES;
- cell.bottomCommentConstraint.constant = 0;
- }
- return cell;
- }
- }
- - (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (IS_IPAD) {
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
- [formatter setDateFormat: DateFormat];
- CallComment *callComment = (CallComment*)[self.commentsHistory objectAtIndex:[indexPath row]];
- NSString *callDate = [formatter stringFromDate: callComment.callDate];
- NSString *text = [NSString stringWithFormat: @"%@: \n%@", callDate, callComment.comment];
- CGSize constraint = CGSizeMake(textViewComments.frame.size.width - (CELL_CONTENT_MARGIN * 2) - 60, 20000.0f);
- NSDictionary *attibutes = @{NSFontAttributeName:_defaultViewFont};
- CGSize size = [text boundingRectWithSize:constraint
- options:NSStringDrawingUsesLineFragmentOrigin
- attributes:attibutes context:nil].size;
- CGFloat height = MAX(size.height, 44.0f);
- return height + (CELL_CONTENT_MARGIN * 2);
- } else { // Phone
- return UITableViewAutomaticDimension;
- }
- }
- #pragma mark Rotation
- - (void)receivedRotate:(UIInterfaceOrientation)toInterfaceOrientation {
- if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
- commentsCellHeight.constant = 110;
- } else if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
- commentsCellHeight.constant = 136;
- }
- }
- #pragma mark - Keyboard
- - (void)keyboardWillShown:(NSNotification*)aNotification
- {
- NSDictionary* userInfo = [aNotification userInfo];
- CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
- keyboardFrame = [self.view convertRect:keyboardFrame fromView:self.view.window];
- double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
- NSInteger animationCurve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
- [UIView transitionWithView:self.navigationController.view
- duration:duration
- options:(animationCurve & UIViewAnimationOptionBeginFromCurrentState)
- animations:^
- {
- UIEdgeInsets contentInset = self.tableViewCommentsHistory.contentInset;
- contentInset.bottom = CGRectGetHeight(keyboardFrame) - 44;
- self.tableViewCommentsHistory.contentInset = contentInset;
- self.tableViewCommentsHistory.scrollIndicatorInsets = contentInset;
- }
- completion:nil];
- }
- - (void)keyboardWillBeHidden:(NSNotification*)aNotification
- {
- NSDictionary* userInfo = [aNotification userInfo];
- CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
- keyboardFrame = [self.view convertRect:keyboardFrame fromView:self.view.window];
- double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
- NSInteger animationCurve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
- [UIView transitionWithView:self.navigationController.view
- duration:duration
- options:(animationCurve & UIViewAnimationOptionBeginFromCurrentState)
- animations:^
- {
- UIEdgeInsets contentInset = self.tableViewCommentsHistory.contentInset;
- contentInset.bottom -= CGRectGetHeight(keyboardFrame);
- self.tableViewCommentsHistory.contentInset = contentInset;
- self.tableViewCommentsHistory.scrollIndicatorInsets = contentInset;
- }
- completion:nil];
- }
- #pragma mark Helper
- - (void)restoreOriginalAuditComment {
- [[DataManager sharedInstance] clearAuditCommentImagesForStoreID:call.storeID andAuditID:call.auditID];
- call.comment = call.commentOriginal;
- NSArray *auditCommentImagesOriginal = [[DataManager sharedInstance] getAllAuditCommentImagesOriginalForStoreID:call.storeID andAuditID:call.auditID andAuditCallID:nil];
- for(CallCommentImage *originalImage in auditCommentImagesOriginal)
- {
- CallCommentImage *restoredImage = [[DataManager sharedInstance] insertNewEntityOfClass:CallCommentImage.class];
- [restoredImage setStoreID:originalImage.storeID];
- [restoredImage setCallID:originalImage.callID];
- [restoredImage setAuditID:originalImage.auditID];
- [restoredImage setBackendID:originalImage.backendID];
- [restoredImage setClientID:originalImage.clientID];
- [restoredImage setImage:originalImage.image];
- [restoredImage setDateTaken:originalImage.dateTaken];
- [restoredImage setWasDeleted:originalImage.wasDeleted];
- [restoredImage setIsTodaysAudit:[NSNumber numberWithBool:YES]];
- NSArray *auditCommentImageTagsOriginal = [[DataManager sharedInstance] getAllAuditCommentImageTagsOriginalForQuestionImageClientID:originalImage.clientID orBackendID:originalImage.backendID];
- for(CallCommentImageTagOriginal *originalImageTag in auditCommentImageTagsOriginal)
- {
- CallCommentImageTag *restoredImageTag = [[DataManager sharedInstance] insertNewEntityOfClass:CallCommentImageTag.class];
- restoredImageTag.imageBackendID = originalImageTag.imageBackendID;
- restoredImageTag.imageClientID = originalImageTag.imageClientID;
- restoredImageTag.imageTagID = originalImageTag.imageTagID;
- restoredImageTag.wasDeleted = originalImageTag.wasDeleted;
- }
- }
- [[DataManager sharedInstance] save];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement