-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathFileListFileCell.m
executable file
·241 lines (222 loc) · 9.67 KB
/
FileListFileCell.m
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
//
// FileListFileCell.m
// Coding_iOS
//
// Created by 王 原闯 on 14/10/29.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#define kFileListFileCell_IconWidth 45.0
#define kFileListFileCell_LeftPading (kPaddingLeftWidth +kFileListFileCell_IconWidth +20.0)
#define kFileListFileCell_TopPading 10.0
#import "FileListFileCell.h"
#import "AFURLSessionManager.h"
#import "Coding_FileManager.h"
#import "ASProgressPopUpView.h"
#import "YLImageView.h"
@interface FileListFileCell ()<ASProgressPopUpViewDelegate>
@property (strong, nonatomic) UIImageView *iconView, *shareLogoV;
@property (strong, nonatomic) UILabel *nameLabel, *infoLabel, *sizeLabel;
@property (strong, nonatomic) UIButton *stateButton;
@property (strong, nonatomic) ASProgressPopUpView *progressView;
@end
@implementation FileListFileCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
if (!_iconView) {
_iconView = [[YLImageView alloc] initWithFrame:CGRectMake(kPaddingLeftWidth, ([FileListFileCell cellHeight] - kFileListFileCell_IconWidth)/2, kFileListFileCell_IconWidth, kFileListFileCell_IconWidth)];
_iconView.layer.masksToBounds = YES;
_iconView.layer.cornerRadius = 2.0;
_iconView.layer.borderWidth = 0.5;
_iconView.layer.borderColor = kColorDDD.CGColor;
[self.contentView addSubview:_iconView];
}
if (!_shareLogoV) {
_shareLogoV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_file_share_logo"]];
_shareLogoV.hidden = YES;
[_iconView addSubview:_shareLogoV];
[_shareLogoV mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(20, 20));
make.top.right.equalTo(_iconView);
}];
}
if (!_nameLabel) {
_nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(kFileListFileCell_LeftPading, kFileListFileCell_TopPading, (kScreen_Width - kFileListFileCell_LeftPading - 60), 25)];
_nameLabel.textColor = kColor222;
_nameLabel.font = [UIFont systemFontOfSize:16];
[self.contentView addSubview:_nameLabel];
}
if (!_sizeLabel) {
_sizeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kFileListFileCell_LeftPading, ([FileListFileCell cellHeight]- 15)/2+3, (kScreen_Width - kFileListFileCell_LeftPading - 60), 15)];
_sizeLabel.textColor = kColor999;
_sizeLabel.font = [UIFont systemFontOfSize:12];
[self.contentView addSubview:_sizeLabel];
}
if (!_infoLabel) {
_infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(kFileListFileCell_LeftPading, ([FileListFileCell cellHeight]- 15 - kFileListFileCell_TopPading), (kScreen_Width - kFileListFileCell_LeftPading - 60), 15)];
_infoLabel.textColor = kColor999;
_infoLabel.font = [UIFont systemFontOfSize:12];
[self.contentView addSubview:_infoLabel];
}
if (!_progressView) {
_progressView = [[ASProgressPopUpView alloc] initWithFrame:CGRectMake(kPaddingLeftWidth, [FileListFileCell cellHeight]-2.5, kScreen_Width- kPaddingLeftWidth, 2.0)];
_progressView.popUpViewCornerRadius = 12.0;
_progressView.delegate = self;
_progressView.font = [UIFont fontWithName:@"Futura-CondensedExtraBold" size:12];
[_progressView setTrackTintColor:[UIColor colorWithHexString:@"0xe6e6e6"]];
_progressView.popUpViewAnimatedColors = @[kColorBrandBlue];
_progressView.hidden = YES;
[self.contentView addSubview:self.progressView];
}
if (!_stateButton) {
_stateButton = [[UIButton alloc] initWithFrame:CGRectMake((kScreen_Width - 60), ([FileListFileCell cellHeight] - 30)/2, 50, 30)];
[_stateButton doBorderWidth:1.0 color:kColorD8DDE4 cornerRadius:3];
_stateButton.titleLabel.font = [UIFont systemFontOfSize:14];
[_stateButton setTitleColor:kColorDark7 forState:UIControlStateNormal];
[_stateButton addTarget:self action:@selector(clickedByUser) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:_stateButton];
}
}
return self;
}
- (void)changeToState:(DownloadState)state{
NSString *stateTitleStr;
switch (state) {
case DownloadStateDefault:
stateTitleStr = @"下载";
break;
case DownloadStateDownloading:
stateTitleStr = @"暂停";
break;
case DownloadStatePausing:
stateTitleStr = @"继续";
break;
case DownloadStateDownloaded:
stateTitleStr = @"查看";
break;
default:
stateTitleStr = @"下载";
break;
}
[self setBackgroundColor:(state == DownloadStateDownloaded)? [UIColor colorWithHexString:@"0x81BCFF" andAlpha:.1]:[UIColor whiteColor]];
//addLineforPlainCell: 重置了 backgroundView,要改 layer 才行
CALayer *bgLayer = self.backgroundView.layer.sublayers.firstObject;
if (bgLayer && [bgLayer isKindOfClass:[CAShapeLayer class]]) {
((CAShapeLayer *)bgLayer).fillColor = self.backgroundColor.CGColor;
}
[self.progressView setHidden:!(state == DownloadStateDownloading || state == DownloadStatePausing)];
[_stateButton setTitle:stateTitleStr forState:UIControlStateNormal];
}
- (void)layoutSubviews{
[super layoutSubviews];
if (!_file) {
return;
}
_shareLogoV.hidden = !_file.share;
_nameLabel.text = _file.name;
_sizeLabel.text = [NSString sizeDisplayWithByte:_file.size.floatValue];
_infoLabel.text = [NSString stringWithFormat:@"%@ 创建于 %@", _file.owner.name, [_file.created_at stringDisplay_HHmm]];
if (_file.preview && _file.preview.length > 0) {
[_iconView sd_setImageWithURL:[NSURL URLWithString:_file.preview]];
}else{
_iconView.image = [UIImage imageWithFileType:_file.fileType];
}
}
+ (CGFloat)cellHeight{
return 75.0;
}
- (void)setFile:(ProjectFile *)file{
_file = file;
if (!_file) {
return;
}
Coding_DownloadTask *cDownloadTask = [_file cDownloadTask];
[self updateProgress:cDownloadTask.progress];
[self changeToState:_file.downloadState];
}
- (void)clickedByUser{
Coding_FileManager *manager = [Coding_FileManager sharedManager];
NSURL *fileUrl = _file.diskFileUrl;
if (fileUrl) {//已经下载到本地了
if (_showDiskFileBlock) {
_showDiskFileBlock(fileUrl, _file);
}
}else{//要下载
NSURLSessionDownloadTask *downloadTask;
if (_file.cDownloadTask) {//暂停或者重新开始
downloadTask = _file.cDownloadTask.task;
switch (downloadTask.state) {
case NSURLSessionTaskStateRunning:
[downloadTask suspend];
[self changeToState:DownloadStatePausing];
break;
case NSURLSessionTaskStateSuspended:
[downloadTask resume];
[self changeToState:DownloadStateDownloading];
break;
default:
break;
}
}else{//新建下载
__weak typeof(self) weakSelf = self;
Coding_DownloadTask *cDownloadTask = [manager addDownloadTaskForObj:self.file completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if (error) {
[weakSelf changeToState:DownloadStateDefault];
[NSObject showError:error];
DebugLog(@"ERROR:%@", error.description);
}else{
[weakSelf changeToState:DownloadStateDownloaded];
DebugLog(@"File downloaded to: %@", filePath);
}
}];
[self updateProgress:cDownloadTask.progress];
[self changeToState:DownloadStateDownloading];
}
}
}
#pragma mark Progress
- (void)updateProgress:(NSProgress *)progress{
if (progress) {
if (_file.size.floatValue/1024/1024 > 5.0) {//大于5M的文件,下载时显示百分比
[_progressView showPopUpViewAnimated:NO];
}else{
[_progressView hidePopUpViewAnimated:NO];
}
@weakify(self);
[[RACObserve(progress, fractionCompleted)
takeUntil:[RACSignal combineLatest:@[self.rac_prepareForReuseSignal, self.rac_willDeallocSignal]]]
subscribeNext:^(NSNumber *fractionCompleted) {
@strongify(self);
dispatch_async(dispatch_get_main_queue(), ^{
[self updateCompleted:fractionCompleted.floatValue];
});
}];
}else{
_progressView.hidden = YES;
}
}
- (void)updateCompleted:(CGFloat)fractionCompleted{
//更新进度
self.progressView.progress = fractionCompleted;
if (ABS(fractionCompleted - 1.0) < 0.0001) {
//已完成
[self.progressView hidePopUpViewAnimated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self changeToState:DownloadStateDownloaded];
self.progressView.hidden = YES;
});
}else{
self.progressView.hidden = NO;
}
}
#pragma mark ASProgressPopUpViewDelegate
- (void)progressViewWillDisplayPopUpView:(ASProgressPopUpView *)progressView;
{
[self.superview bringSubviewToFront:self];
}
- (void)progressViewDidHidePopUpView:(ASProgressPopUpView *)progressView{
progressView.hidden = YES;
}
@end