WFCUFavChannelTableViewController.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 () <UIActionSheetDelegate>
  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 = @"我的频道";
  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. UIActionSheet *actionSheet =
  30. [[UIActionSheet alloc] initWithTitle:@"添加频道"
  31. delegate:self
  32. cancelButtonTitle:@"取消"
  33. destructiveButtonTitle:@"收听别人的频道"
  34. otherButtonTitles:@"新建自己的频道", nil];
  35. [actionSheet showInView:self.view];
  36. }
  37. - (void)refreshList {
  38. NSArray *ids = [[WFCCIMService sharedWFCIMService] getMyChannels];
  39. [self.myChannels removeAllObjects];
  40. [self.favChannels removeAllObjects];
  41. for (NSString *channelId in ids) {
  42. WFCCChannelInfo *channelInfo = [[WFCCIMService sharedWFCIMService] getChannelInfo:channelId refresh:NO];
  43. if (channelInfo) {
  44. channelInfo.channelId = channelId;
  45. [self.myChannels addObject:channelInfo];
  46. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onChannelInfoUpdated:) name:kChannelInfoUpdated object:channelId];
  47. }
  48. }
  49. ids = [[WFCCIMService sharedWFCIMService] getListenedChannels];
  50. for (NSString *channelId in ids) {
  51. WFCCChannelInfo *channelInfo = [[WFCCIMService sharedWFCIMService] getChannelInfo:channelId refresh:NO];
  52. if (channelInfo) {
  53. channelInfo.channelId = channelId;
  54. [self.favChannels addObject:channelInfo];
  55. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onChannelInfoUpdated:) name:kChannelInfoUpdated object:channelId];
  56. }
  57. }
  58. [self.tableView reloadData];
  59. }
  60. - (void)onChannelInfoUpdated:(NSNotification *)notification {
  61. WFCCChannelInfo *channelInfo = notification.userInfo[@"channelInfo"];
  62. for (int i = 0; i < self.myChannels.count; i++) {
  63. if([self.myChannels[i].channelId isEqualToString:channelInfo.channelId]) {
  64. self.myChannels[i] = channelInfo;
  65. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
  66. }
  67. }
  68. for (int i = 0; i < self.favChannels.count; i++) {
  69. if([self.favChannels[i].channelId isEqualToString:channelInfo.channelId]) {
  70. self.favChannels[i] = channelInfo;
  71. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:1]] withRowAnimation:UITableViewRowAnimationFade];
  72. }
  73. }
  74. }
  75. - (void)viewWillAppear:(BOOL)animated {
  76. [super viewWillAppear:animated];
  77. [self refreshList];
  78. self.tabBarController.tabBar.hidden = YES;
  79. }
  80. - (void)didReceiveMemoryWarning {
  81. [super didReceiveMemoryWarning];
  82. // Dispose of any resources that can be recreated.
  83. }
  84. #pragma mark - Table view data source
  85. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  86. return 2;
  87. }
  88. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  89. if (section == 0) {
  90. return self.myChannels.count;
  91. }
  92. return self.favChannels.count;
  93. }
  94. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  95. if (section == 0) {
  96. return @"我的频道";
  97. } else {
  98. return @"收听的频道";
  99. }
  100. }
  101. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  102. WFCUChannelTableViewCell *cell = (WFCUChannelTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"groupCellId"];
  103. if (cell == nil) {
  104. cell = [[WFCUChannelTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"groupCellId"];
  105. }
  106. if (indexPath.section == 0) {
  107. cell.channelInfo = self.myChannels[indexPath.row];
  108. } else {
  109. cell.channelInfo = self.favChannels[indexPath.row];
  110. }
  111. return cell;
  112. }
  113. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  114. WFCCChannelInfo *channelInfo;
  115. if (indexPath.section == 0) {
  116. channelInfo = self.myChannels[indexPath.row];
  117. } else {
  118. channelInfo = self.favChannels[indexPath.row];
  119. }
  120. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  121. NSString *channelId = channelInfo.channelId;
  122. mvc.conversation = [WFCCConversation conversationWithType:Channel_Type target:channelId line:0];
  123. [self.navigationController pushViewController:mvc animated:YES];
  124. }
  125. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  126. return 56;
  127. }
  128. #pragma mark - UIActionSheetDelegate <NSObject>
  129. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  130. if(buttonIndex == 0) {
  131. UIViewController *vc = [[WFCUSearchChannelViewController alloc] init];
  132. [self.navigationController pushViewController:vc animated:YES];
  133. } else if (buttonIndex == 1) {
  134. UIViewController *vc = [[WFCUCreateChannelViewController alloc] init];
  135. [self.navigationController pushViewController:vc animated:YES];
  136. }
  137. }
  138. @end