|
| 1 | +// |
| 2 | +// EALocalCodeListViewController.m |
| 3 | +// Coding_iOS |
| 4 | +// |
| 5 | +// Created by Easeeeeeeeee on 2018/3/28. |
| 6 | +// Copyright © 2018年 Coding. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "EALocalCodeListViewController.h" |
| 10 | +#import "EALocalCodeViewController.h" |
| 11 | +#import "EALocalCodeListCell.h" |
| 12 | + |
| 13 | +@interface EALocalCodeListViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate> |
| 14 | +@property (nonatomic, strong) UITableView *myTableView; |
| 15 | +@property (strong, nonatomic) UISearchBar *mySearchBar; |
| 16 | + |
| 17 | +@property (strong, nonatomic) NSArray *fileList, *searchedFileList; |
| 18 | +@property (strong, nonatomic) NSMutableDictionary *isDirDict; |
| 19 | +@property (strong, nonatomic, readonly) NSArray *dataList; |
| 20 | +@property (assign, nonatomic, readonly) BOOL isSearching; |
| 21 | +@end |
| 22 | + |
| 23 | +@implementation EALocalCodeListViewController |
| 24 | + |
| 25 | +- (void)viewDidLoad { |
| 26 | + [super viewDidLoad]; |
| 27 | + // Do any additional setup after loading the view. |
| 28 | + self.title = _curURL.lastPathComponent; |
| 29 | + _myTableView = ({ |
| 30 | + UITableView *tableView = [UITableView new]; |
| 31 | + tableView.backgroundColor = [UIColor clearColor]; |
| 32 | + tableView.delegate = self; |
| 33 | + tableView.dataSource = self; |
| 34 | + [tableView registerClass:[EALocalCodeListCell class] forCellReuseIdentifier:[EALocalCodeListCell nameOfClass]]; |
| 35 | + tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
| 36 | + [self.view addSubview:tableView]; |
| 37 | + [tableView mas_makeConstraints:^(MASConstraintMaker *make) { |
| 38 | + make.edges.equalTo(self.view); |
| 39 | + }]; |
| 40 | + tableView.estimatedRowHeight = 0; |
| 41 | + tableView.estimatedSectionHeaderHeight = 0; |
| 42 | + tableView.estimatedSectionFooterHeight = 0; |
| 43 | + tableView; |
| 44 | + }); |
| 45 | + _mySearchBar = ({ |
| 46 | + UISearchBar *searchBar = [[UISearchBar alloc] init]; |
| 47 | + searchBar.delegate = self; |
| 48 | + [searchBar sizeToFit]; |
| 49 | + [searchBar setPlaceholder:@"寻找文件"]; |
| 50 | + searchBar; |
| 51 | + }); |
| 52 | + _myTableView.tableHeaderView = _mySearchBar; |
| 53 | + [self setupNavBtn]; |
| 54 | + [self setupData]; |
| 55 | +} |
| 56 | + |
| 57 | +- (void)setupData{ |
| 58 | + NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 59 | + self.fileList = [[fileManager contentsOfDirectoryAtURL:_curURL includingPropertiesForKeys:nil options:0 error:nil] sortedArrayUsingComparator:^NSComparisonResult(NSURL *obj1, NSURL *obj2) { |
| 60 | + NSDictionary *attr1 = [obj1 resourceValuesForKeys:@[NSURLIsDirectoryKey] error:nil]; |
| 61 | + BOOL isDir1 = [attr1[NSURLIsDirectoryKey] boolValue]; |
| 62 | + NSDictionary *attr2 = [obj2 resourceValuesForKeys:@[NSURLIsDirectoryKey] error:nil]; |
| 63 | + BOOL isDir2 = [attr2[NSURLIsDirectoryKey] boolValue]; |
| 64 | + NSComparisonResult result = [(isDir1? @0: @1) compare:(isDir2? @0: @1)]; |
| 65 | + if (result == NSOrderedSame) { |
| 66 | + result = [obj1.lastPathComponent compare:obj2.lastPathComponent]; |
| 67 | + } |
| 68 | + return result; |
| 69 | + }]; |
| 70 | +} |
| 71 | + |
| 72 | +- (BOOL)isSearching{ |
| 73 | + return ![_mySearchBar.text isEmpty]; |
| 74 | +} |
| 75 | + |
| 76 | +- (NSArray *)dataList{ |
| 77 | + return self.isSearching? _searchedFileList: _fileList; |
| 78 | +} |
| 79 | + |
| 80 | +#pragma mark Nav |
| 81 | +- (void)setupNavBtn{ |
| 82 | + if ([_curURL.absoluteString isEqualToString:_curPro.localURL.absoluteString]) {//根目录 |
| 83 | + self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"moreBtn_Nav"] style:UIBarButtonItemStylePlain target:self action:@selector(navBtnClicked)]; |
| 84 | + }else{ |
| 85 | + self.navigationItem.rightBarButtonItem = nil; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +- (void)navBtnClicked{ |
| 90 | + __weak typeof(self) weakSelf = self; |
| 91 | + [[UIActionSheet bk_actionSheetCustomWithTitle:nil buttonTitles:@[@"Pull"] destructiveTitle:@"删除 Repo" cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) { |
| 92 | + if (index == 0) { |
| 93 | + [weakSelf pullRepo]; |
| 94 | + }else if (index == 1){ |
| 95 | + [weakSelf deleteRepo]; |
| 96 | + } |
| 97 | + }] showInView:self.view]; |
| 98 | +} |
| 99 | + |
| 100 | +- (void)pullRepo{ |
| 101 | + if (![_curURL.absoluteString isEqualToString:_curPro.localURL.absoluteString]) {//不是根目录不 pull,任性 |
| 102 | + return; |
| 103 | + } |
| 104 | + __weak typeof(self) weakSelf = self; |
| 105 | + MBProgressHUD *hud = [NSObject showHUDQueryStr:@"正在 pull..."]; |
| 106 | + [_curPro gitPullBlock:^(BOOL result, NSString *tipStr) { |
| 107 | + [NSObject hideHUDQuery]; |
| 108 | + if (tipStr) { |
| 109 | + [NSObject showHudTipStr:tipStr]; |
| 110 | + }else{ |
| 111 | + [NSObject showHudTipStr:@"已更新"]; |
| 112 | + [weakSelf setupData]; |
| 113 | + } |
| 114 | + } progressBlock:^(const git_transfer_progress *progress, BOOL *stop) { |
| 115 | + hud.detailsLabelText = [NSString stringWithFormat:@"%d / %d", progress->received_objects, progress->total_objects]; |
| 116 | + }]; |
| 117 | +} |
| 118 | + |
| 119 | +- (void)deleteRepo{ |
| 120 | + [_curPro deleteLocalRepo]; |
| 121 | + [NSObject showHudTipStr:@"已删除"]; |
| 122 | + [self.navigationController popViewControllerAnimated:YES]; |
| 123 | +} |
| 124 | + |
| 125 | +#pragma mark ScrollView Delegate |
| 126 | +- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ |
| 127 | + if (scrollView == _myTableView) { |
| 128 | + [self.mySearchBar resignFirstResponder]; |
| 129 | + } |
| 130 | +} |
| 131 | +#pragma mark Table |
| 132 | + |
| 133 | +- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ |
| 134 | + return self.isSearching? 44.0: 0.0; |
| 135 | +} |
| 136 | + |
| 137 | +- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ |
| 138 | + if (self.isSearching) { |
| 139 | + UILabel *headerL = [UILabel labelWithSystemFontSize:14 textColorHexString:@"0xB5B5B5"]; |
| 140 | + headerL.frame = CGRectMake(0, 0, kScreen_Width, 40); |
| 141 | + headerL.backgroundColor = [UIColor whiteColor]; |
| 142 | + headerL.textAlignment = NSTextAlignmentCenter; |
| 143 | + headerL.text = [NSString stringWithFormat:@"共搜到 %lu 个与 \"%@\" 相关的文件", (unsigned long)self.searchedFileList.count, self.mySearchBar.text]; |
| 144 | + [headerL doBorderWidth:kLine_MinHeight color:kColorDDD cornerRadius:0]; |
| 145 | + return headerL; |
| 146 | + }else{ |
| 147 | + return [UIView new]; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ |
| 152 | + return self.dataList.count; |
| 153 | +} |
| 154 | + |
| 155 | +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 156 | + EALocalCodeListCell *cell = [tableView dequeueReusableCellWithIdentifier:[EALocalCodeListCell nameOfClass] forIndexPath:indexPath]; |
| 157 | + cell.curURL = self.dataList[indexPath.row]; |
| 158 | + cell.searchText = [_mySearchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; |
| 159 | + [tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth hasSectionLine:NO]; |
| 160 | + return cell; |
| 161 | +} |
| 162 | + |
| 163 | +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 164 | + return 44.0;; |
| 165 | +} |
| 166 | + |
| 167 | +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 168 | + [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 169 | + NSURL *itemURL = self.dataList[indexPath.row]; |
| 170 | + NSDictionary *attributes = [itemURL resourceValuesForKeys:@[NSURLIsDirectoryKey] error:nil]; |
| 171 | + BOOL isDir = [attributes[NSURLIsDirectoryKey] boolValue]; |
| 172 | + if (isDir) { |
| 173 | + EALocalCodeListViewController *vc = [EALocalCodeListViewController new]; |
| 174 | + vc.curPro = _curPro; |
| 175 | + vc.curRepo = _curRepo; |
| 176 | + vc.curURL = itemURL; |
| 177 | + [self.navigationController pushViewController:vc animated:YES]; |
| 178 | + }else{ |
| 179 | + EALocalCodeViewController *vc = [EALocalCodeViewController new]; |
| 180 | + vc.curPro = _curPro; |
| 181 | + vc.curRepo = _curRepo; |
| 182 | + vc.curURL = itemURL; |
| 183 | + [self.navigationController pushViewController:vc animated:YES]; |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +#pragma mark UISearchBarDelegate |
| 188 | + |
| 189 | +- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ |
| 190 | + [self searchFileWithStr:searchText]; |
| 191 | +} |
| 192 | + |
| 193 | +- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ |
| 194 | + [searchBar resignFirstResponder]; |
| 195 | + [self searchFileWithStr:searchBar.text]; |
| 196 | +} |
| 197 | + |
| 198 | +- (void)searchFileWithStr:(NSString *)string{ |
| 199 | + if ([string isEmpty]) { |
| 200 | + [self.myTableView reloadData]; |
| 201 | + }else{ |
| 202 | + NSString *strippedStr = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; |
| 203 | + if (strippedStr.length > 0) { |
| 204 | + [self updateFilteredContentForSearchString:strippedStr]; |
| 205 | + } |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +- (void)updateFilteredContentForSearchString:(NSString *)searchString{ |
| 210 | + self.searchedFileList = [self.fileList filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSURL * _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) { |
| 211 | + NSString *shortPath = evaluatedObject.lastPathComponent; |
| 212 | + return ([shortPath rangeOfString:searchString options:NSCaseInsensitiveSearch].location != NSNotFound); |
| 213 | + }]]; |
| 214 | + [self.myTableView reloadData]; |
| 215 | +} |
| 216 | + |
| 217 | +@end |
0 commit comments