WFCUFavGroupTableViewController.m 5.1 KB

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