FLEXCollectionContentSection.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // FLEXCollectionContentSection.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 8/28/19.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXCollectionContentSection.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXRuntimeUtility.h"
  11. #import "FLEXSubtitleTableViewCell.h"
  12. #import "FLEXTableView.h"
  13. #import "FLEXObjectExplorerFactory.h"
  14. typedef NS_ENUM(NSUInteger, FLEXCollectionType) {
  15. FLEXUnsupportedCollection,
  16. FLEXOrderedCollection,
  17. FLEXUnorderedCollection,
  18. FLEXKeyedCollection
  19. };
  20. @interface FLEXCollectionContentSection ()
  21. @property (nonatomic, copy) id<FLEXCollection> cachedCollection;
  22. @property (nonatomic, readonly) id<FLEXCollection> collection;
  23. @property (nonatomic, readonly) FLEXCollectionContentFuture collectionFuture;
  24. @property (nonatomic, readonly) FLEXCollectionType collectionType;
  25. @end
  26. @implementation FLEXCollectionContentSection
  27. @synthesize filterText = _filterText;
  28. #pragma mark Initialization
  29. + (instancetype)forObject:(id)object {
  30. return [self forCollection:object];
  31. }
  32. + (id)forCollection:(id<FLEXCollection>)collection {
  33. FLEXCollectionContentSection *section = [self new];
  34. section->_collectionType = [self typeForCollection:collection];
  35. section->_collection = collection;
  36. section.cachedCollection = collection;
  37. return section;
  38. }
  39. + (id)forReusableFuture:(FLEXCollectionContentFuture)collectionFuture {
  40. FLEXCollectionContentSection *section = [self new];
  41. section->_collectionFuture = collectionFuture;
  42. section.cachedCollection = collectionFuture(section);
  43. section->_collectionType = [self typeForCollection:section.cachedCollection];
  44. return section;
  45. }
  46. #pragma mark - Misc
  47. + (FLEXCollectionType)typeForCollection:(id<FLEXCollection>)collection {
  48. // Order matters here, as NSDictionary is keyed but it responds to allObjects
  49. if ([collection respondsToSelector:@selector(objectAtIndex:)]) {
  50. return FLEXOrderedCollection;
  51. }
  52. if ([collection respondsToSelector:@selector(objectForKey:)]) {
  53. return FLEXKeyedCollection;
  54. }
  55. if ([collection respondsToSelector:@selector(allObjects)]) {
  56. return FLEXUnorderedCollection;
  57. }
  58. [NSException raise:NSInvalidArgumentException
  59. format:@"Given collection does not properly conform to FLEXCollection"];
  60. return FLEXUnsupportedCollection;
  61. }
  62. /// Row titles
  63. /// - Ordered: the index
  64. /// - Unordered: the object
  65. /// - Keyed: the key
  66. - (NSString *)titleForRow:(NSInteger)row {
  67. switch (self.collectionType) {
  68. case FLEXOrderedCollection:
  69. if (!self.hideOrderIndexes) {
  70. return @(row).stringValue;
  71. }
  72. // Fall-through
  73. case FLEXUnorderedCollection:
  74. return [self describe:[self objectForRow:row]];
  75. case FLEXKeyedCollection:
  76. return [self describe:self.cachedCollection.allKeys[row]];
  77. case FLEXUnsupportedCollection:
  78. return nil;
  79. }
  80. }
  81. /// Row subtitles
  82. /// - Ordered: the object
  83. /// - Unordered: nothing
  84. /// - Keyed: the value
  85. - (NSString *)subtitleForRow:(NSInteger)row {
  86. switch (self.collectionType) {
  87. case FLEXOrderedCollection:
  88. if (!self.hideOrderIndexes) {
  89. nil;
  90. }
  91. // Fall-through
  92. case FLEXKeyedCollection:
  93. return [self describe:[self objectForRow:row]];
  94. case FLEXUnorderedCollection:
  95. return nil;
  96. case FLEXUnsupportedCollection:
  97. return nil;
  98. }
  99. }
  100. - (NSString *)describe:(id)object {
  101. return [FLEXRuntimeUtility summaryForObject:object];
  102. }
  103. - (id)objectForRow:(NSInteger)row {
  104. switch (self.collectionType) {
  105. case FLEXOrderedCollection:
  106. return self.cachedCollection[row];
  107. case FLEXUnorderedCollection:
  108. return self.cachedCollection.allObjects[row];
  109. case FLEXKeyedCollection:
  110. return self.cachedCollection[self.cachedCollection.allKeys[row]];
  111. case FLEXUnsupportedCollection:
  112. return nil;
  113. }
  114. }
  115. #pragma mark - Overrides
  116. - (NSString *)title {
  117. if (!self.hideSectionTitle) {
  118. if (self.customTitle) {
  119. return self.customTitle;
  120. }
  121. return FLEXPluralString(self.cachedCollection.count, @"Entries", @"Entry");
  122. }
  123. return nil;
  124. }
  125. - (NSInteger)numberOfRows {
  126. return self.cachedCollection.count;
  127. }
  128. - (void)setFilterText:(NSString *)filterText {
  129. super.filterText = filterText;
  130. if (filterText.length) {
  131. BOOL (^matcher)(id, id) = self.customFilter ?: ^BOOL(NSString *query, id obj) {
  132. return [[self describe:obj] localizedCaseInsensitiveContainsString:query];
  133. };
  134. NSPredicate *filter = [NSPredicate predicateWithBlock:^BOOL(id obj, NSDictionary *bindings) {
  135. return matcher(filterText, obj);
  136. }];
  137. id<FLEXMutableCollection> tmp = self.collection.mutableCopy;
  138. [tmp filterUsingPredicate:filter];
  139. self.cachedCollection = tmp;
  140. } else {
  141. self.cachedCollection = self.collection ?: self.collectionFuture(self);
  142. }
  143. }
  144. - (void)reloadData {
  145. if (self.collectionFuture) {
  146. self.cachedCollection = self.collectionFuture(self);
  147. } else {
  148. self.cachedCollection = self.collection.copy;
  149. }
  150. }
  151. - (BOOL)canSelectRow:(NSInteger)row {
  152. return YES;
  153. }
  154. - (UIViewController *)viewControllerToPushForRow:(NSInteger)row {
  155. return [FLEXObjectExplorerFactory explorerViewControllerForObject:[self objectForRow:row]];
  156. }
  157. - (NSString *)reuseIdentifierForRow:(NSInteger)row {
  158. return kFLEXDetailCell;
  159. }
  160. - (void)configureCell:(__kindof FLEXTableViewCell *)cell forRow:(NSInteger)row {
  161. cell.titleLabel.text = [self titleForRow:row];
  162. cell.subtitleLabel.text = [self subtitleForRow:row];
  163. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  164. }
  165. @end
  166. #pragma mark - NSMutableDictionary
  167. @implementation NSMutableDictionary (FLEXMutableCollection)
  168. - (void)filterUsingPredicate:(NSPredicate *)predicate {
  169. id test = ^BOOL(id key, NSUInteger idx, BOOL *stop) {
  170. if ([predicate evaluateWithObject:key]) {
  171. return NO;
  172. }
  173. return ![predicate evaluateWithObject:self[key]];
  174. };
  175. NSArray *keys = self.allKeys;
  176. NSIndexSet *remove = [keys indexesOfObjectsPassingTest:test];
  177. [self removeObjectsForKeys:[keys objectsAtIndexes:remove]];
  178. }
  179. @end