77//
88
99#import " FileVersionsViewController.h"
10+ #import " SettingTextViewController.h"
11+ #import " FileViewController.h"
1012
11- @interface FileVersionsViewController ()
13+ #import " ODRefreshControl.h"
14+ #import " Coding_NetAPIManager.h"
15+ #import " Coding_FileManager.h"
16+
17+
18+ #import " FileVersionCell.h"
19+
20+
21+ @interface FileVersionsViewController ()<UITableViewDataSource, UITableViewDelegate, SWTableViewCellDelegate>
1222@property (strong , nonatomic ) ProjectFile *curFile;
23+ @property (strong , nonatomic ) NSMutableArray *versionList;
24+
25+ @property (nonatomic , strong ) UITableView *myTableView;
26+ @property (nonatomic , strong ) ODRefreshControl *myRefreshControl;
27+
28+ @property (assign , nonatomic ) BOOL isLoading;
1329
1430@end
1531
@@ -19,4 +35,225 @@ + (instancetype)vcWithFile:(ProjectFile *)file{
1935 vc.curFile = file;
2036 return vc;
2137}
38+
39+ - (void )viewDidLoad
40+ {
41+ [super viewDidLoad ];
42+ // Do any additional setup after loading the view.
43+ self.title = self.curFile .name ;
44+
45+ // 添加myTableView
46+ _myTableView = ({
47+ UITableView *tableView = [[UITableView alloc ] initWithFrame: self .view.bounds style: UITableViewStylePlain];
48+ tableView.backgroundColor = [UIColor clearColor ];
49+ tableView.dataSource = self;
50+ tableView.delegate = self;
51+ tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
52+ [tableView registerClass: [FileVersionCell class ] forCellReuseIdentifier: kCellIdentifier_FileVersionCell ];
53+ [self .view addSubview: tableView];
54+ [tableView mas_makeConstraints: ^(MASConstraintMaker *make) {
55+ make.edges .equalTo (self.view );
56+ }];
57+ tableView.allowsMultipleSelectionDuringEditing = YES ;
58+ tableView;
59+ });
60+
61+ _myRefreshControl = [[ODRefreshControl alloc ] initInScrollView: self .myTableView];
62+ [_myRefreshControl addTarget: self action: @selector (refresh ) forControlEvents: UIControlEventValueChanged];
63+
64+ [self refresh ];
65+ }
66+
67+ - (void )viewWillAppear : (BOOL )animated {
68+ [super viewWillAppear: animated];
69+ [self .myTableView reloadData ];
70+ }
71+
72+ - (void )refresh {
73+ if (self.isLoading ) {
74+ return ;
75+ }
76+ [self sendRequest ];
77+ }
78+
79+ - (void )sendRequest {
80+ self.isLoading = YES ;
81+ if (self.versionList .count <= 0 ) {
82+ [self .view beginLoading ];
83+ }
84+ @weakify (self);
85+ [[Coding_NetAPIManager sharedManager ] request_VersionListOfFile: _curFile andBlock: ^(id data, NSError *error) {
86+ @strongify (self);
87+ self.isLoading = NO ;
88+ [self .myRefreshControl endRefreshing ];
89+ [self .view endLoading ];
90+ if (data) {
91+ self.versionList = data;
92+ [self .myTableView reloadData ];
93+ }
94+ [self .view configBlankPage: EaseBlankPageTypeView hasData: self .versionList.count > 0 hasError: error != nil reloadButtonBlock: ^(id sender) {
95+ [self refresh ];
96+ }];
97+ }];
98+ }
99+
100+ #pragma mark Table M
101+ - (NSInteger )tableView : (UITableView *)tableView numberOfRowsInSection : (NSInteger )section {
102+ return self.versionList .count ;
103+ }
104+
105+ - (UITableViewCell *)tableView : (UITableView *)tableView cellForRowAtIndexPath : (NSIndexPath *)indexPath {
106+ __weak typeof (self) weakSelf = self;
107+
108+ FileVersionCell *cell = [tableView dequeueReusableCellWithIdentifier: kCellIdentifier_FileVersionCell forIndexPath: indexPath];
109+ FileVersion *curVersion = _versionList[indexPath.row];
110+ cell.curVersion = curVersion;
111+ cell.showDiskFileBlock = ^(NSURL *fileUrl, FileVersion *curVersion){
112+ [weakSelf goToFileVersionVC: curVersion];
113+ };
114+ [cell setRightUtilityButtons: [self rightButtonsWithObj: curVersion] WithButtonWidth: [FileVersionCell cellHeight ]];
115+ cell.delegate = self;
116+ [tableView addLineforPlainCell: cell forRowAtIndexPath: indexPath withLeftSpace: kPaddingLeftWidth ];
117+ return cell;
118+ }
119+
120+ - (CGFloat)tableView : (UITableView *)tableView heightForRowAtIndexPath : (NSIndexPath *)indexPath {
121+ return [FileVersionCell cellHeight ];
122+ }
123+
124+ - (void )tableView : (UITableView *)tableView didSelectRowAtIndexPath : (NSIndexPath *)indexPath {
125+ [tableView deselectRowAtIndexPath: indexPath animated: YES ];
126+ [self goToFileVersionVC: _versionList[indexPath.row]];
127+ }
128+
129+ #pragma mark Edit Table
130+ - (NSArray *)rightButtonsWithObj : (id )obj {
131+ NSMutableArray *rightUtilityButtons = [NSMutableArray new ];
132+ [rightUtilityButtons sw_addUtilityButtonWithColor: [UIColor colorWithHexString: @" 0xe6e6e6" ] icon: [UIImage imageNamed: @" icon_file_cell_rename" ]];
133+ [rightUtilityButtons sw_addUtilityButtonWithColor: [UIColor colorWithHexString: @" 0xff5846" ] icon: [UIImage imageNamed: @" icon_file_cell_delete" ]];
134+ return rightUtilityButtons;
135+ }
136+
137+ - (BOOL )swipeableTableViewCellShouldHideUtilityButtonsOnSwipe : (SWTableViewCell *)cell {
138+ return YES ;
139+ }
140+ - (BOOL )swipeableTableViewCell : (SWTableViewCell *)cell canSwipeToState : (SWCellState)state {
141+ NSIndexPath *indexPath = [_myTableView indexPathForCell: cell];
142+ FileVersion *curVersion = _versionList[indexPath.row];
143+ Coding_DownloadTask *cDownloadTask = [Coding_FileManager cDownloadTaskForKey: curVersion.storage_key];
144+ if (cDownloadTask && cDownloadTask.task && cDownloadTask.task .state == NSURLSessionTaskStateRunning ) {
145+ return NO ;
146+ }else {
147+ return YES ;
148+ }
149+ }
150+
151+ - (void )swipeableTableViewCell : (SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex : (NSInteger )index {
152+ [cell hideUtilityButtonsAnimated: YES ];
153+ NSIndexPath *indexPath = [_myTableView indexPathForCell: cell];
154+ FileVersion *curVersion = _versionList[indexPath.row];
155+ if (index == 0 ) {
156+ [self remarkFileVersion: curVersion];
157+ }else {
158+ [self deleteFileVersion: curVersion];
159+ }
160+ }
161+
162+ - (void )remarkFileVersion : (FileVersion *)curVersion {
163+ __weak typeof (self) weakSelf = self;
164+ [SettingTextViewController showSettingFolderNameVCFromVC: nil withTitle: @" 重命名文件夹" textValue: curVersion.remark type: SettingTypeFileVersionRemark doneBlock: ^(NSString *textValue) {
165+ [weakSelf doRemarkFileVersion: curVersion withRemarkStr: textValue];
166+ }];
167+ }
168+
169+ - (void )doRemarkFileVersion : (FileVersion *)curVersion withRemarkStr : (NSString *)remarkStr {
170+ if (remarkStr && ![remarkStr isEqualToString: curVersion.remark]) {
171+ @weakify (self);
172+ [[Coding_NetAPIManager sharedManager ] request_RemarkFileVersion: curVersion withStr: remarkStr andBlock: ^(id data, NSError *error) {
173+ @strongify (self);
174+ if (data) {
175+ curVersion.remark = remarkStr;
176+ [self .myTableView reloadData ];
177+ }
178+ }];
179+ }
180+ }
181+
182+ - (void )deleteFileVersion : (FileVersion *)curVersion {
183+ __weak typeof (self) weakSelf = self;
184+
185+ NSURL *fileUrl = [Coding_FileManager diskDownloadUrlForKey: curVersion.storage_key];
186+ Coding_DownloadTask *cDownloadTask = [Coding_FileManager cDownloadTaskForKey: curVersion.storage_key];
187+ UIActionSheet *actionSheet;
188+
189+ if (fileUrl) {
190+ actionSheet = [UIActionSheet bk_actionSheetCustomWithTitle: @" 只是删除本地文件还是连同服务器版本一起删除?" buttonTitles: @[@" 仅删除本地文件" ] destructiveTitle: @" 一起删除" cancelTitle: @" 取消" andDidDismissBlock: ^(UIActionSheet *sheet, NSInteger index) {
191+ switch (index) {
192+ case 0 :
193+ [weakSelf doDeleteFileVersion: curVersion fromDisk: YES ];
194+ break ;
195+ case 1 :
196+ [weakSelf doDeleteFileVersion: curVersion fromDisk: NO ];
197+ break ;
198+ default :
199+ break ;
200+ }
201+ }];
202+ }else if (cDownloadTask){
203+ actionSheet = [UIActionSheet bk_actionSheetCustomWithTitle: @" 确定将服务器上的该版本删除?" buttonTitles: @[@" 只是取消下载" ] destructiveTitle: @" 确认删除" cancelTitle: @" 取消" andDidDismissBlock: ^(UIActionSheet *sheet, NSInteger index) {
204+ switch (index) {
205+ case 0 :
206+ [weakSelf doDeleteFileVersion: curVersion fromDisk: YES ];
207+ break ;
208+ case 1 :
209+ [weakSelf doDeleteFileVersion: curVersion fromDisk: NO ];
210+ break ;
211+ default :
212+ break ;
213+ }
214+ }];
215+ }else {
216+ actionSheet = [UIActionSheet bk_actionSheetCustomWithTitle: @" 确定将服务器上的该版本删除?" buttonTitles: nil destructiveTitle: @" 确认删除" cancelTitle: @" 取消" andDidDismissBlock: ^(UIActionSheet *sheet, NSInteger index) {
217+ if (index == 0 ) {
218+ [weakSelf doDeleteFileVersion: curVersion fromDisk: NO ];
219+ }
220+ }];
221+ }
222+ [actionSheet showInView: self .view];
223+ }
224+
225+ - (void )doDeleteFileVersion : (FileVersion *)curVersion fromDisk : (BOOL )fromDisk {
226+ // 取消当前的下载任务
227+ Coding_DownloadTask *cDownloadTask = [Coding_FileManager cDownloadTaskForKey: curVersion.storage_key];
228+ if (cDownloadTask) {
229+ [Coding_FileManager cancelCDownloadTaskForKey: curVersion.storage_key];
230+ }
231+ // 删除本地文件
232+ NSURL *fileUrl = [Coding_FileManager diskDownloadUrlForKey: curVersion.storage_key];
233+ NSString *filePath = fileUrl.path ;
234+ NSFileManager *fm = [NSFileManager defaultManager ];
235+ if ([fm fileExistsAtPath: filePath]) {
236+ NSError *fileError;
237+ [fm removeItemAtPath: filePath error: &fileError];
238+ if (fileError) {
239+ [self showError: fileError];
240+ }
241+ }
242+ [self .myTableView performSelector: @selector (reloadData ) withObject: nil afterDelay: 0.1 ];
243+ // 删除服务器文件
244+ if (!fromDisk) {
245+ __weak typeof (self) weakSelf = self;
246+ [[Coding_NetAPIManager sharedManager ] request_DeleteFileVersion: curVersion andBlock: ^(id data, NSError *error) {
247+ [weakSelf refresh ];
248+ }];
249+ }
250+ }
251+
252+ #pragma mark toVC
253+ - (void )goToFileVersionVC : (FileVersion *)curVersion {
254+ FileViewController *vc = [FileViewController vcWithFile: _curFile andVersion: curVersion];
255+ [self .navigationController pushViewController: vc animated: YES ];
256+ }
257+
258+
22259@end
0 commit comments