summaryrefslogtreecommitdiff
path: root/Juick/MessageCell.m
blob: d396181a1cad2405c82e48e992e6f393de885bcf (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
//  MessageCell.m
//  Juick
//
//  Created by Vitaly Takmazov on 29.10.13.
//  Copyright (c) 2013 com.juick. All rights reserved.
//

#import "MessageCell.h"
#import "ColorScheme.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "NSDate+TimeAgo.h"

@interface MessageCell ()

@property (nonatomic, assign) BOOL didSetupConstraints;

@end

@implementation MessageCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView setBackgroundColor:[UIColor whiteColor]];
        self.contentView.bounds = CGRectMake(0, 0, 320, 999);
        self.avatar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
        [self.avatar setBackgroundColor:[UIColor clearColor]];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
        [self.titleLabel setNumberOfLines:1];
        [self.titleLabel setTextAlignment:NSTextAlignmentLeft];
        [self.titleLabel setTextColor:[ColorScheme linkColor]];
        [self.titleLabel setBackgroundColor:[UIColor clearColor]];
        
        self.timestampLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.timestampLabel setLineBreakMode:NSLineBreakByTruncatingTail];
        [self.timestampLabel setNumberOfLines:1];
        [self.timestampLabel setTextAlignment:NSTextAlignmentLeft];
        [self.timestampLabel setTextColor:[UIColor grayColor]];
        [self.timestampLabel setBackgroundColor:[UIColor clearColor]];
        
        self.bodyLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        self.bodyLabel.dataDetectorTypes = NSTextCheckingTypeLink;
        self.bodyLabel.delegate = self;
        [self.bodyLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
        [self.bodyLabel setLineBreakMode:NSLineBreakByTruncatingTail];
        [self.bodyLabel setNumberOfLines:0];
        [self.bodyLabel setTextAlignment:NSTextAlignmentLeft];
        [self.bodyLabel setTextColor:[UIColor darkGrayColor]];
        [self.bodyLabel setBackgroundColor:[UIColor clearColor]];
        
        
        self.summaryLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        [self.summaryLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self.summaryLabel setLineBreakMode:NSLineBreakByTruncatingTail];
        [self.summaryLabel setNumberOfLines:1];
        [self.summaryLabel setTextAlignment:NSTextAlignmentLeft];
        [self.summaryLabel setTextColor:[UIColor grayColor]];
        [self.summaryLabel setBackgroundColor:[UIColor clearColor]];
        self.attach = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.attach setBackgroundColor:[UIColor clearColor]];
        self.attach.contentMode = UIViewContentModeScaleAspectFit;
        
        [self.contentView addSubview:self.avatar];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.timestampLabel];
        [self.contentView addSubview:self.bodyLabel];
        [self.contentView addSubview:self.attach];
        [self.contentView addSubview:self.summaryLabel];
        
        [self updateFonts];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)updateConstraints {
    [super updateConstraints];
    
    if (self.didSetupConstraints) return;
    
    UIEdgeInsets padding = UIEdgeInsetsMake(10, 20, 10, 20);
    
    [self.avatar makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.top).with.offset(padding.top);
        make.left.equalTo(self.contentView.left).with.offset(padding.left);
        make.width.equalTo(@32);
        make.height.equalTo(@32);
    }];
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.avatar.right).with.offset(padding.right);
        make.top.equalTo(self.contentView.top).with.offset(padding.top);
        make.right.equalTo(self.contentView.right).with.offset(-padding.right);
    }];
    [self.timestampLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.avatar.right).with.offset(padding.left);
        make.top.equalTo(self.titleLabel.bottom).with.offset(padding.top /2);
        make.right.equalTo(self.contentView.right).with.offset(-padding.right);
    }];
    
    [self.bodyLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.left).with.offset(padding.left);
        make.top.equalTo(self.timestampLabel.bottom).with.offset(padding.top);
        make.right.equalTo(self.contentView.right).with.offset(-padding.right);
    }];
    
    [self.attach makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.left).with.offset(padding.left);
        make.right.equalTo(self.contentView.right).with.offset(-padding.right);
        make.top.equalTo(self.bodyLabel.bottom).with.offset(padding.top);
    }];
    
    [self.summaryLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.left).with.offset(padding.left);
        make.bottom.equalTo(self.contentView.bottom).with.offset(-padding.bottom);
        make.right.equalTo(self.contentView.right).with.offset(-padding.right);
        make.top.equalTo(self.attach.bottom).with.offset(padding.top);
    }];
    
    
    
    self.didSetupConstraints = YES;
}


- (void)updateFonts
{
    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
        self.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
        self.bodyLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
        self.timestampLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
        self.summaryLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
    }
}

- (void) bind:(Message *)msg {
    [self.avatar setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%d.png", [msg.userID intValue]]]];
    __weak MessageCell *weakSelf = self;
    self.titleLabel.text = msg.user;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    
    self.timestampLabel.text = [[formatter dateFromString:msg.timestamp] timeAgo];
    if (msg.repliesCount > 0) {
        if ([msg.repliesBy length] > 0) {
            self.summaryLabel.text = [NSString stringWithFormat:@"%@ replies by %@", msg.repliesCount, msg.repliesBy];
        } else {
            self.summaryLabel.text = [NSString stringWithFormat:@"%@ replies", msg.repliesCount];
        }
    } else {
        self.summaryLabel.text = nil;
    }
    self.bodyLabel.text =  [msg.text stringByDecodingHTMLEntities];
    if ([msg.tags count] > 0) {
        self.bodyLabel.text = [[NSString alloc] initWithFormat:@"%@\n%@", [msg.tags componentsJoinedByString:@", "],
                               [msg.text stringByDecodingHTMLEntities]];
    }
    if ([msg.attach length] > 0) {
        [self.attach setImageWithURL:[NSURL URLWithString:msg.attach] placeholderImage:[UIImage imageNamed:@"AttachPlaceholder.png"] options:SDWebImageContinueInBackground
                            progress:^(NSUInteger receivedSize, long long expectedSize) {
           // <#code#>
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
            [weakSelf setNeedsUpdateConstraints];
            [weakSelf updateConstraintsIfNeeded];
            [weakSelf.contentView setNeedsLayout];
            [weakSelf.contentView layoutIfNeeded];
        }];
    } else {
        self.attach.image = nil;
    }
    [weakSelf setNeedsUpdateConstraints];
    [weakSelf updateConstraintsIfNeeded];
    [weakSelf.contentView setNeedsLayout];
    [weakSelf.contentView layoutIfNeeded];
}

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

@end