CCControlCenter.m 16 KB

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