WFCUFavChannelTableViewController.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // FavGroupTableViewController.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/9/13.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUFavChannelTableViewController.h"
  9. #import "WFCUChannelTableViewCell.h"
  10. #import "WFCUMessageListViewController.h"
  11. #import <WFChatClient/WFCChatClient.h>
  12. #import "UIView+Toast.h"
  13. #import "WFCUCreateChannelViewController.h"
  14. #import "WFCUSearchChannelViewController.h"
  15. #import "WFCUImage.h"
  16. @interface WFCUFavChannelTableViewController ()
  17. @property (nonatomic, strong)NSMutableArray<WFCCChannelInfo *> *favChannels;
  18. @end
  19. @implementation WFCUFavChannelTableViewController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.favChannels = [[NSMutableArray alloc] init];
  23. self.title = WFCString(@"MyChannels");
  24. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  25. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[WFCUImage imageNamed:@"bar_plus"] style:UIBarButtonItemStyleDone target:self action:@selector(onRightBarBtn:)];
  26. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSettingUpdated:) name:kSettingUpdated object:nil];
  27. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onChannelInfoUpdated:) name:kChannelInfoUpdated object:nil];
  28. }
  29. - (void)onSettingUpdated:(NSNotification *)notification {
  30. [self refreshList];
  31. }
  32. - (void)onRightBarBtn:(id)sender {
  33. UIViewController *vc = [[WFCUSearchChannelViewController alloc] init];
  34. [self.navigationController pushViewController:vc animated:YES];
  35. }
  36. - (void)refreshList {
  37. __weak typeof(self)ws = self;
  38. [[WFCCIMService sharedWFCIMService] getRemoteListenedChannels:^(NSArray<NSString *> *ids) {
  39. [ws.favChannels removeAllObjects];
  40. for (NSString *channelId in ids) {
  41. WFCCChannelInfo *channelInfo = [[WFCCIMService sharedWFCIMService] getChannelInfo:channelId refresh:NO];
  42. if (channelInfo) {
  43. channelInfo.channelId = channelId;
  44. [ws.favChannels addObject:channelInfo];
  45. }
  46. }
  47. [ws.tableView reloadData];
  48. } error:^(int errorCode) {
  49. }];
  50. [self.tableView reloadData];
  51. }
  52. - (void)onChannelInfoUpdated:(NSNotification *)notification {
  53. BOOL updated = NO;
  54. NSArray<WFCCChannelInfo *> *channelInfoList = notification.userInfo[@"channelInfoList"];
  55. for (WFCCChannelInfo *channelInfo in channelInfoList) {
  56. for (int i = 0; i < self.favChannels.count; i++) {
  57. if([self.favChannels[i].channelId isEqualToString:channelInfo.channelId]) {
  58. self.favChannels[i] = channelInfo;
  59. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:1]] withRowAnimation:UITableViewRowAnimationFade];
  60. updated = YES;
  61. break;
  62. }
  63. }
  64. }
  65. if (!updated) {
  66. [self refreshList];
  67. }
  68. }
  69. - (void)viewWillAppear:(BOOL)animated {
  70. [super viewWillAppear:animated];
  71. [self refreshList];
  72. self.tabBarController.tabBar.hidden = YES;
  73. }
  74. - (void)didReceiveMemoryWarning {
  75. [super didReceiveMemoryWarning];
  76. // Dispose of any resources that can be recreated.
  77. }
  78. #pragma mark - Table view data source
  79. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  80. return 1;
  81. }
  82. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  83. return self.favChannels.count;
  84. }
  85. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  86. WFCUChannelTableViewCell *cell = (WFCUChannelTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"groupCellId"];
  87. if (cell == nil) {
  88. cell = [[WFCUChannelTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"groupCellId"];
  89. }
  90. cell.channelInfo = self.favChannels[indexPath.row];
  91. return cell;
  92. }
  93. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  94. WFCCChannelInfo *channelInfo;
  95. channelInfo = self.favChannels[indexPath.row];
  96. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  97. NSString *channelId = channelInfo.channelId;
  98. mvc.conversation = [WFCCConversation conversationWithType:Channel_Type target:channelId line:0];
  99. [self.navigationController pushViewController:mvc animated:YES];
  100. }
  101. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  102. return 56;
  103. }
  104. - (void)dealloc {
  105. [[NSNotificationCenter defaultCenter] removeObserver:self];
  106. }
  107. @end