CCTransfers.m 27 KB

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