-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathProjectTaskListView.m
executable file
·253 lines (223 loc) · 8.19 KB
/
ProjectTaskListView.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
242
243
244
245
246
247
248
249
250
251
252
253
//
// ProjectTaskListView.m
// Coding_iOS
//
// Created by 王 原闯 on 14-8-16.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#import "ProjectTaskListView.h"
#import "ODRefreshControl.h"
#import "Coding_NetAPIManager.h"
#import "ProjectTaskListViewCell.h"
#import "SVPullToRefresh.h"
@interface ProjectTaskListView ()
@property (strong, nonatomic) Tasks *myTasks;
@property (copy, nonatomic) ProjectTaskBlock block;
@property (strong, nonatomic) UITableView *myTableView;
@property (strong, nonatomic) ODRefreshControl *myRefreshControl;
@property (nonatomic, assign) NSInteger page;
@end
@implementation ProjectTaskListView
#pragma TabBar
- (void)tabBarItemClicked{
if (_myTableView.contentOffset.y > 0) {
[_myTableView setContentOffset:CGPointZero animated:YES];
}else if (!self.myRefreshControl.isAnimating){
[self.myRefreshControl beginRefreshing];
[self.myTableView setContentOffset:CGPointMake(0, -44)];
[self refresh];
}
}
- (void)reloadData{
if (self.myTableView) {
[self.myTableView reloadData];
}
}
- (id)initWithFrame:(CGRect)frame tasks:(Tasks *)tasks project_id:(NSString *)project_id keyword:(NSString *)keyword status:(NSString *)status label:(NSString *)label userId:(NSString *)userId role:(TaskRoleType )role block:(ProjectTaskBlock)block tabBarHeight:(CGFloat)tabBarHeight{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
_myTasks = tasks;
_block = block;
_page = 1;
self.project_id = project_id;
self.keyword = keyword;
self.status = status;
self.label = label;
self.userId = userId;
self.role = role;
_myTableView = ({
UITableView *tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor clearColor];
tableView.delegate = self;
tableView.dataSource = self;
[tableView registerClass:[ProjectTaskListViewCell class] forCellReuseIdentifier:kCellIdentifier_ProjectTaskList];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self addSubview:tableView];
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
if (tabBarHeight != 0) {
UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, tabBarHeight, 0);
tableView.contentInset = insets;
tableView.scrollIndicatorInsets = insets;
}
tableView.estimatedRowHeight = 0;
tableView.estimatedSectionHeaderHeight = 0;
tableView.estimatedSectionFooterHeight = 0;
tableView;
});
_myRefreshControl = [[ODRefreshControl alloc] initInScrollView:self.myTableView];
[_myRefreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
__weak typeof(self) weakSelf = self;
[_myTableView addInfiniteScrollingWithActionHandler:^{
[weakSelf refreshMore];
}];
if (_myTasks.list.count > 0) {
[_myTableView reloadData];
}else{
[self sendRequest];
}
}
return self;
}
- (void)setTasks:(Tasks *)tasks{
if (_myTasks != tasks) {
self.userId = tasks.owner.id.stringValue;
self.project_id = tasks.project.id.stringValue;
self.myTasks = tasks;
[_myTableView reloadData];
[_myTableView.infiniteScrollingView stopAnimating];
_myTableView.showsInfiniteScrolling = self.myTasks.canLoadMore;
if (self.myTasks.list.count > 0) {
[self configBlankPage:EaseBlankPageTypeTask hasData:YES hasError:NO reloadButtonBlock:nil];
}
[self refreshFirst];
}
}
- (void)refreshToQueryData{
[self refresh];
}
- (void)refreshFirst{
if (_myTasks && !_myTasks.list) {
[self refresh];
}
}
- (void)refresh{
if (_myTasks.isLoading) {
return;
}
_page = 1;
_myTasks.willLoadMore = NO;
[self sendRequest];
}
- (void)refreshMore{
if (_myTasks.isLoading || !_myTasks.canLoadMore) {
[_myTableView.infiniteScrollingView stopAnimating];
return;
}
_page++;
_myTasks.willLoadMore = YES;
[self sendRequest];
}
- (void)sendRequest{
if (_myTasks.list.count <= 0) {
[self beginLoading];
}
__weak typeof(self) weakSelf = self;
[[Coding_NetAPIManager sharedManager] request_tasks_searchWithUserId:_userId role:_role project_id:_project_id keyword:_keyword status:_status label:_label page:_page andBlock:^(Tasks *data, NSError *error) {
[weakSelf endLoading];
[weakSelf.myRefreshControl endRefreshing];
[weakSelf.myTableView.infiniteScrollingView stopAnimating];
if (data) {
[weakSelf.myTasks configWithTasks:data];
[weakSelf.myTableView reloadData];
weakSelf.myTableView.showsInfiniteScrolling = weakSelf.myTasks.canLoadMore;
}
[weakSelf configBlankPage:EaseBlankPageTypeTask hasData:(weakSelf.myTasks.list.count > 0) hasError:(error != nil) reloadButtonBlock:^(id sender) {
[weakSelf refresh];
}];
}];
}
#pragma mark Table M
- (NSArray *)tableDataListInSection:(NSInteger)section{
NSArray *dataList;
if (section == 0) {
if (_myTasks.processingList.count > 0) {
dataList = _myTasks.processingList;
}else{
dataList = _myTasks.doneList;
}
}else{
dataList = _myTasks.doneList;
}
return dataList;
}
- (NSInteger)numberOfSections{
NSInteger num = 0;
if (_myTasks.processingList.count > 0) {
num++;
}
if (_myTasks.doneList.count > 0) {
num++;
}
return num;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self numberOfSections];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger num = 0;
if (section == 0) {
if (_myTasks.processingList.count > 0) {
num = _myTasks.processingList.count;
}else{
num = _myTasks.doneList.count;
}
}else{
num = _myTasks.doneList.count;
}
return num;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ProjectTaskListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_ProjectTaskList forIndexPath:indexPath];
NSArray *dataList = [self tableDataListInSection:indexPath.section];
__weak typeof(self) weakSelf = self;
cell.task = [dataList objectAtIndex:indexPath.row];
cell.checkViewClickedBlock = ^(Task *task){
if (task.isRequesting) {
return ;
}else{
task.isRequesting = YES;
}
//ChangeTaskStatus后,task对象的status属性会直接在请求结束后被修改
[[Coding_NetAPIManager sharedManager] request_ChangeTaskStatus:task andBlock:^(id data, NSError *error) {
[weakSelf.myTableView reloadData];
task.isRequesting = NO;
}];
};
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:48];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *dataList = [self tableDataListInSection:indexPath.section];
return [ProjectTaskListViewCell cellHeightWithObj:[dataList objectAtIndex:indexPath.row]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSArray *dataList = [self tableDataListInSection:indexPath.section];
if (_block) {
Task *curTask = [dataList objectAtIndex:indexPath.row];
_block(self, curTask);
}
}
#pragma mark TableViewHeader
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 15;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerV = [UIView new];
headerV.backgroundColor = kColorTableSectionBg;
return headerV;
}
@end