CCControlCenter.m 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. //
  2. // CCControlCenter.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 07/04/16.
  6. // Copyright (c) 2014 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 "CCControlCenter.h"
  24. #import "AppDelegate.h"
  25. #import "CCMain.h"
  26. #import "CCDetail.h"
  27. #define BORDER_TOUCH_UPDOWN 50.0f
  28. #define TOOLBAR_TRANSFER_H 0.0f
  29. #define TOOLBAR_ADD_BORDER 20.0f
  30. #define SIZE_FONT_NORECORD 18.0f
  31. #define ANIMATION_GESTURE 0.50f
  32. #define download 1
  33. #define downloadwwan 2
  34. #define upload 3
  35. #define uploadwwan 4
  36. @interface CCControlCenter ()
  37. {
  38. UIVisualEffectView *_mainView;
  39. UITableView *_tableView;
  40. UILabel *_noRecord;
  41. //UIToolbar *_toolbarTask;
  42. UIImageView *_imageDrag;
  43. UIView *_endLine;
  44. CGFloat start, stop;
  45. UIPanGestureRecognizer *panNavigationBar, *panImageDrag;
  46. UITapGestureRecognizer *_singleFingerTap;
  47. // Datasource
  48. CCSectionDataSource *_sectionDataSource;
  49. }
  50. @end
  51. @implementation CCControlCenter
  52. #pragma --------------------------------------------------------------------------------------------
  53. #pragma mark ===== Init =====
  54. #pragma --------------------------------------------------------------------------------------------
  55. - (id)initWithCoder:(NSCoder *)aDecoder
  56. {
  57. if (self = [super initWithCoder:aDecoder]) {
  58. }
  59. return self;
  60. }
  61. - (void)viewDidLoad
  62. {
  63. [super viewDidLoad];
  64. UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
  65. _mainView = [[UIVisualEffectView alloc] initWithEffect:effect];
  66. [_mainView setFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, 0)];
  67. _mainView.hidden = YES;
  68. // TEST
  69. //_mainView.backgroundColor = [UIColor yellowColor];
  70. _tableView = [[UITableView alloc] init];
  71. [_tableView setFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, 0)];
  72. _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  73. [_tableView registerNib:[UINib nibWithNibName:@"CCControlCenterCell" bundle:nil] forCellReuseIdentifier:@"CCControlCenterCell"];
  74. _tableView.delegate = self;
  75. _tableView.dataSource = self;
  76. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  77. _tableView.backgroundColor = [UIColor clearColor];
  78. [_mainView addSubview:_tableView];
  79. _noRecord =[[UILabel alloc]init];
  80. _noRecord.backgroundColor=[UIColor clearColor];
  81. _noRecord.textColor = COLOR_BRAND;
  82. _noRecord.font = [UIFont systemFontOfSize:SIZE_FONT_NORECORD];
  83. _noRecord.textAlignment = NSTextAlignmentCenter;
  84. _noRecord.text = NSLocalizedString(@"_no_transfer_",nil);
  85. [_mainView addSubview:_noRecord];
  86. /*
  87. _toolbarTask = [[UIToolbar alloc] init];
  88. UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"toolbarTaskCancel"] style:UIBarButtonItemStylePlain target:self action:@selector(cancelAllTask)];
  89. UIBarButtonItem *stopItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"toolbarTaskStop"] style:UIBarButtonItemStylePlain target:self action:@selector(stopAllTask)];
  90. UIBarButtonItem *reloadItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"toolbarTaskReload"] style:UIBarButtonItemStylePlain target:self action:@selector(reloadAllTask)];
  91. UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
  92. [_toolbarTask setItems:[[NSArray alloc] initWithObjects:cancelItem, spacer, stopItem, spacer, reloadItem, nil]];
  93. [_toolbarTask setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
  94. [_toolbarTask setBackgroundImage:[UIImage new] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
  95. [_toolbarTask setShadowImage:[UIImage new] forToolbarPosition:UIBarPositionAny];
  96. [_mainView addSubview:_toolbarTask];
  97. */
  98. _imageDrag = [[UIImageView alloc] init];
  99. [_imageDrag setFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, 0)];
  100. _imageDrag.image = [UIImage imageNamed:image_drag];
  101. _imageDrag.contentMode = UIViewContentModeCenter;
  102. _imageDrag.userInteractionEnabled = YES;
  103. [_mainView addSubview:_imageDrag];
  104. _endLine = [[UIView alloc] init];
  105. [_endLine setFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, 0)];
  106. _endLine.backgroundColor = COLOR_BRAND;
  107. [_mainView addSubview:_endLine];
  108. [self.navigationBar.superview insertSubview:_mainView belowSubview:self.navigationBar];
  109. // Pop Gesture Recognizer
  110. [self.interactivePopGestureRecognizer addTarget:self action:@selector(handlePopGesture:)];
  111. panImageDrag = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
  112. panImageDrag.maximumNumberOfTouches = panImageDrag.minimumNumberOfTouches = 1;
  113. [_imageDrag addGestureRecognizer:panImageDrag];
  114. panNavigationBar = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
  115. panNavigationBar.maximumNumberOfTouches = panNavigationBar.minimumNumberOfTouches = 1;
  116. [self.navigationBar addGestureRecognizer:panNavigationBar];
  117. [self reloadDatasource];
  118. }
  119. /* iOS Developer Library : A panning gesture is continuous. It begins (UIGestureRecognizerStateBegan) when the minimum number of fingers allowed (minimumNumberOfTouches) has moved enough to be considered a pan. It changes (UIGestureRecognizerStateChanged) when a finger moves while at least the minimum number of fingers are pressed down. It ends (UIGestureRecognizerStateEnded) when all fingers are lifted. */
  120. - (void)handlePanGesture:(UIPanGestureRecognizer *)gesture;
  121. {
  122. CGPoint currentPoint = [gesture locationInView:self.view];
  123. CGFloat navigationBarH = self.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
  124. CGFloat navigationBarW = self.navigationBar.frame.size.width;
  125. CGFloat heightScreen = [UIScreen mainScreen].bounds.size.height - self.tabBarController.tabBar.frame.size.height;
  126. CGFloat heightTableView = [UIScreen mainScreen].bounds.size.height - self.tabBarController.tabBar.frame.size.height - navigationBarH - BORDER_TOUCH_UPDOWN - TOOLBAR_TRANSFER_H - TOOLBAR_ADD_BORDER;
  127. float centerMaxH = [self getMaxH] / 2;
  128. float step = [self getMaxH] / 10;
  129. float tableViewY = navigationBarH;
  130. // BUG Scroll to top of UITableView by tapping status bar
  131. _mainView.hidden = NO;
  132. // start
  133. if (gesture.state == UIGestureRecognizerStateBegan) {
  134. start = currentPoint.y;
  135. }
  136. // cancelled (return to 0)
  137. if (gesture.state == UIGestureRecognizerStateCancelled) {
  138. currentPoint.y = 0;
  139. tableViewY = 0;
  140. panNavigationBar.enabled = YES;
  141. }
  142. // end
  143. if (gesture.state == UIGestureRecognizerStateEnded) {
  144. stop = currentPoint.y;
  145. // DOWN
  146. if (start < stop) {
  147. if (stop < (start + step + navigationBarH) && start <= navigationBarH) {
  148. currentPoint.y = 0;
  149. tableViewY = 0;
  150. panNavigationBar.enabled = YES;
  151. } else {
  152. currentPoint.y = [self getMaxH];
  153. tableViewY = navigationBarH;
  154. panNavigationBar.enabled = NO;
  155. }
  156. }
  157. // UP
  158. if (start >= stop && start >= navigationBarH) {
  159. if (stop > (start - step)) {
  160. currentPoint.y = [self getMaxH];
  161. tableViewY = navigationBarH;
  162. panNavigationBar.enabled = NO;
  163. } else {
  164. currentPoint.y = 0;
  165. tableViewY = 0;
  166. panNavigationBar.enabled = YES;
  167. }
  168. }
  169. }
  170. // changed
  171. if (gesture.state == UIGestureRecognizerStateChanged) {
  172. if (currentPoint.y <= navigationBarH) {
  173. currentPoint.y = 0;
  174. tableViewY = 0;
  175. }
  176. else if (currentPoint.y >= [self getMaxH]) {
  177. currentPoint.y = [self getMaxH];
  178. tableViewY = navigationBarH;
  179. }
  180. else {
  181. if (currentPoint.y - navigationBarH < navigationBarH) tableViewY = currentPoint.y - navigationBarH;
  182. else tableViewY = navigationBarH;
  183. }
  184. }
  185. [UIView animateWithDuration:ANIMATION_GESTURE delay:0.0 options:UIViewAnimationOptionTransitionCrossDissolve
  186. animations:^ {
  187. _mainView.frame = CGRectMake(0, 0, navigationBarW, currentPoint.y);
  188. //_tableView.frame = CGRectMake(0, tableViewY, navigationBarW, _mainView.frame.size.height - tableViewY - BORDER_TOUCH_UPDOWN - TOOLBAR_TRANSFER_H - TOOLBAR_ADD_BORDER);
  189. _tableView.frame = CGRectMake(0, currentPoint.y - heightScreen + navigationBarH, navigationBarW, heightTableView);
  190. _noRecord.frame = CGRectMake(0, currentPoint.y - centerMaxH - TOOLBAR_TRANSFER_H, navigationBarW, SIZE_FONT_NORECORD+10);
  191. //_toolbarTask.frame = CGRectMake(0, currentPoint.y - BORDER_TOUCH_UPDOWN - TOOLBAR_TRANSFER_H - TOOLBAR_ADD_BORDER/2, _mainView.frame.size.width, TOOLBAR_TRANSFER_H);
  192. _imageDrag.frame = CGRectMake(0, currentPoint.y - BORDER_TOUCH_UPDOWN, navigationBarW, BORDER_TOUCH_UPDOWN);
  193. _endLine.frame = CGRectMake(0, currentPoint.y - BORDER_TOUCH_UPDOWN, navigationBarW, 1);
  194. } completion:^ (BOOL completed) {
  195. if (_mainView.frame.size.height == [self getMaxH]) {
  196. [self setIsOpen:YES];
  197. } else {
  198. [self setIsOpen:NO];
  199. }
  200. // BUG Scroll to top of UITableView by tapping status bar
  201. if (_mainView.frame.size.height == 0)
  202. _mainView.hidden = YES;
  203. }
  204. ];
  205. }
  206. /* iOS Developer Library : The navigation controller installs this gesture recognizer on its view and uses it to pop the topmost view controller off the navigation stack. You can use this property to retrieve the gesture recognizer and tie it to the behavior of other gesture recognizers in your user interface. When tying your gesture recognizers together, make sure they recognize their gestures simultaneously to ensure that your gesture recognizers are given a chance to handle the event. */
  207. - (void)handlePopGesture:(UIGestureRecognizer *)gesture
  208. {
  209. if (gesture.state == UIGestureRecognizerStateBegan) {
  210. _isPopGesture = YES;
  211. }
  212. if (gesture.state == UIGestureRecognizerStateEnded) {
  213. _isPopGesture = NO;
  214. }
  215. }
  216. // rotazione
  217. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  218. {
  219. if (_isOpen) {
  220. [self setControlCenterHidden:YES];
  221. [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  222. [self openControlCenterToSize:size];
  223. [self setControlCenterHidden: self.navigationBarHidden];
  224. }];
  225. //[self closeControlCenter];
  226. }
  227. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  228. }
  229. - (void)setControlCenterHidden:(BOOL)hidden
  230. {
  231. _mainView.hidden = hidden;
  232. }
  233. - (float)getMaxH
  234. {
  235. //return ([UIScreen mainScreen].bounds.size.height / 4) * 3;
  236. return [UIScreen mainScreen].bounds.size.height - self.tabBarController.tabBar.frame.size.height;
  237. }
  238. - (void)openControlCenterToSize:(CGSize)size
  239. {
  240. CGFloat navigationBarH = self.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
  241. _mainView.frame = CGRectMake(0, 0, size.width, [self getMaxH]);
  242. _tableView.frame = CGRectMake(0, navigationBarH, _mainView.frame.size.width, _mainView.frame.size.height - navigationBarH - BORDER_TOUCH_UPDOWN - TOOLBAR_TRANSFER_H - TOOLBAR_ADD_BORDER);
  243. _noRecord.frame = CGRectMake(0, _mainView.frame.size.height / 2 - TOOLBAR_TRANSFER_H, _mainView.frame.size.width, SIZE_FONT_NORECORD+10);
  244. //_toolbarTask.frame = CGRectMake(0, _mainView.frame.size.height - BORDER_TOUCH_UPDOWN - TOOLBAR_TRANSFER_H - TOOLBAR_ADD_BORDER/2, _mainView.frame.size.width, TOOLBAR_TRANSFER_H);
  245. _imageDrag.frame = CGRectMake(0, _mainView.frame.size.height - BORDER_TOUCH_UPDOWN, _mainView.frame.size.width, BORDER_TOUCH_UPDOWN);
  246. _endLine.frame = CGRectMake(0, _mainView.frame.size.height - BORDER_TOUCH_UPDOWN, _mainView.frame.size.width, 1);
  247. panNavigationBar.enabled = NO;
  248. [self setIsOpen:YES];
  249. }
  250. /*
  251. - (void)closeControlCenterToSize:(CGSize)size
  252. {
  253. _mainView.frame = CGRectMake(0, 0, size.width, 0);
  254. _tableView.frame = CGRectMake(0, 0, _mainView.frame.size.width, 0);
  255. _noRecord.frame = CGRectMake(0, 0, _mainView.frame.size.width, 0);
  256. _imageDrag.frame = CGRectMake(0, 0, _mainView.frame.size.width, 0);
  257. _endLine.frame = CGRectMake(0, 0, _mainView.frame.size.width, 0);
  258. panNavigationBar.enabled = YES;
  259. }
  260. */
  261. - (void)setIsOpen:(BOOL)setOpen
  262. {
  263. if (setOpen) {
  264. if (!_isOpen) {
  265. _isOpen = YES;
  266. [self reloadDatasource];
  267. }
  268. } else {
  269. _isOpen = NO;
  270. }
  271. }
  272. #pragma --------------------------------------------------------------------------------------------
  273. #pragma mark - ===== Single Finger Tap =====
  274. #pragma --------------------------------------------------------------------------------------------
  275. - (void)enableSingleFingerTap:(SEL)selector target:(id)target
  276. {
  277. _singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:target action:selector];
  278. [self.navigationBar addGestureRecognizer:_singleFingerTap];
  279. }
  280. - (void)disableSingleFingerTap
  281. {
  282. [self.navigationBar removeGestureRecognizer:_singleFingerTap];
  283. }
  284. #pragma --------------------------------------------------------------------------------------------
  285. #pragma mark - ===== Progress & Task Button =====
  286. #pragma --------------------------------------------------------------------------------------------
  287. - (void)progressTask:(NSString *)fileID serverUrl:(NSString *)serverUrl cryptated:(BOOL)cryptated progress:(float)progress;
  288. {
  289. // Chech
  290. if (!fileID)
  291. return;
  292. [app.listProgressMetadata setObject:[NSNumber numberWithFloat:progress] forKey:fileID];
  293. NSIndexPath *indexPath = [_sectionDataSource.fileIDIndexPath objectForKey:fileID];
  294. if (indexPath && indexPath.row == 0) {
  295. CCControlCenterCell *cell = (CCControlCenterCell *)[_tableView cellForRowAtIndexPath:indexPath];
  296. if (cryptated) cell.progressView.progressTintColor = COLOR_ENCRYPTED;
  297. else cell.progressView.progressTintColor = COLOR_CLEAR;
  298. cell.progressView.hidden = NO;
  299. [cell.progressView setProgress:progress];
  300. } else {
  301. [self reloadDatasource];
  302. }
  303. }
  304. - (void)reloadTaskButton:(id)sender withEvent:(UIEvent *)event
  305. {
  306. if (app.activeMain == nil)
  307. return;
  308. UITouch * touch = [[event allTouches] anyObject];
  309. CGPoint location = [touch locationInView:_tableView];
  310. NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location];
  311. if (indexPath) {
  312. NSString *fileID = [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
  313. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  314. if (metadata)
  315. [app.activeMain reloadTaskButton:metadata];
  316. }
  317. }
  318. - (void)reloadAllTask
  319. {
  320. if (app.activeMain == nil)
  321. return;
  322. for (NSString *key in _sectionDataSource.allRecordsDataSource.allKeys) {
  323. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:key];
  324. if ([metadata.session containsString:@"download"] && (metadata.sessionTaskIdentifierPlist != k_taskIdentifierDone))
  325. continue;
  326. if ([metadata.session containsString:@"upload"] && (metadata.sessionTaskIdentifier != k_taskIdentifierStop))
  327. continue;
  328. [app.activeMain reloadTaskButton:metadata];
  329. }
  330. }
  331. - (void)cancelTaskButton:(id)sender withEvent:(UIEvent *)event
  332. {
  333. if (app.activeMain == nil)
  334. return;
  335. UITouch * touch = [[event allTouches] anyObject];
  336. CGPoint location = [touch locationInView:_tableView];
  337. NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location];
  338. if (indexPath) {
  339. NSString *fileID = [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
  340. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  341. if (metadata)
  342. [app.activeMain cancelTaskButton:metadata reloadTable:YES];
  343. }
  344. }
  345. - (void)cancelAllTask
  346. {
  347. if (app.activeMain == nil)
  348. return;
  349. BOOL lastAndRefresh = NO;
  350. for (NSString *key in _sectionDataSource.allRecordsDataSource.allKeys) {
  351. if ([key isEqualToString:[_sectionDataSource.allRecordsDataSource.allKeys lastObject]])
  352. lastAndRefresh = YES;
  353. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:key];
  354. if ([metadata.session containsString:@"upload"] && metadata.cryptated && ((metadata.sessionTaskIdentifier == k_taskIdentifierDone && metadata.sessionTaskIdentifierPlist >= 0) || (metadata.sessionTaskIdentifier >= 0 && metadata.sessionTaskIdentifierPlist == k_taskIdentifierDone)))
  355. continue;
  356. [app.activeMain cancelTaskButton:metadata reloadTable:lastAndRefresh];
  357. }
  358. }
  359. - (void)stopTaskButton:(id)sender withEvent:(UIEvent *)event
  360. {
  361. if (app.activeMain == nil)
  362. return;
  363. UITouch * touch = [[event allTouches] anyObject];
  364. CGPoint location = [touch locationInView:_tableView];
  365. NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location];
  366. if (indexPath) {
  367. NSString *fileID = [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
  368. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  369. if (metadata)
  370. [app.activeMain stopTaskButton:metadata];
  371. }
  372. }
  373. - (void)stopAllTask
  374. {
  375. if (app.activeMain == nil)
  376. return;
  377. for (NSString *key in _sectionDataSource.allRecordsDataSource.allKeys) {
  378. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:key];
  379. if ([metadata.session containsString:@"download"]) {
  380. [app.activeMain cancelTaskButton:metadata reloadTable:YES];
  381. continue;
  382. }
  383. if ([metadata.session containsString:@"upload"] && metadata.cryptated && ((metadata.sessionTaskIdentifier == k_taskIdentifierDone && metadata.sessionTaskIdentifierPlist >= 0) || (metadata.sessionTaskIdentifier >= 0 && metadata.sessionTaskIdentifierPlist == k_taskIdentifierDone)))
  384. continue;
  385. [app.activeMain stopTaskButton:metadata];
  386. }
  387. }
  388. #pragma --------------------------------------------------------------------------------------------
  389. #pragma mark - ==== Datasource ====
  390. #pragma --------------------------------------------------------------------------------------------
  391. - (void)reloadDatasource
  392. {
  393. if (app.activeAccount == nil || app.activeUrl == nil)
  394. return;
  395. if (_isOpen) {
  396. NSArray *recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND ((session CONTAINS 'upload') OR (session CONTAINS 'download' AND (sessionSelector != 'loadPlist')))", app.activeAccount] fieldOrder:@"sessionTaskIdentifier" ascending:YES];
  397. _sectionDataSource = [CCSection creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:app.listProgressMetadata groupByField:@"session" replaceDateToExifDate:NO activeAccount:app.activeAccount];
  398. if ([_sectionDataSource.allRecordsDataSource count] == 0) _noRecord.hidden = NO;
  399. else _noRecord.hidden = YES;
  400. }
  401. [_tableView reloadData];
  402. [app updateApplicationIconBadgeNumber];
  403. }
  404. #pragma --------------------------------------------------------------------------------------------
  405. #pragma mark - ==== Table ====
  406. #pragma --------------------------------------------------------------------------------------------
  407. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  408. {
  409. return 50;
  410. }
  411. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  412. {
  413. return [[_sectionDataSource.sectionArrayRow allKeys] count];
  414. }
  415. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  416. {
  417. return [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:section]] count];
  418. }
  419. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  420. {
  421. return 13.0f;
  422. }
  423. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  424. {
  425. UIVisualEffectView *visualEffectView;
  426. NSString *titleSection, *numberTitle;
  427. NSInteger typeOfSession = 0;
  428. if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSString class]]) titleSection = [_sectionDataSource.sections objectAtIndex:section];
  429. if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSDate class]]) titleSection = [CCUtility getTitleSectionDate:[_sectionDataSource.sections objectAtIndex:section]];
  430. NSArray *metadatas = [_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:section]];
  431. NSUInteger rowsCount = [metadatas count];
  432. visualEffectView = [[UIVisualEffectView alloc] init];
  433. visualEffectView.backgroundColor = [UIColor clearColor];
  434. // title section
  435. if ([titleSection isEqualToString:@"_none_"]) {
  436. titleSection = @"";
  437. } else if ([titleSection containsString:@"download"] && ![titleSection containsString:@"wwan"]) {
  438. typeOfSession = download;
  439. titleSection = NSLocalizedString(@"_title_section_download_",nil);
  440. } else if ([titleSection containsString:@"download"] && [titleSection containsString:@"wwan"]) {
  441. typeOfSession = downloadwwan;
  442. titleSection = [NSLocalizedString(@"_title_section_download_",nil) stringByAppendingString:@" Wi-Fi"];
  443. } else if ([titleSection containsString:@"upload"] && ![titleSection containsString:@"wwan"]) {
  444. typeOfSession = upload;
  445. titleSection = NSLocalizedString(@"_title_section_upload_",nil);
  446. } else if ([titleSection containsString:@"upload"] && [titleSection containsString:@"wwan"]) {
  447. typeOfSession = uploadwwan;
  448. titleSection = [NSLocalizedString(@"_title_section_upload_",nil) stringByAppendingString:@" Wi-Fi"];
  449. } else {
  450. titleSection = NSLocalizedString(titleSection,nil);
  451. }
  452. // title label on left
  453. UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(8, 3, 0, 13)];
  454. titleLabel.textColor = COLOR_GRAY;
  455. titleLabel.font = [UIFont systemFontOfSize:9];
  456. titleLabel.textAlignment = NSTextAlignmentLeft;
  457. titleLabel.text = titleSection;
  458. titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  459. [visualEffectView addSubview:titleLabel];
  460. // element (s) on right
  461. UILabel *elementLabel=[[UILabel alloc]initWithFrame:CGRectMake(-8, 3, 0, 13)];
  462. elementLabel.textColor = COLOR_GRAY;
  463. elementLabel.font = [UIFont systemFontOfSize:9];
  464. elementLabel.textAlignment = NSTextAlignmentRight;
  465. elementLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  466. if ((typeOfSession == download && app.queueNunDownload > rowsCount) || (typeOfSession == downloadwwan && app.queueNumDownloadWWan > rowsCount) ||
  467. (typeOfSession == upload && app.queueNumUpload > rowsCount) || (typeOfSession == uploadwwan && app.queueNumUploadWWan > rowsCount)) {
  468. numberTitle = [NSString stringWithFormat:@"%lu+", (unsigned long)rowsCount];
  469. } else {
  470. numberTitle = [NSString stringWithFormat:@"%lu", (unsigned long)rowsCount];
  471. }
  472. if (rowsCount > 1)
  473. elementLabel.text = [NSString stringWithFormat:@"%@ %@", numberTitle, NSLocalizedString(@"_elements_",nil)];
  474. else
  475. elementLabel.text = [NSString stringWithFormat:@"%@ %@", numberTitle, NSLocalizedString(@"_element_",nil)];
  476. // view
  477. [visualEffectView addSubview:elementLabel];
  478. return visualEffectView;
  479. }
  480. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  481. {
  482. NSString *titleSection;
  483. NSString *element_s;
  484. if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSString class]]) titleSection = [_sectionDataSource.sections objectAtIndex:section];
  485. // Prepare view for title in footer
  486. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  487. UILabel *titleFooterLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
  488. titleFooterLabel.textColor = COLOR_GRAY;
  489. titleFooterLabel.font = [UIFont systemFontOfSize:12];
  490. titleFooterLabel.textAlignment = NSTextAlignmentCenter;
  491. // Footer Download
  492. if ([titleSection containsString:@"download"] && ![titleSection containsString:@"wwan"] && titleSection != nil) {
  493. // element or elements ?
  494. if (app.queueNunDownload > 1) element_s = NSLocalizedString(@"_elements_",nil);
  495. else element_s = NSLocalizedString(@"_element_",nil);
  496. // Num record to upload
  497. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_download_", nil), app.queueNunDownload, element_s]];
  498. titleFooterLabel.attributedText = stringFooter;
  499. [view addSubview:titleFooterLabel];
  500. return view;
  501. }
  502. // Footer Download WWAN
  503. if ([titleSection containsString:@"download"] && [titleSection containsString:@"wwan"] && titleSection != nil) {
  504. // element or elements ?
  505. if (app.queueNumDownloadWWan > 1) element_s = NSLocalizedString(@"_elements_",nil);
  506. else element_s = NSLocalizedString(@"_element_",nil);
  507. // Add the symbol WiFi and Num record
  508. NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
  509. attachment.image = [UIImage imageNamed:image_WiFiSmall];
  510. NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
  511. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_download_wwan_", nil), app.queueNumDownloadWWan, element_s]];
  512. [stringFooter insertAttributedString:attachmentString atIndex:0];
  513. titleFooterLabel.attributedText = stringFooter;
  514. [view addSubview:titleFooterLabel];
  515. return view;
  516. }
  517. // Footer Upload
  518. if ([titleSection containsString:@"upload"] && ![titleSection containsString:@"wwan"] && titleSection != nil) {
  519. // element or elements ?
  520. if (app.queueNumUpload > 1) element_s = NSLocalizedString(@"_elements_",nil);
  521. else element_s = NSLocalizedString(@"_element_",nil);
  522. // Num record to upload
  523. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_upload_", nil), app.queueNumUpload, element_s]];
  524. titleFooterLabel.attributedText = stringFooter;
  525. [view addSubview:titleFooterLabel];
  526. return view;
  527. }
  528. // Footer Upload WWAN
  529. if ([titleSection containsString:@"upload"] && [titleSection containsString:@"wwan"] && titleSection != nil) {
  530. // element or elements ?
  531. if (app.queueNumUploadWWan > 1) element_s = NSLocalizedString(@"_elements_",nil);
  532. else element_s = NSLocalizedString(@"_element_",nil);
  533. // Add the symbol WiFi and Num record
  534. NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
  535. attachment.image = [UIImage imageNamed:image_WiFiSmall];
  536. NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
  537. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_upload_wwan_", nil), app.queueNumUploadWWan,element_s]];
  538. [stringFooter insertAttributedString:attachmentString atIndex:0];
  539. titleFooterLabel.attributedText = stringFooter;
  540. [view addSubview:titleFooterLabel];
  541. return view;
  542. }
  543. return nil;
  544. }
  545. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  546. {
  547. //NSString *titleSection;
  548. //if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSString class]])
  549. // titleSection = [_sectionDataSource.sections objectAtIndex:section];
  550. //if ([titleSection rangeOfString:@"upload"].location != NSNotFound && [titleSection rangeOfString:@"wwan"].location != NSNotFound && titleSection != nil) return 18.0f;
  551. //else return 0.0f;
  552. return 18.0f;
  553. }
  554. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
  555. {
  556. return [_sectionDataSource.sections indexOfObject:title];
  557. }
  558. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  559. {
  560. NSString *dataFile;
  561. NSString *lunghezzaFile;
  562. NSString *fileID = [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
  563. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  564. CCControlCenterCell *cell = (CCControlCenterCell *)[tableView dequeueReusableCellWithIdentifier:@"CCControlCenterCell" forIndexPath:indexPath];
  565. cell.backgroundColor = [UIColor clearColor];
  566. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  567. // ----------------------------------------------------------------------------------------------------------
  568. // DEFAULT
  569. // ----------------------------------------------------------------------------------------------------------
  570. cell.fileImageView.image = nil;
  571. cell.statusImageView.image = nil;
  572. cell.labelTitle.enabled = YES;
  573. cell.labelTitle.text = @"";
  574. cell.labelInfoFile.enabled = YES;
  575. cell.labelInfoFile.text = @"";
  576. cell.progressView.progress = 0.0;
  577. cell.progressView.hidden = YES;
  578. cell.cancelTaskButton.hidden = YES;
  579. cell.reloadTaskButton.hidden = YES;
  580. cell.stopTaskButton.hidden = YES;
  581. // colori e font
  582. if (metadata.cryptated) {
  583. cell.labelTitle.textColor = COLOR_ENCRYPTED;
  584. //nameLabel.font = RalewayLight(13.0f);
  585. cell.labelInfoFile.textColor = [UIColor blackColor];
  586. //detailLabel.font = RalewayLight(9.0f);
  587. } else {
  588. cell.labelTitle.textColor = COLOR_CLEAR;
  589. //nameLabel.font = RalewayLight(13.0f);
  590. cell.labelInfoFile.textColor = [UIColor blackColor];
  591. //detailLabel.font = RalewayLight(9.0f);
  592. }
  593. // ----------------------------------------------------------------------------------------------------------
  594. // File Name & Folder
  595. // ----------------------------------------------------------------------------------------------------------
  596. // nome del file
  597. cell.labelTitle.text = metadata.fileNamePrint;
  598. // è una directory
  599. if (metadata.directory) {
  600. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  601. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  602. lunghezzaFile = @" ";
  603. } else {
  604. // è un file
  605. dataFile = [CCUtility dateDiff:metadata.date];
  606. lunghezzaFile = [CCUtility transformedSize:metadata.size];
  607. // Plist ancora da scaricare
  608. if (metadata.cryptated && [metadata.title length] == 0) {
  609. dataFile = @" ";
  610. lunghezzaFile = @" ";
  611. }
  612. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  613. [dateFormatter setDateStyle:NSDateFormatterShortStyle];
  614. [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
  615. cell.accessoryType = UITableViewCellAccessoryNone;
  616. }
  617. // ----------------------------------------------------------------------------------------------------------
  618. // File Image View
  619. // ----------------------------------------------------------------------------------------------------------
  620. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  621. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]]) {
  622. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  623. } else {
  624. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  625. }
  626. // ----------------------------------------------------------------------------------------------------------
  627. // Image Status cyptated & Lock Passcode
  628. // ----------------------------------------------------------------------------------------------------------
  629. // File Cyptated
  630. if (metadata.cryptated && metadata.directory == NO && [metadata.type isEqualToString: k_metadataType_model] == NO) {
  631. cell.statusImageView.image = [UIImage imageNamed:image_lock];
  632. }
  633. // ----------------------------------------------------------------------------------------------------------
  634. // downloadFile
  635. // ----------------------------------------------------------------------------------------------------------
  636. if ([metadata.session length] > 0 && [metadata.session rangeOfString:@"download"].location != NSNotFound) {
  637. if (metadata.cryptated) cell.statusImageView.image = [UIImage imageNamed:image_statusdownloadcrypto];
  638. else cell.statusImageView.image = [UIImage imageNamed:image_statusdownload];
  639. // Fai comparire il RELOAD e lo STOP solo se non è un Task Plist
  640. if (metadata.sessionTaskIdentifierPlist == k_taskIdentifierDone) {
  641. if (metadata.cryptated)[cell.cancelTaskButton setBackgroundImage:[UIImage imageNamed:image_stoptaskcrypto] forState:UIControlStateNormal];
  642. else [cell.cancelTaskButton setBackgroundImage:[UIImage imageNamed:image_stoptask] forState:UIControlStateNormal];
  643. cell.cancelTaskButton.hidden = NO;
  644. if (metadata.cryptated)[cell.reloadTaskButton setBackgroundImage:[UIImage imageNamed:image_reloadtaskcrypto] forState:UIControlStateNormal];
  645. else [cell.reloadTaskButton setBackgroundImage:[UIImage imageNamed:image_reloadtask] forState:UIControlStateNormal];
  646. cell.reloadTaskButton.hidden = NO;
  647. }
  648. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", lunghezzaFile];
  649. float progress = [[app.listProgressMetadata objectForKey:metadata.fileID] floatValue];
  650. if (progress > 0) {
  651. if (metadata.cryptated) cell.progressView.progressTintColor = COLOR_ENCRYPTED;
  652. else cell.progressView.progressTintColor = COLOR_CLEAR;
  653. cell.progressView.progress = progress;
  654. cell.progressView.hidden = NO;
  655. }
  656. // ----------------------------------------------------------------------------------------------------------
  657. // downloadFile Error
  658. // ----------------------------------------------------------------------------------------------------------
  659. if (metadata.sessionTaskIdentifier == k_taskIdentifierError || metadata.sessionTaskIdentifierPlist == k_taskIdentifierError) {
  660. cell.statusImageView.image = [UIImage imageNamed:image_statuserror];
  661. if ([metadata.sessionError length] == 0)
  662. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"_error_",nil), NSLocalizedString(@"_file_not_downloaded_",nil)];
  663. else
  664. cell.labelInfoFile.text = [CCError manageErrorKCF:[metadata.sessionError integerValue] withNumberError:NO];
  665. }
  666. }
  667. // ----------------------------------------------------------------------------------------------------------
  668. // uploadFile
  669. // ----------------------------------------------------------------------------------------------------------
  670. if ([metadata.session length] > 0 && [metadata.session rangeOfString:@"upload"].location != NSNotFound) {
  671. if (metadata.cryptated) cell.statusImageView.image = [UIImage imageNamed:image_statusuploadcrypto];
  672. else cell.statusImageView.image = [UIImage imageNamed:image_statusupload];
  673. if (metadata.cryptated)[cell.cancelTaskButton setBackgroundImage:[UIImage imageNamed:image_removetaskcrypto] forState:UIControlStateNormal];
  674. else [cell.cancelTaskButton setBackgroundImage:[UIImage imageNamed:image_removetask] forState:UIControlStateNormal];
  675. cell.cancelTaskButton.hidden = NO;
  676. if (metadata.sessionTaskIdentifier == k_taskIdentifierStop) {
  677. if (metadata.cryptated)[cell.reloadTaskButton setBackgroundImage:[UIImage imageNamed:image_reloadtaskcrypto] forState:UIControlStateNormal];
  678. else [cell.reloadTaskButton setBackgroundImage:[UIImage imageNamed:image_reloadtask] forState:UIControlStateNormal];
  679. if (metadata.cryptated) cell.statusImageView.image = [UIImage imageNamed:image_statusstopcrypto];
  680. else cell.statusImageView.image = [UIImage imageNamed:image_statusstop];
  681. cell.reloadTaskButton.hidden = NO;
  682. cell.stopTaskButton.hidden = YES;
  683. } else {
  684. if (metadata.cryptated)[cell.stopTaskButton setBackgroundImage:[UIImage imageNamed:image_stoptaskcrypto] forState:UIControlStateNormal];
  685. else [cell.stopTaskButton setBackgroundImage:[UIImage imageNamed:image_stoptask] forState:UIControlStateNormal];
  686. cell.stopTaskButton.hidden = NO;
  687. cell.reloadTaskButton.hidden = YES;
  688. }
  689. // se non c'è una preview in bianconero metti l'immagine di default
  690. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]] == NO)
  691. cell.fileImageView.image = [UIImage imageNamed:image_uploaddisable];
  692. cell.labelTitle.enabled = NO;
  693. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", lunghezzaFile];
  694. float progress = [[app.listProgressMetadata objectForKey:metadata.fileID] floatValue];
  695. if (progress > 0) {
  696. if (metadata.cryptated) cell.progressView.progressTintColor = COLOR_ENCRYPTED;
  697. else cell.progressView.progressTintColor = COLOR_CLEAR;
  698. cell.progressView.progress = progress;
  699. cell.progressView.hidden = NO;
  700. }
  701. // ----------------------------------------------------------------------------------------------------------
  702. // uploadFileError
  703. // ----------------------------------------------------------------------------------------------------------
  704. if (metadata.sessionTaskIdentifier == k_taskIdentifierError || metadata.sessionTaskIdentifierPlist == k_taskIdentifierError) {
  705. cell.labelTitle.enabled = NO;
  706. cell.statusImageView.image = [UIImage imageNamed:image_statuserror];
  707. if ([metadata.sessionError length] == 0)
  708. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"_error_",nil), NSLocalizedString(@"_file_not_uploaded_",nil)];
  709. else
  710. cell.labelInfoFile.text = [CCError manageErrorKCF:[metadata.sessionError integerValue] withNumberError:NO];
  711. }
  712. }
  713. [cell.reloadTaskButton addTarget:self action:@selector(reloadTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  714. [cell.cancelTaskButton addTarget:self action:@selector(cancelTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  715. [cell.stopTaskButton addTarget:self action:@selector(stopTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  716. return cell;
  717. }
  718. @end