// // FLEXGlobalsSection.m // FLEX // // Created by Tanner Bennett on 7/11/19. // Copyright © 2020 FLEX Team. All rights reserved. // #import "FLEXGlobalsSection.h" #import "NSArray+FLEX.h" #import "UIFont+FLEX.h" @interface FLEXGlobalsSection () /// Filtered rows @property (nonatomic) NSArray *rows; /// Unfiltered rows @property (nonatomic) NSArray *allRows; @end @implementation FLEXGlobalsSection #pragma mark - Initialization + (instancetype)title:(NSString *)title rows:(NSArray *)rows { FLEXGlobalsSection *s = [self new]; s->_title = title; s.allRows = rows; return s; } - (void)setAllRows:(NSArray *)allRows { _allRows = allRows.copy; [self reloadData]; } #pragma mark - Overrides - (NSInteger)numberOfRows { return self.rows.count; } - (void)setFilterText:(NSString *)filterText { super.filterText = filterText; [self reloadData]; } - (void)reloadData { NSString *filterText = self.filterText; if (filterText.length) { self.rows = [self.allRows flex_filtered:^BOOL(FLEXGlobalsEntry *entry, NSUInteger idx) { return [entry.entryNameFuture() localizedCaseInsensitiveContainsString:filterText]; }]; } else { self.rows = self.allRows; } } - (BOOL)canSelectRow:(NSInteger)row { return YES; } - (void (^)(__kindof UIViewController *))didSelectRowAction:(NSInteger)row { return (id)self.rows[row].rowAction; } - (UIViewController *)viewControllerToPushForRow:(NSInteger)row { return self.rows[row].viewControllerFuture ? self.rows[row].viewControllerFuture() : nil; } - (void)configureCell:(__kindof UITableViewCell *)cell forRow:(NSInteger)row { cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.textLabel.font = UIFont.flex_defaultTableCellFont; cell.textLabel.text = self.rows[row].entryNameFuture(); } @end @implementation FLEXGlobalsSection (Subscripting) - (id)objectAtIndexedSubscript:(NSUInteger)idx { return self.rows[idx]; } @end