forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPRListViewController.m
More file actions
212 lines (187 loc) · 8.19 KB
/
PRListViewController.m
File metadata and controls
212 lines (187 loc) · 8.19 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
//
// MRPRListViewController.m
// Coding_iOS
//
// Created by Ease on 15/5/29.
// Copyright (c) 2015年 Coding. All rights reserved.
//
#define kMRPRListViewController_BottomViewHeight 49.0
#import "PRListViewController.h"
#import "ODRefreshControl.h"
#import "SVPullToRefresh.h"
#import "MRPRS.h"
#import "Coding_NetAPIManager.h"
#import "MRPRListCell.h"
#import "PRDetailViewController.h"
#import "MRDetailViewController.h"
@interface PRListViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) NSMutableDictionary *dataDict;
@property (strong, nonatomic) UITableView *myTableView;
@property (nonatomic, strong) ODRefreshControl *myRefreshControl;
@property (nonatomic, assign) NSInteger selectedIndex;
@property (strong, nonatomic) UIView *bottomView;
@property (strong, nonatomic) UISegmentedControl *mySegmentedControl;
@end
@implementation PRListViewController
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = kColorTableBG;
self.title = @"Pull Requests";
_dataDict = [NSMutableDictionary new];
_myTableView = ({
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor clearColor];
tableView.dataSource = self;
tableView.delegate = self;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[tableView registerClass:[MRPRListCell class] forCellReuseIdentifier:kCellIdentifier_MRPRListCell];
[self.view addSubview:tableView];
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, kMRPRListViewController_BottomViewHeight, 0);
tableView.contentInset = insets;
tableView.scrollIndicatorInsets = insets;
tableView;
});
_myRefreshControl = [[ODRefreshControl alloc] initInScrollView:self.myTableView];
[_myRefreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
__weak typeof(self) weakSelf = self;
[_myTableView addInfiniteScrollingWithActionHandler:^{
[weakSelf refreshMore:YES];
}];
[self configBottomView];
self.selectedIndex = 0;
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if([self curMRPRS].list.count <= 0) {
[self.view configBlankPage:EaseBlankPageTypeTaskResource hasData:NO hasError:NO reloadButtonBlock:nil];
}
[self refresh];
}
- (void)setSelectedIndex:(NSInteger)selectedIndex{
_selectedIndex = selectedIndex;
if (self.mySegmentedControl.selectedSegmentIndex != _selectedIndex) {
[self.mySegmentedControl setSelectedSegmentIndex:_selectedIndex];
}
[self.myTableView reloadData];
__weak typeof(self) weakSelf = self;
[self.view configBlankPage:EaseBlankPageTypeView hasData:YES hasError:NO reloadButtonBlock:^(id sender) {
[weakSelf refreshMore:NO];
}];
if ([self curMRPRS].list.count <= 0) {
[self refresh];
}else{
self.myTableView.showsInfiniteScrolling = [self curMRPRS].canLoadMore;
}
}
- (MRPRS *)curMRPRS{
MRPRS *curMRPRS = [_dataDict objectForKey:@(_selectedIndex)];
if (!curMRPRS) {
curMRPRS = [[MRPRS alloc] initWithType:MRPRSTypePR statusIsOpen:_selectedIndex == 0 userGK:_curProject.owner_user_name projectName:_curProject.name];
[_dataDict setObject:curMRPRS forKey:@(_selectedIndex)];
}
return curMRPRS;
}
- (void)refresh{
[self refreshMore:NO];
}
- (void)refreshMore:(BOOL)willLoadMore{
MRPRS *curMRPRS = [self curMRPRS];
if (curMRPRS.isLoading) {
return;
}
if (willLoadMore && !curMRPRS.canLoadMore) {
[_myTableView.infiniteScrollingView stopAnimating];
return;
}
curMRPRS.willLoadMore = willLoadMore;
[self sendRequest:curMRPRS];
}
- (void)sendRequest:(MRPRS *)curMRPRS{
if (curMRPRS.list.count <= 0) {
[self.view beginLoading];
}
__weak typeof(self) weakSelf = self;
__weak typeof(curMRPRS) weakMRPRS = curMRPRS;
[[Coding_NetAPIManager sharedManager] request_MRPRS_WithObj:curMRPRS andBlock:^(MRPRS *data, NSError *error) {
[weakSelf.view endLoading];
[weakSelf.myRefreshControl endRefreshing];
[weakSelf.myTableView.infiniteScrollingView stopAnimating];
if (data) {
[weakMRPRS configWithMRPRS:data];
[weakSelf.myTableView reloadData];
weakSelf.myTableView.showsInfiniteScrolling = [weakSelf curMRPRS].canLoadMore;
}
[weakSelf.view configBlankPage:EaseBlankPageTypeView hasData:([weakSelf curMRPRS].list.count > 0) hasError:(error != nil) reloadButtonBlock:^(id sender) {
[weakSelf refreshMore:NO];
}];
}];
}
#pragma mark Segment
- (void)configBottomView{
if (!_bottomView) {
_bottomView = [UIView new];
_bottomView.backgroundColor = self.view.backgroundColor;
[_bottomView addLineUp:YES andDown:NO];
[self.view addSubview:_bottomView];
[_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.equalTo(self.view);
make.height.mas_equalTo(kMRPRListViewController_BottomViewHeight);
}];
}
if (!_mySegmentedControl) {
_mySegmentedControl = ({
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"Open", @"Closed"]];
segmentedControl.tintColor = kColorBrandGreen;
[segmentedControl setTitleTextAttributes:@{
NSFontAttributeName: [UIFont boldSystemFontOfSize:16],
NSForegroundColorAttributeName: [UIColor whiteColor]
}
forState:UIControlStateSelected];
[segmentedControl setTitleTextAttributes:@{
NSFontAttributeName: [UIFont boldSystemFontOfSize:16],
NSForegroundColorAttributeName: kColorBrandGreen
} forState:UIControlStateNormal];
[segmentedControl addTarget:self action:@selector(segmentedControlSelected:) forControlEvents:UIControlEventValueChanged];
segmentedControl;
});
_mySegmentedControl.frame = CGRectMake(kPaddingLeftWidth, (kMRPRListViewController_BottomViewHeight - 30)/2, kScreen_Width - 2*kPaddingLeftWidth, 30);
[_bottomView addSubview:_mySegmentedControl];
}
}
- (void)segmentedControlSelected:(id)sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
self.selectedIndex = segmentedControl.selectedSegmentIndex;
}
#pragma mark TableM
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self curMRPRS].list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MRPR *curMRPR = [[self curMRPRS].list objectAtIndex:indexPath.row];
MRPRListCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_MRPRListCell forIndexPath:indexPath];
cell.curMRPR = curMRPR;
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:60];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [MRPRListCell cellHeight];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
MRPR *curMRPR = [[self curMRPRS].list objectAtIndex:indexPath.row];
if(curMRPR.isPR) {
PRDetailViewController *vc = [PRDetailViewController new];
vc.curMRPR = curMRPR;
vc.curProject = _curProject;
[self.navigationController pushViewController:vc animated:YES];
} else {
MRDetailViewController *vc = [MRDetailViewController new];
vc.curMRPR = curMRPR;
vc.curProject = _curProject;
[self.navigationController pushViewController:vc animated:YES];
}
}
@end