GroupTableDataSource.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 "GroupTableDataSource.h"
  21. #import "EntityManager.h"
  22. #import "ErrorHandler.h"
  23. #import "PickerGroupCell.h"
  24. #ifdef DEBUG
  25. static const DDLogLevel ddLogLevel = DDLogLevelInfo;
  26. #else
  27. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  28. #endif
  29. @interface GroupTableDataSource () <NSFetchedResultsControllerDelegate>
  30. @property NSArray *filteredGroups;
  31. @property NSFetchedResultsController *fetchedResultsController;
  32. @property (nonatomic) NSMutableSet *selectedGroups;
  33. @property EntityManager *entityManager;
  34. @property id<NSFetchedResultsControllerDelegate> fetchedResultsControllerDelegate;
  35. @property BOOL ignoreUpdates;
  36. @end
  37. @implementation GroupTableDataSource
  38. + (instancetype)groupTableDataSource {
  39. return [[GroupTableDataSource alloc] initWithFetchedResultsControllerDelegate:nil];
  40. }
  41. + (instancetype)groupTableDataSourceWithFetchedResultsControllerDelegate:(id<NSFetchedResultsControllerDelegate>)delegate {
  42. return [[GroupTableDataSource alloc] initWithFetchedResultsControllerDelegate:delegate];
  43. }
  44. - (void)dealloc {
  45. _fetchedResultsController.delegate = nil;
  46. _fetchedResultsControllerDelegate = nil;
  47. }
  48. - (instancetype)initWithFetchedResultsControllerDelegate:(id<NSFetchedResultsControllerDelegate>)delegate
  49. {
  50. self = [super init];
  51. if (self) {
  52. _entityManager = [[EntityManager alloc] init];
  53. _selectedGroups = [NSMutableSet set];
  54. if (delegate) {
  55. _fetchedResultsControllerDelegate = delegate;
  56. delegate = self;
  57. }
  58. [self setupFetchedResultsControllerWithDelegate:delegate];
  59. }
  60. return self;
  61. }
  62. - (void)setupFetchedResultsControllerWithDelegate:(id<NSFetchedResultsControllerDelegate>)delegate {
  63. NSFetchedResultsController *fetchedResultsController = [_entityManager.entityFetcher fetchedResultsControllerForGroups];
  64. fetchedResultsController.delegate = delegate;
  65. _fetchedResultsController = fetchedResultsController;
  66. NSError *error = nil;
  67. if (![_fetchedResultsController performFetch:&error]) {
  68. DDLogError(@"Unresolved error %@, %@", error, [error userInfo]);
  69. [ErrorHandler abortWithError: error];
  70. }
  71. }
  72. -(void)filterByWords:(NSArray *)words {
  73. if (words) {
  74. _filteredGroups = [_entityManager.entityFetcher groupConversationsFilteredByWords:words];
  75. } else {
  76. _filteredGroups= nil;
  77. }
  78. }
  79. - (NSSet *)selectedConversations {
  80. NSMutableSet *conversations = [NSMutableSet setWithCapacity:[_selectedGroups count]];
  81. for (GroupProxy *group in _selectedGroups) {
  82. [conversations addObject:group.conversation];
  83. }
  84. return conversations;
  85. }
  86. - (NSArray *)groupsForConversations:(NSArray *)conversations {
  87. NSMutableArray *groups = [NSMutableArray arrayWithCapacity: [conversations count]];
  88. for (Conversation *conversation in conversations) {
  89. GroupProxy *group = [GroupProxy groupProxyForConversation:conversation];
  90. if ([group canSendInGroup]) {
  91. [groups addObject:group];
  92. }
  93. }
  94. return groups;
  95. }
  96. - (void)setIgnoreFRCUpdates:(BOOL)ignoreFRCUpdates {
  97. _ignoreUpdates = ignoreFRCUpdates;
  98. }
  99. - (BOOL)ignoreFRCUpdates {
  100. return _ignoreUpdates;
  101. }
  102. - (BOOL)isFiltered {
  103. return (_filteredGroups != nil);
  104. }
  105. #pragma mark - Table view
  106. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  107. {
  108. return 1;
  109. }
  110. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  111. {
  112. if (_filteredGroups) {
  113. return [_filteredGroups count];
  114. }
  115. id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
  116. return [sectionInfo numberOfObjects];
  117. }
  118. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  119. {
  120. GroupProxy *group = [self groupAtIndexPath:indexPath];
  121. PickerGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PickerGroupCell"];
  122. cell.group = group;
  123. return cell;
  124. }
  125. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  126. return nil;
  127. }
  128. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  129. return nil;
  130. }
  131. - (void)selectedCellAtIndexPath:(NSIndexPath *)indexPath selected:(BOOL)selected {
  132. GroupProxy *group = [self groupAtIndexPath:indexPath];
  133. if (selected) {
  134. if (![_selectedGroups containsObject:group]) {
  135. [_selectedGroups addObject:group];
  136. }
  137. } else {
  138. [_selectedGroups enumerateObjectsUsingBlock:^(GroupProxy *tmpGroup, BOOL * _Nonnull stop) {
  139. if ([group.groupId isEqualToData:tmpGroup.groupId]) {
  140. [_selectedGroups removeObject:tmpGroup];
  141. *stop = YES;
  142. }
  143. }];
  144. }
  145. }
  146. - (GroupProxy *)groupAtIndexPath:(NSIndexPath *)indexPath {
  147. Conversation *conversation;
  148. if (_filteredGroups) {
  149. conversation = [_filteredGroups objectAtIndex:indexPath.row];
  150. } else {
  151. conversation = [_fetchedResultsController objectAtIndexPath:indexPath];
  152. }
  153. return [GroupProxy groupProxyForConversation:conversation];
  154. }
  155. - (NSIndexPath *)indexPathForObject:(id)object {
  156. return [_fetchedResultsController indexPathForObject:((GroupProxy *)object).conversation];
  157. }
  158. #pragma mark - Fetched results controller
  159. - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
  160. {
  161. if (_ignoreUpdates || _filteredGroups != nil) {
  162. return;
  163. }
  164. [_fetchedResultsControllerDelegate controllerWillChangeContent:controller];
  165. }
  166. - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
  167. atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
  168. {
  169. if (_ignoreUpdates || _filteredGroups != nil) {
  170. return;
  171. }
  172. [_fetchedResultsControllerDelegate controller:controller didChangeSection:sectionInfo atIndex:sectionIndex forChangeType:type];
  173. }
  174. - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
  175. atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  176. newIndexPath:(NSIndexPath *)newIndexPath
  177. {
  178. if (_ignoreUpdates || _filteredGroups != nil) {
  179. return;
  180. }
  181. if ([anObject isKindOfClass:[Conversation class]]) {
  182. anObject = [GroupProxy groupProxyForConversation:anObject];
  183. }
  184. [_fetchedResultsControllerDelegate controller:controller didChangeObject:anObject atIndexPath:indexPath forChangeType:type newIndexPath:newIndexPath];
  185. }
  186. - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
  187. {
  188. if (_ignoreUpdates || _filteredGroups != nil) {
  189. return;
  190. }
  191. [_fetchedResultsControllerDelegate controllerDidChangeContent:controller];
  192. }
  193. @end