CCTransfers.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. //
  2. // CCTransfers.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 12/04/17.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCTransfers.h"
  24. #import "AppDelegate.h"
  25. #import "CCMain.h"
  26. #import "CCDetail.h"
  27. #import "CCSection.h"
  28. #import "CCCellMainTransfer.h"
  29. #import "NCBridgeSwift.h"
  30. #define download 1
  31. #define downloadwwan 2
  32. #define upload 3
  33. #define uploadwwan 4
  34. @interface CCTransfers ()
  35. {
  36. AppDelegate *appDelegate;
  37. // Datasource
  38. CCSectionDataSourceMetadata *sectionDataSource;
  39. }
  40. @end
  41. @implementation CCTransfers
  42. #pragma --------------------------------------------------------------------------------------------
  43. #pragma mark ===== Init =====
  44. #pragma --------------------------------------------------------------------------------------------
  45. - (id)initWithCoder:(NSCoder *)aDecoder
  46. {
  47. if (self = [super initWithCoder:aDecoder]) {
  48. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  49. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  50. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  51. }
  52. return self;
  53. }
  54. - (void)viewDidLoad {
  55. [super viewDidLoad];
  56. // Custom Cell
  57. [self.tableView registerNib:[UINib nibWithNibName:@"CCCellMainTransfer" bundle:nil] forCellReuseIdentifier:@"CellMainTransfer"];
  58. self.tableView.delegate = self;
  59. self.tableView.dataSource = self;
  60. self.tableView.emptyDataSetDelegate = self;
  61. self.tableView.emptyDataSetSource = self;
  62. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  63. self.tableView.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  64. self.title = NSLocalizedString(@"_transfers_", nil);
  65. [self reloadDatasource];
  66. }
  67. // Apparirà
  68. - (void)viewWillAppear:(BOOL)animated
  69. {
  70. [super viewWillAppear:animated];
  71. // Color
  72. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  73. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  74. [self reloadDatasource];
  75. }
  76. - (void)changeTheming
  77. {
  78. if (self.isViewLoaded && self.view.window)
  79. [appDelegate changeTheming:self];
  80. }
  81. #pragma --------------------------------------------------------------------------------------------
  82. #pragma mark ==== DZNEmptyDataSetSource ====
  83. #pragma --------------------------------------------------------------------------------------------
  84. - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
  85. {
  86. return 0.0f;
  87. }
  88. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  89. {
  90. return [UIColor whiteColor];
  91. }
  92. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  93. {
  94. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loadNoRecord"] multiplier:2 color:[NCBrandColor sharedInstance].graySoft];
  95. }
  96. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  97. {
  98. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_no_transfer_", nil)];
  99. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  100. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  101. }
  102. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  103. {
  104. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_no_transfer_sub_", nil)];
  105. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  106. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  107. paragraph.alignment = NSTextAlignmentCenter;
  108. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  109. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  110. }
  111. #pragma --------------------------------------------------------------------------------------------
  112. #pragma mark - ===== Progress & Task Button =====
  113. #pragma --------------------------------------------------------------------------------------------
  114. - (void)triggerProgressTask:(NSNotification *)notification
  115. {
  116. NSDictionary *dict = notification.userInfo;
  117. NSString *fileID = [dict valueForKey:@"fileID"];
  118. // NSString *serverUrl = [dict valueForKey:@"serverUrl"];
  119. long status = [[dict valueForKey:@"status"] longValue];
  120. NSString *statusString = @"";
  121. float progress = [[dict valueForKey:@"progress"] floatValue];
  122. long long totalBytes = [[dict valueForKey:@"totalBytes"] longLongValue];
  123. long long totalBytesExpected = [[dict valueForKey:@"totalBytesExpected"] longLongValue];
  124. // Check
  125. if (!fileID || [fileID isEqualToString: @""])
  126. return;
  127. [appDelegate.listProgressMetadata setObject:[NSNumber numberWithFloat:progress] forKey:fileID];
  128. NSIndexPath *indexPath = [sectionDataSource.fileIDIndexPath objectForKey:fileID];
  129. if (indexPath && indexPath.row == 0) {
  130. CCCellMainTransfer *cell = (CCCellMainTransfer *)[self.tableView cellForRowAtIndexPath:indexPath];
  131. if (status == k_metadataStatusInDownload) {
  132. statusString = @"↓";
  133. } else if (status == k_metadataStatusInUpload) {
  134. statusString = @"↑";
  135. }
  136. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ - %@%@", [CCUtility transformedSize:totalBytesExpected], statusString, [CCUtility transformedSize:totalBytes]];
  137. if ([cell isKindOfClass:[CCCellMainTransfer class]]) {
  138. cell.transferButton.progress = progress;
  139. }
  140. } else {
  141. [self reloadDatasource];
  142. }
  143. }
  144. - (void)cancelTaskButton:(id)sender withEvent:(UIEvent *)event
  145. {
  146. UITouch * touch = [[event allTouches] anyObject];
  147. CGPoint location = [touch locationInView:self.tableView];
  148. NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:location];
  149. if (indexPath) {
  150. NSString *fileID = [[sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
  151. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", fileID]];
  152. if (metadata)
  153. [appDelegate.activeMain cancelTaskButton:metadata reloadTable:YES];
  154. }
  155. }
  156. - (void)cancelAllTask
  157. {
  158. if (appDelegate.activeMain == nil)
  159. return;
  160. BOOL lastAndRefresh = NO;
  161. for (NSString *key in sectionDataSource.allRecordsDataSource.allKeys) {
  162. if ([key isEqualToString:[sectionDataSource.allRecordsDataSource.allKeys lastObject]])
  163. lastAndRefresh = YES;
  164. tableMetadata *metadata = [sectionDataSource.allRecordsDataSource objectForKey:key];
  165. if ([metadata.session containsString:@"upload"] && ((metadata.sessionTaskIdentifier == k_taskIdentifierDone) || (metadata.sessionTaskIdentifier >= 0)))
  166. continue;
  167. [appDelegate.activeMain cancelTaskButton:metadata reloadTable:lastAndRefresh];
  168. }
  169. }
  170. #pragma --------------------------------------------------------------------------------------------
  171. #pragma mark - ==== Datasource ====
  172. #pragma --------------------------------------------------------------------------------------------
  173. - (void)reloadDatasource
  174. {
  175. // test
  176. if (appDelegate.activeAccount.length == 0)
  177. return;
  178. NSArray *recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND ((session CONTAINS 'upload') OR (session CONTAINS 'download'))", appDelegate.activeAccount] sorted:@"sessionTaskIdentifier" ascending:YES];
  179. sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:appDelegate.listProgressMetadata groupByField:@"session" fileIDHide:nil activeAccount:appDelegate.activeAccount];
  180. [self.tableView reloadData];
  181. }
  182. #pragma --------------------------------------------------------------------------------------------
  183. #pragma mark - ==== Table ====
  184. #pragma --------------------------------------------------------------------------------------------
  185. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  186. {
  187. return 60;
  188. }
  189. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  190. {
  191. return [[sectionDataSource.sectionArrayRow allKeys] count];
  192. }
  193. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  194. {
  195. return [[sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:section]] count];
  196. }
  197. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  198. {
  199. return 13.0f;
  200. }
  201. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  202. {
  203. UIVisualEffectView *visualEffectView;
  204. NSString *titleSection, *numberTitle;
  205. NSInteger typeOfSession = 0;
  206. NSInteger queueDownload = [[[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND (session == %@ OR session == %@)", appDelegate.activeAccount, k_download_session, k_download_session_foreground] sorted:nil ascending:NO] count];
  207. NSInteger queueDownloadWWan = [[[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND session == %@", appDelegate.activeAccount, k_download_session_wwan] sorted:nil ascending:NO] count];
  208. NSInteger queueUpload = [[[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND (session == %@ OR session == %@)", appDelegate.activeAccount, k_upload_session, k_upload_session_foreground] sorted:nil ascending:NO] count];
  209. NSInteger queueUploadWWan = [[[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND session == %@", appDelegate.activeAccount, k_upload_session_wwan] sorted:nil ascending:NO] count];
  210. if ([[sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSString class]]) titleSection = [sectionDataSource.sections objectAtIndex:section];
  211. if ([[sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSDate class]]) titleSection = [CCUtility getTitleSectionDate:[sectionDataSource.sections objectAtIndex:section]];
  212. NSArray *metadatas = [sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:section]];
  213. NSUInteger rowsCount = [metadatas count];
  214. visualEffectView = [[UIVisualEffectView alloc] init];
  215. visualEffectView.backgroundColor = [UIColor clearColor];
  216. // title section
  217. if ([titleSection isEqualToString:@"_none_"]) {
  218. titleSection = @"";
  219. } else if ([titleSection containsString:@"download"] && ![titleSection containsString:@"wwan"]) {
  220. typeOfSession = download;
  221. titleSection = NSLocalizedString(@"_title_section_download_",nil);
  222. } else if ([titleSection containsString:@"download"] && [titleSection containsString:@"wwan"]) {
  223. typeOfSession = downloadwwan;
  224. titleSection = [NSLocalizedString(@"_title_section_download_",nil) stringByAppendingString:@" Wi-Fi"];
  225. } else if ([titleSection containsString:@"upload"] && ![titleSection containsString:@"wwan"]) {
  226. typeOfSession = upload;
  227. titleSection = NSLocalizedString(@"_title_section_upload_",nil);
  228. } else if ([titleSection containsString:@"upload"] && [titleSection containsString:@"wwan"]) {
  229. typeOfSession = uploadwwan;
  230. titleSection = [NSLocalizedString(@"_title_section_upload_",nil) stringByAppendingString:@" Wi-Fi"];
  231. } else {
  232. titleSection = NSLocalizedString(titleSection,nil);
  233. }
  234. // title label on left
  235. UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(8, 3, 0, 13)];
  236. titleLabel.textColor = [UIColor blackColor];
  237. titleLabel.font = [UIFont systemFontOfSize:9];
  238. titleLabel.textAlignment = NSTextAlignmentLeft;
  239. titleLabel.text = titleSection;
  240. titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  241. [visualEffectView.contentView addSubview:titleLabel];
  242. // element (s) on right
  243. UILabel *elementLabel=[[UILabel alloc]initWithFrame:CGRectMake(-8, 3, 0, 13)];
  244. elementLabel.textColor = [UIColor blackColor];
  245. elementLabel.font = [UIFont systemFontOfSize:9];
  246. elementLabel.textAlignment = NSTextAlignmentRight;
  247. elementLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  248. if ((typeOfSession == download && queueDownload > rowsCount) || (typeOfSession == downloadwwan && queueDownloadWWan > rowsCount) ||
  249. (typeOfSession == upload && queueUpload > rowsCount) || (typeOfSession == uploadwwan && queueUploadWWan > rowsCount)) {
  250. numberTitle = [NSString stringWithFormat:@"%lu+", (unsigned long)rowsCount];
  251. } else {
  252. numberTitle = [NSString stringWithFormat:@"%lu", (unsigned long)rowsCount];
  253. }
  254. if (rowsCount > 1)
  255. elementLabel.text = [NSString stringWithFormat:@"%@ %@", numberTitle, NSLocalizedString(@"_elements_",nil)];
  256. else
  257. elementLabel.text = [NSString stringWithFormat:@"%@ %@", numberTitle, NSLocalizedString(@"_element_",nil)];
  258. // view
  259. [visualEffectView.contentView addSubview:elementLabel];
  260. return visualEffectView;
  261. }
  262. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  263. {
  264. NSString *titleSection;
  265. NSString *element_s;
  266. if ([[sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSString class]]) titleSection = [sectionDataSource.sections objectAtIndex:section];
  267. // Prepare view for title in footer
  268. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  269. UILabel *titleFooterLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
  270. titleFooterLabel.textColor = [UIColor blackColor];
  271. titleFooterLabel.font = [UIFont systemFontOfSize:12];
  272. titleFooterLabel.textAlignment = NSTextAlignmentCenter;
  273. // Footer Download
  274. if ([titleSection containsString:@"download"] && ![titleSection containsString:@"wwan"] && titleSection != nil) {
  275. NSInteger queueDownload = [[[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND (session == %@ OR session == %@)", appDelegate.activeAccount, k_download_session, k_download_session_foreground] sorted:nil ascending:NO] count];
  276. // element or elements ?
  277. if (queueDownload > 1) element_s = NSLocalizedString(@"_elements_",nil);
  278. else element_s = NSLocalizedString(@"_element_",nil);
  279. // Num record to upload
  280. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_download_", nil), queueDownload, element_s]];
  281. titleFooterLabel.attributedText = stringFooter;
  282. [view addSubview:titleFooterLabel];
  283. return view;
  284. }
  285. // Footer Download WWAN
  286. if ([titleSection containsString:@"download"] && [titleSection containsString:@"wwan"] && titleSection != nil) {
  287. NSInteger queueDownloadWWan = [[[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND session == %@", appDelegate.activeAccount, k_download_session_wwan] sorted:nil ascending:NO] count];
  288. // element or elements ?
  289. if (queueDownloadWWan > 1) element_s = NSLocalizedString(@"_elements_",nil);
  290. else element_s = NSLocalizedString(@"_element_",nil);
  291. // Add the symbol WiFi and Num record
  292. NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
  293. attachment.image = [UIImage imageNamed:@"WiFiSmall"];
  294. NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
  295. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[@" " stringByAppendingString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_download_wwan_", nil), queueDownloadWWan, element_s]]];
  296. [stringFooter insertAttributedString:attachmentString atIndex:0];
  297. titleFooterLabel.attributedText = stringFooter;
  298. [view addSubview:titleFooterLabel];
  299. return view;
  300. }
  301. // Footer Upload
  302. if ([titleSection containsString:@"upload"] && ![titleSection containsString:@"wwan"] && titleSection != nil) {
  303. NSInteger queueUpload = [[[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND (session == %@ OR session == %@)", appDelegate.activeAccount, k_upload_session, k_upload_session_foreground] sorted:nil ascending:NO] count];
  304. // element or elements ?
  305. if (queueUpload > 1) element_s = NSLocalizedString(@"_elements_",nil);
  306. else element_s = NSLocalizedString(@"_element_",nil);
  307. // Num record to upload
  308. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_upload_", nil), queueUpload, element_s]];
  309. titleFooterLabel.attributedText = stringFooter;
  310. [view addSubview:titleFooterLabel];
  311. return view;
  312. }
  313. // Footer Upload WWAN
  314. if ([titleSection containsString:@"upload"] && [titleSection containsString:@"wwan"] && titleSection != nil) {
  315. NSInteger queueUploadWWan = [[[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND session == %@", appDelegate.activeAccount, k_upload_session_wwan] sorted:nil ascending:NO] count];
  316. // element or elements ?
  317. if (queueUploadWWan > 1) element_s = NSLocalizedString(@"_elements_",nil);
  318. else element_s = NSLocalizedString(@"_element_",nil);
  319. // Add the symbol WiFi and Num record
  320. NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
  321. attachment.image = [UIImage imageNamed:@"WiFiSmall"];
  322. NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
  323. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[@" " stringByAppendingString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_upload_wwan_", nil), queueUploadWWan,element_s]]];
  324. [stringFooter insertAttributedString:attachmentString atIndex:0];
  325. titleFooterLabel.attributedText = stringFooter;
  326. [view addSubview:titleFooterLabel];
  327. return view;
  328. }
  329. return nil;
  330. }
  331. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  332. {
  333. //NSString *titleSection;
  334. //if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSString class]])
  335. // titleSection = [_sectionDataSource.sections objectAtIndex:section];
  336. //if ([titleSection rangeOfString:@"upload"].location != NSNotFound && [titleSection rangeOfString:@"wwan"].location != NSNotFound && titleSection != nil) return 18.0f;
  337. //else return 0.0f;
  338. return 18.0f;
  339. }
  340. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
  341. {
  342. return [sectionDataSource.sections indexOfObject:title];
  343. }
  344. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  345. {
  346. NSString *fileID = [[sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
  347. tableMetadata *metadata = [sectionDataSource.allRecordsDataSource objectForKey:fileID];
  348. // Create File System
  349. if (metadata.directory) {
  350. [CCUtility getDirectoryProviderStorageFileID:metadata.fileID];
  351. } else {
  352. [CCUtility getDirectoryProviderStorageFileID:metadata.fileID fileName:metadata.fileNameView];
  353. }
  354. CCCellMainTransfer *cell = [tableView dequeueReusableCellWithIdentifier:@"CellMainTransfer" forIndexPath:indexPath];
  355. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  356. cell.accessoryType = UITableViewCellAccessoryNone;
  357. cell.file.image = nil;
  358. cell.status.image = nil;
  359. cell.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  360. cell.labelTitle.textColor = [UIColor blackColor];
  361. cell.labelTitle.text = metadata.fileNameView;
  362. cell.transferButton.tintColor = [NCBrandColor sharedInstance].icon;
  363. // Write status on Label Info
  364. NSString *statusString = @"";
  365. switch (metadata.status) {
  366. case 2:
  367. statusString = NSLocalizedString(@"_status_wait_download_",nil);
  368. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ %@", [CCUtility transformedSize:metadata.size], statusString];
  369. break;
  370. case 3:
  371. statusString = NSLocalizedString(@"_status_in_download_",nil);
  372. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ %@", [CCUtility transformedSize:metadata.size], statusString];
  373. break;
  374. case 4:
  375. statusString = NSLocalizedString(@"_status_downloading_",nil);
  376. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", [CCUtility transformedSize:metadata.size]];
  377. break;
  378. case 6:
  379. statusString = NSLocalizedString(@"_status_wait_upload_",nil);
  380. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", statusString];
  381. break;
  382. case 7:
  383. statusString = NSLocalizedString(@"_status_in_upload_",nil);
  384. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", statusString];
  385. break;
  386. case 8:
  387. statusString = NSLocalizedString(@"_status_uploading_",nil);
  388. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ %@", [CCUtility transformedSize:metadata.size], statusString];
  389. break;
  390. default:
  391. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", [CCUtility transformedSize:metadata.size]];
  392. break;
  393. }
  394. BOOL iconFileExists = [[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  395. if (iconFileExists) {
  396. cell.file.image = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  397. } else {
  398. if (metadata.iconName.length > 0) {
  399. cell.file.image = [UIImage imageNamed:metadata.iconName];
  400. } else {
  401. cell.file.image = [UIImage imageNamed:@"file"];
  402. }
  403. }
  404. // Session Upload Extension
  405. if ([metadata.session isEqualToString:k_upload_session_extension] && (metadata.status == k_metadataStatusInUpload || metadata.status == k_metadataStatusUploading)) {
  406. cell.labelTitle.enabled = NO;
  407. cell.labelInfoFile.enabled = NO;
  408. cell.userInteractionEnabled = NO;
  409. cell.transferButton.hidden = YES;
  410. } else {
  411. cell.labelTitle.enabled = YES;
  412. cell.labelInfoFile.enabled = YES;
  413. cell.userInteractionEnabled = YES;
  414. }
  415. // downloadFile
  416. if (metadata.status == k_metadataStatusWaitDownload || metadata.status == k_metadataStatusInDownload || metadata.status == k_metadataStatusDownloading || metadata.status == k_metadataStatusDownloadError) {
  417. //
  418. }
  419. // downloadFile Error
  420. if (metadata.status == k_metadataStatusDownloadError) {
  421. cell.status.image = [UIImage imageNamed:@"statuserror"];
  422. if ([metadata.sessionError length] == 0) {
  423. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"_error_",nil), NSLocalizedString(@"_file_not_downloaded_",nil)];
  424. } else {
  425. cell.labelInfoFile.text = metadata.sessionError;
  426. }
  427. }
  428. // uploadFile
  429. if (metadata.status == k_metadataStatusWaitUpload || metadata.status == k_metadataStatusInUpload || metadata.status == k_metadataStatusUploading || metadata.status == k_metadataStatusUploadError) {
  430. if (!iconFileExists) {
  431. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"uploadCloud"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement];
  432. }
  433. cell.labelTitle.enabled = NO;
  434. }
  435. // uploadFileError
  436. if (metadata.status == k_metadataStatusUploadError) {
  437. cell.labelTitle.enabled = NO;
  438. cell.status.image = [UIImage imageNamed:@"statuserror"];
  439. if (!iconFileExists) {
  440. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"uploadCloud"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement];
  441. }
  442. if ([metadata.sessionError length] == 0) {
  443. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"_error_",nil), NSLocalizedString(@"_file_not_uploaded_",nil)];
  444. } else {
  445. cell.labelInfoFile.text = metadata.sessionError;
  446. }
  447. }
  448. // Progress
  449. float progress = [[appDelegate.listProgressMetadata objectForKey:metadata.fileID] floatValue];
  450. cell.transferButton.progress = progress;
  451. // gesture Transfer
  452. [cell.transferButton.stopButton addTarget:self action:@selector(cancelTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  453. UILongPressGestureRecognizer *stopLongGesture = [UILongPressGestureRecognizer new];
  454. [stopLongGesture addTarget:self action:@selector(cancelAllTask:)];
  455. [cell.transferButton.stopButton addGestureRecognizer:stopLongGesture];
  456. return cell;
  457. }
  458. @end