FLEXGlobalsSection.m 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // FLEXGlobalsSection.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 7/11/19.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXGlobalsSection.h"
  9. #import "NSArray+Functional.h"
  10. @interface FLEXGlobalsSection ()
  11. /// Filtered rows
  12. @property (nonatomic) NSArray<FLEXGlobalsEntry *> *rows;
  13. /// Unfiltered rows
  14. @property (nonatomic) NSArray<FLEXGlobalsEntry *> *allRows;
  15. @end
  16. @implementation FLEXGlobalsSection
  17. #pragma mark - Initialization
  18. + (instancetype)title:(NSString *)title rows:(NSArray<FLEXGlobalsEntry *> *)rows {
  19. FLEXGlobalsSection *s = [self new];
  20. s->_title = title;
  21. s.allRows = rows;
  22. return s;
  23. }
  24. - (void)setAllRows:(NSArray<FLEXGlobalsEntry *> *)allRows {
  25. _allRows = allRows.copy;
  26. [self reloadData];
  27. }
  28. #pragma mark - Overrides
  29. - (NSInteger)numberOfRows {
  30. return self.rows.count;
  31. }
  32. - (void)setFilterText:(NSString *)filterText {
  33. super.filterText = filterText;
  34. [self reloadData];
  35. }
  36. - (void)reloadData {
  37. NSString *filterText = self.filterText;
  38. if (filterText.length) {
  39. self.rows = [self.allRows flex_filtered:^BOOL(FLEXGlobalsEntry *entry, NSUInteger idx) {
  40. return [entry.entryNameFuture() localizedCaseInsensitiveContainsString:filterText];
  41. }];
  42. } else {
  43. self.rows = self.allRows;
  44. }
  45. }
  46. - (BOOL)canSelectRow:(NSInteger)row {
  47. return YES;
  48. }
  49. - (void (^)(__kindof UIViewController *))didSelectRowAction:(NSInteger)row {
  50. return (id)self.rows[row].rowAction;
  51. }
  52. - (UIViewController *)viewControllerToPushForRow:(NSInteger)row {
  53. return self.rows[row].viewControllerFuture ? self.rows[row].viewControllerFuture() : nil;
  54. }
  55. - (void)configureCell:(__kindof UITableViewCell *)cell forRow:(NSInteger)row {
  56. cell.textLabel.text = self.rows[row].entryNameFuture();
  57. }
  58. @end
  59. @implementation FLEXGlobalsSection (Subscripting)
  60. - (id)objectAtIndexedSubscript:(NSUInteger)idx {
  61. return self.rows[idx];
  62. }
  63. @end