FLEXMutableListSection.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // FLEXMutableListSection.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 3/9/20.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXMutableListSection.h"
  9. @interface FLEXMutableListSection ()
  10. @property (nonatomic, readonly) FLEXMutableListCellForElement configureCell;
  11. @end
  12. @implementation FLEXMutableListSection
  13. @synthesize cellRegistrationMapping = _cellRegistrationMapping;
  14. #pragma mark - Initialization
  15. + (instancetype)list:(NSArray *)list
  16. cellConfiguration:(FLEXMutableListCellForElement)cellConfig
  17. filterMatcher:(BOOL(^)(NSString *, id))filterBlock {
  18. return [[self alloc] initWithList:list configurationBlock:cellConfig filterMatcher:filterBlock];
  19. }
  20. - (id)initWithList:(NSArray *)list
  21. configurationBlock:(FLEXMutableListCellForElement)cellConfig
  22. filterMatcher:(BOOL(^)(NSString *, id))filterBlock {
  23. self = [super init];
  24. if (self) {
  25. _configureCell = cellConfig;
  26. self.list = list.mutableCopy;
  27. self.customFilter = filterBlock;
  28. self.hideSectionTitle = YES;
  29. }
  30. return self;
  31. }
  32. #pragma mark - Public
  33. - (NSArray *)list {
  34. return (id)_collection;
  35. }
  36. - (void)setList:(NSMutableArray *)list {
  37. NSParameterAssert(list);
  38. _collection = (id)list;
  39. [self reloadData];
  40. }
  41. - (NSArray *)filteredList {
  42. return (id)_cachedCollection;
  43. }
  44. - (void)mutate:(void (^)(NSMutableArray *))block {
  45. block((NSMutableArray *)_collection);
  46. [self reloadData];
  47. }
  48. #pragma mark - Overrides
  49. - (void)setCustomTitle:(NSString *)customTitle {
  50. super.customTitle = customTitle;
  51. self.hideSectionTitle = customTitle == nil;
  52. }
  53. - (BOOL)canSelectRow:(NSInteger)row {
  54. return self.selectionHandler != nil;
  55. }
  56. - (UIViewController *)viewControllerToPushForRow:(NSInteger)row {
  57. return nil;
  58. }
  59. - (void (^)(__kindof UIViewController *))didSelectRowAction:(NSInteger)row {
  60. if (self.selectionHandler) {
  61. __weak __typeof(self) weakSelf = self;
  62. return ^(UIViewController *host) {
  63. __strong __typeof(self) strongSelf = weakSelf;
  64. if (strongSelf) {
  65. strongSelf.selectionHandler(host, strongSelf.filteredList[row]);
  66. }
  67. };
  68. }
  69. return nil;
  70. }
  71. - (void)configureCell:(__kindof UITableViewCell *)cell forRow:(NSInteger)row {
  72. self.configureCell(cell, self.filteredList[row], row);
  73. }
  74. - (NSString *)reuseIdentifierForRow:(NSInteger)row {
  75. if (self.cellRegistrationMapping.count) {
  76. return self.cellRegistrationMapping.allKeys.firstObject;
  77. }
  78. return [super reuseIdentifierForRow:row];
  79. }
  80. - (void)setCellRegistrationMapping:(NSDictionary<NSString *,Class> *)cellRegistrationMapping {
  81. NSParameterAssert(cellRegistrationMapping.count <= 1);
  82. _cellRegistrationMapping = cellRegistrationMapping;
  83. }
  84. @end