RecentTableDataSource.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "RecentTableDataSource.h"
  21. #import "EntityManager.h"
  22. #import "ErrorHandler.h"
  23. #import "PickerContactCell.h"
  24. #import "PickerGroupCell.h"
  25. #ifdef DEBUG
  26. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  27. #else
  28. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  29. #endif
  30. @interface RecentTableDataSource ()
  31. @property NSFetchedResultsController *fetchedResultsController;
  32. @property NSMutableSet *selectedConversations;
  33. @end
  34. @implementation RecentTableDataSource
  35. + (instancetype)recentTableDataSource {
  36. return [[RecentTableDataSource alloc] init];
  37. }
  38. - (instancetype)init
  39. {
  40. self = [super init];
  41. if (self) {
  42. [self initFetchedResultsController];
  43. _selectedConversations = [NSMutableSet set];
  44. }
  45. return self;
  46. }
  47. - (void)initFetchedResultsController
  48. {
  49. EntityManager *entityManager = [[EntityManager alloc] init];
  50. _fetchedResultsController = [entityManager.entityFetcher fetchedResultsControllerForConversations];
  51. NSError *error = nil;
  52. if (![_fetchedResultsController performFetch:&error]) {
  53. DDLogError(@"Unresolved error %@, %@", error, [error userInfo]);
  54. [ErrorHandler abortWithError: error];
  55. }
  56. }
  57. - (void)selectedCellAtIndexPath:(NSIndexPath *)indexPath selected:(BOOL)selected {
  58. Conversation *conversation = [_fetchedResultsController objectAtIndexPath:indexPath];
  59. if (selected) {
  60. if (![_selectedConversations containsObject:conversation]) {
  61. [_selectedConversations addObject:conversation];
  62. }
  63. } else {
  64. if ([_selectedConversations containsObject:conversation]) {
  65. [_selectedConversations removeObject:conversation];
  66. }
  67. }
  68. }
  69. #pragma mark - ContactGroupDataSource
  70. - (void)filterByWords:(NSArray *)words {
  71. ;// nope
  72. }
  73. #pragma mark - Table view
  74. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  75. {
  76. return [[_fetchedResultsController sections] count];
  77. }
  78. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  79. {
  80. id <NSFetchedResultsSectionInfo> sectionInfo = [_fetchedResultsController sections][section];
  81. return [sectionInfo numberOfObjects];
  82. }
  83. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  84. {
  85. Conversation *conversation = [_fetchedResultsController objectAtIndexPath:indexPath];
  86. if ([conversation isGroup]) {
  87. GroupProxy *group = [GroupProxy groupProxyForConversation:conversation];
  88. PickerGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PickerGroupCell"];
  89. cell.group = group;
  90. return cell;
  91. } else {
  92. PickerContactCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PickerContactCell"];
  93. cell.contact = conversation.contact;
  94. return cell;
  95. }
  96. }
  97. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  98. Conversation *conversation = [_fetchedResultsController objectAtIndexPath:indexPath];
  99. NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:
  100. conversation, kKeyConversation,
  101. nil];
  102. [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationShowConversation object:nil
  103. userInfo:info];
  104. }
  105. @end