説明なし

SessionListController.m 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. //
  2. // MessageListViewController.m
  3. // FirstLink
  4. //
  5. // Created by mac on 14-9-10.
  6. // Copyright (c) 2014年 FirstLink. All rights reserved.
  7. //
  8. #import "SessionListController.h"
  9. #import "FKSessionListViewModel.h"
  10. #import "FirstLinkAppDelegate.h"
  11. #import "PindanMessageController.h"
  12. #import "UIImageView+FLAddition.h"
  13. #import "FKMessageManager.h"
  14. @interface SessionListController ()
  15. <SRRefreshDelegate, UITableViewDataSource, UITableViewDelegate>
  16. @property (nonatomic, strong) FKSessionListViewModel *listViewModel;
  17. @property (nonatomic, strong) UILabel *badgeView;
  18. @end
  19. @implementation SessionListController
  20. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  21. self = [super initWithCoder:aDecoder];
  22. if (self) {
  23. }
  24. return self;
  25. }
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. // Do any additional setup after loading the view from its nib.
  29. [self configUnreadRefreshCallBackAfterViewDidLoad];
  30. [self refreshUsersAndUnreadMessage];
  31. }
  32. - (void)refreshUsersAndUnreadMessage {
  33. [self requestUsersForHuanxin];
  34. [[FKMessageManager sharedInstance] refreshMessageUnreadMessage];
  35. }
  36. - (void)dealloc {
  37. [[NSNotificationCenter defaultCenter] removeObserver:self];
  38. }
  39. - (void)viewWillAppear:(BOOL)animated {
  40. [super viewWillAppear:animated];
  41. self.navigationItem.title = @"消息";
  42. [UIApplication sharedApplication].statusBarHidden = NO;
  43. [self.navigationController setNavigationBarHidden:NO animated:NO];
  44. [self.tableView reloadData];
  45. if (![FKMessageManager sharedInstance].remoteMessageBadge) {
  46. [self refreshUsersAndUnreadMessage];
  47. }
  48. }
  49. - (void)viewDidAppear:(BOOL)animated {
  50. [super viewDidAppear:animated];
  51. [[FKMessageManager sharedInstance] addEaseMobDelegate:self
  52. observerType:FKEaseMobObserverTypeService];
  53. }
  54. - (void)viewWillDisappear:(BOOL)animated {
  55. [super viewWillDisappear:animated];
  56. }
  57. - (void)viewDidDisappear:(BOOL)animated {
  58. [super viewDidDisappear:animated];
  59. [[FKMessageManager sharedInstance] removeEaseMobDelegate:self
  60. observerType:FKEaseMobObserverTypeService];
  61. }
  62. - (void)didReceiveMemoryWarning {
  63. [super didReceiveMemoryWarning];
  64. // Dispose of any resources that can be recreated.
  65. }
  66. /*
  67. #pragma mark - Navigation
  68. // In a storyboard-based application, you will often want to do a little preparation before navigation
  69. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  70. {
  71. // Get the new view controller using [segue destinationViewController].
  72. // Pass the selected object to the new view controller.
  73. }
  74. */
  75. #pragma mark -
  76. - (NSMutableArray *)loadDataSource {
  77. WeakSelf(weakSelf);
  78. __block NSInteger chatterID;
  79. __block EMMessage *latestCustomMessage;
  80. __weak FKMessageManager *weakMessageManager = [FKMessageManager sharedInstance];
  81. NSArray *conversations = [[EMClient sharedClient].chatManager loadAllConversationsFromDB];
  82. [conversations enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  83. EMConversation *conversation = (EMConversation*)obj;
  84. chatterID = conversation.conversationId.integerValue;
  85. if (conversation.type != EMConversationTypeGroupChat) {
  86. if (chatterID >= MinCustomerConversationID && chatterID <= MaxCustomerConversationID) {
  87. EMMessage *message = [weakSelf realLastMessage:conversation];
  88. if (latestCustomMessage == nil) {
  89. latestCustomMessage = message;
  90. } else {
  91. if (latestCustomMessage.timestamp < message.timestamp) {
  92. latestCustomMessage = message;
  93. }
  94. }
  95. } else {
  96. [conversation markAllMessagesAsRead:nil];
  97. }
  98. }
  99. }];
  100. if (latestCustomMessage) {
  101. EMConversation *latestConversation = [[EMClient sharedClient].chatManager getConversation:latestCustomMessage.conversationId
  102. type:EMConversationTypeChat
  103. createIfNotExist:NO];
  104. FKLatestMessage *message = [[FKLatestMessage alloc] init];
  105. message.userID = latestCustomMessage.from;
  106. message.text = [self subTitleMessageByConversation:latestConversation];
  107. weakMessageManager.latestCustomerMessage = message;
  108. }
  109. return nil;
  110. }
  111. + (NSInteger)localUnreadMessageCount {
  112. __block NSInteger chatterID;
  113. __block NSInteger unreadCount = 0;
  114. NSArray *conversations = [[EMClient sharedClient].chatManager loadAllConversationsFromDB];
  115. [conversations enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  116. EMConversation *conversation = (EMConversation*)obj;
  117. chatterID = conversation.conversationId.integerValue;
  118. if (conversation.type != EMConversationTypeGroupChat) {
  119. if (chatterID >= MinCustomerConversationID && chatterID <= MaxCustomerConversationID) {
  120. unreadCount += conversation.unreadMessagesCount;
  121. } else {
  122. [conversation markAllMessagesAsRead:nil];
  123. }
  124. }
  125. }];
  126. return unreadCount;
  127. }
  128. #pragma mark - Unread Message
  129. - (void)configUnreadRefreshCallBackBeforeViewDidLoad {
  130. WeakSelf(weakSelf);
  131. __weak FKMessageManager *weakMessageManage = [FKMessageManager sharedInstance];
  132. [FKMessageManager sharedInstance].unreadRefreshCallBackBeforeViewDidLoad = ^() {
  133. NSInteger localBadge = [SessionListController localUnreadMessageCount];
  134. [weakSelf setBadgeViewNumber:(localBadge + weakMessageManage.remoteMessageBadge.totalBadge)];
  135. if ([FKUserManager sharedManager].applicationMode == ApplicationModeVisitor) {
  136. [weakSelf setBadgeViewNumber:0];
  137. }
  138. };
  139. }
  140. - (void)configUnreadRefreshCallBackAfterViewDidLoad {
  141. WeakSelf(weakSelf);
  142. __weak FKMessageManager *weakMessageManage = [FKMessageManager sharedInstance];
  143. [FKMessageManager sharedInstance].unreadRefreshCallBackAfterViewDidLoad = ^() {
  144. NSInteger localBadge = [SessionListController localUnreadMessageCount];
  145. [weakSelf setBadgeViewNumber:(localBadge + weakMessageManage.remoteMessageBadge.totalBadge)];
  146. if ([FKUserManager sharedManager].applicationMode == ApplicationModeVisitor) {
  147. [weakSelf setBadgeViewNumber:0];
  148. }
  149. [weakSelf.tableView reloadData];
  150. };
  151. }
  152. #pragma mark - Method
  153. - (void)configBadgeView {
  154. FirstLinkAppDelegate *delegate = (FirstLinkAppDelegate *)[[UIApplication sharedApplication] delegate];
  155. UITabBarController *tabbarController = (UITabBarController*)delegate.window.rootViewController;
  156. if ([tabbarController isKindOfClass:[UITabBarController class]]) {
  157. [tabbarController.tabBar addSubview:self.badgeView];
  158. }
  159. }
  160. - (void)setBadgeViewNumber:(NSInteger)count {
  161. if (count <= 0) {
  162. self.badgeView.hidden = YES;
  163. } else {
  164. self.badgeView.hidden = NO;
  165. self.badgeView.text = [NSString stringWithFormat:@"%lu", (long)count];
  166. }
  167. }
  168. #pragma mark - TableView Delegate
  169. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  170. return 4;
  171. }
  172. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  173. return 64;
  174. }
  175. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  176. SessionListCell *cell = [tableView dequeueReusableCellWithIdentifier:SessionListCellIndetifier];
  177. if (cell) {
  178. cell.iconView.image = [self.listViewModel imageAtIndex:indexPath.row];
  179. cell.tagLabel.text = [self.listViewModel titleAtIndex:indexPath.row];
  180. FKMessageManager *messageManage = [FKMessageManager sharedInstance];
  181. switch (indexPath.row) {
  182. case 0: {
  183. [cell setCellType:SessionListCellTypeDefault];
  184. [cell setUnreadNumberString:messageManage.remoteMessageBadge.systemBadge];
  185. break;
  186. }
  187. case 1: {
  188. [cell setCellType:SessionListCellTypeDefault];
  189. [cell setUnreadNumberString:messageManage.remoteMessageBadge.pindanBadge];
  190. break;
  191. }
  192. case 2: {
  193. [cell setCellType:SessionListCellTypeMessage];
  194. [cell setUnreadNumberString:messageManage.remoteMessageBadge.commentBadge];
  195. [cell setMessageText:messageManage.latestCommentMessage.text];
  196. cell.headView.hidden = cell.messageLabel.hidden;
  197. if (messageManage.latestCommentMessage.headURL) {
  198. [cell.headView setImageWithURL:messageManage.latestCommentMessage.headURL
  199. placeholderImage:nil
  200. width:HEAD_PHOTO_LENGTH_32
  201. height:HEAD_PHOTO_LENGTH_32];
  202. }
  203. break;
  204. }
  205. case 3: {
  206. [cell setCellType:SessionListCellTypeMessage];
  207. [cell setUnreadNumberString:[NSString stringWithFormat:@"%lu", (long)[SessionListController localUnreadMessageCount]]];
  208. [cell setMessageText:messageManage.latestCustomerMessage.text];
  209. [self setHeadView:cell userID:messageManage.latestCustomerMessage.userID];
  210. break;
  211. }
  212. default:
  213. break;
  214. }
  215. }
  216. if ([FKUserManager sharedManager].applicationMode == ApplicationModeVisitor) {
  217. cell.unreadLabel.hidden = YES;
  218. }
  219. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  220. [cell setSeparatorInset:UIEdgeInsetsZero];
  221. }
  222. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  223. [cell setLayoutMargins:UIEdgeInsetsZero];
  224. }
  225. return cell;
  226. }
  227. - (void)setHeadView:(SessionListCell *)cell userID:(NSString*)userID {
  228. NSString *urlString = self.nicknameAndHearurlDict[userID][@"headURL"];
  229. [cell.headView setImageWithURL:urlString
  230. placeholderImage:nil
  231. width:HEAD_PHOTO_LENGTH_32
  232. height:HEAD_PHOTO_LENGTH_32];
  233. }
  234. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  235. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  236. if ([FKUserManager sharedManager].applicationMode == ApplicationModeVisitor) {
  237. [self showLoginActionMenu];
  238. return;
  239. }
  240. UIViewController *viewController;
  241. FKMessageManager *messageManager = [FKMessageManager sharedInstance];
  242. switch (indexPath.row) {
  243. case 0: {
  244. messageManager.remoteMessageBadge.systemBadge = @"0";
  245. viewController = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:@"SysMSGViewController"];
  246. break;
  247. }
  248. case 1: {
  249. messageManager.remoteMessageBadge.pindanBadge = @"0";
  250. viewController = [[PindanMessageController alloc] init];
  251. break;
  252. }
  253. case 2: {
  254. messageManager.remoteMessageBadge.commentBadge = @"0";
  255. viewController = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:@"CommentMessageController"];
  256. break;
  257. }
  258. case 3: {
  259. CustomerListController *cusController = [[CustomerListController alloc] init];
  260. cusController.nicknameAndHearurlDict = self.nicknameAndHearurlDict;
  261. cusController.hidesBottomBarWhenPushed = YES;
  262. viewController = cusController;
  263. break;
  264. }
  265. default:
  266. break;
  267. }
  268. if (viewController) {
  269. [self.navigationController pushViewController:viewController animated:YES];
  270. }
  271. }
  272. #pragma mark -
  273. - (void)setupReadPindanMessageBlock:(PindanMessageController*)controller {
  274. WeakSelf(weakSelf);
  275. __weak FKMessageManager *weakMessageManage = [FKMessageManager sharedInstance];
  276. controller.readPindanMessageBlock = ^() {
  277. int count = weakMessageManage.remoteMessageBadge.pindanBadge.intValue;
  278. weakMessageManage.remoteMessageBadge.pindanBadge = [NSString stringWithFormat:@"%d", MAX(0, --count)];
  279. [weakSelf.tableView reloadData];
  280. };
  281. }
  282. #pragma mark - IChatMangerDelegate
  283. // 统计未读消息数
  284. -(void)setupUnreadMessageCount {
  285. NSInteger unreadCount = [SessionListController localUnreadMessageCount];
  286. __weak FKMessageManager *weakMessageManage = [FKMessageManager sharedInstance];
  287. if (weakMessageManage.remoteMessageBadge) {
  288. unreadCount += weakMessageManage.remoteMessageBadge.totalBadge;
  289. }
  290. [self setBadgeViewNumber:unreadCount];
  291. }
  292. // 收到消息回调
  293. -(void)messagesDidReceive:(NSArray *)aMessages {
  294. FKMessageManager *messageManage = [FKMessageManager sharedInstance];
  295. EMMessage *message = aMessages.lastObject;
  296. BOOL needShowNotification = (message.chatType == EMChatTypeGroupChat) ? [self needShowNotification:message.conversationId] : YES;
  297. if (needShowNotification) {
  298. #if !TARGET_IPHONE_SIMULATOR
  299. [self playSoundAndVibration];
  300. NSInteger unreadCount = [SessionListController localUnreadMessageCount];
  301. if (messageManage.remoteMessageBadge) {
  302. unreadCount += messageManage.remoteMessageBadge.totalBadge;
  303. }
  304. UIApplicationState state = [[UIApplication sharedApplication] applicationState];
  305. if (state == UIApplicationStateBackground) {
  306. [self showNotificationWithMessage:message];
  307. } else {
  308. if (unreadCount > 0) {
  309. [self setBadgeViewNumber:(int)(unreadCount)];
  310. } else {
  311. [self setBadgeViewNumber:0];
  312. }
  313. }
  314. #endif
  315. }
  316. [self loadDataSource];
  317. [self.tableView reloadData];
  318. }
  319. #pragma mark - Property
  320. - (FKSessionListViewModel *)listViewModel {
  321. if (!_listViewModel) {
  322. _listViewModel = [[FKSessionListViewModel alloc] init];
  323. }
  324. return _listViewModel;
  325. }
  326. - (UILabel*)badgeView {
  327. if (!_badgeView) {
  328. _badgeView = [[UILabel alloc]init];
  329. _badgeView.textAlignment = NSTextAlignmentCenter;
  330. _badgeView.frame = CGRectMake(UISCREENWIDTH*7/10 + 4, 4, 16, 16);
  331. _badgeView.layer.cornerRadius = 8;
  332. _badgeView.textColor = [UIColor whiteColor];
  333. _badgeView.backgroundColor = UIColorFromRGB(0xff676b);
  334. _badgeView.font = [UIFont systemFontOfSize:11];
  335. _badgeView.clipsToBounds = YES;
  336. _badgeView.hidden = YES;
  337. }
  338. return _badgeView;
  339. }
  340. @end