ContactAndWorkContactTableDataSource.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 "ContactAndWorkContactTableDataSource.h"
  21. #import <CoreData/CoreData.h>
  22. #import "EntityManager.h"
  23. #import "ErrorHandler.h"
  24. #import "PickerContactCell.h"
  25. #import "UserSettings.h"
  26. #ifdef DEBUG
  27. static const DDLogLevel ddLogLevel = DDLogLevelInfo;
  28. #else
  29. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  30. #endif
  31. @interface ContactAndWorkContactTableDataSource () <NSFetchedResultsControllerDelegate>
  32. @property NSArray *filteredContacts;
  33. @property NSFetchedResultsController *fetchedResultsController;
  34. @property NSFetchedResultsController *gatewayFetchedResultsController;
  35. @property (nonatomic) NSMutableSet *selectedContacts;
  36. @property EntityManager *entityManager;
  37. @property id<NSFetchedResultsControllerDelegate> fetchedResultsControllerDelegate;
  38. @property BOOL ignoreUpdates;
  39. @end
  40. @implementation ContactAndWorkContactTableDataSource
  41. + (instancetype)contactAndWorkContactTableDataSource {
  42. return [[ContactAndWorkContactTableDataSource alloc] initWithFetchedResultsControllerDelegate:nil members:nil];
  43. }
  44. + (instancetype)contactAndWorkContactTableDataSourceWithMembers:(NSMutableSet *)members {
  45. return [[ContactAndWorkContactTableDataSource alloc] initWithFetchedResultsControllerDelegate:nil members:members];
  46. }
  47. + (instancetype)contactAndWorkContactTableDataSourceWithFetchedResultsControllerDelegate:(id<NSFetchedResultsControllerDelegate>)delegate members:(NSMutableSet *)members {
  48. return [[ContactAndWorkContactTableDataSource alloc] initWithFetchedResultsControllerDelegate:delegate members:members];
  49. }
  50. - (void)dealloc {
  51. _fetchedResultsController.delegate = nil;
  52. _gatewayFetchedResultsController.delegate = nil;
  53. }
  54. - (instancetype)initWithFetchedResultsControllerDelegate:(id<NSFetchedResultsControllerDelegate>)delegate members:(NSMutableSet *)members {
  55. self = [super init];
  56. if (self) {
  57. _entityManager = [[EntityManager alloc] init];
  58. if (members != nil) {
  59. _selectedContacts = members;
  60. } else {
  61. _selectedContacts = [NSMutableSet set];
  62. }
  63. if (delegate) {
  64. _fetchedResultsControllerDelegate = delegate;
  65. delegate = self;
  66. }
  67. [self setupFetchedResultsControllerWithDelegate:delegate];
  68. // include gateway contacts by default
  69. [self loadGatewayContacts];
  70. }
  71. return self;
  72. }
  73. - (void)setExcludeGatewayContacts:(BOOL)excludeGatewayContacts {
  74. _excludeGatewayContacts = excludeGatewayContacts;
  75. [self loadGatewayContacts];
  76. }
  77. - (void)setExcludeEchoEcho:(BOOL)excludeEchoEcho {
  78. _excludeEchoEcho = excludeEchoEcho;
  79. if (_excludeEchoEcho && _excludeGatewayContacts) {
  80. NSFetchedResultsController *fetchedResultsController = [_entityManager.entityFetcher fetchedResultsControllerForContactTypes:ContactsNoGatewayNoEchoecho list:ContactListContactsAndWork members:_selectedContacts];
  81. fetchedResultsController.delegate = _fetchedResultsController.delegate;
  82. _fetchedResultsController = fetchedResultsController;
  83. NSError *error = nil;
  84. if (![_fetchedResultsController performFetch:&error]) {
  85. DDLogError(@"Unresolved error %@, %@", error, [error userInfo]);
  86. [ErrorHandler abortWithError: error];
  87. }
  88. } else if (_excludeEchoEcho) {
  89. NSFetchedResultsController *fetchedResultsController = [_entityManager.entityFetcher fetchedResultsControllerForContactTypes:ContactsNoEchoEcho list:ContactListContactsAndWork members:_selectedContacts];
  90. fetchedResultsController.delegate = _fetchedResultsController.delegate;
  91. _fetchedResultsController = fetchedResultsController;
  92. NSError *error = nil;
  93. if (![_fetchedResultsController performFetch:&error]) {
  94. DDLogError(@"Unresolved error %@, %@", error, [error userInfo]);
  95. [ErrorHandler abortWithError: error];
  96. }
  97. }
  98. }
  99. - (void)loadGatewayContacts {
  100. if (_excludeGatewayContacts == NO) {
  101. NSFetchedResultsController *fetchedResultsController = [_entityManager.entityFetcher fetchedResultsControllerForContactTypes:ContactsGatewayOnly list:ContactListContactsAndWork members:nil];
  102. fetchedResultsController.delegate = self;
  103. _gatewayFetchedResultsController = fetchedResultsController;
  104. NSError *error = nil;
  105. if (![_gatewayFetchedResultsController performFetch:&error]) {
  106. DDLogError(@"Unresolved error %@, %@", error, [error userInfo]);
  107. [ErrorHandler abortWithError: error];
  108. }
  109. } else {
  110. _gatewayFetchedResultsController.delegate = nil;
  111. _gatewayFetchedResultsController = nil;
  112. }
  113. }
  114. - (void)setupFetchedResultsControllerWithDelegate:(id<NSFetchedResultsControllerDelegate>)delegate {
  115. NSFetchedResultsController *fetchedResultsController = [_entityManager.entityFetcher fetchedResultsControllerForContactTypes:ContactsNoGateway list:ContactListContacts members:_selectedContacts];
  116. fetchedResultsController.delegate = delegate;
  117. _fetchedResultsController = fetchedResultsController;
  118. NSError *error = nil;
  119. if (![_fetchedResultsController performFetch:&error]) {
  120. DDLogError(@"Unresolved error %@, %@", error, [error userInfo]);
  121. [ErrorHandler abortWithError: error];
  122. }
  123. }
  124. - (Contact *)contactAtIndexPath:(NSIndexPath *)indexPath {
  125. if (_filteredContacts) {
  126. return [_filteredContacts objectAtIndex:indexPath.row];
  127. }
  128. if (indexPath.section == self.fetchedResultsController.sections.count) {
  129. NSIndexPath *gatewayIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
  130. return [_gatewayFetchedResultsController objectAtIndexPath:gatewayIndexPath];
  131. }
  132. return [self.fetchedResultsController objectAtIndexPath:indexPath];
  133. }
  134. - (NSIndexPath *)indexPathForObject:(id)object {
  135. return [_fetchedResultsController indexPathForObject:object];
  136. }
  137. - (NSSet *)getSelectedContacts {
  138. return [NSSet setWithSet:_selectedContacts];
  139. }
  140. - (void)updateSelectedContacts:(NSSet *)contacts {
  141. _selectedContacts = [NSMutableSet setWithSet:contacts];
  142. }
  143. - (void)refreshContactSortIndices {
  144. for (Contact *contact in _fetchedResultsController.fetchedObjects) {
  145. [contact updateSortInitial];
  146. }
  147. for (Contact *contact in _gatewayFetchedResultsController.fetchedObjects) {
  148. [contact updateSortInitial];
  149. }
  150. }
  151. - (NSUInteger)countOfContacts {
  152. if (_excludeGatewayContacts) {
  153. return _fetchedResultsController.fetchedObjects.count;
  154. }
  155. return _fetchedResultsController.fetchedObjects.count + _gatewayFetchedResultsController.fetchedObjects.count;
  156. }
  157. #pragma mark - ContactGroupDataSource
  158. -(void)filterByWords:(NSArray *)words {
  159. if (words) {
  160. ContactTypes type = ContactsAll;
  161. if (_excludeEchoEcho && _excludeGatewayContacts) {
  162. type = ContactsNoGatewayNoEchoecho;
  163. }
  164. else if (_excludeGatewayContacts) {
  165. type = ContactsNoGateway;
  166. }
  167. else if (_excludeEchoEcho) {
  168. type = ContactsNoEchoEcho;
  169. }
  170. _filteredContacts = [_entityManager.entityFetcher contactsFilteredByWords:words forContactTypes:type list:ContactListContactsAndWork members:_selectedContacts];
  171. } else {
  172. _filteredContacts = nil;
  173. }
  174. }
  175. - (NSSet *)selectedConversations {
  176. NSMutableSet *conversations = [NSMutableSet setWithCapacity:[_selectedContacts count]];
  177. for (Contact *contact in _selectedContacts) {
  178. __block Conversation *conversation = [_entityManager.entityFetcher conversationForContact:contact];
  179. if (conversation == nil) {
  180. // create & immediately save
  181. [_entityManager performSyncBlockAndSafe:^{
  182. conversation = [_entityManager.entityCreator conversation];
  183. conversation.contact = contact;
  184. }];
  185. }
  186. [conversations addObject:conversation];
  187. }
  188. return conversations;
  189. }
  190. - (void)selectedCellAtIndexPath:(NSIndexPath *)indexPath selected:(BOOL)selected {
  191. Contact *contact = [self contactAtIndexPath:indexPath];
  192. if (selected) {
  193. if (![_selectedContacts containsObject:contact]) {
  194. [_selectedContacts addObject:contact];
  195. }
  196. } else {
  197. if ([_selectedContacts containsObject:contact]) {
  198. [_selectedContacts removeObject:contact];
  199. }
  200. }
  201. }
  202. - (void)setIgnoreFRCUpdates:(BOOL)ignoreFRCUpdates {
  203. _ignoreUpdates = ignoreFRCUpdates;
  204. }
  205. - (BOOL)ignoreFRCUpdates {
  206. return _ignoreUpdates;
  207. }
  208. - (BOOL)isFiltered {
  209. return (_filteredContacts != nil);
  210. }
  211. #pragma mark - Table view
  212. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  213. {
  214. if (_filteredContacts) {
  215. return 1;
  216. }
  217. NSInteger count = [[self.fetchedResultsController sections] count];
  218. if (_gatewayFetchedResultsController.sections.count > 0) {
  219. count++;
  220. }
  221. return count;
  222. }
  223. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  224. {
  225. if (_filteredContacts) {
  226. return _filteredContacts.count;
  227. }
  228. NSArray *frcSections = [self.fetchedResultsController sections];
  229. if (section >= frcSections.count) {
  230. NSArray *gatewaySections = [_gatewayFetchedResultsController sections];
  231. if (gatewaySections.count > 0) {
  232. id <NSFetchedResultsSectionInfo> sectionInfo = gatewaySections[0];
  233. return [sectionInfo numberOfObjects];
  234. } else {
  235. return 0;
  236. }
  237. }
  238. id <NSFetchedResultsSectionInfo> sectionInfo = frcSections[section];
  239. return [sectionInfo numberOfObjects];
  240. }
  241. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  242. if (_filteredContacts) {
  243. return nil;
  244. }
  245. NSMutableArray *sectionTitles = [NSMutableArray arrayWithArray:[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]];
  246. [sectionTitles addObject:@"*"];
  247. return sectionTitles;
  248. }
  249. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
  250. if (_filteredContacts) {
  251. return 0;
  252. }
  253. if ([title isEqualToString:@"*"]) {
  254. return self.fetchedResultsController.sections.count;
  255. }
  256. int frcIndex = 0;
  257. for (id<NSFetchedResultsSectionInfo> section in self.fetchedResultsController.sections) {
  258. if ([section.name intValue] <= index) {
  259. frcIndex++;
  260. } else {
  261. break;
  262. }
  263. }
  264. frcIndex--;
  265. if (frcIndex < 0) {
  266. frcIndex = 0;
  267. }
  268. return frcIndex;
  269. }
  270. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  271. if (_filteredContacts) {
  272. return nil;
  273. }
  274. if (section == self.fetchedResultsController.sections.count) {
  275. return @"*";
  276. }
  277. id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
  278. /* the section "name" of the FRC is actually an index into the UILocalizedIndexedCollation sectionIndexTitles */
  279. int sitIdx = [[sectionInfo name] intValue];
  280. NSArray *sectionIndexTitles = [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
  281. if (sitIdx >= 0 && sitIdx < [sectionIndexTitles count]) {
  282. return [sectionIndexTitles objectAtIndex:sitIdx];
  283. } else {
  284. return @" ";
  285. }
  286. }
  287. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  288. {
  289. Contact *contact = [self contactAtIndexPath:indexPath];
  290. PickerContactCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PickerContactCell"];
  291. cell.contact = contact;
  292. return cell;
  293. }
  294. #pragma mark - Fetched results controller
  295. - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
  296. {
  297. if (_ignoreUpdates || _filteredContacts != nil) {
  298. return;
  299. }
  300. [_fetchedResultsControllerDelegate controllerWillChangeContent:controller];
  301. }
  302. - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
  303. atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
  304. {
  305. if (_ignoreUpdates || _filteredContacts != nil) {
  306. return;
  307. }
  308. if (controller == _fetchedResultsController) {
  309. [_fetchedResultsControllerDelegate controller:controller didChangeSection:sectionInfo atIndex:sectionIndex forChangeType:type];
  310. } else {
  311. ;//ignore
  312. }
  313. }
  314. - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
  315. atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  316. newIndexPath:(NSIndexPath *)newIndexPath
  317. {
  318. if (_ignoreUpdates || _filteredContacts != nil) {
  319. return;
  320. }
  321. if (controller == _fetchedResultsController) {
  322. [_fetchedResultsControllerDelegate controller:controller didChangeObject:anObject atIndexPath:indexPath forChangeType:type newIndexPath:newIndexPath];
  323. } else {
  324. ;//ignore
  325. }
  326. }
  327. - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
  328. {
  329. if (_ignoreUpdates || _filteredContacts != nil) {
  330. return;
  331. }
  332. [_fetchedResultsControllerDelegate controllerDidChangeContent:controller];
  333. }
  334. @end