WFCUFavGroupTableViewController.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "WFCUFavGroupTableViewController.h"
  9. #import "WFCUGroupTableViewCell.h"
  10. #import "WFCUMessageListViewController.h"
  11. #import <WFChatClient/WFCChatClient.h>
  12. #import "WFCUGroupMemberTableViewController.h"
  13. #import "WFCUInviteGroupMemberViewController.h"
  14. #import "UIView+Toast.h"
  15. #import "WFCUConfigManager.h"
  16. @interface WFCUFavGroupTableViewController ()
  17. @property (nonatomic, strong)NSMutableArray<WFCCGroupInfo *> *groups;
  18. @end
  19. @implementation WFCUFavGroupTableViewController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.groups = [[NSMutableArray alloc] init];
  23. self.title = WFCString(@"MyGroup");
  24. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  25. self.tableView.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  26. }
  27. - (void)refreshList {
  28. NSArray *ids = [[WFCCIMService sharedWFCIMService] getFavGroups];
  29. [self.groups removeAllObjects];
  30. for (NSString *groupId in ids) {
  31. WFCCGroupInfo *groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:groupId refresh:NO];
  32. if (groupInfo) {
  33. groupInfo.target = groupId;
  34. [self.groups addObject:groupInfo];
  35. }
  36. }
  37. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onGroupInfoUpdated:) name:kGroupInfoUpdated object:nil];
  38. [self.tableView reloadData];
  39. }
  40. - (void)onGroupInfoUpdated:(NSNotification *)notification {
  41. NSArray<WFCCGroupInfo *> *groupInfoList = notification.userInfo[@"groupInfoList"];
  42. for (int i = 0; i < self.groups.count; ++i) {
  43. for (WFCCGroupInfo *groupInfo in groupInfoList) {
  44. if([self.groups[i].target isEqualToString:groupInfo.target]) {
  45. self.groups[i] = groupInfo;
  46. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
  47. }
  48. }
  49. }
  50. }
  51. - (void)viewWillAppear:(BOOL)animated {
  52. [super viewWillAppear:animated];
  53. [self refreshList];
  54. self.tabBarController.tabBar.hidden = YES;
  55. }
  56. - (void)didReceiveMemoryWarning {
  57. [super didReceiveMemoryWarning];
  58. // Dispose of any resources that can be recreated.
  59. }
  60. #pragma mark - Table view data source
  61. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  62. return self.groups.count;
  63. }
  64. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  65. WFCUGroupTableViewCell *cell = (WFCUGroupTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"groupCellId"];
  66. if (cell == nil) {
  67. cell = [[WFCUGroupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"groupCellId"];
  68. }
  69. cell.groupInfo = self.groups[indexPath.row];
  70. return cell;
  71. }
  72. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  73. WFCCGroupInfo *groupInfo = self.groups[indexPath.row];
  74. WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
  75. NSString *groupId = groupInfo.target;
  76. mvc.conversation = [WFCCConversation conversationWithType:Group_Type target:groupId line:0];
  77. [self.navigationController pushViewController:mvc animated:YES];
  78. }
  79. // Override to support conditional editing of the table view.
  80. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  81. // Return NO if you do not want the specified item to be editable.
  82. return YES;
  83. }
  84. // Override to support editing the table view.
  85. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  86. if (editingStyle == UITableViewCellEditingStyleDelete) {
  87. // Delete the row from the data source
  88. // [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  89. } else if (editingStyle == UITableViewCellEditingStyleInsert) {
  90. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
  91. }
  92. }
  93. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  94. return 51;
  95. }
  96. - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  97. NSString *groupId = self.groups[indexPath.row].target;
  98. __weak typeof(self) ws = self;
  99. UITableViewRowAction *cancel = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:WFCString(@"Remove") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  100. [[WFCCIMService sharedWFCIMService] setFavGroup:groupId fav:NO success:^{
  101. [ws.view makeToast:WFCString(@"Removed") duration:2 position:CSToastPositionCenter];
  102. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  103. [ws refreshList];
  104. });
  105. } error:^(int error_code) {
  106. [ws.view makeToast:WFCString(@"OperationFailure") duration:2 position:CSToastPositionCenter];
  107. }];
  108. }];
  109. cancel.backgroundColor = [UIColor redColor];
  110. return @[cancel];
  111. };
  112. - (void)dealloc {
  113. [[NSNotificationCenter defaultCenter] removeObserver:self];
  114. }
  115. @end