JXFileViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. //
  2. // JXFileViewController.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 17/7/4.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "JXFileViewController.h"
  9. #import "JXShareFileTableViewCell.h"
  10. #import "JXShareFileObject.h"
  11. #import "JX_DownListView.h"
  12. #import "JXFileDetailViewController.h"
  13. #import "MCDownloader.h"
  14. #import "QBImagePickerController.h"
  15. //#import <AssetsLibrary/ALAssetsLibrary.h>
  16. @interface JXFileViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,QBImagePickerControllerDelegate>
  17. @property (nonatomic, strong) NSMutableArray * dataArray;
  18. @property (nonatomic, strong) NSIndexPath * currentIndexpath;
  19. @property (nonatomic, strong) UIButton * addFileButton;
  20. @end
  21. @implementation JXFileViewController
  22. -(instancetype)init{
  23. self = [super init];
  24. if (self) {
  25. self.isGotoBack = YES;
  26. self.heightHeader = JX_SCREEN_TOP;
  27. self.heightFooter = 0;
  28. self.title = Localized(@"JXRoomMemberVC_ShareFile");
  29. _dataArray = [[NSMutableArray alloc] init];
  30. }
  31. return self;
  32. }
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. [self createHeadAndFoot];
  36. [self customView];
  37. _table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  38. _table.tableFooterView = [[UIView alloc] init];
  39. [_table registerClass:[JXShareFileTableViewCell class] forCellReuseIdentifier:NSStringFromClass([JXShareFileTableViewCell class])];
  40. [self getServerData];
  41. }
  42. -(void)customView{
  43. if (!_addFileButton){
  44. NSString *image = THESIMPLESTYLE ? @"im_003_more_button_black" : @"im_003_more_button_normal";
  45. _addFileButton = [UIFactory createButtonWithImage:image
  46. highlight:nil
  47. target:self
  48. selector:@selector(addNewShareFile)];
  49. _addFileButton.frame = CGRectMake(JX_SCREEN_WIDTH-15-18, JX_SCREEN_TOP-15-18, 18, 18);
  50. [self.tableHeader addSubview:_addFileButton];
  51. }
  52. }
  53. -(void)getServerData{
  54. [g_server roomShareListRoomId:_room.roomId userId:nil pageSize:12 pageIndex:_page toView:self];
  55. }
  56. - (void)didReceiveMemoryWarning {
  57. [super didReceiveMemoryWarning];
  58. // Dispose of any resources that can be recreated.
  59. }
  60. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  61. return _dataArray.count;
  62. }
  63. - (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  64. JXShareFileTableViewCell * cell = [_tableView dequeueReusableCellWithIdentifier:NSStringFromClass([JXShareFileTableViewCell class]) forIndexPath:indexPath];
  65. JXShareFileObject * fileObject = _dataArray[indexPath.row];
  66. [cell setShareFileListCellWith:fileObject indexPath:indexPath];
  67. return cell;
  68. }
  69. - (CGFloat)tableView:(UITableView *)_tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;{
  70. return 60;
  71. }
  72. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  73. JXShareFileObject * fileObject = _dataArray[indexPath.row];
  74. memberData * myMem = nil;
  75. for (memberData * member in _room.members) {
  76. if ([[NSString stringWithFormat:@"%ld",member.userId] isEqualToString:g_myself.userId]) {
  77. myMem = member;
  78. break;
  79. }
  80. }
  81. if ([myMem.role integerValue] <= 2 || [fileObject.userId isEqualToString:g_myself.userId]) {
  82. return YES;
  83. }else{
  84. return NO;
  85. }
  86. }
  87. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  88. JXShareFileObject *obj = _dataArray[indexPath.row];
  89. memberData * myMem = nil;
  90. for (memberData * member in _room.members) {
  91. if ([[NSString stringWithFormat:@"%ld",member.userId] isEqualToString:g_myself.userId]) {
  92. myMem = member;
  93. break;
  94. }
  95. }
  96. if ([myMem.role integerValue] <= 2 || [obj.userId isEqualToString:MY_USER_ID]) {
  97. return UITableViewCellEditingStyleDelete;
  98. }
  99. return UITableViewCellEditingStyleNone;
  100. }
  101. - (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
  102. if (editingStyle == UITableViewCellEditingStyleDelete) {
  103. JXShareFileObject * fileObject = _dataArray[indexPath.row];
  104. _currentIndexpath = indexPath;
  105. [g_server roomShareDeleteRoomId:fileObject.roomId shareId:fileObject.shareId toView:self];
  106. }
  107. }
  108. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  109. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  110. JXShareFileObject * shareFile = _dataArray[indexPath.row];
  111. JXFileDetailViewController * detailVC = [[JXFileDetailViewController alloc] init];
  112. detailVC.shareFile = shareFile;
  113. // [g_window addSubview:detailVC.view];
  114. [g_navigation pushViewController:detailVC animated:YES];
  115. }
  116. #pragma mark - Network
  117. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  118. [_wait hide];
  119. [self stopLoading];
  120. if([aDownload.action isEqualToString:act_shareList]){
  121. if (_page == 0) {
  122. [_dataArray removeAllObjects];
  123. }
  124. NSMutableArray * tempAray = [[NSMutableArray alloc] init];
  125. for (NSDictionary * dict in array1) {
  126. JXShareFileObject * shareFile = [JXShareFileObject shareFileWithDict:dict];
  127. [tempAray addObject:shareFile];
  128. }
  129. if (tempAray.count > 0) {
  130. [_dataArray addObjectsFromArray:tempAray];
  131. }
  132. [_table reloadData];
  133. }else if([aDownload.action isEqualToString:act_shareAdd]){
  134. JXShareFileObject * shareFile = [JXShareFileObject shareFileWithDict:dict];
  135. [_dataArray addObject:shareFile];
  136. [_table reloadData];
  137. }else if([aDownload.action isEqualToString:act_shareGet]){
  138. }else if([aDownload.action isEqualToString:act_shareDelete]){
  139. [g_server showMsg:Localized(@"JXFile_deleteRoomFileSuccess")];
  140. [_dataArray removeObjectAtIndex:_currentIndexpath.row];
  141. [_table deleteRowsAtIndexPaths:@[_currentIndexpath] withRowAnimation:UITableViewRowAnimationRight];
  142. }else if ([aDownload.action isEqualToString:act_UploadFile]){
  143. NSArray * listArray = @[@"audios",@"images",@"others",@"videos"];
  144. NSString * fileUrl = nil;
  145. NSString * fileName = nil;
  146. NSInteger fileType = 0;
  147. int tbreak = 0;
  148. for (int i = 0; i<listArray.count; i++) {
  149. NSArray * dataArray = [dict objectForKey:listArray[i]];
  150. if ([dataArray count]) {
  151. for (NSDictionary * dataDict in dataArray) {
  152. fileUrl = dataDict[@"oUrl"];
  153. fileName = dataDict[@"oFileName"];
  154. tbreak = 1;
  155. switch (i) {
  156. case 0:
  157. fileType = 2;//音频
  158. break;
  159. case 1:
  160. fileType = 1;//图片
  161. break;
  162. case 2:
  163. fileType = 9;//其他
  164. break;
  165. case 3:
  166. fileType = 3;//视频
  167. break;
  168. default:{
  169. NSString * fileExt = [fileName pathExtension];
  170. if ([fileExt isEqualToString:@"jpg"] || [fileExt isEqualToString:@"jpeg"] || [fileExt isEqualToString:@"png"] || [fileExt isEqualToString:@"gif"] || [fileExt isEqualToString:@"bmp"])
  171. fileType = 1;
  172. else if ([fileExt isEqualToString:@"amr"] || [fileExt isEqualToString:@"mp3"] || [fileExt isEqualToString:@"wav"])
  173. fileType = 2;
  174. else if ([fileExt isEqualToString:@"mp4"] || [fileExt isEqualToString:@"mov"])
  175. fileType = 3;
  176. else if ([fileExt isEqualToString:@"ppt"] || [fileExt isEqualToString:@"pptx"])
  177. fileType = 4;
  178. else if ([fileExt isEqualToString:@"xls"] || [fileExt isEqualToString:@"xlsx"])
  179. fileType = 5;
  180. else if ([fileExt isEqualToString:@"doc"] || [fileExt isEqualToString:@"docx"])
  181. fileType = 6;
  182. else if ([fileExt isEqualToString:@"zip"] || [fileExt isEqualToString:@"rar"])
  183. fileType = 7;
  184. else if ([fileExt isEqualToString:@"txt"])
  185. fileType = 8;
  186. else if ([fileExt isEqualToString:@"pdf"])
  187. fileType = 10;
  188. else
  189. fileType = 9;
  190. break;
  191. }
  192. }
  193. }
  194. }
  195. if (tbreak == 1){
  196. break;
  197. }
  198. }
  199. [g_server roomShareAddRoomId:_room.roomId url:fileUrl fileName:fileName size:[NSNumber numberWithLong:aDownload.uploadDataSize] type:fileType toView:self];
  200. }
  201. }
  202. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  203. [_wait hide];
  204. return show_error;
  205. }
  206. -(int) didServerConnectError:(JXConnection*)aDownload error:(NSError *)error{//error为空时,代表超时
  207. [_wait hide];
  208. return show_error;
  209. }
  210. -(void) didServerConnectStart:(JXConnection*)aDownload{
  211. if ([aDownload.action isEqualToString:act_UploadFile]) {
  212. [_wait start:Localized(@"JXFile_uploading")];
  213. }else{
  214. [_wait start];
  215. }
  216. }
  217. #pragma mark ----------图片选择完成-------------
  218. //UIImagePickerController代理方法
  219. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  220. {
  221. if ([picker isMemberOfClass:[QBImagePickerController class]]) {
  222. if (info[@"UIImagePickerControllerMediaType"] == ALAssetTypeVideo){
  223. // NSURL * videoUrl = info[@"UIImagePickerControllerReferenceURL"];
  224. }
  225. }else{
  226. UIImage * chosedImage=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
  227. // UIImage * editedImage=[info objectForKey:@"UIImagePickerControllerEditedImage"];
  228. // int imageWidth = chosedImage.size.width;
  229. // int imageHeight = chosedImage.size.height;
  230. [self dismissViewControllerAnimated:NO completion:^{
  231. NSString* file = [FileInfo getUUIDFileName:@"jpg"];
  232. [g_server saveImageToFile:chosedImage file:file isOriginal:NO];
  233. [g_server uploadFile:file validTime:@"-1" messageId:nil toView:self];
  234. }];
  235. }
  236. }
  237. -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  238. {
  239. [self dismissViewControllerAnimated:NO completion:^{
  240. }];
  241. }
  242. #pragma mark - actions
  243. -(void)addNewShareFile{
  244. memberData *data = [self.room getMember:g_myself.userId];
  245. BOOL flag = [data.role intValue] == 1 || [data.role intValue] == 2;
  246. if (!flag && !self.room.allowUploadFile) {
  247. [g_App showAlert:Localized(@"JX_NotUploadSharedFiles")];
  248. return;
  249. }
  250. if ([data.role integerValue] == 4) {
  251. [JXMyTools showTipView:@"隐身人不能上传共享文件"];
  252. return;
  253. }
  254. UIWindow *window = [[UIApplication sharedApplication].delegate window];
  255. CGRect moreFrame = [self.tableHeader convertRect:_addFileButton.frame toView:window];
  256. JX_DownListView * downListView = [[JX_DownListView alloc] initWithFrame:self.view.bounds];
  257. // downListView.listContents = @[@"上传文件",@"上传图片",@"上传视频"];
  258. // downListView.listImages = @[@"me_press",@"me_press",@"me_press"];
  259. downListView.listContents = @[Localized(@"JXFile_uploadPhoto")];
  260. // Localized(@"JXFile_uploadVideo")
  261. __weak typeof(self) weakSelf = self;
  262. [downListView downlistPopOption:^(NSInteger index, NSString *content) {
  263. // if (index == 0) {
  264. // [weakSelf showSelLocalFileView];
  265. // }else if (index == 1) {
  266. [weakSelf showSelImagePicker];
  267. // }else if (index == 1) {
  268. // [weakSelf showSelVideo];
  269. // }
  270. } whichFrame:moreFrame animate:YES];
  271. [downListView show];
  272. }
  273. -(void)showSelLocalFileView{
  274. NSFileManager *fileManager = [NSFileManager defaultManager];
  275. NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  276. NSString *strPath = [documentsDirectory stringByAppendingPathComponent:MCDownloadCacheFolderName];
  277. NSDirectoryEnumerator<NSString *> * myDirectoryEnumerator;
  278. myDirectoryEnumerator= [fileManager enumeratorAtPath:strPath];
  279. while (strPath = [myDirectoryEnumerator nextObject]) {
  280. for (NSString * namePath in strPath.pathComponents) {
  281. NSLog(@"-----AAA-----%@", namePath );
  282. }
  283. }
  284. }
  285. -(void)showSelImagePicker{
  286. UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
  287. ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  288. ipc.delegate = self;
  289. ipc.allowsEditing = YES;
  290. ipc.modalPresentationStyle = UIModalPresentationFullScreen;
  291. // [g_window addSubview:ipc.view];
  292. if (IS_PAD) {
  293. UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:ipc];
  294. [pop presentPopoverFromRect:CGRectMake((self.view.frame.size.width - 320) / 2, 0, 300, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  295. }else {
  296. [self presentViewController:ipc animated:YES completion:nil];
  297. }
  298. }
  299. -(void)showSelVideo{
  300. QBImagePickerController * videoPick = [[QBImagePickerController alloc] init];
  301. videoPick.filterType = QBImagePickerFilterTypeAllVideos;
  302. videoPick.delegate = self;
  303. videoPick.showsCancelButton = YES;
  304. videoPick.fullScreenLayoutEnabled = YES;
  305. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:videoPick];
  306. [self presentViewController:navigationController animated:YES completion:NULL];
  307. // ALAssetsLibrary *library1 = [[ALAssetsLibrary alloc] init];
  308. // [library1 enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
  309. // if (group) {
  310. // [group setAssetsFilter:[ALAssetsFilter allVideos]];
  311. // [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
  312. //
  313. // if (result) {
  314. // AlbumVideoInfo *videoInfo = [[AlbumVideoInfo alloc] init];
  315. // videoInfo.thumbnail = [UIImage imageWithCGImage:result.thumbnail];
  316. // // videoInfo.videoURL = [result valueForProperty:ALAssetPropertyAssetURL];
  317. // videoInfo.videoURL = result.defaultRepresentation.url;
  318. // videoInfo.duration = [result valueForProperty:ALAssetPropertyDuration];
  319. // videoInfo.name = [self getFormatedDateStringOfDate:[result valueForProperty:ALAssetPropertyDate]];
  320. // videoInfo.size = result.defaultRepresentation.size; //Bytes
  321. // videoInfo.format = [result.defaultRepresentation.filename pathExtension];
  322. // [_albumVideoInfos addObject:videoInfo];
  323. // }
  324. // }];
  325. // } else {
  326. // //没有更多的group时,即可认为已经加载完成。
  327. // NSLog(@"after load, the total alumvideo count is %ld",_albumVideoInfos.count);
  328. // dispatch_async(dispatch_get_main_queue(), ^{
  329. // [self showAlbumVideos];
  330. // });
  331. // }
  332. //
  333. // } failureBlock:^(NSError *error) {
  334. // NSLog(@"Failed.");
  335. // }];
  336. }
  337. @end