SearchTableViewController.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. #import "SearchTableViewController.h"
  6. #import "NCAPIController.h"
  7. #import "NCDatabaseManager.h"
  8. #import "NCUser.h"
  9. #import "PlaceholderView.h"
  10. #import "NextcloudTalk-Swift.h"
  11. @interface SearchTableViewController ()
  12. {
  13. PlaceholderView *_contactsBackgroundView;
  14. }
  15. @end
  16. @implementation SearchTableViewController
  17. - (void)viewDidLoad
  18. {
  19. [super viewDidLoad];
  20. [self.tableView registerNib:[UINib nibWithNibName:kContactsTableCellNibName bundle:nil] forCellReuseIdentifier:kContactCellIdentifier];
  21. // Align header's title to ContactsTableViewCell's label
  22. self.tableView.separatorInset = UIEdgeInsetsMake(0, 72, 0, 0);
  23. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  24. // Contacts placeholder view
  25. _contactsBackgroundView = [[PlaceholderView alloc] init];
  26. [_contactsBackgroundView setImage:[UIImage imageNamed:@"contacts-placeholder"]];
  27. [_contactsBackgroundView.placeholderTextView setText:NSLocalizedString(@"No results found", nil)];
  28. [self showSearchingUI];
  29. self.tableView.backgroundView = _contactsBackgroundView;
  30. }
  31. - (void)didReceiveMemoryWarning
  32. {
  33. [super didReceiveMemoryWarning];
  34. }
  35. - (void)showSearchingUI
  36. {
  37. [self setContacts:nil withIndexes:nil];
  38. [_contactsBackgroundView.placeholderView setHidden:YES];
  39. [_contactsBackgroundView.loadingView startAnimating];
  40. [_contactsBackgroundView.loadingView setHidden:NO];
  41. }
  42. - (void)hideSearchingUI
  43. {
  44. [_contactsBackgroundView.loadingView stopAnimating];
  45. [_contactsBackgroundView.loadingView setHidden:YES];
  46. }
  47. - (void)setContacts:(NSMutableDictionary *)contacts withIndexes:(NSArray *)indexes
  48. {
  49. _contacts = contacts;
  50. _indexes = indexes;
  51. [self.tableView reloadData];
  52. }
  53. - (void)setSearchResultContacts:(NSMutableDictionary *)contacts withIndexes:(NSArray *)indexes
  54. {
  55. [self hideSearchingUI];
  56. [_contactsBackgroundView.placeholderView setHidden:(contacts.count > 0)];
  57. [self setContacts:contacts withIndexes:indexes];
  58. }
  59. #pragma mark - Table view data source
  60. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  61. return _indexes.count;
  62. }
  63. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  64. NSString *index = [_indexes objectAtIndex:section];
  65. NSArray *contacts = [_contacts objectForKey:index];
  66. return contacts.count;
  67. }
  68. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  69. {
  70. return kContactsTableCellHeight;
  71. }
  72. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  73. {
  74. return [_indexes objectAtIndex:section];
  75. }
  76. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  77. {
  78. NSString *index = [_indexes objectAtIndex:indexPath.section];
  79. NSArray *contacts = [_contacts objectForKey:index];
  80. NCUser *contact = [contacts objectAtIndex:indexPath.row];
  81. ContactsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kContactCellIdentifier forIndexPath:indexPath];
  82. if (!cell) {
  83. cell = [[ContactsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kContactCellIdentifier];
  84. }
  85. cell.labelTitle.text = contact.name;
  86. [cell.contactImage setActorAvatarForId:contact.userId withType:contact.source withDisplayName:contact.name withRoomToken:nil];
  87. return cell;
  88. }
  89. @end