PickContactsViewController.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 "ThreemaFramework.h"
  21. #import "PickContactsViewController.h"
  22. #import "ContactAndWorkContactTableDataSource.h"
  23. #import "ContactCell.h"
  24. #import "BundleUtil.h"
  25. #import "UserSettings.h"
  26. @interface PickContactsViewController ()
  27. @property ContactAndWorkContactTableDataSource *contactsDatasource;
  28. @property ContactAndWorkContactTableDataSource *searchDatasource;
  29. @property NSMutableSet *selectedContacts;
  30. @property (strong, nonatomic) UIView *statusBarView;
  31. @end
  32. @implementation PickContactsViewController
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. _contactsDatasource = [ContactAndWorkContactTableDataSource contactAndWorkContactTableDataSource];
  36. _contactsDatasource.excludeGatewayContacts = YES;
  37. _contactsDatasource.excludeEchoEcho = YES;
  38. _searchDatasource = [ContactAndWorkContactTableDataSource contactAndWorkContactTableDataSource];
  39. _searchDatasource.excludeGatewayContacts = YES;
  40. _searchDatasource.excludeEchoEcho = YES;
  41. self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
  42. self.searchController.delegate = self;
  43. self.searchController.searchBar.showsScopeBar = NO;
  44. self.searchController.searchBar.scopeButtonTitles = nil;
  45. self.searchController.searchBar.delegate = self;
  46. self.searchController.searchResultsUpdater = self;
  47. self.searchController.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  48. [self.searchController.searchBar sizeToFit];
  49. self.searchController.searchBar.barStyle = UISearchBarStyleMinimal;
  50. self.searchController.dimsBackgroundDuringPresentation = NO;
  51. self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
  52. self.definesPresentationContext = YES;
  53. if (@available(iOS 11.0, *)) {
  54. self.navigationItem.searchController = _searchController;
  55. } else {
  56. [self.view addSubview:self.searchController.searchBar];
  57. self.statusBarView = [[UIView alloc] initWithFrame:[[UIApplication sharedApplication] statusBarFrame]];
  58. self.edgesForExtendedLayout = UIRectEdgeNone;
  59. self.automaticallyAdjustsScrollViewInsets = NO;
  60. self.extendedLayoutIncludesOpaqueBars = NO;
  61. }
  62. [self setupColors];
  63. self.tableView.rowHeight = UITableViewAutomaticDimension;
  64. if (@available(iOS 11.0, *)) {
  65. self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
  66. } else {
  67. self.tableView.estimatedRowHeight = 44.0;
  68. }
  69. }
  70. - (void)setupColors {
  71. [self.view setBackgroundColor:[Colors backgroundLight]];
  72. [Colors updateTableView:self.tableView];
  73. if (@available(iOS 11.0, *)) {
  74. self.searchController.searchBar.barTintColor = [UIColor clearColor];
  75. self.searchController.searchBar.backgroundColor = [UIColor clearColor];
  76. UINavigationBar *navigationBar = self.navigationController.navigationBar;
  77. if (navigationBar) {
  78. navigationBar.barTintColor = [Colors backgroundBaseColor];
  79. }
  80. } else {
  81. self.searchController.searchBar.backgroundColor = [Colors backgroundBaseColor];
  82. UINavigationBar *navigationBar = self.navigationController.navigationBar;
  83. if (navigationBar) {
  84. navigationBar.barTintColor = [Colors backgroundBaseColor];
  85. }
  86. }
  87. [Colors updateSearchBar:_searchController.searchBar];
  88. if (@available(iOS 11.0, *)) {
  89. } else {
  90. [_statusBarView setBackgroundColor:[Colors searchBarStatusBar]];
  91. }
  92. }
  93. -(void)viewWillAppear:(BOOL)animated {
  94. [super viewWillAppear:animated];
  95. self.title = NSLocalizedString(@"send_profileimage_contacts", nil);
  96. _searchController.searchBar.placeholder = NSLocalizedString(@"search", nil);
  97. NSArray *contactIdentities = [UserSettings sharedUserSettings].profilePictureContactList;
  98. if (contactIdentities.count) {
  99. _selectedContacts = [NSMutableSet setWithArray:contactIdentities];
  100. } else {
  101. _selectedContacts = [NSMutableSet set];
  102. }
  103. if (@available(iOS 11.0, *)) {
  104. } else {
  105. self.tableView.contentInset = UIEdgeInsetsMake(44.0, 0.0, 0.0, 0.0);
  106. }
  107. }
  108. - (void)viewWillDisappear:(BOOL)animated {
  109. [super viewWillDisappear:animated];
  110. }
  111. #pragma mark - Table view data source
  112. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  113. if (_searchController.isActive) {
  114. return [_searchDatasource numberOfSectionsInTableView:tableView];
  115. }
  116. return [_contactsDatasource numberOfSectionsInTableView:tableView];
  117. }
  118. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  119. if (_searchController.isActive) {
  120. return [_searchDatasource tableView:tableView numberOfRowsInSection:section];
  121. }
  122. return [_contactsDatasource tableView:tableView numberOfRowsInSection:section];
  123. }
  124. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  125. if (_searchController.isActive) {
  126. return [_searchDatasource sectionIndexTitlesForTableView:tableView];
  127. }
  128. return [_contactsDatasource sectionIndexTitlesForTableView:tableView];
  129. }
  130. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
  131. if (_searchController.isActive) {
  132. return [_searchDatasource tableView:tableView sectionForSectionIndexTitle:title atIndex:index];
  133. }
  134. return [_contactsDatasource tableView:tableView sectionForSectionIndexTitle:title atIndex:index];
  135. }
  136. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  137. if (_searchController.isActive) {
  138. return [_searchDatasource tableView:tableView titleForHeaderInSection:section];
  139. }
  140. return [_contactsDatasource tableView:tableView titleForHeaderInSection:section];
  141. }
  142. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  143. return UITableViewAutomaticDimension;
  144. }
  145. -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
  146. return UITableViewAutomaticDimension;
  147. }
  148. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  149. {
  150. ContactCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ContactCell"];
  151. Contact *contact;
  152. if (_searchController.isActive) {
  153. contact = [_searchDatasource contactAtIndexPath:indexPath];
  154. } else {
  155. contact = [_contactsDatasource contactAtIndexPath:indexPath];
  156. }
  157. cell.contact = contact;
  158. if ([_selectedContacts containsObject:cell.contact.identity]) {
  159. cell.checkmarkView.image = [StyleKit check];
  160. cell.accessibilityTraits = UIAccessibilityTraitSelected;
  161. } else {
  162. cell.checkmarkView.image = [StyleKit uncheck];
  163. cell.accessibilityTraits = 0;
  164. }
  165. return cell;
  166. }
  167. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  168. [Colors updateTableViewCell:cell];
  169. }
  170. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  171. Contact *contact;
  172. if (_searchController.isActive) {
  173. contact = [_searchDatasource contactAtIndexPath:indexPath];
  174. } else {
  175. contact = [_contactsDatasource contactAtIndexPath:indexPath];
  176. }
  177. if ([_selectedContacts containsObject:contact.identity]) {
  178. [_selectedContacts removeObject:contact.identity];
  179. } else {
  180. [_selectedContacts addObject:contact.identity];
  181. }
  182. [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  183. }
  184. #pragma mark - Search controller delegate
  185. - (void)willPresentSearchController:(UISearchController *)searchController {
  186. if (@available(iOS 11.0, *)) {
  187. } else {
  188. self.tableView.contentInset = UIEdgeInsetsMake(64.0, 0.0, 0.0, 0.0);
  189. [self.view addSubview:_statusBarView];
  190. }
  191. }
  192. - (void)willDismissSearchController:(UISearchController *)searchController {
  193. if (@available(iOS 11.0, *)) {
  194. } else {
  195. self.tableView.contentInset = UIEdgeInsetsMake(44.0, 0.0, 0.0, 0.0);
  196. [_statusBarView removeFromSuperview];
  197. }
  198. }
  199. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  200. NSArray *searchWords = [self searchWordsForText:searchController.searchBar.text];
  201. [_searchDatasource filterByWords: searchWords];
  202. [self.tableView reloadData];
  203. }
  204. - (NSArray *)searchWordsForText:(NSString *)text {
  205. NSArray *searchWords = nil;
  206. if (text && [text length] > 0) {
  207. searchWords = [text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  208. }
  209. return searchWords;
  210. }
  211. #pragma mark - Actions
  212. - (IBAction)saveAction:(id)sender {
  213. [self dismissViewControllerAnimated:YES completion:^{
  214. [[UserSettings sharedUserSettings] setProfilePictureContactList:_selectedContacts.allObjects];
  215. }];
  216. }
  217. - (IBAction)cancelAction:(id)sender {
  218. [self dismissViewControllerAnimated:YES completion:nil];
  219. }
  220. @end