CSRouterSelectController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. //
  2. // CSRouterSelectController.m
  3. // zhuxun
  4. //
  5. // Created by winsoft on 17/6/28.
  6. //
  7. //
  8. #import "CSRouterSelectController.h"
  9. #import "IWNavigationController.h"
  10. #import "IWNavigationController.h"
  11. #import "CloudStorageTableView.h"
  12. #import "CSFileCell.h"
  13. #import "CloudRouterShowView.h"
  14. #import "HttpRequest.h"
  15. #import <MJExtension/MJExtension.h>
  16. #import "UIBarButtonItem+Addition.h"
  17. #import "UIActionSheet+Blocks.h"
  18. #import "UIAlertView+Blocks.h"
  19. #import "CSAlertView.h"
  20. #import "CloudStorageDB.h"
  21. #import "Utils.h"
  22. #define CloudRouterShowViewHeight 50
  23. @interface CSRouterSelectController()<UITableViewDelegate, UITableViewDataSource>
  24. @property (nonatomic , weak) CloudStorageTableView *mainTableView;
  25. @property (nonatomic , weak) CloudRouterShowView *cloudRouterShowView;
  26. @property (nonatomic , strong) NSMutableArray *folderModelArray;
  27. /**
  28. * 父节点
  29. */
  30. @property (nonatomic , strong) CSFileModel *father_csFileModel;
  31. @end
  32. @implementation CSRouterSelectController
  33. - (NSMutableArray *)folderModelArray
  34. {
  35. if (!_folderModelArray) {
  36. _folderModelArray = [NSMutableArray array];
  37. }
  38. return _folderModelArray;
  39. }
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. if([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
  43. {
  44. self.edgesForExtendedLayout = UIRectEdgeNone;
  45. }
  46. self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(cancerSelect) icon:nil highlightedIcon:nil title:@"取消" iconSize:CGSizeZero fontSize:14 titleColor:[UIColor whiteColor]];
  47. if (!self.pid) {
  48. self.pid = @"0";
  49. self.title = @"我的网盘";
  50. }else self.title = self.father_csFileModel.name;
  51. [self initSubViews];
  52. __weak typeof(self)weakSelf = self;
  53. __weak typeof(self.mainTableView)weakTableView = self.mainTableView;
  54. [self getTopFolderLists];
  55. if (self.fileMD5 && self.fileName) {
  56. [self judgeSameFileIsExitThisFolder];
  57. }
  58. }
  59. - (void)cancerSelect
  60. {
  61. [self dismissViewControllerAnimated:YES completion:nil];
  62. }
  63. - (void)initSubViews
  64. {
  65. CloudStorageTableView *mainTableView = [[CloudStorageTableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NavBarHeight - STATUSBAR_HEIGHT - CloudRouterShowViewHeight) style:UITableViewStylePlain] ;
  66. mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  67. mainTableView.delegate = self;
  68. mainTableView.dataSource = self;
  69. mainTableView.tableFooterView = [[UIView alloc]init];
  70. mainTableView.tableFooterView.backgroundColor = [UIColor clearColor];
  71. [self.view addSubview:mainTableView];
  72. self.mainTableView = mainTableView;
  73. __weak typeof(self)weakSelf = self;
  74. CloudRouterShowView *cloudRouterShowView = [CloudRouterShowView cloudRouterShowViewWithItems:@[@"新建文件夹",@"选定"] bkColors:@[RGB(125,125,125),RGB(36, 143, 233)]];
  75. cloudRouterShowView.backgroundColor = [UIColor blackColor];
  76. cloudRouterShowView.frame = CGRectMake(0,CGRectGetMaxY(mainTableView.frame), self.view.frame.size.width, CloudRouterShowViewHeight);
  77. cloudRouterShowView.actionBlock = ^(NSInteger index)
  78. {
  79. if (index == 0) {
  80. [weakSelf createNewFileFolder];
  81. }else{
  82. [weakSelf saveFileToCloudFolde];
  83. }
  84. };
  85. [self.view addSubview:cloudRouterShowView];
  86. self.cloudRouterShowView = cloudRouterShowView;
  87. }
  88. #pragma mark --
  89. #pragma mark -- tableViewDatasource
  90. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  91. {
  92. return 1;
  93. }
  94. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  95. {
  96. return self.folderModelArray.count;
  97. }
  98. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  99. {
  100. CSFileCell *csFileCell = [CSFileCell fileCellWithTableView:tableView indexPath:indexPath identifier:CSFileCellIdentifier opItems:nil];
  101. csFileCell.indexPath = indexPath;
  102. csFileCell.csFileFrameModel = self.folderModelArray[indexPath.row];
  103. return csFileCell;
  104. }
  105. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  106. {
  107. CSFileFrameModel *csFileFrameModel = self.folderModelArray[indexPath.row];
  108. return csFileFrameModel.cellHeight;
  109. }
  110. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  111. {
  112. return 30;
  113. }
  114. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  115. {
  116. return self.father_csFileModel?[NSString stringWithFormat:@"选择目标文件夹 (已选:%@)",self.father_csFileModel.name]:[NSString stringWithFormat:@"选择目标文件夹 (已选:我的网盘)"];
  117. }
  118. #pragma mark --
  119. #pragma mark -- tableViewDelegate
  120. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  123. CSFileFrameModel *csFileFrameModel = self.folderModelArray[indexPath.row];
  124. //
  125. CSFileModel *csFileModel = csFileFrameModel.fileModel;
  126. if (csFileModel.isdir) {
  127. //文件夹
  128. CSRouterSelectController *csPage = [CSRouterSelectController new];
  129. csPage.fileMD5 = self.fileMD5;
  130. csPage.fileName = self.fileName;
  131. csPage.pid = csFileModel.sid;
  132. csPage.father_csFileModel = csFileModel;
  133. [self.navigationController pushViewController:csPage animated:YES];
  134. }
  135. }
  136. #pragma mark --
  137. #pragma mark -- rques
  138. - (void)getTopFolderLists
  139. {
  140. [self getgetTopFolderListsWithPid:self.pid];
  141. }
  142. - (void)judgeSameFileIsExitThisFolder
  143. {
  144. __weak typeof(self.cloudRouterShowView)weakRouterShowView = self.cloudRouterShowView;
  145. //只要同名就不可以存放
  146. [[HttpRequest sharedManager]cloudStorageCheckFileNameIsExitWithPid:self.pid fileName:self.fileName isdir:NO ext:[self.fileName componentsSeparatedByString:@"."].lastObject complete:^(BOOL success, id data, NSError *error) {
  147. /*
  148. {
  149. data = 0;
  150. message = "";
  151. success = 1;
  152. }
  153. */
  154. if (data && success) {
  155. if ([data[@"data"] boolValue]) {
  156. [weakRouterShowView setIndexActionBtnEnableWithIndex:1 enable:NO];
  157. }
  158. }else [weakRouterShowView setIndexActionBtnEnableWithIndex:1 enable:NO];
  159. NSLog(@"%@",data);
  160. //不可选择
  161. //
  162. //请求失败也是不可选.
  163. }];
  164. }
  165. - (void)getgetTopFolderListsWithPid:(NSString *)pid
  166. {
  167. [self.folderModelArray removeAllObjects];
  168. __weak typeof(self)weakSelf = self;
  169. __weak typeof(self.mainTableView)weakTableView = self.mainTableView;
  170. [[HttpRequest sharedManager]cloudStorageGetAllFolderWithPid:pid excludeIds:nil complete:^(BOOL success, id data, NSError *error) {
  171. if (success && data) {
  172. NSArray *fileDataModel = [CSFileModel mj_objectArrayWithKeyValuesArray:data[@"data"]];
  173. for (CSFileModel *fileModel in fileDataModel) {
  174. if (fileModel.isdir) {
  175. fileModel.serverFullPath = self.father_csFileModel?[NSString stringWithFormat:@"%@%@/",self.father_csFileModel.serverFullPath,fileModel.name]:[NSString stringWithFormat:@"%@/",fileModel.name];
  176. }else fileModel.serverFullPath = self.father_csFileModel?[NSString stringWithFormat:@"%@%@",self.father_csFileModel.serverFullPath,fileModel.name]:[NSString stringWithFormat:@"%@",fileModel.name];;
  177. // [CloudStorageDB addcsFiletoDB:fileModel];
  178. CSFileFrameModel *csFileFrameModel = [[CSFileFrameModel alloc]initWithFileModel:fileModel];
  179. [weakSelf.folderModelArray addObject:csFileFrameModel];
  180. }
  181. [weakTableView reloadData];
  182. }
  183. }];
  184. }
  185. - (void)createNewFileFolder
  186. {
  187. CSAlertView * alertView = [[CSAlertView alloc]initWithTitle:@"新建文件夹" message:@"" inputMode:YES sureBtn:@"创建" cancleBtn:@"取消"];
  188. [alertView showXBAlertView];
  189. __weak typeof(self)weakSelf = self;
  190. __weak typeof(self.mainTableView)weakTableView = self.mainTableView;
  191. __weak typeof(alertView)weakalertView = alertView;
  192. alertView.alertresult = ^(NSString *folderName){
  193. [[HttpRequest sharedManager]cloudStorageCreateNewFileFolderWithFolderName:folderName pid:weakSelf.pid delflag:@"0" complete:^(BOOL success, id data, NSError *error) {
  194. if (success && data) {
  195. [weakalertView stopActityAndRemoveAlertView];
  196. [Utils showSuccess:@"文件夹创建成功" toView:weakTableView finish:nil];
  197. //reload.
  198. CSFileModel *csfileModel = [CSFileModel mj_objectWithKeyValues:data[@"data"]];
  199. csfileModel.serverFullPath = self.father_csFileModel?[NSString stringWithFormat:@"%@%@/",self.father_csFileModel.serverFullPath,csfileModel.name]:[NSString stringWithFormat:@"%@/",csfileModel.name];
  200. // [CloudStorageDB addcsFiletoDB:csfileModel];
  201. CSFileFrameModel *csframeModel = [[CSFileFrameModel alloc]initWithFileModel:csfileModel];
  202. [weakSelf.folderModelArray addObject:csframeModel];
  203. [weakTableView reloadData];
  204. // [weakTableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:[weakTableView numberOfRowsInSection:0] - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
  205. //
  206. [weakTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:weakSelf.folderModelArray.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  207. }else{
  208. [weakalertView showErrorMsg:@"文件夹创建失败"];
  209. //[Utils showError:@"文件夹创建失败" ToView:weakTableView];
  210. }
  211. }];
  212. };
  213. /*
  214. UIAlertViewStyle type = UIAlertViewStylePlainTextInput;
  215. __weak typeof(self)weakSelf = self;
  216. __weak typeof(self.mainTableView)weakTableView = self.mainTableView;
  217. //待使用新的createNEWFOLDERVIEW
  218. [UIAlertView showAlertViewWithType:type Title:@"新建文件夹" message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] onDismiss:^(int buttonIndex, UIAlertView *alertView) {
  219. UITextField *applyTextField = [alertView textFieldAtIndex:0];
  220. if (!applyTextField.text.length) {
  221. [Utils showError:@"请输入文件夹名称" ToView:weakTableView];
  222. return ;
  223. }
  224. [Utils showWaitingToView:weakTableView];
  225. #warning 2016-06-20 貌似有连续请求的问题..
  226. [[HttpRequest sharedManager]cloudStorageCreateNewFileFolderWithFolderName:applyTextField.text.length?applyTextField.text:@"新建文件夹" pid:weakSelf.pid delflag:@"0" complete:^(BOOL success, id data, NSError *error) {
  227. [Utils HiddenWaitingToView:weakTableView];
  228. if (success && data) {
  229. //
  230. [Utils showSuccess:@"文件夹创建成功" toView:weakTableView finish:nil];
  231. //reload.
  232. CSFileModel *csfileModel = [CSFileModel mj_objectWithKeyValues:data[@"data"]];
  233. csfileModel.serverFullPath = self.father_csFileModel?[NSString stringWithFormat:@"%@%@/",self.father_csFileModel.serverFullPath,csfileModel.name]:[NSString stringWithFormat:@"%@/",csfileModel.name];
  234. // [CloudStorageDB addcsFiletoDB:csfileModel];
  235. CSFileFrameModel *csframeModel = [[CSFileFrameModel alloc]initWithFileModel:csfileModel];
  236. [weakSelf.folderModelArray addObject:csframeModel];
  237. [weakTableView reloadData];
  238. // [weakTableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:[weakTableView numberOfRowsInSection:0] - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
  239. //
  240. [weakTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:weakSelf.folderModelArray.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  241. }else{
  242. [Utils showError:@"文件夹创建失败" ToView:weakTableView];
  243. }
  244. }];
  245. } onCancel:^(UIAlertView *alertView) {
  246. }];
  247. */
  248. }
  249. #warning 通过self的navcontroller 将数据将模型传出.
  250. - (void)saveFileToCloudFolde
  251. {
  252. IWNavigationController *iwNav = (IWNavigationController *)self.navigationController;
  253. if (iwNav.cloudControlerBlock) {
  254. iwNav.cloudControlerBlock(self.father_csFileModel.sid?self.father_csFileModel.sid:@"0",self.father_csFileModel.serverFullPath);
  255. }
  256. [self cancerSelect];;
  257. }
  258. @end