summaryrefslogtreecommitdiff
path: root/Juick/Views/MessageCell.m
blob: ffb564a97a9dd4b0fa2ec71c6e25af1af0a5b166 (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
//
//  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"

@implementation MessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.text.textColor = [ColorScheme textColor];
    self.text.enabledTextCheckingTypes = NSTextCheckingTypeLink;
    self.text.delegate = self;
    self.text.linkAttributes = @{ (id)kCTForegroundColorAttributeName: [ColorScheme linkColor],
                                  (id)kCTUnderlineStyleAttributeName : [NSNumber numberWithInt:NSUnderlineStyleSingle] };
    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:[NSString stringWithFormat:@"https://i.juick.com/a/%d.png", [msg.user.uid intValue]]]] 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.text =  [msg.text stringByDecodingHTMLEntities];
    [self.tags removeAllTags];
    if ([msg.tags count] > 0) {
        [msg.tags enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self.tags addTag:obj];
        }];
    }
}

-(void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
    [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}

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


@end