WFCUFavGroupTableViewController.m 5.3 KB

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