summaryrefslogtreecommitdiff
path: root/Juick/Views/MessageCell.m
blob: 6929436ecc730605c3d43b75e5b62a6d72ba9da5 (plain) (blame)
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
//
//  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"

@interface MessageCell()
@property(nonatomic, readonly) NSMutableParagraphStyle *quoteStyle;
@property(nonatomic, readonly) UIFont *boldFont;
@property(nonatomic, readonly) UIFont *italicFont;
@end

@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];
    UIGestureRecognizer *avatarTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarClicked:)];
    [avatarTapRecognizer setEnabled:YES];
    [self.avatar addGestureRecognizer:avatarTapRecognizer];
    [self.avatar setUserInteractionEnabled:YES];
    _quoteStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    _quoteStyle.firstLineHeadIndent = 12.0f;
    _quoteStyle.headIndent = 12.0f;
    _quoteStyle.paragraphSpacing = 6.0f;
    UIFontDescriptor* fontDescriptor = [UIFontDescriptor
                                        preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
    UIFontDescriptor* boldFontDescriptor = [fontDescriptor
                                            fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
    _boldFont =  [UIFont fontWithDescriptor:boldFontDescriptor size: 0.0];
    UIFontDescriptor* italicFontDescriptor = [fontDescriptor
                                              fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic];
    _italicFont =  [UIFont fontWithDescriptor:italicFontDescriptor size: 0.0];
}

- (void) configureWithMessage:(Message *)msg {
    self.message = msg;
    self.avatar.image = nil;
    __weak UIImageView *weakAvatar = self.avatar;
    [[APIClient sharedClient] fetchImageWithURL:[NSURL URLWithString:msg.user.avatar] callback:^(NSData *data) {
        [UIView transitionWithView:weakAvatar
                          duration:0.3
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:^{
                            weakAvatar.image = [UIImage imageWithData:data];
                        }
                        completion: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 (Entity *entity in msg.entities) {
            NSUInteger start = entity.start ? [entity.start unsignedIntegerValue] : 0;
            NSUInteger end =  entity.end ? [entity.end unsignedIntegerValue] : 0;
            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.link range:currentRange];
            }
            if ([entity.type isEqualToString:@"q"]) {
                [txt addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:currentRange];
                [txt addAttribute:NSParagraphStyleAttributeName value:_quoteStyle range:currentRange];
            }
            if ([entity.type isEqualToString:@"u"]) {
                [txt addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:currentRange];
            }
            
            if ([entity.type isEqualToString:@"b"]) {
                [txt addAttribute:NSFontAttributeName value:_boldFont range:currentRange];
            }
            if ([entity.type isEqualToString:@"i"]) {
                [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];
            }
        }];
        if ([msg.tags count] > 0) {
            NSString *tagsList = [NSString stringWithFormat:@"%@\n", [msg.tags componentsJoinedByString:@", "]];
            [txt insertAttributedString:[[NSAttributedString alloc]
                                         initWithString:tagsList
                                         attributes:@{
                                                      NSFontAttributeName:_italicFont,
                                                      NSForegroundColorAttributeName:[UIColor darkGrayColor]
                                                      }] atIndex:0];
        }
        [txt endEditing];
        self.text.attributedText = txt;
    }
}

-(void) avatarClicked:(UIGestureRecognizer *)gestureRecognizer {
    [self.delegate avatarClicked:self.title.text];
}

@end