FLEXGlobalsSection.m 2.0 KB

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