|
| 1 | +// |
| 2 | +// NProjectViewController.m |
| 3 | +// Coding_iOS |
| 4 | +// |
| 5 | +// Created by Ease on 15/3/11. |
| 6 | +// Copyright (c) 2015年 Coding. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "NProjectViewController.h" |
| 10 | +#import "ProjectInfoCell.h" |
| 11 | +#import "ProjectItemsCell.h" |
| 12 | +#import "ProjectDescriptionCell.h" |
| 13 | +#import "ProjectViewController.h" |
| 14 | +#import "Coding_NetAPIManager.h" |
| 15 | +#import "ODRefreshControl.h" |
| 16 | + |
| 17 | +@interface NProjectViewController ()<UITableViewDataSource, UITableViewDelegate> |
| 18 | +@property (nonatomic, strong) UITableView *myTableView; |
| 19 | +@property (nonatomic, strong) ODRefreshControl *refreshControl; |
| 20 | + |
| 21 | +@end |
| 22 | + |
| 23 | +@implementation NProjectViewController |
| 24 | +- (void)viewDidLoad{ |
| 25 | + [super viewDidLoad]; |
| 26 | + |
| 27 | + self.title = @"项目首页"; |
| 28 | + // 添加myTableView |
| 29 | + _myTableView = ({ |
| 30 | + UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; |
| 31 | + tableView.backgroundColor = [UIColor clearColor]; |
| 32 | + tableView.dataSource = self; |
| 33 | + tableView.delegate = self; |
| 34 | + tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
| 35 | + [tableView registerClass:[ProjectInfoCell class] forCellReuseIdentifier:kCellIdentifier_ProjectInfoCell]; |
| 36 | + [tableView registerClass:[ProjectItemsCell class] forCellReuseIdentifier:kCellIdentifier_ProjectItemsCell_Private]; |
| 37 | + [tableView registerClass:[ProjectItemsCell class] forCellReuseIdentifier:kCellIdentifier_ProjectItemsCell_Public]; |
| 38 | + [tableView registerClass:[ProjectDescriptionCell class] forCellReuseIdentifier:kCellIdentifier_ProjectDescriptionCell]; |
| 39 | + [self.view addSubview:tableView]; |
| 40 | + [tableView mas_makeConstraints:^(MASConstraintMaker *make) { |
| 41 | + make.edges.equalTo(self.view); |
| 42 | + }]; |
| 43 | + tableView; |
| 44 | + }); |
| 45 | + |
| 46 | + _refreshControl = [[ODRefreshControl alloc] initInScrollView:self.myTableView]; |
| 47 | + [_refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged]; |
| 48 | + |
| 49 | + [self refresh]; |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +- (void)refresh{ |
| 54 | + if (_myProject.isLoadingDetail) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + __weak typeof(self) weakSelf = self; |
| 59 | + [[Coding_NetAPIManager sharedManager] request_ProjectDetail_WithObj:_myProject andBlock:^(id data, NSError *error) { |
| 60 | + [weakSelf.refreshControl endRefreshing]; |
| 61 | + if (data) { |
| 62 | + weakSelf.myProject = data; |
| 63 | + [weakSelf.myTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone]; |
| 64 | + } |
| 65 | + }]; |
| 66 | +} |
| 67 | + |
| 68 | +#pragma mark Table M |
| 69 | + |
| 70 | +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ |
| 71 | + NSInteger section = 0; |
| 72 | + if (_myProject.is_public) { |
| 73 | + section = _myProject.is_public.boolValue? 2: 1; |
| 74 | + } |
| 75 | + return section; |
| 76 | +} |
| 77 | + |
| 78 | +- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ |
| 79 | + UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 20)]; |
| 80 | + footerView.backgroundColor = [UIColor colorWithHexString:@"0xe5e5e5"]; |
| 81 | + [footerView addLineUp:YES andDown:NO andColor:tableView.separatorColor]; |
| 82 | + return footerView; |
| 83 | +} |
| 84 | + |
| 85 | +- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ |
| 86 | + if (section == _myProject.is_public.boolValue? 1: 0) { |
| 87 | + return 0; |
| 88 | + } |
| 89 | + return 20; |
| 90 | +} |
| 91 | + |
| 92 | +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ |
| 93 | + NSInteger row = 0; |
| 94 | + if (_myProject.is_public) { |
| 95 | + switch (section) { |
| 96 | + case 0: |
| 97 | + row = 2; |
| 98 | + break; |
| 99 | + case 1: |
| 100 | + default: |
| 101 | + row = _myProject.is_public.boolValue? 1: 0; |
| 102 | + break; |
| 103 | + } |
| 104 | + } |
| 105 | + return row; |
| 106 | +} |
| 107 | + |
| 108 | +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 109 | + if (_myProject.is_public.boolValue) { |
| 110 | + if (indexPath.section == 0) { |
| 111 | + if (indexPath.row == 0) { |
| 112 | + ProjectInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_ProjectInfoCell forIndexPath:indexPath]; |
| 113 | + cell.curProject = _myProject; |
| 114 | + return cell; |
| 115 | + }else{ |
| 116 | + ProjectDescriptionCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_ProjectDescriptionCell forIndexPath:indexPath]; |
| 117 | + cell.curProject = _myProject; |
| 118 | + cell.gitButtonClickedBlock = ^(NSInteger index){ |
| 119 | + [self gitButtonClicked:index]; |
| 120 | + }; |
| 121 | + return cell; |
| 122 | + } |
| 123 | + }else{ |
| 124 | + ProjectItemsCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_ProjectItemsCell_Public forIndexPath:indexPath]; |
| 125 | + cell.curProject = _myProject; |
| 126 | + cell.itemClickedBlock = ^(NSInteger index){ |
| 127 | + [self goToIndex:index]; |
| 128 | + }; |
| 129 | + return cell; |
| 130 | + } |
| 131 | + }else{ |
| 132 | + if (indexPath.row == 0) { |
| 133 | + ProjectInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_ProjectInfoCell forIndexPath:indexPath]; |
| 134 | + cell.curProject = _myProject; |
| 135 | + return cell; |
| 136 | + }else{ |
| 137 | + ProjectItemsCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_ProjectItemsCell_Private forIndexPath:indexPath]; |
| 138 | + cell.curProject = _myProject; |
| 139 | + cell.itemClickedBlock = ^(NSInteger index){ |
| 140 | + [self goToIndex:index]; |
| 141 | + }; |
| 142 | + return cell; |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 148 | + CGFloat cellHeight = 0; |
| 149 | + if (_myProject.is_public.boolValue) { |
| 150 | + if (indexPath.section == 0) { |
| 151 | + cellHeight = indexPath.row == 0? [ProjectInfoCell cellHeight]: [ProjectDescriptionCell cellHeightWithObj:_myProject]; |
| 152 | + }else{ |
| 153 | + cellHeight = [ProjectItemsCell cellHeightWithObj:_myProject]; |
| 154 | + } |
| 155 | + }else{ |
| 156 | + cellHeight = indexPath.row == 0? [ProjectInfoCell cellHeight]: [ProjectItemsCell cellHeightWithObj:_myProject]; |
| 157 | + } |
| 158 | + return cellHeight; |
| 159 | +} |
| 160 | + |
| 161 | +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 162 | + [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 163 | +} |
| 164 | + |
| 165 | +#pragma mark goTo VC |
| 166 | +- (void)goToIndex:(NSInteger)index{ |
| 167 | + ProjectViewController *vc = [[ProjectViewController alloc] init]; |
| 168 | + vc.myProject = self.myProject; |
| 169 | + vc.curIndex = index; |
| 170 | + [self.navigationController pushViewController:vc animated:YES]; |
| 171 | +} |
| 172 | + |
| 173 | +#pragma mark Git_Btn |
| 174 | +- (void)gitButtonClicked:(NSInteger)index{ |
| 175 | + __weak typeof(self) weakSelf = self; |
| 176 | + switch (index) { |
| 177 | + case 0://Star |
| 178 | + { |
| 179 | + if (!_myProject.isStaring) { |
| 180 | + [[Coding_NetAPIManager sharedManager] request_StarProject:_myProject andBlock:^(id data, NSError *error) { |
| 181 | + [weakSelf.myTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone]; |
| 182 | + }]; |
| 183 | + } |
| 184 | + } |
| 185 | + break; |
| 186 | + case 1://Watch |
| 187 | + { |
| 188 | + if (!_myProject.isWatching) { |
| 189 | + [[Coding_NetAPIManager sharedManager] request_WatchProject:_myProject andBlock:^(id data, NSError *error) { |
| 190 | + [weakSelf.myTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone]; |
| 191 | + }]; |
| 192 | + } |
| 193 | + } |
| 194 | + break; |
| 195 | + default://Fork |
| 196 | + { |
| 197 | + if (_myProject.forked.boolValue |
| 198 | + || [_myProject.owner_user_name isEqualToString:[Login curLoginUser].global_key]) { |
| 199 | + return; |
| 200 | + }else{ |
| 201 | + |
| 202 | + [[Coding_NetAPIManager sharedManager] request_ForkProject:_myProject andBlock:^(id data, NSError *error) { |
| 203 | + [weakSelf.myTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone]; |
| 204 | + if (data) { |
| 205 | + NProjectViewController *vc = [[NProjectViewController alloc] init]; |
| 206 | + vc.myProject = data; |
| 207 | + [weakSelf.navigationController pushViewController:vc animated:YES]; |
| 208 | + } |
| 209 | + }]; |
| 210 | + } |
| 211 | + } |
| 212 | + break; |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +@end |
0 commit comments