// // MessageCell.m // Juick // // Created by Vitaly Takmazov on 03/12/2017. // Copyright © 2017 com.juick. All rights reserved. // #import "MessageCell.h" #import "ColorScheme.h" #import "APIClient.h" #import "Entity.h" @implementation MessageCell - (void)awakeFromNib { [super awakeFromNib]; self.text.textColor = [ColorScheme textColor]; self.text.dataDetectorTypes = UIDataDetectorTypeAll; self.text.tintColor = [ColorScheme linkColor]; self.title.textColor = [ColorScheme linkColor]; self.timestamp.textColor = [UIColor grayColor]; self.summary.textColor = [UIColor grayColor]; self.tags.textFont = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]; UIGestureRecognizer *avatarTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarClicked:)]; [avatarTapRecognizer setEnabled:YES]; [self.avatar addGestureRecognizer:avatarTapRecognizer]; [self.avatar setUserInteractionEnabled:YES]; } - (void) configureWithMessage:(Message *)msg { self.message = msg; self.avatar.image = nil; __weak UIImageView *weakAvatar = self.avatar; [self.avatar setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:msg.user.avatar]] placeholderImage:nil success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) { [UIView transitionWithView:weakAvatar duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ weakAvatar.image = image; } completion:nil]; } failure:nil]; if ([msg.attach length] > 0) { CGFloat imageHeight = [msg.attachment.small.height floatValue] / [[UIScreen mainScreen] scale]; self.attachmentHeight.constant = imageHeight; __weak UIImageView *weakAttach = self.attach; [self.attach setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:msg.attach]] placeholderImage:nil success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) { [UIView transitionWithView:weakAttach duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ weakAttach.image = image; } completion:nil]; } failure:nil]; } else { self.attachmentHeight.constant = 0; self.attach.image = nil; } self.title.text = msg.user.uname; self.timestamp.text = [[[APIClient sharedClient].dateFormatter dateFromString:msg.timestamp] timeAgoSinceNow]; NSUInteger count = [msg.repliesCount unsignedIntegerValue]; if (count > 0) { if ([msg.repliesBy length] > 0) { self.summary.text = [NSString stringWithFormat:@"%@ replies by %@", msg.repliesCount, msg.repliesBy]; } else { self.summary.text = [NSString stringWithFormat:@"%@ replies", msg.repliesCount]; } } else { self.summary.text = nil; } self.text.attributedText = nil; if (msg.text) { NSMutableAttributedString *txt = [[NSMutableAttributedString alloc] initWithString:msg.text attributes:@{NSFontAttributeName:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]}]; [txt beginEditing]; for (NSDictionary *entity in msg.entities) { NSUInteger start = [entity[@"start"] unsignedIntegerValue]; NSUInteger end = [entity[@"end"] unsignedIntegerValue]; NSString *text = entity[@"text"] ? entity[@"text"] : @""; NSRange currentRange = NSMakeRange(start, end - start); [txt addAttribute:@"displayText" value:text range:currentRange]; if ([entity[@"type"] isEqualToString:@"a"]) { [txt addAttribute:NSLinkAttributeName value:entity[@"url"] range:currentRange]; } if ([entity[@"type"] isEqualToString:@"q"]) { [txt addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:currentRange]; NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; paragraphStyle.firstLineHeadIndent = 12.0f; paragraphStyle.headIndent = 12.0f; paragraphStyle.paragraphSpacing = 12.0f; [txt addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:currentRange]; } if ([entity[@"type"] isEqualToString:@"u"]) { [txt addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:currentRange]; } UIFontDescriptor* fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody]; if ([entity[@"type"] isEqualToString:@"b"]) { UIFontDescriptor* boldFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold]; UIFont* boldFont = [UIFont fontWithDescriptor:boldFontDescriptor size: 0.0]; [txt addAttribute:NSFontAttributeName value:boldFont range:currentRange]; } if ([entity[@"type"] isEqualToString:@"i"]) { UIFontDescriptor* italicFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic]; UIFont* italicFont = [UIFont fontWithDescriptor:italicFontDescriptor size: 0.0]; [txt addAttribute:NSFontAttributeName value:italicFont range:currentRange]; } } [txt enumerateAttribute:@"displayText" inRange:NSMakeRange(0, [txt length]) options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) { if (value) { [txt replaceCharactersInRange:range withString:value]; } }]; [txt endEditing]; self.text.attributedText = txt; } [self.tags removeAllTags]; if ([msg.tags count] > 0) { [msg.tags enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [self.tags addTag:obj]; }]; } } -(void) avatarClicked:(UIGestureRecognizer *)gestureRecognizer { [self.delegate avatarClicked:self.title.text]; } @end