CCControlCenter.m 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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_CRYPTOCLOUD;
  297. else cell.progressView.progressTintColor = COLOR_TEXT_ANTHRACITE;
  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. // test
  394. if (app.activeAccount.length == 0)
  395. return;
  396. if (_isOpen) {
  397. NSArray *recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND ((session CONTAINS 'upload') OR (session CONTAINS 'download' AND (sessionSelector != 'loadPlist')))", app.activeAccount] fieldOrder:@"sessionTaskIdentifier" ascending:YES];
  398. _sectionDataSource = [CCSection creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:app.listProgressMetadata groupByField:@"session" replaceDateToExifDate:NO activeAccount:app.activeAccount];
  399. if ([_sectionDataSource.allRecordsDataSource count] == 0) _noRecord.hidden = NO;
  400. else _noRecord.hidden = YES;
  401. }
  402. [_tableView reloadData];
  403. [app updateApplicationIconBadgeNumber];
  404. }
  405. #pragma --------------------------------------------------------------------------------------------
  406. #pragma mark - ==== Table ====
  407. #pragma --------------------------------------------------------------------------------------------
  408. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  409. {
  410. return 50;
  411. }
  412. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  413. {
  414. return [[_sectionDataSource.sectionArrayRow allKeys] count];
  415. }
  416. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  417. {
  418. return [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:section]] count];
  419. }
  420. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  421. {
  422. return 13.0f;
  423. }
  424. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  425. {
  426. UIVisualEffectView *visualEffectView;
  427. NSString *titleSection, *numberTitle;
  428. NSInteger typeOfSession = 0;
  429. if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSString class]]) titleSection = [_sectionDataSource.sections objectAtIndex:section];
  430. if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSDate class]]) titleSection = [CCUtility getTitleSectionDate:[_sectionDataSource.sections objectAtIndex:section]];
  431. NSArray *metadatas = [_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:section]];
  432. NSUInteger rowsCount = [metadatas count];
  433. visualEffectView = [[UIVisualEffectView alloc] init];
  434. visualEffectView.backgroundColor = [UIColor clearColor];
  435. // title section
  436. if ([titleSection isEqualToString:@"_none_"]) {
  437. titleSection = @"";
  438. } else if ([titleSection containsString:@"download"] && ![titleSection containsString:@"wwan"]) {
  439. typeOfSession = download;
  440. titleSection = NSLocalizedString(@"_title_section_download_",nil);
  441. } else if ([titleSection containsString:@"download"] && [titleSection containsString:@"wwan"]) {
  442. typeOfSession = downloadwwan;
  443. titleSection = [NSLocalizedString(@"_title_section_download_",nil) stringByAppendingString:@" Wi-Fi"];
  444. } else if ([titleSection containsString:@"upload"] && ![titleSection containsString:@"wwan"]) {
  445. typeOfSession = upload;
  446. titleSection = NSLocalizedString(@"_title_section_upload_",nil);
  447. } else if ([titleSection containsString:@"upload"] && [titleSection containsString:@"wwan"]) {
  448. typeOfSession = uploadwwan;
  449. titleSection = [NSLocalizedString(@"_title_section_upload_",nil) stringByAppendingString:@" Wi-Fi"];
  450. } else {
  451. titleSection = NSLocalizedString(titleSection,nil);
  452. }
  453. // title label on left
  454. UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(8, 3, 0, 13)];
  455. titleLabel.textColor = COLOR_TEXT_ANTHRACITE;
  456. titleLabel.font = [UIFont systemFontOfSize:9];
  457. titleLabel.textAlignment = NSTextAlignmentLeft;
  458. titleLabel.text = titleSection;
  459. titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  460. [visualEffectView addSubview:titleLabel];
  461. // element (s) on right
  462. UILabel *elementLabel=[[UILabel alloc]initWithFrame:CGRectMake(-8, 3, 0, 13)];
  463. elementLabel.textColor = COLOR_TEXT_ANTHRACITE;
  464. elementLabel.font = [UIFont systemFontOfSize:9];
  465. elementLabel.textAlignment = NSTextAlignmentRight;
  466. elementLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  467. if ((typeOfSession == download && app.queueNunDownload > rowsCount) || (typeOfSession == downloadwwan && app.queueNumDownloadWWan > rowsCount) ||
  468. (typeOfSession == upload && app.queueNumUpload > rowsCount) || (typeOfSession == uploadwwan && app.queueNumUploadWWan > rowsCount)) {
  469. numberTitle = [NSString stringWithFormat:@"%lu+", (unsigned long)rowsCount];
  470. } else {
  471. numberTitle = [NSString stringWithFormat:@"%lu", (unsigned long)rowsCount];
  472. }
  473. if (rowsCount > 1)
  474. elementLabel.text = [NSString stringWithFormat:@"%@ %@", numberTitle, NSLocalizedString(@"_elements_",nil)];
  475. else
  476. elementLabel.text = [NSString stringWithFormat:@"%@ %@", numberTitle, NSLocalizedString(@"_element_",nil)];
  477. // view
  478. [visualEffectView addSubview:elementLabel];
  479. return visualEffectView;
  480. }
  481. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  482. {
  483. NSString *titleSection;
  484. NSString *element_s;
  485. if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSString class]]) titleSection = [_sectionDataSource.sections objectAtIndex:section];
  486. // Prepare view for title in footer
  487. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  488. UILabel *titleFooterLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
  489. titleFooterLabel.textColor = COLOR_TEXT_ANTHRACITE;
  490. titleFooterLabel.font = [UIFont systemFontOfSize:12];
  491. titleFooterLabel.textAlignment = NSTextAlignmentCenter;
  492. // Footer Download
  493. if ([titleSection containsString:@"download"] && ![titleSection containsString:@"wwan"] && titleSection != nil) {
  494. // element or elements ?
  495. if (app.queueNunDownload > 1) element_s = NSLocalizedString(@"_elements_",nil);
  496. else element_s = NSLocalizedString(@"_element_",nil);
  497. // Num record to upload
  498. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_download_", nil), app.queueNunDownload, element_s]];
  499. titleFooterLabel.attributedText = stringFooter;
  500. [view addSubview:titleFooterLabel];
  501. return view;
  502. }
  503. // Footer Download WWAN
  504. if ([titleSection containsString:@"download"] && [titleSection containsString:@"wwan"] && titleSection != nil) {
  505. // element or elements ?
  506. if (app.queueNumDownloadWWan > 1) element_s = NSLocalizedString(@"_elements_",nil);
  507. else element_s = NSLocalizedString(@"_element_",nil);
  508. // Add the symbol WiFi and Num record
  509. NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
  510. attachment.image = [UIImage imageNamed:image_WiFiSmall];
  511. NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
  512. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_download_wwan_", nil), app.queueNumDownloadWWan, element_s]];
  513. [stringFooter insertAttributedString:attachmentString atIndex:0];
  514. titleFooterLabel.attributedText = stringFooter;
  515. [view addSubview:titleFooterLabel];
  516. return view;
  517. }
  518. // Footer Upload
  519. if ([titleSection containsString:@"upload"] && ![titleSection containsString:@"wwan"] && titleSection != nil) {
  520. // element or elements ?
  521. if (app.queueNumUpload > 1) element_s = NSLocalizedString(@"_elements_",nil);
  522. else element_s = NSLocalizedString(@"_element_",nil);
  523. // Num record to upload
  524. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_upload_", nil), app.queueNumUpload, element_s]];
  525. titleFooterLabel.attributedText = stringFooter;
  526. [view addSubview:titleFooterLabel];
  527. return view;
  528. }
  529. // Footer Upload WWAN
  530. if ([titleSection containsString:@"upload"] && [titleSection containsString:@"wwan"] && titleSection != nil) {
  531. // element or elements ?
  532. if (app.queueNumUploadWWan > 1) element_s = NSLocalizedString(@"_elements_",nil);
  533. else element_s = NSLocalizedString(@"_element_",nil);
  534. // Add the symbol WiFi and Num record
  535. NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
  536. attachment.image = [UIImage imageNamed:image_WiFiSmall];
  537. NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
  538. NSMutableAttributedString *stringFooter= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:NSLocalizedString(@"_tite_footer_upload_wwan_", nil), app.queueNumUploadWWan,element_s]];
  539. [stringFooter insertAttributedString:attachmentString atIndex:0];
  540. titleFooterLabel.attributedText = stringFooter;
  541. [view addSubview:titleFooterLabel];
  542. return view;
  543. }
  544. return nil;
  545. }
  546. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  547. {
  548. //NSString *titleSection;
  549. //if ([[_sectionDataSource.sections objectAtIndex:section] isKindOfClass:[NSString class]])
  550. // titleSection = [_sectionDataSource.sections objectAtIndex:section];
  551. //if ([titleSection rangeOfString:@"upload"].location != NSNotFound && [titleSection rangeOfString:@"wwan"].location != NSNotFound && titleSection != nil) return 18.0f;
  552. //else return 0.0f;
  553. return 18.0f;
  554. }
  555. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
  556. {
  557. return [_sectionDataSource.sections indexOfObject:title];
  558. }
  559. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  560. {
  561. NSString *dataFile;
  562. NSString *lunghezzaFile;
  563. NSString *fileID = [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
  564. CCMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  565. CCControlCenterCell *cell = (CCControlCenterCell *)[tableView dequeueReusableCellWithIdentifier:@"CCControlCenterCell" forIndexPath:indexPath];
  566. cell.backgroundColor = [UIColor clearColor];
  567. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  568. // ----------------------------------------------------------------------------------------------------------
  569. // DEFAULT
  570. // ----------------------------------------------------------------------------------------------------------
  571. cell.fileImageView.image = nil;
  572. cell.statusImageView.image = nil;
  573. cell.labelTitle.enabled = YES;
  574. cell.labelTitle.text = @"";
  575. cell.labelInfoFile.enabled = YES;
  576. cell.labelInfoFile.text = @"";
  577. cell.progressView.progress = 0.0;
  578. cell.progressView.hidden = YES;
  579. cell.cancelTaskButton.hidden = YES;
  580. cell.reloadTaskButton.hidden = YES;
  581. cell.stopTaskButton.hidden = YES;
  582. // colori e font
  583. if (metadata.cryptated) {
  584. cell.labelTitle.textColor = COLOR_CRYPTOCLOUD;
  585. cell.labelInfoFile.textColor = [UIColor blackColor];
  586. } else {
  587. cell.labelTitle.textColor = COLOR_TEXT_ANTHRACITE;
  588. cell.labelInfoFile.textColor = [UIColor blackColor];
  589. }
  590. // ----------------------------------------------------------------------------------------------------------
  591. // File Name & Folder
  592. // ----------------------------------------------------------------------------------------------------------
  593. // nome del file
  594. cell.labelTitle.text = metadata.fileNamePrint;
  595. // è una directory
  596. if (metadata.directory) {
  597. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  598. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  599. lunghezzaFile = @" ";
  600. } else {
  601. // è un file
  602. dataFile = [CCUtility dateDiff:metadata.date];
  603. lunghezzaFile = [CCUtility transformedSize:metadata.size];
  604. // Plist ancora da scaricare
  605. if (metadata.cryptated && [metadata.title length] == 0) {
  606. dataFile = @" ";
  607. lunghezzaFile = @" ";
  608. }
  609. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  610. [dateFormatter setDateStyle:NSDateFormatterShortStyle];
  611. [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
  612. cell.accessoryType = UITableViewCellAccessoryNone;
  613. }
  614. // ----------------------------------------------------------------------------------------------------------
  615. // File Image View
  616. // ----------------------------------------------------------------------------------------------------------
  617. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  618. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]]) {
  619. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  620. } else {
  621. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  622. }
  623. // ----------------------------------------------------------------------------------------------------------
  624. // Image Status cyptated & Lock Passcode
  625. // ----------------------------------------------------------------------------------------------------------
  626. // File Cyptated
  627. if (metadata.cryptated && metadata.directory == NO && [metadata.type isEqualToString: k_metadataType_template] == NO) {
  628. cell.statusImageView.image = [UIImage imageNamed:image_lock];
  629. }
  630. // ----------------------------------------------------------------------------------------------------------
  631. // downloadFile
  632. // ----------------------------------------------------------------------------------------------------------
  633. if ([metadata.session length] > 0 && [metadata.session rangeOfString:@"download"].location != NSNotFound) {
  634. if (metadata.cryptated) cell.statusImageView.image = [UIImage imageNamed:image_statusdownloadcrypto];
  635. else cell.statusImageView.image = [UIImage imageNamed:image_statusdownload];
  636. // Fai comparire il RELOAD e lo STOP solo se non è un Task Plist
  637. if (metadata.sessionTaskIdentifierPlist == k_taskIdentifierDone) {
  638. if (metadata.cryptated)[cell.cancelTaskButton setBackgroundImage:[UIImage imageNamed:image_stoptaskcrypto] forState:UIControlStateNormal];
  639. else [cell.cancelTaskButton setBackgroundImage:[UIImage imageNamed:image_stoptask] forState:UIControlStateNormal];
  640. cell.cancelTaskButton.hidden = NO;
  641. if (metadata.cryptated)[cell.reloadTaskButton setBackgroundImage:[UIImage imageNamed:image_reloadtaskcrypto] forState:UIControlStateNormal];
  642. else [cell.reloadTaskButton setBackgroundImage:[UIImage imageNamed:image_reloadtask] forState:UIControlStateNormal];
  643. cell.reloadTaskButton.hidden = NO;
  644. }
  645. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", lunghezzaFile];
  646. float progress = [[app.listProgressMetadata objectForKey:metadata.fileID] floatValue];
  647. if (progress > 0) {
  648. if (metadata.cryptated) cell.progressView.progressTintColor = COLOR_CRYPTOCLOUD;
  649. else cell.progressView.progressTintColor = COLOR_TEXT_ANTHRACITE;
  650. cell.progressView.progress = progress;
  651. cell.progressView.hidden = NO;
  652. }
  653. // ----------------------------------------------------------------------------------------------------------
  654. // downloadFile Error
  655. // ----------------------------------------------------------------------------------------------------------
  656. if (metadata.sessionTaskIdentifier == k_taskIdentifierError || metadata.sessionTaskIdentifierPlist == k_taskIdentifierError) {
  657. cell.statusImageView.image = [UIImage imageNamed:image_statuserror];
  658. if ([metadata.sessionError length] == 0)
  659. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"_error_",nil), NSLocalizedString(@"_file_not_downloaded_",nil)];
  660. else
  661. cell.labelInfoFile.text = [CCError manageErrorKCF:[metadata.sessionError integerValue] withNumberError:NO];
  662. }
  663. }
  664. // ----------------------------------------------------------------------------------------------------------
  665. // uploadFile
  666. // ----------------------------------------------------------------------------------------------------------
  667. if ([metadata.session length] > 0 && [metadata.session rangeOfString:@"upload"].location != NSNotFound) {
  668. if (metadata.cryptated) cell.statusImageView.image = [UIImage imageNamed:image_statusuploadcrypto];
  669. else cell.statusImageView.image = [UIImage imageNamed:image_statusupload];
  670. if (metadata.cryptated)[cell.cancelTaskButton setBackgroundImage:[UIImage imageNamed:image_removetaskcrypto] forState:UIControlStateNormal];
  671. else [cell.cancelTaskButton setBackgroundImage:[UIImage imageNamed:image_removetask] forState:UIControlStateNormal];
  672. cell.cancelTaskButton.hidden = NO;
  673. if (metadata.sessionTaskIdentifier == k_taskIdentifierStop) {
  674. if (metadata.cryptated)[cell.reloadTaskButton setBackgroundImage:[UIImage imageNamed:image_reloadtaskcrypto] forState:UIControlStateNormal];
  675. else [cell.reloadTaskButton setBackgroundImage:[UIImage imageNamed:image_reloadtask] forState:UIControlStateNormal];
  676. if (metadata.cryptated) cell.statusImageView.image = [UIImage imageNamed:image_statusstopcrypto];
  677. else cell.statusImageView.image = [UIImage imageNamed:image_statusstop];
  678. cell.reloadTaskButton.hidden = NO;
  679. cell.stopTaskButton.hidden = YES;
  680. } else {
  681. if (metadata.cryptated)[cell.stopTaskButton setBackgroundImage:[UIImage imageNamed:image_stoptaskcrypto] forState:UIControlStateNormal];
  682. else [cell.stopTaskButton setBackgroundImage:[UIImage imageNamed:image_stoptask] forState:UIControlStateNormal];
  683. cell.stopTaskButton.hidden = NO;
  684. cell.reloadTaskButton.hidden = YES;
  685. }
  686. // se non c'è una preview in bianconero metti l'immagine di default
  687. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]] == NO)
  688. cell.fileImageView.image = [UIImage imageNamed:image_uploaddisable];
  689. cell.labelTitle.enabled = NO;
  690. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", lunghezzaFile];
  691. float progress = [[app.listProgressMetadata objectForKey:metadata.fileID] floatValue];
  692. if (progress > 0) {
  693. if (metadata.cryptated) cell.progressView.progressTintColor = COLOR_CRYPTOCLOUD;
  694. else cell.progressView.progressTintColor = COLOR_TEXT_ANTHRACITE;
  695. cell.progressView.progress = progress;
  696. cell.progressView.hidden = NO;
  697. }
  698. // ----------------------------------------------------------------------------------------------------------
  699. // uploadFileError
  700. // ----------------------------------------------------------------------------------------------------------
  701. if (metadata.sessionTaskIdentifier == k_taskIdentifierError || metadata.sessionTaskIdentifierPlist == k_taskIdentifierError) {
  702. cell.labelTitle.enabled = NO;
  703. cell.statusImageView.image = [UIImage imageNamed:image_statuserror];
  704. if ([metadata.sessionError length] == 0)
  705. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"_error_",nil), NSLocalizedString(@"_file_not_uploaded_",nil)];
  706. else
  707. cell.labelInfoFile.text = [CCError manageErrorKCF:[metadata.sessionError integerValue] withNumberError:NO];
  708. }
  709. }
  710. [cell.reloadTaskButton addTarget:self action:@selector(reloadTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  711. [cell.cancelTaskButton addTarget:self action:@selector(cancelTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  712. [cell.stopTaskButton addTarget:self action:@selector(stopTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  713. return cell;
  714. }
  715. @end