|
6 | 6 | // Copyright (c) 2015年 Coding. All rights reserved. |
7 | 7 | // |
8 | 8 |
|
| 9 | +#define kMRPRDetailViewController_BottomViewHeight 49.0 |
| 10 | + |
9 | 11 | #import "MRPRDetailViewController.h" |
| 12 | +#import "Coding_NetAPIManager.h" |
| 13 | +#import "ODRefreshControl.h" |
| 14 | + |
| 15 | +#import "MRPRTopCell.h" |
| 16 | +#import "MRPRDetailCell.h" |
| 17 | +#import "MRPRDisclosureCell.h" |
| 18 | +#import "MRPRCommentCell.h" |
| 19 | +#import "AddCommentCell.h" |
| 20 | + |
| 21 | +#import "WebViewController.h" |
| 22 | +#import "MJPhotoBrowser.h" |
| 23 | + |
| 24 | +#import "MRPRCommitsViewController.h" |
| 25 | +#import "MRPRFilesViewController.h" |
| 26 | +#import "AddMDCommentViewController.h" |
| 27 | + |
| 28 | +@interface MRPRDetailViewController ()<UITableViewDataSource, UITableViewDelegate, TTTAttributedLabelDelegate> |
| 29 | +@property (strong, nonatomic) MRPRBaseInfo *curMRPRInfo; |
| 30 | +@property (strong, nonatomic) UITableView *myTableView; |
| 31 | +@property (nonatomic, strong) ODRefreshControl *myRefreshControl; |
| 32 | +@property (strong, nonatomic) UIView *bottomView; |
| 33 | +@end |
10 | 34 |
|
11 | 35 | @implementation MRPRDetailViewController |
| 36 | +- (void)viewDidLoad{ |
| 37 | + [super viewDidLoad]; |
| 38 | + self.title = [NSString stringWithFormat:@"#%@", _curMRPR.iid.stringValue]; |
| 39 | + |
| 40 | + _myTableView = ({ |
| 41 | + UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; |
| 42 | + tableView.backgroundColor = kColorTableSectionBg; |
| 43 | + tableView.dataSource = self; |
| 44 | + tableView.delegate = self; |
| 45 | + tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
| 46 | + |
| 47 | + [tableView registerClass:[MRPRTopCell class] forCellReuseIdentifier:kCellIdentifier_MRPRTopCell]; |
| 48 | + [tableView registerClass:[MRPRDetailCell class] forCellReuseIdentifier:kCellIdentifier_MRPRDetailCell]; |
| 49 | + [tableView registerClass:[MRPRDisclosureCell class] forCellReuseIdentifier:kCellIdentifier_MRPRDisclosureCell]; |
| 50 | + [tableView registerClass:[MRPRCommentCell class] forCellReuseIdentifier:kCellIdentifier_MRPRCommentCell]; |
| 51 | + [tableView registerClass:[MRPRCommentCell class] forCellReuseIdentifier:kCellIdentifier_MRPRCommentCell_Media]; |
| 52 | + [tableView registerClass:[AddCommentCell class] forCellReuseIdentifier:kCellIdentifier_AddCommentCell]; |
| 53 | + |
| 54 | + [self.view addSubview:tableView]; |
| 55 | + [tableView mas_makeConstraints:^(MASConstraintMaker *make) { |
| 56 | + make.edges.equalTo(self.view); |
| 57 | + }]; |
| 58 | + tableView; |
| 59 | + }); |
| 60 | + _myRefreshControl = [[ODRefreshControl alloc] initInScrollView:self.myTableView]; |
| 61 | + [_myRefreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged]; |
| 62 | + [self refresh]; |
| 63 | +} |
| 64 | + |
| 65 | +- (void)configBottomView{ |
| 66 | + if (_bottomView) { |
| 67 | + [_bottomView removeFromSuperview]; |
| 68 | + } |
| 69 | + if (_curMRPR.status <= MRPRStatusCannotMerge) { |
| 70 | + _bottomView = [UIView new]; |
| 71 | + _bottomView.backgroundColor = [UIColor redColor]; |
| 72 | + [self.view addSubview:_bottomView]; |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) { |
| 78 | + make.left.right.bottom.equalTo(self.view); |
| 79 | + make.height.mas_equalTo(kMRPRDetailViewController_BottomViewHeight); |
| 80 | + }]; |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, |
| 86 | + _curMRPR.status <= MRPRStatusCannotMerge? kMRPRDetailViewController_BottomViewHeight: 0, |
| 87 | + 0); |
| 88 | + _myTableView.contentInset = insets; |
| 89 | + _myTableView.scrollIndicatorInsets = insets; |
| 90 | + |
| 91 | +} |
| 92 | +- (void)refresh{ |
| 93 | + if (_curMRPR.isLoading) { |
| 94 | + return; |
| 95 | + } |
| 96 | + if (!_curMRPRInfo) { |
| 97 | + [self.view beginLoading]; |
| 98 | + } |
| 99 | + __weak typeof(self) weakSelf = self; |
| 100 | + [[Coding_NetAPIManager sharedManager] request_MRPRBaseInfo_WithObj:_curMRPR andBlock:^(MRPRBaseInfo *data, NSError *error) { |
| 101 | + [weakSelf.view endLoading]; |
| 102 | + [weakSelf.myRefreshControl endRefreshing]; |
| 103 | + if (data) { |
| 104 | + weakSelf.curMRPRInfo = data; |
| 105 | + [weakSelf.myTableView reloadData]; |
| 106 | + } |
| 107 | + [weakSelf.view configBlankPage:EaseBlankPageTypeView hasData:(_curMRPRInfo != nil) hasError:(error != nil) reloadButtonBlock:^(id sender) { |
| 108 | + [weakSelf refresh]; |
| 109 | + }]; |
| 110 | + }]; |
| 111 | +} |
| 112 | + |
| 113 | +#pragma mark TableM Footer Header |
| 114 | +- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ |
| 115 | + return 20.0; |
| 116 | +} |
| 117 | +- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ |
| 118 | + return 0.5; |
| 119 | +} |
| 120 | + |
| 121 | +- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ |
| 122 | + UIView *view = [UIView new]; |
| 123 | + view.backgroundColor = [UIColor colorWithHexString:@"0xe5e5e5"]; |
| 124 | + return view; |
| 125 | +} |
| 126 | + |
| 127 | +#pragma mark TableM |
| 128 | +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ |
| 129 | + return _curMRPRInfo == nil? 0: _curMRPRInfo.discussions.count <= 0? 3: 4; |
| 130 | +} |
| 131 | +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ |
| 132 | + NSInteger row = 0; |
| 133 | + if (section == 0 || section == 1) { |
| 134 | + row = 2; |
| 135 | + }else if (_curMRPRInfo.discussions.count > 0 && section == 2){ |
| 136 | + row = _curMRPRInfo.discussions.count; |
| 137 | + }else{ |
| 138 | + row = 1; |
| 139 | + } |
| 140 | + return row; |
| 141 | +} |
| 142 | + |
| 143 | +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 144 | + __weak typeof(self) weakSelf = self; |
| 145 | + if (indexPath.section == 0) {//Content |
| 146 | + if (indexPath.row == 0) { |
| 147 | + MRPRTopCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_MRPRTopCell forIndexPath:indexPath]; |
| 148 | + cell.curMRPRInfo = _curMRPRInfo; |
| 149 | + return cell; |
| 150 | + }else{ |
| 151 | + MRPRDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_MRPRDetailCell forIndexPath:indexPath]; |
| 152 | + cell.curMRPRInfo = _curMRPRInfo; |
| 153 | + cell.cellHeightChangedBlock = ^(){ |
| 154 | + [weakSelf.myTableView reloadData]; |
| 155 | + }; |
| 156 | + cell.loadRequestBlock = ^(NSURLRequest *curRequest){ |
| 157 | + [weakSelf loadRequest:curRequest]; |
| 158 | + }; |
| 159 | + return cell; |
| 160 | + } |
| 161 | + }else if (indexPath.section == 1){//Disclosure |
| 162 | + MRPRDisclosureCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_MRPRDisclosureCell forIndexPath:indexPath]; |
| 163 | + if (indexPath.row == 0) { |
| 164 | + [cell setImageStr:@"mrpr_icon_commit" andTitle:@"提交记录"]; |
| 165 | + }else{ |
| 166 | + [cell setImageStr:@"mrpr_icon_fileChange" andTitle:@"文件改动"]; |
| 167 | + } |
| 168 | + [tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:50]; |
| 169 | + return cell; |
| 170 | + }else if (_curMRPRInfo.discussions.count > 0 && indexPath.section == 2){//Comment |
| 171 | + MRPRCommentItem *curCommentItem = [[_curMRPRInfo.discussions objectAtIndex:indexPath.row] firstObject]; |
| 172 | + MRPRCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:curCommentItem.htmlMedia.imageItems.count> 0? kCellIdentifier_MRPRCommentCell_Media: kCellIdentifier_MRPRCommentCell forIndexPath:indexPath]; |
| 173 | + cell.curItem = curCommentItem; |
| 174 | + cell.contentLabel.delegate = self; |
| 175 | + [tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:50]; |
| 176 | + return cell; |
| 177 | + }else{//Add Comment |
| 178 | + AddCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_AddCommentCell forIndexPath:indexPath]; |
| 179 | + [tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:50]; |
| 180 | + return cell; |
| 181 | + } |
| 182 | +} |
| 183 | +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 184 | + CGFloat cellHeight = 0; |
| 185 | + if (indexPath.section == 0) {//Content |
| 186 | + if (indexPath.row == 0) { |
| 187 | + return [MRPRTopCell cellHeightWithObj:_curMRPRInfo]; |
| 188 | + }else{ |
| 189 | + return [MRPRDetailCell cellHeightWithObj:_curMRPRInfo]; |
| 190 | + } |
| 191 | + }else if (indexPath.section == 1){//Disclosure |
| 192 | + return [MRPRDisclosureCell cellHeight]; |
| 193 | + }else if (_curMRPRInfo.discussions.count > 0 && indexPath.section == 2){//Comment |
| 194 | + MRPRCommentItem *curCommentItem = [[_curMRPRInfo.discussions objectAtIndex:indexPath.row] firstObject]; |
| 195 | + return [MRPRCommentCell cellHeightWithObj:curCommentItem]; |
| 196 | + }else{//Add Comment |
| 197 | + return [AddCommentCell cellHeight]; |
| 198 | + } |
| 199 | + return cellHeight; |
| 200 | +} |
| 201 | + |
| 202 | +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 203 | + [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 204 | + if (indexPath.section == 0) {//Content |
| 205 | + }else if (indexPath.section == 1){//Disclosure |
| 206 | + if (indexPath.row == 0) { |
| 207 | + MRPRCommitsViewController *vc = [MRPRCommitsViewController new]; |
| 208 | + vc.curMRPR = _curMRPR; |
| 209 | + [self.navigationController pushViewController:vc animated:YES]; |
| 210 | + }else{ |
| 211 | + MRPRFilesViewController *vc = [MRPRFilesViewController new]; |
| 212 | + vc.curMRPR = _curMRPR; |
| 213 | + [self.navigationController pushViewController:vc animated:YES]; |
| 214 | + } |
| 215 | + }else if (_curMRPRInfo.discussions.count > 0 && indexPath.section == 2){//Comment |
| 216 | + MRPRCommentItem *curCommentItem = [[_curMRPRInfo.discussions objectAtIndex:indexPath.row] firstObject]; |
| 217 | + DebugLog(@"%@", curCommentItem.content); |
| 218 | + }else{//Add Comment |
| 219 | + AddMDCommentViewController *vc = [AddMDCommentViewController new]; |
| 220 | + [self.navigationController pushViewController:vc animated:YES]; |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +#pragma mark loadCellRequest |
| 225 | +- (void)loadRequest:(NSURLRequest *)curRequest |
| 226 | +{ |
| 227 | + NSString *linkStr = curRequest.URL.absoluteString; |
| 228 | + [self analyseLinkStr:linkStr]; |
| 229 | +} |
| 230 | + |
| 231 | +- (void)analyseLinkStr:(NSString *)linkStr |
| 232 | +{ |
| 233 | + if (linkStr.length <= 0) { |
| 234 | + return; |
| 235 | + } |
| 236 | + UIViewController *vc = [BaseViewController analyseVCFromLinkStr:linkStr]; |
| 237 | + if (vc) { |
| 238 | + [self.navigationController pushViewController:vc animated:YES]; |
| 239 | + }else{ |
| 240 | + // 可能是图片链接 |
| 241 | + HtmlMedia *htmlMedia = self.curMRPRInfo.htmlMedia; |
| 242 | + if (htmlMedia.imageItems.count > 0) { |
| 243 | + for (HtmlMediaItem *item in htmlMedia.imageItems) { |
| 244 | + if ((item.src.length > 0 && [item.src isEqualToString:linkStr]) |
| 245 | + || (item.href.length > 0 && [item.href isEqualToString:linkStr])) { |
| 246 | + [MJPhotoBrowser showHtmlMediaItems:htmlMedia.imageItems originalItem:item]; |
| 247 | + return; |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + // 跳转去网页 |
| 252 | + WebViewController *webVc = [WebViewController webVCWithUrlStr:linkStr]; |
| 253 | + [self.navigationController pushViewController:webVc animated:YES]; |
| 254 | + } |
| 255 | +} |
| 256 | + |
| 257 | +#pragma mark TTTAttributedLabelDelegate |
| 258 | +- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTransitInformation:(NSDictionary *)components{ |
| 259 | + HtmlMediaItem *clickedItem = [components objectForKey:@"value"]; |
| 260 | + [self analyseLinkStr:clickedItem.href]; |
| 261 | +} |
| 262 | + |
12 | 263 |
|
13 | 264 | @end |
0 commit comments