Aucune description

FLControllerHelper.m 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // FLPushViewControllerHelper.m
  3. // FirstLink
  4. //
  5. // Created by unicode on 14-9-26.
  6. // Copyright (c) 2014年 FirstLink. All rights reserved.
  7. //
  8. #import "FLControllerHelper.h"
  9. #import "FirstLinkAppDelegate.h"
  10. #import "LoginViewController.h"
  11. #import "FKPersonOrderController.h"
  12. #import "FKOrderSegmentController.h"
  13. #import "FKMessageManager.h"
  14. static UIImagePickerController *imagePicker = nil;
  15. //static UITabBarController *globalSaveTabBarController = nil;
  16. #define kLoginAnimDuration 0.3f
  17. @implementation FLControllerHelper
  18. #pragma mark - Pop Login View Controller
  19. + (void)presentLoginViewController{
  20. [self presentLoginControllerAnimated:YES completion:nil];
  21. }
  22. + (void)dismissLoginViewController{
  23. [self dismissLoginControllerAnimated:YES completion:nil];
  24. }
  25. + (void)presentLoginControllerAnimated:(BOOL)animated completion:(completion)completion {
  26. FirstLinkAppDelegate *delegate = (FirstLinkAppDelegate *)[[UIApplication sharedApplication] delegate];
  27. if ([self isShowingLoginNavigationController]) return;
  28. UITabBarController *tabbarController = (UITabBarController*)delegate.window.rootViewController;
  29. LoginViewController *login = [[self currentStoryBoard] instantiateViewControllerWithIdentifier:@"LoginViewController"];
  30. UINavigationController *addNav = [[UINavigationController alloc]initWithRootViewController:login];
  31. UINavigationController *curNaviController = (UINavigationController*)tabbarController.selectedViewController;
  32. if ([curNaviController isKindOfClass:[UINavigationController class]]) {
  33. FLViewController *currentController = (FLViewController*)curNaviController.topViewController;
  34. [currentController presentViewController:addNav animated:YES completion:completion];
  35. }
  36. }
  37. + (void)dismissLoginControllerAnimated:(BOOL)animated completion:(completion)completion {
  38. [SystemUtil asyncRequestUserAuthority];
  39. [[FKMessageManager sharedInstance] refreshMessageUnreadMessage];
  40. if (![self isShowingLoginNavigationController]) return;
  41. UINavigationController *currentNav = [self currentNavigationController];
  42. if (currentNav.presentingViewController){
  43. [currentNav dismissViewControllerAnimated:animated completion:completion];
  44. }
  45. // 通知购物车需要更新,用户登录或者更换账号
  46. [[NSNotificationCenter defaultCenter] postNotificationName:BASKET_NEED_TO_REFRESH object:nil];
  47. }
  48. #pragma mark - Camera Manager
  49. +(void)launchAppCamera:(UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>*)viewController type:(UIImagePickerControllerSourceType)type {
  50. if (type == UIImagePickerControllerSourceTypeCamera && ![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
  51. UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil
  52. message:@"无法使用相机,请前往设置打开"
  53. delegate:nil
  54. cancelButtonTitle:@"确定"
  55. otherButtonTitles:nil, nil];
  56. [alert show];
  57. alert = nil;
  58. return;
  59. }
  60. if (!imagePicker) {
  61. imagePicker = [[UIImagePickerController alloc] init];
  62. }
  63. imagePicker.allowsEditing = YES;
  64. imagePicker.sourceType = type;
  65. imagePicker.delegate = viewController;
  66. imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
  67. [viewController presentViewController:imagePicker
  68. animated:YES
  69. completion:^{
  70. }];
  71. }
  72. #pragma mark - Helper
  73. + (BOOL)isShowingLoginNavigationController {
  74. // 判断当前是否正在展示登录界面的navigation
  75. UINavigationController *currentNav = [self currentNavigationController];
  76. if (currentNav) {
  77. for (UIViewController *vc in currentNav.viewControllers) {
  78. if ([vc isKindOfClass:[LoginViewController class]]) return YES;
  79. }
  80. }
  81. return NO;
  82. }
  83. + (FLViewController *)currentController {
  84. UINavigationController *currentNav = [self currentNavigationController];
  85. if (currentNav) {
  86. return (FLViewController *)currentNav.topViewController;
  87. }
  88. return nil;
  89. }
  90. + (NSArray<FLViewController *> *)currentControllers {
  91. UINavigationController *currentNav = [self currentNavigationController];
  92. return currentNav.viewControllers;
  93. }
  94. + (UITabBarController*)currentTabBarController {
  95. FirstLinkAppDelegate *delegate = (FirstLinkAppDelegate *)[[UIApplication sharedApplication] delegate];
  96. id controller = delegate.window.rootViewController;
  97. if ([controller isKindOfClass:[UITabBarController class]]) {
  98. return controller;
  99. }
  100. return nil;
  101. }
  102. + (UINavigationController *)currentNavigationController{
  103. UITabBarController *tabBar = [self currentTabBarController];
  104. if (!tabBar) return nil;
  105. UINavigationController *selectNav = tabBar.selectedViewController;
  106. if ([selectNav isKindOfClass:[UINavigationController class]]){
  107. UINavigationController *presentNav = (UINavigationController *)selectNav.topViewController.presentedViewController;
  108. if ([presentNav isKindOfClass:[UINavigationController class]]) {
  109. return presentNav;
  110. }
  111. return selectNav;
  112. }
  113. return nil;
  114. }
  115. + (void)backToMainController
  116. {
  117. UITabBarController *tabbarController = [self currentTabBarController];
  118. if (!tabbarController) return;
  119. NSInteger selectIndex = tabbarController.selectedIndex;
  120. NSArray *viewControllers = tabbarController.viewControllers;
  121. if (viewControllers.count > 0) {
  122. UINavigationController *naviController = viewControllers[selectIndex];
  123. if ([naviController isKindOfClass:[UINavigationController class]]) {
  124. tabbarController.selectedIndex = 0;
  125. [tabbarController.tabBar setHidden:NO];
  126. [naviController popToRootViewControllerAnimated:YES];
  127. }
  128. }
  129. }
  130. + (void)backToOrderListController
  131. {
  132. UITabBarController *tabbarController = [self currentTabBarController];
  133. if (!tabbarController) return;
  134. NSInteger selectIndex = tabbarController.selectedIndex;
  135. NSArray *viewControllers = tabbarController.viewControllers;
  136. if (viewControllers.count > 0) {
  137. UINavigationController *naviController = viewControllers[selectIndex];
  138. if ([naviController isKindOfClass:[UINavigationController class]]) {
  139. NSMutableArray *arrayM = [NSMutableArray arrayWithArray:naviController.viewControllers];
  140. [arrayM removeObjectsInRange:NSMakeRange(1, arrayM.count - 1)];
  141. FKOrderSegmentController *controller = [[FKOrderSegmentController alloc] init];
  142. controller.selectedType = FKPersonOrderClassifyNonpayment;
  143. controller.hidesBottomBarWhenPushed = YES;
  144. [arrayM addObject:controller];
  145. [naviController setViewControllers:arrayM animated:YES];
  146. }
  147. }
  148. }
  149. +(void)selectTabbarControllerAtIndex:(NSInteger)index {
  150. UITabBarController *tabbarController = [FLControllerHelper currentTabBarController];
  151. if (!tabbarController) return;
  152. if (index < tabbarController.viewControllers.count) {
  153. tabbarController.selectedIndex = index;
  154. }
  155. }
  156. + (UIStoryboard*)currentStoryBoard {
  157. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"]
  158. bundle:[NSBundle mainBundle]];
  159. return storyboard;
  160. }
  161. @end