2
0

WFCUFilesViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. //
  2. // WFCUFilesViewController.m
  3. // WFChatUIKit
  4. //
  5. // Created by dali on 2020/8/2.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUFilesViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "WFCUFileRecordTableViewCell.h"
  11. #import "WFCUBrowserViewController.h"
  12. #import "WFCUConfigManager.h"
  13. #import "UIImage+ERCategory.h"
  14. @interface WFCUFilesViewController () <UITableViewDelegate, UITableViewDataSource, UISearchControllerDelegate, UISearchResultsUpdating>
  15. @property(nonatomic, strong)UITableView *tableView;
  16. @property(nonatomic, strong)UIActivityIndicatorView *activityView;
  17. @property (nonatomic, strong)UISearchController *searchController;
  18. @property(nonatomic, strong)NSMutableArray<WFCCFileRecord *> *searchedRecords;
  19. @property(nonatomic, strong)NSMutableArray<WFCCFileRecord *> *fileRecords;
  20. @property(nonatomic, assign)BOOL hasMore;
  21. @property(nonatomic, assign)BOOL searchMore;
  22. @property(nonatomic, assign)BOOL isLoading;
  23. @property(nonatomic, strong)NSString *keyword;
  24. @end
  25. @implementation WFCUFilesViewController
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  29. self.searchController.searchResultsUpdater = self;
  30. self.searchController.delegate = self;
  31. self.searchController.dimsBackgroundDuringPresentation = NO;
  32. if (@available(iOS 13, *)) {
  33. self.searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
  34. self.searchController.searchBar.searchTextField.backgroundColor = [WFCUConfigManager globalManager].naviBackgroudColor;
  35. UIImage* searchBarBg = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(self.view.frame.size.width - 8 * 2, 36) cornerRadius:4];
  36. [self.searchController.searchBar setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
  37. } else {
  38. [self.searchController.searchBar setValue:WFCString(@"Cancel") forKey:@"_cancelButtonText"];
  39. }
  40. if (@available(iOS 9.1, *)) {
  41. self.searchController.obscuresBackgroundDuringPresentation = NO;
  42. }
  43. self.searchController.searchBar.placeholder = WFCString(@"Search");
  44. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  45. self.tableView.dataSource = self;
  46. self.tableView.delegate = self;
  47. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  48. if (@available(iOS 15, *)) {
  49. self.tableView.sectionHeaderTopPadding = 0;
  50. }
  51. if (@available(iOS 11.0, *)) {
  52. self.navigationItem.searchController = _searchController;
  53. } else {
  54. self.tableView.tableHeaderView = _searchController.searchBar;
  55. }
  56. [self.view addSubview:self.tableView];
  57. self.activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  58. self.activityView.center = self.view.center;
  59. [self.view addSubview:self.activityView];
  60. if (self.myFiles) {
  61. self.title = WFCString(@"MyFiles");
  62. } else if(self.userFiles) {
  63. WFCCUserInfo *user = [[WFCCIMService sharedWFCIMService] getUserInfo:self.userId refresh:NO];
  64. if (user.friendAlias.length) {
  65. self.title = [NSString stringWithFormat:WFCString(@"%@`s files"), user.friendAlias];
  66. } else if (user.displayName.length) {
  67. self.title = [NSString stringWithFormat:WFCString(@"%@`s files"), user.displayName];
  68. } else {
  69. self.title = WFCString(@"Files");
  70. }
  71. } else if(self.conversation) {
  72. self.title = WFCString(@"ConversationFiles");
  73. } else {
  74. self.title = WFCString(@"AllFiles");
  75. }
  76. self.hasMore = YES;
  77. self.fileRecords = [[NSMutableArray alloc] init];
  78. [self loadMoreData];
  79. }
  80. - (void)loadMoreData {
  81. if (!self.hasMore) {
  82. return;
  83. }
  84. if(self.isLoading) {
  85. return;
  86. }
  87. __weak typeof(self)ws = self;
  88. long long lastId = 0;
  89. if (self.fileRecords.count) {
  90. lastId = self.fileRecords.lastObject.messageUid;
  91. }
  92. self.activityView.hidden = NO;
  93. [self.activityView startAnimating];
  94. self.isLoading = YES;
  95. [self loadData:lastId count:20 success:^(NSArray<WFCCFileRecord *> *files) {
  96. [ws.fileRecords addObjectsFromArray:files];
  97. [ws.tableView reloadData];
  98. ws.activityView.hidden = YES;
  99. [ws.activityView stopAnimating];
  100. ws.isLoading = NO;
  101. if (files.count < 20) {
  102. self.hasMore = NO;
  103. }
  104. } error:^(int error_code) {
  105. NSLog(@"load fire record error %d", error_code);
  106. ws.activityView.hidden = YES;
  107. [ws.activityView stopAnimating];
  108. ws.isLoading = NO;
  109. }];
  110. }
  111. - (void)setIsLoading:(BOOL)isLoading {
  112. _isLoading = isLoading;
  113. [self updateTableViewFooter];
  114. }
  115. - (void)setHasMore:(BOOL)hasMore {
  116. _hasMore = hasMore;
  117. [self updateTableViewFooter];
  118. }
  119. - (void)updateTableViewFooter {
  120. if(!_hasMore) {
  121. UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 21)];
  122. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1)];
  123. line.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.f];
  124. [footView addSubview:line];
  125. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 1, self.view.frame.size.width, 20)];
  126. label.text = WFCString(@"NoMoreData");
  127. label.textAlignment = NSTextAlignmentCenter;
  128. label.font = [UIFont systemFontOfSize:12];
  129. label.textColor = [UIColor grayColor];
  130. [footView addSubview:label];
  131. self.tableView.tableFooterView = footView;
  132. } else if(_isLoading) {
  133. UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
  134. UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  135. [footView addSubview:activityView];
  136. activityView.center = footView.center;
  137. self.tableView.tableFooterView = footView;
  138. } else {
  139. self.tableView.tableFooterView = nil;
  140. }
  141. }
  142. - (void)loadData:(long long)startPos count:(int)count success:(void(^)(NSArray<WFCCFileRecord *> *files))successBlock
  143. error:(void(^)(int error_code))errorBlock {
  144. if (self.myFiles) {
  145. [[WFCCIMService sharedWFCIMService] getMyFiles:startPos order:FileRecordOrder_TIME_DESC count:count success:successBlock error:errorBlock];
  146. } else if(self.userFiles) {
  147. [[WFCCIMService sharedWFCIMService] getConversationFiles:nil fromUser:self.userId beforeMessageUid:startPos order:FileRecordOrder_TIME_DESC count:count success:successBlock error:errorBlock];
  148. } else {
  149. [[WFCCIMService sharedWFCIMService] getConversationFiles:self.conversation fromUser:nil beforeMessageUid:startPos order:FileRecordOrder_TIME_DESC count:count success:successBlock error:errorBlock];
  150. }
  151. }
  152. - (void)searchMoreData {
  153. if (!self.searchMore) {
  154. return;
  155. }
  156. __weak typeof(self)ws = self;
  157. long long lastId = 0;
  158. if (self.searchedRecords.count) {
  159. lastId = self.searchedRecords.lastObject.messageUid;
  160. }
  161. self.activityView.hidden = NO;
  162. [self searchData:lastId count:20 success:^(NSArray<WFCCFileRecord *> *files) {
  163. [ws.searchedRecords addObjectsFromArray:files];
  164. [ws.tableView reloadData];
  165. ws.activityView.hidden = YES;
  166. if (files.count < 20) {
  167. self.searchMore = NO;
  168. }
  169. } error:^(int error_code) {
  170. NSLog(@"load fire record error %d", error_code);
  171. ws.activityView.hidden = YES;
  172. }];
  173. }
  174. - (void)searchData:(long long)startPos count:(int)count success:(void(^)(NSArray<WFCCFileRecord *> *files))successBlock
  175. error:(void(^)(int error_code))errorBlock {
  176. if (self.myFiles) {
  177. [[WFCCIMService sharedWFCIMService] searchMyFiles:self.keyword beforeMessageUid:startPos order:FileRecordOrder_TIME_DESC count:count success:successBlock error:errorBlock];
  178. } else if(self.userFiles) {
  179. [[WFCCIMService sharedWFCIMService] searchFiles:self.keyword conversation:nil fromUser:self.userId beforeMessageUid:startPos order:FileRecordOrder_TIME_DESC count:count success:successBlock error:errorBlock];
  180. } else {
  181. [[WFCCIMService sharedWFCIMService] searchFiles:self.keyword conversation:self.conversation fromUser:nil beforeMessageUid:startPos order:FileRecordOrder_TIME_DESC count:count success:successBlock error:errorBlock];
  182. }
  183. }
  184. - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
  185. if (ceil(targetContentOffset->y)+1 >= ceil(scrollView.contentSize.height - scrollView.bounds.size.height)) {
  186. if (!self.searchController.active && self.hasMore) {
  187. [self loadMoreData];
  188. }
  189. if (self.searchController.active && self.searchMore) {
  190. [self searchMoreData];
  191. }
  192. }
  193. }
  194. - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  195. WFCUFileRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  196. if (!cell) {
  197. cell = [[WFCUFileRecordTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
  198. }
  199. WFCCFileRecord *record;
  200. if (self.searchController.active) {
  201. record = self.searchedRecords[indexPath.row];
  202. } else {
  203. record = self.fileRecords[indexPath.row];
  204. }
  205. cell.fileRecord = record;
  206. return cell;
  207. }
  208. - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  209. if (self.searchController.active) {
  210. return self.searchedRecords.count;
  211. }
  212. return self.fileRecords.count;
  213. }
  214. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  215. WFCCFileRecord *record;
  216. if (self.searchController.active) {
  217. record = self.searchedRecords[indexPath.row];
  218. } else {
  219. record = self.fileRecords[indexPath.row];
  220. }
  221. return [WFCUFileRecordTableViewCell sizeOfRecord:record withCellWidth:self.view.bounds.size.width];
  222. }
  223. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  224. WFCCFileRecord *record;
  225. if (self.searchController.active) {
  226. record = self.searchedRecords[indexPath.row];
  227. } else {
  228. record = self.fileRecords[indexPath.row];
  229. }
  230. if ([record.userId isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  231. return YES;
  232. } else if(record.conversation.type == Group_Type) {
  233. WFCCGroupInfo *groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:record.conversation.target refresh:NO];
  234. if ([groupInfo.owner isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  235. return YES;
  236. }
  237. WFCCGroupMember *member = [[WFCCIMService sharedWFCIMService] getGroupMember:record.conversation.target memberId:[WFCCNetworkService sharedInstance].userId];
  238. if (member.type != Member_Type_Manager) {
  239. return NO;
  240. }
  241. WFCCGroupMember *senderMember = [[WFCCIMService sharedWFCIMService] getGroupMember:record.conversation.target memberId:record.userId];
  242. if (senderMember.type != Member_Type_Manager && senderMember.type != Member_Type_Owner) {
  243. return YES;
  244. }
  245. }
  246. return NO;
  247. }
  248. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  249. if (editingStyle == UITableViewCellEditingStyleDelete) {
  250. WFCCFileRecord *record;
  251. if (self.searchController.active) {
  252. record = self.searchedRecords[indexPath.row];
  253. } else {
  254. record = self.fileRecords[indexPath.row];
  255. }
  256. __weak typeof(self) ws = self;
  257. [[WFCCIMService sharedWFCIMService] deleteFileRecord:record.messageUid success:^{
  258. [ws.fileRecords removeObject:record];
  259. [ws.tableView reloadData];
  260. } error:^(int error_code) {
  261. }];
  262. }
  263. }
  264. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  265. WFCCFileRecord *record;
  266. if (self.searchController.active) {
  267. record = self.searchedRecords[indexPath.row];
  268. } else {
  269. record = self.fileRecords[indexPath.row];
  270. }
  271. __weak typeof(self)ws = self;
  272. [[WFCCIMService sharedWFCIMService] getAuthorizedMediaUrl:record.messageUid mediaType:Media_Type_FILE mediaPath:record.url success:^(NSString *authorizedUrl, NSString *backupUrl) {
  273. WFCUBrowserViewController *bvc = [[WFCUBrowserViewController alloc] init];
  274. bvc.url = authorizedUrl;
  275. [ws.navigationController pushViewController:bvc animated:YES];
  276. } error:^(int error_code) {
  277. WFCUBrowserViewController *bvc = [[WFCUBrowserViewController alloc] init];
  278. bvc.url = record.url;
  279. [ws.navigationController pushViewController:bvc animated:YES];
  280. }];
  281. }
  282. #pragma mark - UISearchControllerDelegate
  283. - (void)didPresentSearchController:(UISearchController *)searchController {
  284. self.searchController.view.frame = self.view.bounds;
  285. self.tabBarController.tabBar.hidden = YES;
  286. self.extendedLayoutIncludesOpaqueBars = YES;
  287. }
  288. - (void)willDismissSearchController:(UISearchController *)searchController {
  289. self.tabBarController.tabBar.hidden = NO;
  290. self.extendedLayoutIncludesOpaqueBars = NO;
  291. }
  292. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  293. NSString *searchString = [self.searchController.searchBar text];
  294. self.searchedRecords = [[NSMutableArray alloc] init];
  295. self.searchMore = YES;
  296. self.keyword = searchString;
  297. if (searchString.length) {
  298. [self searchMoreData];
  299. }
  300. [self.tableView reloadData];
  301. }
  302. @end