CCControlCenter.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 "CCControlCenterTransfer.h"
  25. #import "CCControlCenterActivity.h"
  26. #import "AppDelegate.h"
  27. #import "CCMain.h"
  28. #import "CCDetail.h"
  29. #define BORDER_TOUCH_UPDOWN 50.0f
  30. #define TOOLBAR_ADD_BORDER 20.0f
  31. #define SIZE_FONT_NORECORD 18.0f
  32. #define ANIMATION_GESTURE 0.50f
  33. @interface CCControlCenter ()
  34. {
  35. UIVisualEffectView *_mainView;
  36. UIImageView *_imageDrag;
  37. UIView *_endLine;
  38. CGFloat start, stop;
  39. UIPanGestureRecognizer *panNavigationBar, *panImageDrag;
  40. UITapGestureRecognizer *_singleFingerTap;
  41. UIPageControl *pageControl;
  42. }
  43. @end
  44. @implementation CCControlCenter
  45. #pragma --------------------------------------------------------------------------------------------
  46. #pragma mark ===== Init =====
  47. #pragma --------------------------------------------------------------------------------------------
  48. - (id)initWithCoder:(NSCoder *)aDecoder
  49. {
  50. if (self = [super initWithCoder:aDecoder]) {
  51. app.controlCenter = self;
  52. }
  53. return self;
  54. }
  55. - (void)viewDidLoad
  56. {
  57. [super viewDidLoad];
  58. _controlCenterPagesContent = [NSMutableArray new];
  59. UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
  60. _mainView = [[UIVisualEffectView alloc] initWithEffect:effect];
  61. [_mainView setFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, 0)];
  62. _mainView.hidden = YES;
  63. // TEST
  64. //_mainView.backgroundColor = [UIColor yellowColor];
  65. // Create data model
  66. _pageType = @[k_pageControlCenterTransfer, k_pageControlCenterActivity];
  67. // Create page view controller
  68. _pageViewController = [[UIStoryboard storyboardWithName: @"ControlCenter" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ControlCenterPageViewController"];
  69. _pageViewController.dataSource = self;
  70. _pageViewController.delegate = self;
  71. UIViewController *startingViewController;
  72. NSArray *viewControllers;
  73. startingViewController= [self viewControllerAtIndex:0];
  74. viewControllers = @[startingViewController];
  75. [_pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
  76. [_pageViewController.view setFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, 0)];
  77. _pageViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  78. [_mainView addSubview:_pageViewController.view];
  79. _labelMessageNoRecord =[[UILabel alloc]init];
  80. _labelMessageNoRecord.backgroundColor=[UIColor clearColor];
  81. _labelMessageNoRecord.textColor = COLOR_CONTROL_CENTER;
  82. _labelMessageNoRecord.font = [UIFont systemFontOfSize:SIZE_FONT_NORECORD];
  83. _labelMessageNoRecord.textAlignment = NSTextAlignmentCenter;
  84. [_mainView addSubview:_labelMessageNoRecord];
  85. _imageDrag = [[UIImageView alloc] init];
  86. [_imageDrag setFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, 0)];
  87. _imageDrag.image = [UIImage imageNamed:image_drag];
  88. _imageDrag.contentMode = UIViewContentModeCenter;
  89. _imageDrag.userInteractionEnabled = YES;
  90. [_mainView addSubview:_imageDrag];
  91. _endLine = [[UIView alloc] init];
  92. [_endLine setFrame:CGRectMake(0, 0, self.navigationBar.frame.size.width, 0)];
  93. _endLine.backgroundColor = COLOR_CONTROL_CENTER;
  94. [_mainView addSubview:_endLine];
  95. [self.navigationBar.superview insertSubview:_mainView belowSubview:self.navigationBar];
  96. // Pop Gesture Recognizer
  97. [self.interactivePopGestureRecognizer addTarget:self action:@selector(handlePopGesture:)];
  98. panImageDrag = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
  99. panImageDrag.maximumNumberOfTouches = panImageDrag.minimumNumberOfTouches = 1;
  100. [_imageDrag addGestureRecognizer:panImageDrag];
  101. panNavigationBar = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
  102. panNavigationBar.maximumNumberOfTouches = panNavigationBar.minimumNumberOfTouches = 1;
  103. [self.navigationBar addGestureRecognizer:panNavigationBar];
  104. }
  105. /* 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. */
  106. - (void)handlePanGesture:(UIPanGestureRecognizer *)gesture;
  107. {
  108. CGPoint currentPoint = [gesture locationInView:self.view];
  109. CGFloat navigationBarH = self.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
  110. CGFloat navigationBarW = self.navigationBar.frame.size.width;
  111. CGFloat heightScreen = [UIScreen mainScreen].bounds.size.height - self.tabBarController.tabBar.frame.size.height;
  112. CGFloat heightTableView = [UIScreen mainScreen].bounds.size.height - self.tabBarController.tabBar.frame.size.height - self.navigationBar.frame.size.height - BORDER_TOUCH_UPDOWN - TOOLBAR_ADD_BORDER;
  113. float centerMaxH = [self getMaxH] / 2;
  114. float step = [self getMaxH] / 10;
  115. float tableViewY = navigationBarH;
  116. // BUG Scroll to top of UITableView by tapping status bar
  117. _mainView.hidden = NO;
  118. // start
  119. if (gesture.state == UIGestureRecognizerStateBegan) {
  120. start = currentPoint.y;
  121. }
  122. // cancelled (return to 0)
  123. if (gesture.state == UIGestureRecognizerStateCancelled) {
  124. currentPoint.y = 0;
  125. tableViewY = 0;
  126. panNavigationBar.enabled = YES;
  127. }
  128. // end
  129. if (gesture.state == UIGestureRecognizerStateEnded) {
  130. stop = currentPoint.y;
  131. // DOWN
  132. if (start < stop) {
  133. if (stop < (start + step + navigationBarH) && start <= navigationBarH) {
  134. currentPoint.y = 0;
  135. tableViewY = 0;
  136. panNavigationBar.enabled = YES;
  137. } else {
  138. currentPoint.y = [self getMaxH];
  139. tableViewY = navigationBarH;
  140. panNavigationBar.enabled = NO;
  141. }
  142. }
  143. // UP
  144. if (start >= stop && start >= navigationBarH) {
  145. if (stop > (start - step)) {
  146. currentPoint.y = [self getMaxH];
  147. tableViewY = navigationBarH;
  148. panNavigationBar.enabled = NO;
  149. } else {
  150. currentPoint.y = 0;
  151. tableViewY = 0;
  152. panNavigationBar.enabled = YES;
  153. }
  154. }
  155. }
  156. // changed
  157. if (gesture.state == UIGestureRecognizerStateChanged) {
  158. if (currentPoint.y <= navigationBarH) {
  159. currentPoint.y = 0;
  160. tableViewY = 0;
  161. }
  162. else if (currentPoint.y >= [self getMaxH]) {
  163. currentPoint.y = [self getMaxH];
  164. tableViewY = navigationBarH;
  165. }
  166. else {
  167. if (currentPoint.y - navigationBarH < navigationBarH) tableViewY = currentPoint.y - navigationBarH;
  168. else tableViewY = navigationBarH;
  169. }
  170. }
  171. [UIView animateWithDuration:ANIMATION_GESTURE delay:0.0 options:UIViewAnimationOptionTransitionCrossDissolve
  172. animations:^ {
  173. _mainView.frame = CGRectMake(0, 0, navigationBarW, currentPoint.y);
  174. _pageViewController.view.frame = CGRectMake(0, currentPoint.y - heightScreen + navigationBarH, navigationBarW, heightTableView);
  175. _labelMessageNoRecord.frame = CGRectMake(0, currentPoint.y - centerMaxH, navigationBarW, SIZE_FONT_NORECORD+10);
  176. _imageDrag.frame = CGRectMake(0, currentPoint.y - BORDER_TOUCH_UPDOWN, navigationBarW, BORDER_TOUCH_UPDOWN);
  177. _endLine.frame = CGRectMake(0, currentPoint.y - BORDER_TOUCH_UPDOWN, navigationBarW, 1);
  178. } completion:^ (BOOL completed) {
  179. if (_mainView.frame.size.height == [self getMaxH]) {
  180. [self setIsOpen:YES];
  181. } else {
  182. [self setIsOpen:NO];
  183. }
  184. // BUG Scroll to top of UITableView by tapping status bar
  185. if (_mainView.frame.size.height == 0)
  186. _mainView.hidden = YES;
  187. }
  188. ];
  189. }
  190. /* 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. */
  191. - (void)handlePopGesture:(UIGestureRecognizer *)gesture
  192. {
  193. if (gesture.state == UIGestureRecognizerStateBegan) {
  194. _isPopGesture = YES;
  195. }
  196. if (gesture.state == UIGestureRecognizerStateEnded) {
  197. _isPopGesture = NO;
  198. }
  199. }
  200. // rotazione
  201. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  202. {
  203. if (_isOpen) {
  204. [self setControlCenterHidden:YES];
  205. [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  206. [self openControlCenterToSize];
  207. [self setControlCenterHidden: self.navigationBarHidden];
  208. }];
  209. //[self closeControlCenter];
  210. }
  211. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  212. }
  213. - (void)setControlCenterHidden:(BOOL)hidden
  214. {
  215. _mainView.hidden = hidden;
  216. }
  217. - (float)getMaxH
  218. {
  219. //return ([UIScreen mainScreen].bounds.size.height / 4) * 3;
  220. return [UIScreen mainScreen].bounds.size.height - self.tabBarController.tabBar.frame.size.height;
  221. }
  222. - (void)openControlCenterToSize
  223. {
  224. CGFloat navigationBarH = self.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
  225. _mainView.frame = CGRectMake(0, 0, self.navigationBar.frame.size.width, [self getMaxH]);
  226. _pageViewController.view.frame = CGRectMake(0, navigationBarH, _mainView.frame.size.width, _mainView.frame.size.height - self.navigationBar.frame.size.height - BORDER_TOUCH_UPDOWN - TOOLBAR_ADD_BORDER);
  227. _labelMessageNoRecord.frame = CGRectMake(0, _mainView.frame.size.height / 2, _mainView.frame.size.width, SIZE_FONT_NORECORD+10);
  228. _imageDrag.frame = CGRectMake(0, _mainView.frame.size.height - BORDER_TOUCH_UPDOWN, _mainView.frame.size.width, BORDER_TOUCH_UPDOWN);
  229. _endLine.frame = CGRectMake(0, _mainView.frame.size.height - BORDER_TOUCH_UPDOWN, _mainView.frame.size.width, 1);
  230. panNavigationBar.enabled = NO;
  231. [self setIsOpen:YES];
  232. }
  233. /*
  234. - (void)closeControlCenterToSize:(CGSize)size
  235. {
  236. _mainView.frame = CGRectMake(0, 0, size.width, 0);
  237. _tableView.frame = CGRectMake(0, 0, _mainView.frame.size.width, 0);
  238. _noRecord.frame = CGRectMake(0, 0, _mainView.frame.size.width, 0);
  239. _imageDrag.frame = CGRectMake(0, 0, _mainView.frame.size.width, 0);
  240. _endLine.frame = CGRectMake(0, 0, _mainView.frame.size.width, 0);
  241. panNavigationBar.enabled = YES;
  242. }
  243. */
  244. - (void)setIsOpen:(BOOL)setOpen
  245. {
  246. if (setOpen) {
  247. if (!_isOpen) {
  248. _isOpen = YES;
  249. [app.controlCenterTransfer reloadDatasource];
  250. [app.controlCenterActivity reloadDatasource];
  251. }
  252. } else {
  253. _isOpen = NO;
  254. }
  255. [self changeTabBarApplicationFileImage];
  256. }
  257. #pragma --------------------------------------------------------------------------------------------
  258. #pragma mark - ===== Single Finger Tap =====
  259. #pragma --------------------------------------------------------------------------------------------
  260. - (void)enableSingleFingerTap:(SEL)selector target:(id)target
  261. {
  262. _singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:target action:selector];
  263. [self.navigationBar addGestureRecognizer:_singleFingerTap];
  264. }
  265. - (void)disableSingleFingerTap
  266. {
  267. [self.navigationBar removeGestureRecognizer:_singleFingerTap];
  268. }
  269. #pragma --------------------------------------------------------------------------------------------
  270. #pragma mark ===== Page =====
  271. #pragma --------------------------------------------------------------------------------------------
  272. - (UIViewController *)viewControllerAtIndex:(NSUInteger)index
  273. {
  274. if (([self.pageType count] == 0) || (index >= [self.pageType count])) {
  275. return nil;
  276. }
  277. UIViewController *pageContentViewController;
  278. if ([self.controlCenterPagesContent count] >= index+1) {
  279. pageContentViewController = [self.controlCenterPagesContent objectAtIndex:index];
  280. } else {
  281. if (index == 0)
  282. pageContentViewController = [[UIStoryboard storyboardWithName: @"ControlCenter" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ControlCenterTransfer"];
  283. if (index == 1)
  284. pageContentViewController = [[UIStoryboard storyboardWithName: @"ControlCenter" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ControlCenterActivity"];
  285. [self.controlCenterPagesContent addObject:pageContentViewController];
  286. }
  287. ((CCControlCenterTransfer *) pageContentViewController).pageIndex = index;
  288. ((CCControlCenterTransfer *) pageContentViewController).pageType = self.pageType[index];
  289. return pageContentViewController;
  290. }
  291. - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
  292. {
  293. NSUInteger index = ((CCControlCenterTransfer *) viewController).pageIndex;
  294. if ((index == 0) || (index == NSNotFound)) {
  295. return nil;
  296. }
  297. index--;
  298. return [self viewControllerAtIndex:index];
  299. }
  300. - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
  301. {
  302. NSUInteger index = ((CCControlCenterTransfer *) viewController).pageIndex;
  303. if (index == NSNotFound) {
  304. return nil;
  305. }
  306. index++;
  307. if (index == [self.pageType count]) {
  308. return nil;
  309. }
  310. return [self viewControllerAtIndex:index];
  311. }
  312. - (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
  313. {
  314. return [self.pageType count];
  315. }
  316. - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
  317. {
  318. return 0;
  319. }
  320. /*
  321. - (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers
  322. {
  323. }
  324. */
  325. - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed
  326. {
  327. [self changeTabBarApplicationFileImage];
  328. }
  329. - (void)changeTabBarApplicationFileImage
  330. {
  331. // Standard Image
  332. if (!_isOpen) {
  333. self.title = NSLocalizedString(@"_home_", nil);
  334. UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex: k_tabBarApplicationIndexFile];
  335. item.selectedImage = [UIImage imageNamed:image_tabBarFile];
  336. item.image = [UIImage imageNamed:image_tabBarFile];
  337. return;
  338. }
  339. // Change Image
  340. if ([[self getActivePage] isEqualToString:k_pageControlCenterActivity]) {
  341. self.title = NSLocalizedString(@"_activity_", nil);
  342. UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex: k_tabBarApplicationIndexFile];
  343. item.selectedImage = [UIImage imageNamed:image_tabBarActivity];
  344. item.image = [UIImage imageNamed:image_tabBarActivity];
  345. }
  346. if ([[self getActivePage] isEqualToString:k_pageControlCenterTransfer]) {
  347. self.title = NSLocalizedString(@"_transfers_", nil);
  348. UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex: k_tabBarApplicationIndexFile];
  349. item.selectedImage = [UIImage imageNamed:image_tabBarTransfer];
  350. item.image = [UIImage imageNamed:image_tabBarTransfer];
  351. }
  352. }
  353. - (NSString *)getActivePage
  354. {
  355. CCControlCenterTransfer *vc = [self.pageViewController.viewControllers lastObject];
  356. return vc.pageType;
  357. }
  358. @end