forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectTaskListViewCell.m
More file actions
executable file
·388 lines (362 loc) · 15.8 KB
/
ProjectTaskListViewCell.m
File metadata and controls
executable file
·388 lines (362 loc) · 15.8 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//
// ProjectTaskListViewCell.m
// Coding_iOS
//
// Created by 王 原闯 on 14-8-16.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#define kProjectTaskListViewCell_LeftPading 93.0
#define kProjectTaskListViewCell_RightPading 10.0
#define kProjectTaskListViewCell_CheckBoxWidth 20.0
#define kProjectTaskListViewCell_UserIconWidth 33.0
#define kProjectTaskListViewCell_UpDownPading 10.0
#define kProjectTaskListViewCell_MaxContentHeight 20.0
#define kProjectTaskListViewCell_ContentWidth (kScreen_Width - kProjectTaskListViewCell_LeftPading - kProjectTaskListViewCell_RightPading)
#define kProjectTaskListViewCell_ContentFont [UIFont systemFontOfSize:15]
#define kProjectTaskListViewCell_TextPading 10.0
#define kProjectTaskListViewCellTagsView_Font [UIFont systemFontOfSize:12]
#import "ProjectTaskListViewCell.h"
#import "Coding_NetAPIManager.h"
#import "ProjectTagLabel.h"
@interface ProjectTaskListViewCell ()
@property (strong, nonatomic) UIImageView *userIconView, *commentIconView, *timeClockIconView, *mdIconView, *taskPriorityView;
@property (strong, nonatomic) UITapImageView *checkView;
@property (strong, nonatomic) UILabel *contentLabel, *userNameLabel, *timeLabel, *commentCountLabel, *mdLabel, *numLabel;
@property (strong, nonatomic) ProjectTaskListViewCellTagsView *tagsView;
@end
@implementation ProjectTaskListViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.backgroundColor = [UIColor clearColor];
if (!_checkView) {
_checkView = [UITapImageView new];
_checkView.contentMode = UIViewContentModeCenter;
__weak typeof(self) weakSelf = self;
[_checkView addTapBlock:^(id obj) {
//用户点击后,直接改变任务状态,在block中处理网络请求
[weakSelf.checkView setImage:[UIImage imageNamed:(weakSelf.task.status.integerValue != 2? @"checkbox_checked":@"checkbox_unchecked")]];
if (weakSelf.checkViewClickedBlock) {
weakSelf.checkViewClickedBlock(weakSelf.task);
}
}];
[self.contentView addSubview:_checkView];
}
if (!_userIconView) {
_userIconView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kProjectTaskListViewCell_UserIconWidth, kProjectTaskListViewCell_UserIconWidth)];
[_userIconView doCircleFrame];
[self.contentView addSubview:_userIconView];
}
if (!_taskPriorityView) {
_taskPriorityView = [UIImageView new];
_taskPriorityView.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview:_taskPriorityView];
}
if (!_contentLabel) {
_contentLabel = [UILabel new];
_contentLabel.textColor = [UIColor colorWithHexString:@"0x222222"];
_contentLabel.font = kProjectTaskListViewCell_ContentFont;
[self.contentView addSubview:_contentLabel];
}
if (!_tagsView) {
_tagsView = [ProjectTaskListViewCellTagsView new];
[self.contentView addSubview:_tagsView];
}
if (!_numLabel) {
_numLabel = [UILabel new];
_numLabel.font = [UIFont systemFontOfSize:10];
_numLabel.textColor = [UIColor colorWithHexString:@"0x222222"];
[self.contentView addSubview:_numLabel];
}
if (!_userNameLabel) {
_userNameLabel = [UILabel new];
_userNameLabel.font = [UIFont systemFontOfSize:10];
_userNameLabel.textColor = [UIColor colorWithHexString:@"0x666666"];
[self.contentView addSubview:_userNameLabel];
}
if (!_timeClockIconView) {
_timeClockIconView = [UIImageView new];
_timeClockIconView.image = [UIImage imageNamed:@"time_clock_icon"];
[self.contentView addSubview:_timeClockIconView];
}
if (!_timeLabel) {
_timeLabel = [UILabel new];
_timeLabel.font = [UIFont systemFontOfSize:10];
_timeLabel.textColor = [UIColor colorWithHexString:@"0x999999"];
[self.contentView addSubview:_timeLabel];
}
if (!_commentIconView) {
_commentIconView = [UIImageView new];
[_commentIconView setImage:[UIImage imageNamed:@"topic_comment_icon"]];
[self.contentView addSubview:_commentIconView];
}
if (!_commentCountLabel) {
_commentCountLabel = [UILabel new];
_commentCountLabel.font = [UIFont systemFontOfSize:10];
_commentCountLabel.textColor = [UIColor colorWithHexString:@"0x999999"];
[self.contentView addSubview:_commentCountLabel];
}
if (!_mdIconView) {
_mdIconView = [UIImageView new];
[_mdIconView setImage:[UIImage imageNamed:@"task_description_icon"]];
[self.contentView addSubview:_mdIconView];
}
if (!_mdLabel) {
_mdLabel = [UILabel new];
_mdLabel.font = [UIFont systemFontOfSize:10];
_mdLabel.textColor = [UIColor colorWithHexString:@"0x999999"];
_mdLabel.text = @"描述";
[self.contentView addSubview:_mdLabel];
}
[_checkView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(kPaddingLeftWidth);
make.centerY.equalTo(self.contentView);
make.size.mas_equalTo(CGSizeMake(kProjectTaskListViewCell_CheckBoxWidth,
kProjectTaskListViewCell_CheckBoxWidth));
}];
[_userIconView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.checkView.mas_right).offset(10);
make.centerY.equalTo(self.contentView);
make.size.mas_equalTo(CGSizeMake(kProjectTaskListViewCell_UserIconWidth,
kProjectTaskListViewCell_UserIconWidth));
}];
[_taskPriorityView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView).offset(10);
make.left.equalTo(self.userIconView.mas_right).offset(10);
make.size.mas_equalTo(CGSizeMake(17, 17));
}];
[_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.taskPriorityView);
make.left.equalTo(self.taskPriorityView.mas_right).offset(10);
make.right.equalTo(self.contentView).offset(-kPaddingLeftWidth);
make.height.mas_equalTo(20);
}];
[_tagsView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentLabel.mas_bottom).offset(10);
make.left.equalTo(self.userIconView.mas_right).offset(10);
make.height.mas_equalTo(25);
make.right.equalTo(self.contentView).offset(-kPaddingLeftWidth);
}];
[_numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.contentView).offset(-10);
make.left.equalTo(self.userIconView.mas_right).offset(10);
make.height.mas_equalTo(15);
}];
[_userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.numLabel.mas_right).offset(5);
make.centerY.equalTo(self.numLabel);
make.height.mas_equalTo(15);
}];
[_timeClockIconView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.userNameLabel.mas_right).offset(10);
make.centerY.equalTo(self.userNameLabel);
make.size.mas_equalTo(CGSizeMake(12, 12));
}];
[_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.timeClockIconView.mas_right).offset(5);
make.centerY.equalTo(self.userNameLabel);
make.height.mas_equalTo(15);
}];
[_commentIconView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.timeLabel.mas_right).offset(10);
make.centerY.equalTo(self.userNameLabel);
make.size.mas_equalTo(CGSizeMake(12, 12));
}];
[_commentCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.commentIconView.mas_right).offset(5);
make.centerY.equalTo(self.userNameLabel);
make.height.mas_equalTo(15);
}];
[_mdIconView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.commentCountLabel.mas_right).offset(10);
make.centerY.equalTo(self.userNameLabel);
make.size.mas_equalTo(CGSizeMake(12, 12));
}];
[_mdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mdIconView.mas_right).offset(5);
make.centerY.equalTo(self.userNameLabel);
make.height.mas_equalTo(15);
}];
}
return self;
}
- (void)layoutSubviews{
[super layoutSubviews];
if (!_task) {
return;
}
//Top
[_checkView setImage:[UIImage imageNamed:_task.status.integerValue == 1? @"checkbox_unchecked" : @"checkbox_checked"]];
[_userIconView sd_setImageWithURL:[_task.owner.avatar urlImageWithCodePathResize:2*kProjectTaskListViewCell_UserIconWidth] placeholderImage:kPlaceholderMonkeyRoundWidth(kProjectTaskListViewCell_UserIconWidth)];
[_taskPriorityView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"taskPriority%@_small", _task.priority.stringValue]]];
_contentLabel.textColor = [UIColor colorWithHexString:_task.status.integerValue == 1? @"0x222222" : @"0x999999"];
_contentLabel.text = [_task.content stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//Tags
if (_task.deadline_date || _task.labels.count > 0) {
_tagsView.tags = _task.labels;
_tagsView.deadline_date = _task.deadline_date;
_tagsView.done = (_task.status.integerValue != 1);
[_tagsView reloadData];
_tagsView.hidden = NO;
}else{
_tagsView.hidden = YES;
}
//Bottom
_numLabel.text = [NSString stringWithFormat:@"#%@", _task.number.stringValue];
_userNameLabel.text = _task.creator.name;
_timeLabel.text = [_task.created_at stringDisplay_MMdd];
_commentCountLabel.text = _task.comments.stringValue;
_mdIconView.hidden = _mdLabel.hidden = !_task.has_description.boolValue;
}
+ (CGFloat)cellHeightWithObj:(id)obj{
CGFloat cellHeight = 0;
if ([obj isKindOfClass:[Task class]]) {
Task *task = (Task *)obj;
if (task.deadline_date || task.labels.count > 0) {
cellHeight = 95;
}else{
cellHeight = 60;
}
}
return cellHeight;
}
@end
@interface ProjectTaskListViewCellTagsView ()
@property (strong, nonatomic) NSMutableArray *tagLabelList;
@property (strong, nonatomic) ProjectTaskListViewCellDateView *dateView;
@end
@implementation ProjectTaskListViewCellTagsView
+ (instancetype)viewWithTags:(NSArray *)tags andDate:(NSDate *)deadline_date{
ProjectTaskListViewCellTagsView *view = [self new];
view.tags = tags;
view.deadline_date = deadline_date;
[view reloadData];
return view;
}
- (void)reloadData{
if (_deadline_date) {
if (!_dateView) {
_dateView = [ProjectTaskListViewCellDateView viewWithDate:_deadline_date andDone:_done];
[self addSubview:_dateView];
}else{
[_dateView setDate:_deadline_date andDone:_done];
}
_dateView.hidden = NO;
}else{
_dateView.hidden = YES;
}
[_tagLabelList makeObjectsPerformSelector:@selector(removeFromSuperview)];
CGFloat viewWidth = kScreen_Width - 2*kPaddingLeftWidth - 2*10 - kProjectTaskListViewCell_CheckBoxWidth - kProjectTaskListViewCell_UserIconWidth;
CGFloat curRightX = (!_dateView || _dateView.hidden)? 0: CGRectGetMaxX(_dateView.frame) + 5;
for (int index = 0; index < _tags.count; index++) {
ProjectTagLabel *label = [self p_getLabelWithIndex:index];
[label setX:curRightX];
[self addSubview:label];
curRightX += CGRectGetWidth(label.frame);
if (curRightX > viewWidth
|| (viewWidth - curRightX < 25 && index < _tags.count - 1)) {
[self p_makeMoreStyleWithTagLabel:label];
break;
}
curRightX += 5;
}
}
- (ProjectTagLabel *)p_getLabelWithIndex:(NSInteger)index{
if (index >= _tags.count) {
return nil;
}
ProjectTagLabel *label;
if (!_tagLabelList) {
_tagLabelList = [NSMutableArray new];
}
if (index < _tagLabelList.count) {
label = _tagLabelList[index];
label.curTag = _tags[index];
}else{
label = [ProjectTagLabel labelWithTag:_tags[index] font:kProjectTaskListViewCellTagsView_Font height:20 widthPadding:10];
[_tagLabelList addObject:label];
}
return label;
}
- (void)p_makeMoreStyleWithTagLabel:(ProjectTagLabel *)tagLabel{
tagLabel.layer.backgroundColor = [UIColor clearColor].CGColor;
tagLabel.textColor = [UIColor colorWithHexString:@"0x999999"];
tagLabel.text = @"···";
[tagLabel setWidth:15];
}
@end
@interface ProjectTaskListViewCellDateView ()
@property (strong, nonatomic) UIImageView *dateIcon;
@property (strong, nonatomic) UILabel *dateStrL;
@end
@implementation ProjectTaskListViewCellDateView
- (instancetype)init
{
self = [super init];
if (self) {
[self doBorderWidth:0.5 color:[UIColor clearColor] cornerRadius:2.0];
{
_dateIcon = [UIImageView new];
[self addSubview:_dateIcon];
}
{
_dateStrL = [UILabel new];
_dateStrL.font = kProjectTaskListViewCellTagsView_Font;
[self addSubview:_dateStrL];
}
{
[_dateIcon mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self);
make.left.equalTo(self).offset(5);
make.size.mas_equalTo(CGSizeMake(12, 12));
}];
[_dateStrL mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self);
make.left.equalTo(self.dateIcon.mas_right).offset(5);
}];
}
}
return self;
}
+ (instancetype)viewWithDate:(NSDate *)deadline_date andDone:(BOOL)done{
ProjectTaskListViewCellDateView *view = [self new];
[view setDate:deadline_date andDone:done];
return view;
}
- (void)setDate:(NSDate *)deadline_date andDone:(BOOL)done{
if (!deadline_date) {
[self setSize:CGSizeZero];
return;
}
NSString *textColorStr, *deadlineStr;
NSInteger leftDayCount = [deadline_date leftDayCount];
switch (leftDayCount) {
case 0:
textColorStr = @"0xF5A523";
deadlineStr = @"今天";
break;
case 1:
textColorStr = @"0x95B763";
deadlineStr = @"明天";
break;
default:
textColorStr = leftDayCount > 0? @"0x9AAFC2": @"0xF24B4B";
deadlineStr = [deadline_date stringWithFormat:@"MM/dd"];
break;
}
if (done) {
textColorStr = @"0xB5B5B5";
}
UIColor *textColor = [UIColor colorWithHexString:textColorStr];
self.layer.borderColor = textColor.CGColor;
_dateIcon.image = [UIImage imageNamed:[NSString stringWithFormat:@"calendar_%@", textColorStr]];
_dateStrL.textColor = textColor;
_dateStrL.text = deadlineStr;
CGFloat textWidth = [deadlineStr getWidthWithFont:kProjectTaskListViewCellTagsView_Font constrainedToSize:CGSizeMake(CGFLOAT_MAX, 15)];
CGSize viewSize = CGSizeMake(textWidth + 12 + 5 * 3, 20);
[self setSize:viewSize];
}
@end