summaryrefslogtreecommitdiff
path: root/Juick/MessageCell.m
blob: d47c3d22d35b970d103c966166c1a9e2d9a91b11 (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
//
//  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 "NSDate+TimeAgo.h"
#import "UIImage+Utils.h"

#define kAvatarSizeX 32
#define kAvatarSizeY 32
#define kPaddingX 10
#define kPaddingY 10

@implementation MessageCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView setBackgroundColor:[UIColor whiteColor]];
        self.avatar = [[UIImageView alloc] initWithFrame:CGRectZero];
        [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 = [[UILabel 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 = [[UILabel alloc] initWithFrame:CGRectZero];
        [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 = UIViewContentModeTopLeft;
        
        [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
}


- (CGRect) avatarRect {
    return CGRectMake(kPaddingX, kPaddingY, kAvatarSizeX, kAvatarSizeY);
}

- (CGRect) bodyRect {
    CGRect bodySize = [UILabel sizeForLabel:self.bodyLabel width:(self.contentView.bounds.size.width - kPaddingX * 2)];
    return CGRectMake(kPaddingX, kPaddingY + kAvatarSizeY + kPaddingY * 2, bodySize.size.width, bodySize.size.height);
}

- (CGRect) titleRect {
    CGRect titleSize = [UILabel sizeForLabel:self.titleLabel width:self.contentView.bounds.size.width - [self avatarRect].size.width - kPaddingX * 2];
    return CGRectMake(kPaddingX + kAvatarSizeX + kPaddingX, kPaddingY, titleSize.size.width, titleSize.size.height);
}

- (CGRect) timestampRect {
    CGRect timeStampSize = [UILabel sizeForLabel:self.timestampLabel width:self.contentView.bounds.size.width - [self avatarRect].size.width - kPaddingX * 2];
    return CGRectMake(kPaddingX + [self avatarRect].size.width + kPaddingX, kPaddingY + [self titleRect].size.height + kPaddingY, timeStampSize.size.width, timeStampSize.size.height);
}

- (CGRect) summaryRect {
    CGRect summarySize = [UILabel sizeForLabel:self.summaryLabel width:(self.contentView.bounds.size.width - 20)];
    float summaryY = 10 + 32 + 10 + [self bodyRect].size.height + 10;
    if (self.message.attach != nil) {
        summaryY += [self attachRect].size.height + 10;
    }
    return CGRectMake(10, summaryY , summarySize.size.width, summarySize.size.height);
}

- (CGRect) attachRect {
    return  CGRectMake(10, 10 + 32 + 10 + [self bodyRect].size.height + 10, self.attach.image.size.width, self.attach.image.size.height);
}

- (CGFloat) heightForCell {
    CGFloat baseSize = 10 + [self avatarRect].size.height + 20 + [self bodyRect].size.height + 10;
    if (self.message.repliesCount > 0) {
        baseSize += 10 + [self summaryRect].size.height;
    }
    if (self.attach.image != nil) {
        baseSize += 10 + [self attachRect].size.height + 10;
    }
    return baseSize;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.avatar.frame = [self avatarRect];
    self.titleLabel.frame = [self titleRect];
    self.timestampLabel.frame = [self timestampRect];
    self.bodyLabel.frame = [self bodyRect];
    if (self.message.repliesCount > 0) {
        self.summaryLabel.frame = [self summaryRect];
    }
    if (self.message.attach != nil) {
        self.attach.frame = [self attachRect];
    }
}


- (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) setMessage:(Message *)msg {
    _message = msg;
    [self.avatar setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%d.png", [msg.userID intValue]]] placeholderImage:[UIImage imageNamed:@"0.png"] options:SDWebImageContinueInBackground];
    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]];
    }
    [self setNeedsLayout];
}

- (UITableView *) getTable {
    id view = [self superview];
    while ([view isKindOfClass:[UITableView class]] == NO) {
        view = [view superview];
    }
    return (UITableView *)view;
}

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

@end