|
| 1 | +// |
| 2 | +// MRPRListViewController.m |
| 3 | +// Coding_iOS |
| 4 | +// |
| 5 | +// Created by Ease on 15/5/29. |
| 6 | +// Copyright (c) 2015年 Coding. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#define MRPRListViewController_BottomViewHeight 49.0 |
| 10 | + |
| 11 | +#import "MRPRListViewController.h" |
| 12 | +#import "ODRefreshControl.h" |
| 13 | +#import "SVPullToRefresh.h" |
| 14 | +#import "MRPRS.h" |
| 15 | +#import "Coding_NetAPIManager.h" |
| 16 | +#import "MRPRListCell.h" |
| 17 | + |
| 18 | +@interface MRPRListViewController ()<UITableViewDataSource, UITableViewDelegate> |
| 19 | +@property (strong, nonatomic) NSMutableDictionary *dataDict; |
| 20 | +@property (strong, nonatomic) UITableView *myTableView; |
| 21 | +@property (nonatomic, strong) ODRefreshControl *myRefreshControl; |
| 22 | +@property (nonatomic, assign) NSInteger selectedIndex; |
| 23 | + |
| 24 | +@property (strong, nonatomic) UIView *bottomView; |
| 25 | +@property (strong, nonatomic) UISegmentedControl *mySegmentedControl; |
| 26 | +@end |
| 27 | + |
| 28 | +@implementation MRPRListViewController |
| 29 | +- (void)viewDidLoad{ |
| 30 | + [super viewDidLoad]; |
| 31 | + self.title = _curProject.is_public.boolValue? @"Pull Requests": @"Merge Requests"; |
| 32 | + _dataDict = [NSMutableDictionary new]; |
| 33 | + |
| 34 | + _myTableView = ({ |
| 35 | + UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; |
| 36 | + tableView.dataSource = self; |
| 37 | + tableView.delegate = self; |
| 38 | + tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
| 39 | + [tableView registerClass:[MRPRListCell class] forCellReuseIdentifier:kCellIdentifier_MRPRListCell]; |
| 40 | + [self.view addSubview:tableView]; |
| 41 | + [tableView mas_makeConstraints:^(MASConstraintMaker *make) { |
| 42 | + make.edges.equalTo(self.view); |
| 43 | + }]; |
| 44 | + UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, MRPRListViewController_BottomViewHeight, 0); |
| 45 | + tableView.contentInset = insets; |
| 46 | + tableView.scrollIndicatorInsets = insets; |
| 47 | + tableView; |
| 48 | + }); |
| 49 | + _myRefreshControl = [[ODRefreshControl alloc] initInScrollView:self.myTableView]; |
| 50 | + [_myRefreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged]; |
| 51 | + |
| 52 | + __weak typeof(self) weakSelf = self; |
| 53 | + [_myTableView addInfiniteScrollingWithActionHandler:^{ |
| 54 | + [weakSelf refreshMore:YES]; |
| 55 | + }]; |
| 56 | + [self configBottomView]; |
| 57 | + |
| 58 | + self.selectedIndex = 0; |
| 59 | +} |
| 60 | + |
| 61 | +- (void)setSelectedIndex:(NSInteger)selectedIndex{ |
| 62 | + _selectedIndex = selectedIndex; |
| 63 | + if (self.mySegmentedControl.selectedSegmentIndex != _selectedIndex) { |
| 64 | + [self.mySegmentedControl setSelectedSegmentIndex:_selectedIndex]; |
| 65 | + } |
| 66 | + [self.myTableView reloadData]; |
| 67 | + |
| 68 | + __weak typeof(self) weakSelf = self; |
| 69 | + [self.view configBlankPage:EaseBlankPageTypeView hasData:YES hasError:NO reloadButtonBlock:^(id sender) { |
| 70 | + [weakSelf refreshMore:NO]; |
| 71 | + }]; |
| 72 | + |
| 73 | + if ([self curMRPRS].list.count <= 0) { |
| 74 | + [self refresh]; |
| 75 | + }else{ |
| 76 | + self.myTableView.showsInfiniteScrolling = [self curMRPRS].canLoadMore; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +- (MRPRS *)curMRPRS{ |
| 81 | + MRPRS *curMRPRS = [_dataDict objectForKey:@(_selectedIndex)]; |
| 82 | + if (!curMRPRS) { |
| 83 | + MRPRSType type = _curProject.is_public.boolValue? _selectedIndex +4: _selectedIndex +1; |
| 84 | + curMRPRS = [MRPRS MRPRSWithType:type userGK:_curProject.owner_user_name projectName:_curProject.name]; |
| 85 | + [_dataDict setObject:curMRPRS forKey:@(_selectedIndex)]; |
| 86 | + } |
| 87 | + return curMRPRS; |
| 88 | +} |
| 89 | + |
| 90 | +- (void)refresh{ |
| 91 | + [self refreshMore:NO]; |
| 92 | +} |
| 93 | +- (void)refreshMore:(BOOL)willLoadMore{ |
| 94 | + MRPRS *curMRPRS = [self curMRPRS]; |
| 95 | + if (curMRPRS.isLoading) { |
| 96 | + return; |
| 97 | + } |
| 98 | + if (willLoadMore && !curMRPRS.canLoadMore) { |
| 99 | + [_myTableView.infiniteScrollingView stopAnimating]; |
| 100 | + return; |
| 101 | + } |
| 102 | + curMRPRS.willLoadMore = willLoadMore; |
| 103 | + [self sendRequest:curMRPRS]; |
| 104 | +} |
| 105 | + |
| 106 | +- (void)sendRequest:(MRPRS *)curMRPRS{ |
| 107 | + if (curMRPRS.list.count <= 0) { |
| 108 | + [self.view beginLoading]; |
| 109 | + } |
| 110 | + __weak typeof(self) weakSelf = self; |
| 111 | + __weak typeof(curMRPRS) weakMRPRS = curMRPRS; |
| 112 | + [[Coding_NetAPIManager sharedManager] request_MRPRS_WithObj:curMRPRS andBlock:^(MRPRS *data, NSError *error) { |
| 113 | + [weakSelf.view endLoading]; |
| 114 | + [weakSelf.myRefreshControl endRefreshing]; |
| 115 | + [weakSelf.myTableView.infiniteScrollingView stopAnimating]; |
| 116 | + if (data) { |
| 117 | + [weakMRPRS configWithMRPRS:data]; |
| 118 | + [weakSelf.myTableView reloadData]; |
| 119 | + weakSelf.myTableView.showsInfiniteScrolling = [weakSelf curMRPRS].canLoadMore; |
| 120 | + } |
| 121 | + [weakSelf.view configBlankPage:EaseBlankPageTypeView hasData:([weakSelf curMRPRS].list.count > 0) hasError:(error != nil) reloadButtonBlock:^(id sender) { |
| 122 | + [weakSelf refreshMore:NO]; |
| 123 | + }]; |
| 124 | + }]; |
| 125 | +} |
| 126 | + |
| 127 | +#pragma mark Segment |
| 128 | +- (void)configBottomView{ |
| 129 | + if (!_bottomView) { |
| 130 | + _bottomView = [UIView new]; |
| 131 | + _bottomView.backgroundColor = self.view.backgroundColor; |
| 132 | + [_bottomView addLineUp:YES andDown:NO]; |
| 133 | + [self.view addSubview:_bottomView]; |
| 134 | + [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) { |
| 135 | + make.left.right.bottom.equalTo(self.view); |
| 136 | + make.height.mas_equalTo(MRPRListViewController_BottomViewHeight); |
| 137 | + }]; |
| 138 | + } |
| 139 | + if (!_mySegmentedControl) { |
| 140 | + _mySegmentedControl = ({ |
| 141 | + UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"Open", @"Closed"]]; |
| 142 | + segmentedControl.tintColor = [UIColor colorWithHexString:@"0x3bbd79"]; |
| 143 | + [segmentedControl setTitleTextAttributes:@{ |
| 144 | + NSFontAttributeName: [UIFont boldSystemFontOfSize:16], |
| 145 | + NSForegroundColorAttributeName: [UIColor whiteColor] |
| 146 | + } |
| 147 | + forState:UIControlStateSelected]; |
| 148 | + [segmentedControl setTitleTextAttributes:@{ |
| 149 | + NSFontAttributeName: [UIFont boldSystemFontOfSize:16], |
| 150 | + NSForegroundColorAttributeName: [UIColor colorWithHexString:@"0x3bbd79"] |
| 151 | + } forState:UIControlStateNormal]; |
| 152 | + [segmentedControl addTarget:self action:@selector(segmentedControlSelected:) forControlEvents:UIControlEventValueChanged]; |
| 153 | + segmentedControl; |
| 154 | + }); |
| 155 | + _mySegmentedControl.frame = CGRectMake(kPaddingLeftWidth, (MRPRListViewController_BottomViewHeight - 30)/2, kScreen_Width - 2*kPaddingLeftWidth, 30); |
| 156 | + [_bottomView addSubview:_mySegmentedControl]; |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +- (void)segmentedControlSelected:(id)sender{ |
| 161 | + UISegmentedControl *segmentedControl = (UISegmentedControl *)sender; |
| 162 | + self.selectedIndex = segmentedControl.selectedSegmentIndex; |
| 163 | +} |
| 164 | + |
| 165 | +#pragma mark TableM |
| 166 | +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ |
| 167 | + return [self curMRPRS].list.count; |
| 168 | +} |
| 169 | +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 170 | + MRPR *curMRPR = [[self curMRPRS].list objectAtIndex:indexPath.row]; |
| 171 | + MRPRListCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_MRPRListCell forIndexPath:indexPath]; |
| 172 | + [cell configWithMRPR:curMRPR]; |
| 173 | + [tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:60]; |
| 174 | + return cell; |
| 175 | +} |
| 176 | + |
| 177 | +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 178 | + return [MRPRListCell cellHeight]; |
| 179 | +} |
| 180 | +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 181 | + [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 182 | + MRPR *curMRPR = [[self curMRPRS].list objectAtIndex:indexPath.row]; |
| 183 | + NSLog(@"%@", curMRPR.title); |
| 184 | +} |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | +@end |
0 commit comments