ChatEditButton Implementation

in ChatItemCell.h

 

@class CVCommentItem;

 

 

@protocol CVChatItemCellDelegate <NSObject>

 

@required

– (void)didEditComment:(Comment *)comment;

 

@required

– (void)bubbleTouchedAtCell:(UITableViewCell *)cell;

 

@optional

– (void)inlineImage:(DTLazyImageView *)image didFinishTouchInCell:(UITableViewCell *)cell;

 

@end

 

 

@interface CVChatItemCell : CVTableViewCell <DTLazyImageViewDelegate, CVLazyImageContentViewActionDelegate>

 

@property(nonatomic, retain) NSString* commentKey;

@property(nonatomic, retain) UIView* bgView;

@property(nonatomic, retain) CVPlayVoiceView* playVoiceView;

@property(nonatomic, retain) TTImageView* mediaCommentView;

@property(nonatomic, retain) CVCommentItem* item;

@property(nonatomic, unsafe_unretained) id<CVCommentItemCellDelegate> delegate;

@property(nonatomic, unsafe_unretained) id<CVChatItemCellDelegate> chatDelegate;

@property(nonatomic, unsafe_unretained) id<CVLazyImageContentViewDelegate> lazyImageContentViewDelegate;

 

@end

in ChatItemCell.m

– (void)editMenuTouched:(UIMenuController*)menuController {

    Comment* comment = [Comment commentWithUniqueId:_commentKey inManagedObjectContext:[[DataStore sharedDataStore] managedObjectContext]];

    

    if (_chatDelegate && [_chatDelegate respondsToSelector:@selector(didEditComment:)])

        [_chatDelegate didEditComment:comment];

}

 

 in chatViewController.m

 

@property(nonatomic, retain) UITextView* textView;

@property(nonatomic, assign) BOOL isEditComment;

@property(nonatomic, retain) NSString* commentKey;

 

    _textView = [[UITextView alloc] initWithFrame:CGRectMake (CHAT_BOX_PADDING, CHAT_BOX_PADDING,CHAT_BOX_WIDTH,self.bottomBar.height – CHAT_BOX_PADDING * 2)];

    _textView.layer.cornerRadius = 5;

#pragma mark –

#pragma mark UITextViewDelegate

 

– (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;

{

    if ([text isEqualToString:@”\n”]) {

        [textView resignFirstResponder];

        

        if (_isEditComment) {

            NSString* editCommonWithHTMLFormat = @””;

            NSString* editCommon = textView.text;

            NSArray* splitEditCommentIntoEachLine = [editCommon componentsSeparatedByString: @”\n”];

            NSMutableString* eachLineContent = [[NSMutableString alloc] init];

            NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:

                                     //[NSNumber numberWithFloat:1.23], NSTextSizeMultiplierDocumentOption, // default font size 12pt

                                     //[NSValue valueWithCGSize:maxImageSize], DTMaxImageSize,

                                     @”Helvetica”, DTDefaultFontFamily,

                                     @”darkblue”, DTDefaultLinkColor,

                                     nil];

            

 

            for (eachLineContent in splitEditCommentIntoEachLine) {

 

                NSData* htmlData = [eachLineContent dataUsingEncoding:NSUTF8StringEncoding];

                DTRichTextEditorView* temp = [[DTRichTextEditorView alloc] init];

                [temp setAttributedText:[[NSAttributedString alloc] initWithHTMLData:htmlData options:options documentAttributes:nil]];

                editCommonWithHTMLFormat = [NSString stringWithFormat:@”%@%@%@”, editCommonWithHTMLFormat, [temp.attributedText htmlFragment], @”\n”];

            }

 

            _textView.text = editCommonWithHTMLFormat;

            [self postEditComment];

            _isEditComment = NO;

 

        } else {

            [self post];

        }

        return NO;

    }

    return YES;

}

 

– (void)postEditComment{    

   

    // display activity indicator

    [self startActivityIndicator];

    

    [_activityIndicator startAnimating];

    

    Comment* comment = [Comment commentWithUniqueId:_commentKey inManagedObjectContext:[[DataStore sharedDataStore] managedObjectContext]];

    comment.text = _textView.text;

    comment.timeEdited = [[NSDate date] timeIntervalSince1970];

    comment.creationTime = comment.timeEdited;

    comment.inWhichTask = [Task taskWithUniqueId:self.key inManagedObjectContext:[[DataStore sharedDataStore] managedObjectContext]];

    [self postNewComment:comment];

 

    _textView.text=@””;

}

 

– (void)didEditComment:(Comment *)comment {

    NSRange range = [comment.text rangeOfString:@”</p>”];

    

    // Comment typed by comment page(with HTML format)

    if (range.length > 0){

        NSString* commentShown = [[NSString alloc] init];

        NSArray* splitEachLineInComment = [comment.text componentsSeparatedByString: @”</p>”];

        for (int index = 0; index < [splitEachLineInComment count]-1; index++){

            NSRange rangeOfEachLine = [[splitEachLineInComment objectAtIndex:index] rangeOfString:@”>”];

            NSString *EachLineContentWithoutHTMLFormat = [[splitEachLineInComment objectAtIndex:index] substringFromIndex:rangeOfEachLine.location+1];

            commentShown = [commentShown stringByAppendingString:EachLineContentWithoutHTMLFormat];

            commentShown = [commentShown stringByAppendingString:@”\n”];

//            NSLog(@”substring: ‘%@'”, substring);

        }

        _textView.text = commentShown;

    } else {

        _textView.text = comment.text;

    }

    

    _isEditComment = YES;

    _commentKey = comment.commentKey;

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s