forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalFoldersViewController.m
More file actions
316 lines (285 loc) · 12.5 KB
/
LocalFoldersViewController.m
File metadata and controls
316 lines (285 loc) · 12.5 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//
// LocalFoldersViewController.m
// Coding_iOS
//
// Created by Ease on 15/9/22.
// Copyright © 2015年 Coding. All rights reserved.
//
#import "LocalFoldersViewController.h"
#import "Coding_FileManager.h"
#import "ODRefreshControl.h"
#import "Coding_NetAPIManager.h"
#import "LocalFolderCell.h"
#import "LocalFilesViewController.h"
#import "EaseToolBar.h"
@interface LocalFoldersViewController ()<UITableViewDataSource, UITableViewDelegate, SWTableViewCellDelegate, EaseToolBarDelegate>
@property (assign, nonatomic) BOOL isLoading;
@property (strong, nonatomic) UITableView *myTableView;
@property (strong, nonatomic) ODRefreshControl *myRefreshControl;
@property (nonatomic, strong) EaseToolBar *myEditToolBar;
@property (strong, nonatomic) NSMutableArray *projectId_list;
@property (strong, nonatomic) NSMutableDictionary *projectList_dict;
@end
@implementation LocalFoldersViewController
- (void)viewDidLoad{
[super viewDidLoad];
self.title = @"本地文件";
_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:[LocalFolderCell class] forCellReuseIdentifier:kCellIdentifier_LocalFolderCell];
tableView.sectionIndexBackgroundColor = [UIColor clearColor];
tableView.sectionIndexTrackingBackgroundColor = [UIColor clearColor];
tableView.sectionIndexColor = kColor666;
[self.view addSubview:tableView];
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
tableView.allowsMultipleSelectionDuringEditing = YES;
tableView;
});
_myRefreshControl = [[ODRefreshControl alloc] initInScrollView:self.myTableView];
[_myRefreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self changeEditStateToEditing:NO];
[self refresh];
}
- (void)refresh{
BOOL hasData = [self findLocalFile];
[self.view configBlankPage:EaseBlankPageTypeView hasData:hasData hasError:NO reloadButtonBlock:nil];
if (!hasData) {
[self.myRefreshControl endRefreshing];
return;
}
if (_isLoading) {
return;
}
__weak typeof(self) weakSelf = self;
weakSelf.isLoading = YES;
[[Coding_NetAPIManager sharedManager] request_Projects_WithObj:[Projects projectsWithType:ProjectsTypeAll andUser:nil] andBlock:^(Projects *data, NSError *error) {
weakSelf.isLoading = NO;
[weakSelf.myRefreshControl endRefreshing];
[weakSelf refreshProjectNameWithProjects:data];
}];
}
- (BOOL)findLocalFile{
if (_projectId_list) {
[_projectId_list removeAllObjects];
}else{
_projectId_list = [NSMutableArray new];
}
if (_projectList_dict) {
[[_projectList_dict allValues] enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj[@"list"] isKindOfClass:[NSMutableArray class]]) {
[(NSMutableArray *)obj[@"list"] removeAllObjects];
}
}];
}else{
_projectList_dict = [NSMutableDictionary new];
}
NSArray *localFileUrlList = [Coding_FileManager localFileUrlList];
if (localFileUrlList.count > 0) {
for (NSURL *fileUrl in localFileUrlList) {
NSArray *valueList = [[[fileUrl.path componentsSeparatedByString:@"/"] lastObject] componentsSeparatedByString:@"|||"];
if (valueList.count == 3) {
NSString *projectId = valueList[1];
if (![_projectId_list containsObject:projectId]) {
[_projectId_list addObject:projectId];
}
NSMutableDictionary *pro_dict = _projectList_dict[projectId];
if (!pro_dict) {
pro_dict = [@{@"name" : @"...",
@"list" : [@[] mutableCopy]} mutableCopy];
_projectList_dict[projectId] = pro_dict;
}
[pro_dict[@"list"] addObject:fileUrl];
}
}
}
[self.myTableView reloadData];
return localFileUrlList.count > 0;
}
- (void)refreshProjectNameWithProjects:(Projects *)projects{
if (!projects || projects.list.count <= 0) {
return;
}
for (Project *curPro in projects.list) {
if (_projectList_dict[curPro.id.stringValue]) {
NSMutableDictionary *pro_dict = _projectList_dict[curPro.id.stringValue];
pro_dict[@"name"] = curPro.name;
}
}
[self.myTableView reloadData];
}
#pragma mark T
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _projectId_list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
LocalFolderCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LocalFolderCell forIndexPath:indexPath];
NSString *key = _projectId_list[indexPath.row];
NSDictionary *pro_dict = _projectList_dict[key];
[cell setProjectName:pro_dict[@"name"] fileCount:[(NSArray *)pro_dict[@"list"] count]];
cell.delegate = self;
[tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [LocalFolderCell cellHeight];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView.isEditing) {
}else{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *key = _projectId_list[indexPath.row];
NSDictionary *pro_dict = _projectList_dict[key];
LocalFilesViewController *vc = [LocalFilesViewController new];
vc.projectName = pro_dict[@"name"];
vc.fileList = pro_dict[@"list"];
[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];
NSString *projectId = _projectId_list[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 deleteFilesWithProjectIdList:@[projectId]];
}
}];
[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 < _projectId_list.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 deleteSelectedFolders];
}
}];
[actionSheet showInView:self.view];
}
}
}
- (void)deleteSelectedFolders{
NSArray *selectedIndexPath = [_myTableView indexPathsForSelectedRows];
NSMutableArray *selectedFolders = [[NSMutableArray alloc] initWithCapacity:selectedIndexPath.count];
for (NSIndexPath *indexPath in selectedIndexPath) {
NSString *projectId = _projectId_list[indexPath.row];
if (projectId.length > 0) {
[selectedFolders addObject:projectId];
}
}
[self deleteFilesWithProjectIdList:selectedFolders];
}
#pragma mark Delete
- (void)deleteFilesWithProjectIdList:(NSArray *)projectIdList{
NSMutableArray *urlList = [NSMutableArray new];
for (NSString *key in projectIdList) {
NSDictionary *pro_dict = _projectList_dict[key];
NSArray *curList = pro_dict[@"list"];
if (curList.count > 0) {
[urlList addObjectsFromArray:curList];
}
}
[self deleteFilesWithUrlList:urlList];
}
- (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 findLocalFile];
[self changeEditStateToEditing:NO];
[NSObject showHudTipStr:@"本地文件删除成功"];
});
});
}
@end