WFCUSelectFileViewController.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // SelectFileViewController.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/10/28.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUSelectFileViewController.h"
  9. #import "WFCUSelectedFileCollectionViewCell.h"
  10. #define DocumentPath [NSString stringWithFormat:@"%@",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]]
  11. @interface WFCUSelectFileViewController ()<UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
  12. @property (strong ,nonatomic)UICollectionView *fileCV;
  13. @property (strong ,nonatomic)NSMutableArray *fileArray;
  14. @property (strong ,nonatomic)NSMutableArray *selectedFiles;
  15. @property (nonatomic, strong)NSString *currentPath;
  16. @property (nonatomic, strong)NSString *documentPath;
  17. @end
  18. @implementation WFCUSelectFileViewController
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(onCancel:)];
  22. self.navigationItem.leftBarButtonItem = cancel;
  23. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  24. [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
  25. flowLayout.itemSize = CGSizeMake(144, 144);
  26. self.fileCV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-60) collectionViewLayout:flowLayout];
  27. [self.view addSubview:self.fileCV];
  28. [self.fileCV registerClass:[WFCUSelectedFileCollectionViewCell class] forCellWithReuseIdentifier:@"fileCellID"];
  29. self.fileCV.delegate = self;
  30. self.fileCV.dataSource = self;
  31. self.fileCV.backgroundColor = [UIColor whiteColor];
  32. self.documentPath = DocumentPath;
  33. self.currentPath = self.documentPath;
  34. [self loadDatas];
  35. }
  36. - (void)updateRightButton {
  37. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"确定(%d/10)", (int)self.selectedFiles.count] style:UIBarButtonItemStyleDone target:self action:@selector(onSelect:)];
  38. if (self.selectedFiles.count == 0) {
  39. rightItem.enabled = NO;
  40. }
  41. self.navigationItem.rightBarButtonItem = rightItem;
  42. }
  43. - (void)onCancel:(id)sender {
  44. [self dismissViewControllerAnimated:YES completion:nil];
  45. }
  46. - (void)onSelect:(id)sender {
  47. if (self.selectResult) {
  48. self.selectResult(self.selectedFiles);
  49. }
  50. [self dismissViewControllerAnimated:YES completion:nil];
  51. }
  52. - (void)loadDatas {
  53. NSFileManager *fileManager = [NSFileManager defaultManager];
  54. self.fileArray = [[NSMutableArray alloc]initWithArray:[fileManager contentsOfDirectoryAtPath:self.currentPath error:nil]];
  55. NSLog(@"%@",self.fileArray);
  56. if (![self.currentPath isEqualToString:self.documentPath]) {
  57. NSMutableArray *tmp = [self.fileArray mutableCopy];
  58. [tmp insertObject:@".." atIndex:0];
  59. self.fileArray = tmp;
  60. }
  61. [self.fileCV reloadData];
  62. [self updateRightButton];
  63. self.title = [self.currentPath lastPathComponent];
  64. }
  65. - (NSMutableArray *)selectedFiles {
  66. if (!_selectedFiles) {
  67. _selectedFiles = [NSMutableArray array];
  68. }
  69. return _selectedFiles;
  70. }
  71. - (void)didReceiveMemoryWarning {
  72. [super didReceiveMemoryWarning];
  73. }
  74. - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
  75. WFCUSelectedFileCollectionViewCell *fileCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"fileCellID" forIndexPath:indexPath];
  76. fileCell.fileNameLbl.text = self.fileArray[indexPath.row];
  77. NSFileManager *fileManager = [NSFileManager defaultManager];
  78. BOOL isDir = NO;
  79. if (![fileManager fileExistsAtPath:[self.currentPath stringByAppendingPathComponent:self.fileArray[indexPath.row]] isDirectory:&isDir]) {
  80. isDir = NO;
  81. }
  82. if (isDir) {
  83. fileCell.backIV.image = [UIImage imageNamed:@"dir_icon"];
  84. fileCell.selectIV.hidden = YES;
  85. } else {
  86. fileCell.backIV.image = [UIImage imageNamed:@"file_icon"];
  87. fileCell.selectIV.hidden = NO;
  88. NSString *fullPath = [self.currentPath stringByAppendingPathComponent:self.fileArray[indexPath.row]];
  89. if (![self.selectedFiles containsObject:fullPath]) {
  90. fileCell.selectIV.image = [UIImage imageNamed:@"multi_unselected"];
  91. }else{
  92. fileCell.selectIV.image = [UIImage imageNamed:@"multi_selected"];
  93. }
  94. }
  95. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
  96. [fileCell.contentView addGestureRecognizer:longPress];
  97. return fileCell;
  98. }
  99. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  100. if ([self.fileArray[indexPath.row] isEqualToString:@".."]) {
  101. self.currentPath = [self.currentPath stringByDeletingLastPathComponent];
  102. } else {
  103. NSFileManager *fileManager = [NSFileManager defaultManager];
  104. BOOL isDir = NO;
  105. if (![fileManager fileExistsAtPath:[self.currentPath stringByAppendingPathComponent:self.fileArray[indexPath.row]] isDirectory:&isDir]) {
  106. isDir = NO;
  107. }
  108. if (isDir) {
  109. self.currentPath =[self.currentPath stringByAppendingPathComponent:self.fileArray[indexPath.row]];
  110. } else {
  111. NSString *fullPath = [self.currentPath stringByAppendingPathComponent:self.fileArray[indexPath.row]];
  112. if ([self.selectedFiles containsObject:fullPath]) {
  113. [self.selectedFiles removeObject:fullPath];
  114. } else {
  115. [self.selectedFiles addObject:fullPath];
  116. }
  117. }
  118. }
  119. [self loadDatas];
  120. }
  121. - (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
  122. __weak typeof(self)weakSelf = self;
  123. if ([longPress state] == UIGestureRecognizerStateBegan) {
  124. CGPoint p = [longPress locationInView:self.fileCV];
  125. NSIndexPath *indexPath = [self.fileCV indexPathForItemAtPoint:p];
  126. if ([self.currentPath isEqualToString:self.documentPath] && indexPath.row == 0) {
  127. return;
  128. }
  129. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"您要删除这个文件吗?" preferredStyle:UIAlertControllerStyleAlert];
  130. UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  131. [[NSFileManager defaultManager]removeItemAtPath:[weakSelf.currentPath stringByAppendingPathComponent:weakSelf.fileArray[indexPath.row]] error:nil];
  132. [weakSelf.selectedFiles removeObject:[weakSelf.currentPath stringByAppendingPathComponent:weakSelf.fileArray[indexPath.row]]];
  133. [weakSelf.fileArray removeObjectAtIndex:indexPath.row];
  134. [weakSelf.fileCV reloadData];
  135. }];
  136. UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  137. }];
  138. [alert addAction:action1];
  139. [alert addAction:action2];
  140. [self presentViewController:alert animated:YES completion:nil];
  141. }
  142. }
  143. - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  144. return self.fileArray.count;
  145. }
  146. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  147. return CGSizeMake(96, 120);
  148. }
  149. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  150. return UIEdgeInsetsMake(8, 8, 8, 8);
  151. }
  152. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  153. return 12;
  154. }
  155. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  156. return 8;
  157. }
  158. @end