NTESMainTabController.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // MainTabController.m
  3. // NIMDemo
  4. //
  5. // Created by chris on 15/2/2.
  6. // Copyright (c) 2015年 Netease. All rights reserved.
  7. //
  8. #import "NTESMainTabController.h"
  9. //#import "NTESAppDelegate.h"
  10. //#import "NTESSessionListViewController.h"
  11. //#import "NTESContactViewController.h"
  12. #import "UIImage+NTESColor.h"
  13. #import "NTESCustomNotificationDB.h"
  14. #import "NTESNotificationCenter.h"
  15. #import "NTESNavigationHandler.h"
  16. #import "NTESNavigationAnimator.h"
  17. #import "NTESBundleSetting.h"
  18. #define TabbarVC @"vc"
  19. #define TabbarTitle @"title"
  20. #define TabbarImage @"image"
  21. #define TabbarSelectedImage @"selectedImage"
  22. #define TabbarItemBadgeValue @"badgeValue"
  23. #define TabBarCount 4
  24. typedef NS_ENUM(NSInteger,NTESMainTabType) {
  25. NTESMainTabTypeMessageList, //聊天
  26. NTESMainTabTypeContact, //通讯录
  27. NTESMainTabTypeChatroomList, //聊天室
  28. NTESMainTabTypeSetting, //设置
  29. };
  30. @interface NTESMainTabController ()<NIMSystemNotificationManagerDelegate,NIMConversationManagerDelegate>
  31. @property (nonatomic,strong) NSArray *navigationHandlers;
  32. @property (nonatomic,strong) NTESNavigationAnimator *animator;
  33. @property (nonatomic,assign) NSInteger sessionUnreadCount;
  34. @property (nonatomic,assign) NSInteger systemUnreadCount;
  35. @property (nonatomic,assign) NSInteger customSystemUnreadCount;
  36. @property (nonatomic,copy) NSDictionary *configs;
  37. @end
  38. @implementation NTESMainTabController
  39. + (instancetype)instance{
  40. // NTESAppDelegate *delegete = (NTESAppDelegate *)[UIApplication sharedApplication].delegate;
  41. // UIViewController *vc = delegete.window.rootViewController;
  42. // if ([vc isKindOfClass:[NTESMainTabController class]]) {
  43. // return (NTESMainTabController *)vc;
  44. // }else{
  45. // return nil;
  46. // }
  47. return nil;
  48. }
  49. - (void)viewDidLoad {
  50. [super viewDidLoad];
  51. [self setUpSubNav];
  52. [[NIMSDK sharedSDK].systemNotificationManager addDelegate:self];
  53. [[NIMSDK sharedSDK].conversationManager addDelegate:self];
  54. extern NSString *NTESCustomNotificationCountChanged;
  55. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onCustomNotifyChanged:) name:NTESCustomNotificationCountChanged object:nil];
  56. }
  57. - (void)viewWillAppear:(BOOL)animated{
  58. [super viewWillAppear:animated];
  59. [self setUpStatusBar];
  60. }
  61. - (void)viewDidAppear:(BOOL)animated
  62. {
  63. [super viewDidAppear:animated];
  64. //会话界面发送拍摄的视频,拍摄结束后点击发送后可能顶部会有红条,导致的界面错位。
  65. self.view.frame = [UIScreen mainScreen].bounds;
  66. }
  67. - (void)dealloc{
  68. [[NIMSDK sharedSDK].systemNotificationManager removeDelegate:self];
  69. [[NIMSDK sharedSDK].conversationManager removeDelegate:self];
  70. [[NSNotificationCenter defaultCenter] removeObserver:self];
  71. }
  72. - (NSArray*)tabbars{
  73. self.sessionUnreadCount = [NIMSDK sharedSDK].conversationManager.allUnreadCount;
  74. self.systemUnreadCount = [NIMSDK sharedSDK].systemNotificationManager.allUnreadCount;
  75. self.customSystemUnreadCount = [[NTESCustomNotificationDB sharedInstance] unreadCount];
  76. NSMutableArray *items = [[NSMutableArray alloc] init];
  77. for (NSInteger tabbar = 0; tabbar < TabBarCount; tabbar++) {
  78. [items addObject:@(tabbar)];
  79. }
  80. return items;
  81. }
  82. - (void)setUpSubNav{
  83. NSMutableArray *handleArray = [[NSMutableArray alloc] init];
  84. NSMutableArray *vcArray = [[NSMutableArray alloc] init];
  85. [self.tabbars enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  86. NSDictionary * item =[self vcInfoForTabType:[obj integerValue]];
  87. NSString *vcName = item[TabbarVC];
  88. NSString *title = item[TabbarTitle];
  89. NSString *imageName = item[TabbarImage];
  90. NSString *imageSelected = item[TabbarSelectedImage];
  91. Class clazz = NSClassFromString(vcName);
  92. UIViewController *vc = [[clazz alloc] initWithNibName:nil bundle:nil];
  93. vc.hidesBottomBarWhenPushed = NO;
  94. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
  95. nav.tabBarItem = [[UITabBarItem alloc] initWithTitle:title
  96. image:[UIImage imageNamed:imageName]
  97. selectedImage:[UIImage imageNamed:imageSelected]];
  98. nav.tabBarItem.tag = idx;
  99. NSInteger badge = [item[TabbarItemBadgeValue] integerValue];
  100. if (badge) {
  101. nav.tabBarItem.badgeValue = [NSString stringWithFormat:@"%zd",badge];
  102. }
  103. NTESNavigationHandler *handler = [[NTESNavigationHandler alloc] initWithNavigationController:nav];
  104. nav.delegate = handler;
  105. [vcArray addObject:nav];
  106. [handleArray addObject:handler];
  107. }];
  108. self.viewControllers = [NSArray arrayWithArray:vcArray];
  109. self.navigationHandlers = [NSArray arrayWithArray:handleArray];
  110. }
  111. - (void)setUpStatusBar{
  112. UIStatusBarStyle style = UIStatusBarStyleDefault;
  113. [[UIApplication sharedApplication] setStatusBarStyle:style
  114. animated:NO];
  115. }
  116. #pragma mark - NIMConversationManagerDelegate
  117. - (void)didAddRecentSession:(NIMRecentSession *)recentSession
  118. totalUnreadCount:(NSInteger)totalUnreadCount{
  119. self.sessionUnreadCount = totalUnreadCount;
  120. [self refreshSessionBadge];
  121. }
  122. - (void)didUpdateRecentSession:(NIMRecentSession *)recentSession
  123. totalUnreadCount:(NSInteger)totalUnreadCount{
  124. self.sessionUnreadCount = totalUnreadCount;
  125. [self refreshSessionBadge];
  126. }
  127. - (void)didRemoveRecentSession:(NIMRecentSession *)recentSession totalUnreadCount:(NSInteger)totalUnreadCount{
  128. self.sessionUnreadCount = totalUnreadCount;
  129. [self refreshSessionBadge];
  130. }
  131. - (void)messagesDeletedInSession:(NIMSession *)session{
  132. self.sessionUnreadCount = [NIMSDK sharedSDK].conversationManager.allUnreadCount;
  133. [self refreshSessionBadge];
  134. }
  135. - (void)allMessagesDeleted{
  136. self.sessionUnreadCount = 0;
  137. [self refreshSessionBadge];
  138. }
  139. #pragma mark - NIMSystemNotificationManagerDelegate
  140. - (void)onSystemNotificationCountChanged:(NSInteger)unreadCount
  141. {
  142. self.systemUnreadCount = unreadCount;
  143. [self refreshContactBadge];
  144. }
  145. #pragma mark - Notification
  146. - (void)onCustomNotifyChanged:(NSNotification *)notification
  147. {
  148. NTESCustomNotificationDB *db = [NTESCustomNotificationDB sharedInstance];
  149. self.customSystemUnreadCount = db.unreadCount;
  150. [self refreshSettingBadge];
  151. }
  152. - (void)refreshSessionBadge{
  153. UINavigationController *nav = self.viewControllers[NTESMainTabTypeMessageList];
  154. nav.tabBarItem.badgeValue = self.sessionUnreadCount ? @(self.sessionUnreadCount).stringValue : nil;
  155. }
  156. - (void)refreshContactBadge{
  157. UINavigationController *nav = self.viewControllers[NTESMainTabTypeContact];
  158. NSInteger badge = self.systemUnreadCount;
  159. nav.tabBarItem.badgeValue = badge ? @(badge).stringValue : nil;
  160. }
  161. - (void)refreshSettingBadge{
  162. UINavigationController *nav = self.viewControllers[NTESMainTabTypeSetting];
  163. NSInteger badge = self.customSystemUnreadCount;
  164. nav.tabBarItem.badgeValue = badge ? @(badge).stringValue : nil;
  165. }
  166. - (UIStatusBarStyle)preferredStatusBarStyle {
  167. return UIStatusBarStyleDefault;
  168. }
  169. #pragma mark - Rotate
  170. - (BOOL)shouldAutorotate{
  171. BOOL enableRotate = [NTESBundleSetting sharedConfig].enableRotate;
  172. return enableRotate ? [self.selectedViewController shouldAutorotate] : NO;
  173. }
  174. - (UIInterfaceOrientationMask)supportedInterfaceOrientations{
  175. BOOL enableRotate = [NTESBundleSetting sharedConfig].enableRotate;
  176. return enableRotate ? [self.selectedViewController supportedInterfaceOrientations] : UIInterfaceOrientationMaskPortrait;
  177. }
  178. #pragma mark - VC
  179. - (NSDictionary *)vcInfoForTabType:(NTESMainTabType)type{
  180. if (_configs == nil)
  181. {
  182. _configs = @{
  183. @(NTESMainTabTypeMessageList) : @{
  184. TabbarVC : @"NTESSessionListViewController",
  185. TabbarTitle : @"云信",
  186. TabbarImage : @"icon_message_normal",
  187. TabbarSelectedImage: @"icon_message_pressed",
  188. TabbarItemBadgeValue: @(self.sessionUnreadCount)
  189. },
  190. @(NTESMainTabTypeContact) : @{
  191. TabbarVC : @"NTESContactViewController",
  192. TabbarTitle : @"通讯录",
  193. TabbarImage : @"icon_contact_normal",
  194. TabbarSelectedImage: @"icon_contact_pressed",
  195. TabbarItemBadgeValue: @(self.systemUnreadCount)
  196. },
  197. @(NTESMainTabTypeChatroomList): @{
  198. TabbarVC : @"NTESChatroomListViewController",
  199. TabbarTitle : @"直播间",
  200. TabbarImage : @"icon_chatroom_normal",
  201. TabbarSelectedImage: @"icon_chatroom_pressed",
  202. },
  203. @(NTESMainTabTypeSetting) : @{
  204. TabbarVC : @"NTESSettingViewController",
  205. TabbarTitle : @"设置",
  206. TabbarImage : @"icon_setting_normal",
  207. TabbarSelectedImage: @"icon_setting_pressed",
  208. TabbarItemBadgeValue: @(self.customSystemUnreadCount)
  209. }
  210. };
  211. }
  212. return _configs[@(type)];
  213. }
  214. @end