ContactGroupMembershipViewController.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "ContactGroupMembershipViewController.h"
  21. #import "GroupProxy.h"
  22. #import "ContactGroupCell.h"
  23. #import "EntityManager.h"
  24. @interface ContactGroupMembershipViewController ()
  25. @property NSArray *groups;
  26. @property Contact *contact;
  27. @end
  28. @implementation ContactGroupMembershipViewController
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. self.title = NSLocalizedString(@"groups", nil);
  32. [self updateGroups];
  33. }
  34. - (void)setGroupContact:(Contact *)contact {
  35. _contact = contact;
  36. [self updateGroups];
  37. }
  38. - (void)updateGroups {
  39. EntityManager *entityManager = [[EntityManager alloc] init];
  40. NSArray *groupConversations = [entityManager.entityFetcher groupConversationsForContact:_contact];
  41. NSMutableArray *newGroups = [NSMutableArray array];
  42. for (Conversation *conversation in groupConversations) {
  43. GroupProxy *group = [GroupProxy groupProxyForConversation:conversation];
  44. [newGroups addObject:group];
  45. }
  46. _groups = newGroups;
  47. }
  48. #pragma mark - Table view data source
  49. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  50. return 1;
  51. }
  52. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  53. return [_groups count];
  54. }
  55. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  56. ContactGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContactGroupCell"];
  57. if (cell == nil) {
  58. cell = [[ContactGroupCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ContactGroupCell"];
  59. }
  60. GroupProxy *group = [_groups objectAtIndex:indexPath.row];
  61. cell.group = group;
  62. return cell;
  63. }
  64. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  65. GroupProxy *group = [_groups objectAtIndex:indexPath.row];
  66. [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationShowGroup object:nil userInfo:[NSDictionary dictionaryWithObject:group forKey:kKeyGroup]];
  67. }
  68. @end