WFCUFavChannelTableViewController.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. @interface WFCUFavChannelTableViewController ()
  16. @property (nonatomic, strong)NSMutableArray<WFCCChannelInfo *> *myChannels;
  17. @property (nonatomic, strong)NSMutableArray<WFCCChannelInfo *> *favChannels;
  18. @end
  19. @implementation WFCUFavChannelTableViewController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.myChannels = [[NSMutableArray alloc] init];
  23. self.favChannels = [[NSMutableArray alloc] init];
  24. self.title = WFCString(@"MyChannels");
  25. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  26. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bar_plus"] style:UIBarButtonItemStyleDone target:self action:@selector(onRightBarBtn:)];
  27. }
  28. - (void)onRightBarBtn:(id)sender {
  29. __weak typeof(self)ws = self;
  30. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:WFCString(@"AddChannel") message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  31. UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:WFCString(@"Cancel") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  32. }];
  33. UIAlertAction *actionSubscribe = [UIAlertAction actionWithTitle:WFCString(@"SubscribeChannel") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  34. UIViewController *vc = [[WFCUSearchChannelViewController alloc] init];
  35. [ws.navigationController pushViewController:vc animated:YES];
  36. }];
  37. UIAlertAction *actionCreate = [UIAlertAction actionWithTitle:WFCString(@"CreateChannel") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  38. UIViewController *vc = [[WFCUCreateChannelViewController alloc] init];
  39. [ws.navigationController pushViewController:vc animated:YES];
  40. }];
  41. //把action添加到actionSheet里
  42. [actionSheet addAction:actionSubscribe];
  43. [actionSheet addAction:actionCreate];
  44. [actionSheet addAction:actionCancel];
  45. //相当于之前的[actionSheet show];
  46. [self presentViewController:actionSheet animated:YES completion:nil];
  47. }
  48. - (void)refreshList {
  49. NSArray *ids = [[WFCCIMService sharedWFCIMService] getMyChannels];
  50. [self.myChannels removeAllObjects];
  51. [self.favChannels removeAllObjects];
  52. for (NSString *channelId in ids) {
  53. WFCCChannelInfo *channelInfo = [[WFCCIMService sharedWFCIMService] getChannelInfo:channelId refresh:NO];
  54. if (channelInfo) {
  55. channelInfo.channelId = channelId;
  56. [self.myChannels addObject:channelInfo];
  57. __weak typeof(self)ws = self;
  58. [[NSNotificationCenter defaultCenter] addObserverForName:kChannelInfoUpdated object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  59. [ws onChannelInfoUpdated:note];
  60. }];
  61. }
  62. }
  63. ids = [[WFCCIMService sharedWFCIMService] getListenedChannels];
  64. for (NSString *channelId in ids) {
  65. WFCCChannelInfo *channelInfo = [[WFCCIMService sharedWFCIMService] getChannelInfo:channelId refresh:NO];
  66. if (channelInfo) {
  67. channelInfo.channelId = channelId;
  68. [self.favChannels addObject:channelInfo];
  69. __weak typeof(self)ws = self;
  70. [[NSNotificationCenter defaultCenter] addObserverForName:kChannelInfoUpdated object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  71. [ws onChannelInfoUpdated:note];
  72. }];
  73. }
  74. }
  75. [self.tableView reloadData];
  76. }
  77. - (void)onChannelInfoUpdated:(NSNotification *)notification {
  78. WFCCChannelInfo *channelInfo = notification.userInfo[@"channelInfo"];
  79. for (int i = 0; i < self.myChannels.count; i++) {
  80. if([self.myChannels[i].channelId isEqualToString:channelInfo.channelId]) {
  81. self.myChannels[i] = channelInfo;
  82. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
  83. }
  84. }
  85. for (int i = 0; i < self.favChannels.count; i++) {
  86. if([self.favChannels[i].channelId isEqualToString:channelInfo.channelId]) {
  87. self.favChannels[i] = channelInfo;
  88. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:1]] withRowAnimation:UITableViewRowAnimationFade];
  89. }
  90. }
  91. }
  92. - (void)viewWillAppear:(BOOL)animated {
  93. [super viewWillAppear:animated];
  94. [self refreshList];
  95. self.tabBarController.tabBar.hidden = YES;
  96. }
  97. - (void)didReceiveMemoryWarning {
  98. [super didReceiveMemoryWarning];
  99. // Dispose of any resources that can be recreated.
  100. }
  101. #pragma mark - Table view data source
  102. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  103. return 2;
  104. }
  105. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  106. if (section == 0) {
  107. return self.myChannels.count;
  108. }
  109. return self.favChannels.count;
  110. }
  111. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  112. if (section == 0) {
  113. return WFCString(@"MyChannels");
  114. } else {
  115. return WFCString(@"SubscribedChannels");
  116. }
  117. }
  118. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  119. WFCUChannelTableViewCell *cell = (WFCUChannelTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"groupCellId"];
  120. if (cell == nil) {
  121. cell = [[WFCUChannelTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"groupCellId"];
  122. }
  123. if (indexPath.section == 0) {
  124. cell.channelInfo = self.myChannels[indexPath.row];
  125. } else {
  126. cell.channelInfo = self.favChannels[indexPath.row];
  127. }
  128. return cell;
  129. }
  130. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  131. WFCCChannelInfo *channelInfo;
  132. if (indexPath.section == 0) {
  133. channelInfo = self.myChannels[indexPath.row];
  134. } else {
  135. channelInfo = self.favChannels[indexPath.row];
  136. }
  137. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  138. NSString *channelId = channelInfo.channelId;
  139. mvc.conversation = [WFCCConversation conversationWithType:Channel_Type target:channelId line:0];
  140. [self.navigationController pushViewController:mvc animated:YES];
  141. }
  142. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  143. return 56;
  144. }
  145. - (void)dealloc {
  146. [[NSNotificationCenter defaultCenter] removeObserver:self];
  147. }
  148. @end