forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalFilesViewController.m
More file actions
206 lines (187 loc) · 8.47 KB
/
LocalFilesViewController.m
File metadata and controls
206 lines (187 loc) · 8.47 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
//
// LocalFilesViewController.m
// Coding_iOS
//
// Created by Ease on 15/9/22.
// Copyright © 2015年 Coding. All rights reserved.
//
#import "LocalFilesViewController.h"
#import "LocalFileCell.h"
#import "LocalFileViewController.h"
#import "EaseToolBar.h"
@interface LocalFilesViewController ()<UITableViewDataSource, UITableViewDelegate, SWTableViewCellDelegate, EaseToolBarDelegate>
@property (strong, nonatomic) UITableView *myTableView;
@property (nonatomic, strong) EaseToolBar *myEditToolBar;
@end
@implementation LocalFilesViewController
- (void)viewDidLoad{
[super viewDidLoad];
self.title = self.projectName;
_myTableView = ({
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor clearColor];
tableView.delegate = self;
tableView.dataSource = self;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[tableView registerClass:[LocalFileCell class] forCellReuseIdentifier:kCellIdentifier_LocalFileCell];
tableView.sectionIndexBackgroundColor = [UIColor clearColor];
tableView.sectionIndexTrackingBackgroundColor = [UIColor clearColor];
tableView.sectionIndexColor = [UIColor colorWithHexString:@"0x666666"];
[self.view addSubview:tableView];
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
tableView.allowsMultipleSelectionDuringEditing = YES;
tableView;
});
[self changeEditStateToEditing:NO];
}
#pragma mark T
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _fileList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
LocalFileCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LocalFileCell forIndexPath:indexPath];
cell.fileUrl = _fileList[indexPath.row];
cell.delegate = self;
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [LocalFileCell cellHeight];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView.isEditing) {
}else{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
LocalFileViewController *vc = [LocalFileViewController new];
vc.projectName = self.projectName;
vc.fileUrl = self.fileList[indexPath.row];
[self.navigationController pushViewController:vc animated:YES];
}
}
#pragma mark SWTableViewCellDelegate
- (BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell{
return YES;
}
- (BOOL)swipeableTableViewCell:(SWTableViewCell *)cell canSwipeToState:(SWCellState)state{
return YES;
}
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index {
[cell hideUtilityButtonsAnimated:YES];
NSIndexPath *indexPath = [self.myTableView indexPathForCell:cell];
NSURL *fileUrl = self.fileList[indexPath.row];
__weak typeof(self) weakSelf = self;
UIActionSheet *actionSheet = [UIActionSheet bk_actionSheetCustomWithTitle:@"确定要删除本地文件吗?" buttonTitles:nil destructiveTitle:@"确认删除" cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
if (index == 0) {
[weakSelf deleteFilesWithUrlList:@[fileUrl]];
}
}];
[actionSheet showInView:self.view];
}
#pragma mark Edit
- (void)changeEditStateToEditing:(BOOL)isEditing{
[_myTableView setEditing:isEditing animated:YES];
NSArray *rightBarButtonItems;
if (isEditing) {
UIBarButtonItem *item1 = [UIBarButtonItem itemWithBtnTitle:@"完成" target:self action:@selector(changeEditState)];
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spaceItem.width = 20;
UIBarButtonItem *item2 = [UIBarButtonItem itemWithBtnTitle:@"反选" target:self action:@selector(reverseSelect)];
rightBarButtonItems = @[item1, spaceItem, item2];
}else{
UIBarButtonItem *item1 = [UIBarButtonItem itemWithBtnTitle:@"编辑" target:self action:@selector(changeEditState)];
rightBarButtonItems = @[item1];
}
[self.navigationItem setRightBarButtonItems:rightBarButtonItems animated:YES];
[self configToolBar];
[self.myTableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.3];
}
- (void)changeEditState{
[self changeEditStateToEditing:!_myTableView.isEditing];
}
- (void)reverseSelect{
if (_myTableView.isEditing) {
NSArray *selectedIndexList = [_myTableView indexPathsForSelectedRows];
NSMutableArray *reverseIndexList = [NSMutableArray new];
for (NSInteger index = 0; index < _fileList.count; index++) {
NSIndexPath *curIndex = [NSIndexPath indexPathForRow:index inSection:0];
if (![selectedIndexList containsObject:curIndex]) {
[reverseIndexList addObject:curIndex];
}
}
for (NSIndexPath *indexPath in selectedIndexList) {
[_myTableView deselectRowAtIndexPath:indexPath animated:YES];
}
for (NSIndexPath *indexPath in reverseIndexList) {
[_myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
}
}
- (void)configToolBar{
//添加底部ToolBar
if (!_myEditToolBar) {
EaseToolBarItem *item = [EaseToolBarItem easeToolBarItemWithTitle:@" 删除" image:@"button_file_denete_enable" disableImage:nil];
_myEditToolBar = [EaseToolBar easeToolBarWithItems:@[item]];
_myEditToolBar.delegate = self;
[self.view addSubview:_myEditToolBar];
[_myEditToolBar mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.view.mas_bottom);
make.size.mas_equalTo(_myEditToolBar.frame.size);
}];
}
_myEditToolBar.hidden = !_myTableView.isEditing;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0,_myTableView.isEditing? CGRectGetHeight(_myEditToolBar.frame): 0.0, 0.0);
self.myTableView.contentInset = contentInsets;
self.myTableView.scrollIndicatorInsets = contentInsets;
}
#pragma mark EaseToolBarDelegate
- (void)easeToolBar:(EaseToolBar *)toolBar didClickedIndex:(NSInteger)index{
NSArray *selectedIndexPath = [_myTableView indexPathsForSelectedRows];
if (selectedIndexPath.count <= 0) {
return;
}
if (toolBar == _myEditToolBar) {
if (index == 0) {
__weak typeof(self) weakSelf = self;
UIActionSheet *actionSheet = [UIActionSheet bk_actionSheetCustomWithTitle:@"确定要删除选中的本地文件吗?" buttonTitles:nil destructiveTitle:@"确认删除" cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
if (index == 0) {
[weakSelf deleteSelectedFiles];
}
}];
[actionSheet showInView:self.view];
}
}
}
- (void)deleteSelectedFiles{
NSArray *selectedIndexPath = [_myTableView indexPathsForSelectedRows];
NSMutableArray *selectedFiles = [[NSMutableArray alloc] initWithCapacity:selectedIndexPath.count];
for (NSIndexPath *indexPath in selectedIndexPath) {
[selectedFiles addObject:_fileList[indexPath.row]];
}
[self deleteFilesWithUrlList:selectedFiles];
}
#pragma mark Delete
- (void)deleteFilesWithUrlList:(NSArray *)urlList{
@weakify(self);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSFileManager *fm = [NSFileManager defaultManager];
for (NSURL *fileUrl in urlList) {
NSString *filePath = fileUrl.path;
if ([fm fileExistsAtPath:filePath]) {
NSError *fileError;
[fm removeItemAtPath:filePath error:&fileError];
if (fileError) {
[NSObject showError:fileError];
}
}
}
dispatch_async(dispatch_get_main_queue(), ^{
@strongify(self);
[self.fileList removeObjectsInArray:urlList];
[self changeEditStateToEditing:NO];
[NSObject showHudTipStr:@"本地文件删除成功"];
});
});
}
@end