2
0

WFPttChannelListViewController.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // WFPttListViewController.m
  3. // PttUIKit
  4. //
  5. // Created by Hao Jia on 2021/10/14.
  6. //
  7. #ifdef WFC_PTT
  8. #import "WFPttChannelListViewController.h"
  9. #import <PttClient/WFPttClient.h>
  10. #import "WFPttCreateChannelViewController.h"
  11. #import "WFPttJoinChannelViewController.h"
  12. #import "WFPttChannelViewController.h"
  13. #import <WFChatClient/WFCChatClient.h>
  14. @interface WFPttChannelListViewController () <UITableViewDelegate, UITableViewDataSource>
  15. @property(nonatomic, strong)UITableView *tableView;
  16. @property(nonatomic, strong)NSArray *items;
  17. @end
  18. @implementation WFPttChannelListViewController
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
  22. if (@available(iOS 15, *)) {
  23. self.tableView.sectionHeaderTopPadding = 0;
  24. }
  25. self.tableView.delegate = self;
  26. self.tableView.dataSource = self;
  27. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  28. [self.view addSubview:self.tableView];
  29. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStyleDone target:self action:@selector(onAddBtn:)];
  30. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onGroupInfoUpdated:) name:kGroupInfoUpdated object:nil];
  31. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSettingUpdated:) name:kSettingUpdated object:nil];
  32. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onTalkingBegin:) name:kWFPttTalkingBeginNotification object:nil];
  33. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onTalkingEnd:) name:kWFPttTalkingEndNotification object:nil];
  34. }
  35. - (void)onGroupInfoUpdated:(NSNotification *)notification {
  36. [self loadData];
  37. }
  38. - (void)onSettingUpdated:(NSNotification *)notification {
  39. [self loadData];
  40. }
  41. - (void)onTalkingBegin:(NSNotification *)notification {
  42. [self loadData];
  43. }
  44. - (void)onTalkingEnd:(NSNotification *)notification {
  45. [self loadData];
  46. }
  47. - (void)loadData {
  48. self.items = [[WFPttClient sharedClient] getSubscribedChannels].reverseObjectEnumerator.allObjects;
  49. [self.tableView reloadData];
  50. }
  51. - (void)onAddBtn:(id)sender {
  52. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  53. UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  54. }];
  55. __weak typeof(self)ws = self;
  56. UIAlertAction *createAction = [UIAlertAction actionWithTitle:@"创建频道" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  57. WFPttCreateChannelViewController *vc = [[WFPttCreateChannelViewController alloc] init];
  58. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
  59. nav.modalPresentationStyle = UIModalPresentationFullScreen;
  60. [ws.navigationController presentViewController:nav animated:YES completion:nil];
  61. }];
  62. UIAlertAction *joinAction = [UIAlertAction actionWithTitle:@"加入频道" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  63. WFPttJoinChannelViewController *vc = [[WFPttJoinChannelViewController alloc] init];
  64. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
  65. nav.modalPresentationStyle = UIModalPresentationFullScreen;
  66. [ws.navigationController presentViewController:nav animated:YES completion:nil];
  67. }];
  68. [actionSheet addAction:createAction];
  69. [actionSheet addAction:joinAction];
  70. [actionSheet addAction:actionCancel];
  71. [self presentViewController:actionSheet animated:YES completion:nil];
  72. }
  73. - (void)viewWillAppear:(BOOL)animated {
  74. [self loadData];
  75. }
  76. #pragma mark - UITableViewDataSource
  77. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  78. return self.items.count;
  79. }
  80. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  81. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  82. if(!cell) {
  83. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  84. }
  85. NSString *channelId = self.items[indexPath.row];
  86. WFPttChannelInfo *channelInfo = [[WFPttClient sharedClient] getChannelInfo:channelId];
  87. NSString *text = channelInfo.name.length ? channelInfo.name : @"频道";
  88. NSDictionary<NSString *, NSNumber *> *talkingMembers = [[WFPttClient sharedClient] getTalkingMember:channelId];
  89. NSArray *keys = talkingMembers.allKeys;
  90. for (int i = 0; i < talkingMembers.count; i++) {
  91. NSString *key = keys[i];
  92. WFCCUserInfo *talkingUser = [[WFCCIMService sharedWFCIMService] getUserInfo:key refresh:NO];
  93. if(i) {
  94. text = [text stringByAppendingString:@","];
  95. } else {
  96. text = [text stringByAppendingString:@" "];
  97. }
  98. text = [text stringByAppendingString:talkingUser.displayName];
  99. }
  100. cell.textLabel.text = text;
  101. return cell;
  102. }
  103. #pragma mark - UITableViewDelegate
  104. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  105. WFPttChannelViewController *vc = [[WFPttChannelViewController alloc] init];
  106. vc.channelId = self.items[indexPath.row];
  107. [self.navigationController pushViewController:vc animated:YES];
  108. }
  109. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  110. return YES;
  111. }
  112. -(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  113. NSString *channelId = self.items[indexPath.row];
  114. NSString *owner = [[WFPttClient sharedClient] getChannelOwner:channelId];
  115. UITableViewRowAction *action;
  116. if([[WFCCNetworkService sharedInstance].userId isEqualToString:owner]) {
  117. action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  118. [[WFPttClient sharedClient] destroyChannel:channelId success:nil error:nil];
  119. }];
  120. } else {
  121. action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"退出" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  122. [[WFPttClient sharedClient] leaveChanel:channelId success:nil error:nil];
  123. }];
  124. }
  125. action.backgroundColor = [UIColor redColor];
  126. return @[action];
  127. }
  128. @end
  129. #endif //WFC_PTT