FLEXTableViewController.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // FLEXTableViewController.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 7/5/19.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXTableViewController.h"
  9. @interface Block : NSObject
  10. - (void)invoke;
  11. @end
  12. CGFloat const kFLEXDebounceInstant = 0.f;
  13. CGFloat const kFLEXDebounceFast = 0.05;
  14. CGFloat const kFLEXDebounceForAsyncSearch = 0.15;
  15. CGFloat const kFLEXDebounceForExpensiveIO = 0.5;
  16. @interface FLEXTableViewController ()
  17. @property (nonatomic) NSTimer *debounceTimer;
  18. @end
  19. @implementation FLEXTableViewController
  20. #pragma mark - Public
  21. - (id)init {
  22. #if FLEX_AT_LEAST_IOS13_SDK
  23. if (@available(iOS 13.0, *)) {
  24. self = [self initWithStyle:UITableViewStyleInsetGrouped];
  25. } else {
  26. self = [self initWithStyle:UITableViewStyleGrouped];
  27. }
  28. #else
  29. self = [self initWithStyle:UITableViewStyleGrouped];
  30. #endif
  31. return self;
  32. }
  33. - (id)initWithStyle:(UITableViewStyle)style {
  34. self = [super initWithStyle:style];
  35. if (self) {
  36. self.searchBarDebounceInterval = kFLEXDebounceFast;
  37. }
  38. return self;
  39. }
  40. - (void)setShowsSearchBar:(BOOL)showsSearchBar {
  41. if (_showsSearchBar == showsSearchBar) return;
  42. _showsSearchBar = showsSearchBar;
  43. UIViewController *results = self.searchResultsController;
  44. self.searchController = [[UISearchController alloc] initWithSearchResultsController:results];
  45. self.searchController.searchBar.placeholder = @"Filter";
  46. self.searchController.searchResultsUpdater = (id)self;
  47. self.searchController.delegate = (id)self;
  48. self.searchController.dimsBackgroundDuringPresentation = NO;
  49. self.searchController.hidesNavigationBarDuringPresentation = NO;
  50. if (@available(iOS 11.0, *)) {
  51. self.navigationItem.searchController = self.searchController;
  52. } else {
  53. self.tableView.tableHeaderView = self.searchController.searchBar;
  54. }
  55. }
  56. - (NSInteger)selectedScope {
  57. return self.searchController.searchBar.selectedScopeButtonIndex;
  58. }
  59. - (NSString *)searchText {
  60. return self.searchController.searchBar.text;
  61. }
  62. - (void)setAutomaticallyShowsSearchBarCancelButton:(BOOL)autoShowCancel {
  63. #if FLEX_AT_LEAST_IOS13_SDK
  64. if (@available(iOS 13, *)) {
  65. self.searchController.automaticallyShowsCancelButton = autoShowCancel;
  66. } else {
  67. _automaticallyShowsSearchBarCancelButton = autoShowCancel;
  68. }
  69. #else
  70. _automaticallyShowsSearchBarCancelButton = autoShowCancel;
  71. #endif
  72. }
  73. - (void)updateSearchResults:(NSString *)newText { }
  74. - (void)onBackgroundQueue:(NSArray *(^)())backgroundBlock thenOnMainQueue:(void(^)(NSArray *))mainBlock {
  75. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  76. NSArray *items = backgroundBlock();
  77. dispatch_async(dispatch_get_main_queue(), ^{
  78. mainBlock(items);
  79. });
  80. });
  81. }
  82. #pragma mark - View Controller Lifecycle
  83. - (void)viewDidLoad {
  84. [super viewDidLoad];
  85. self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
  86. }
  87. - (void)viewWillAppear:(BOOL)animated {
  88. [super viewWillAppear:animated];
  89. // Make the search bar re-appear instead of hiding
  90. if (@available(iOS 11.0, *)) {
  91. self.navigationItem.hidesSearchBarWhenScrolling = NO;
  92. }
  93. }
  94. - (void)viewDidAppear:(BOOL)animated {
  95. [super viewDidAppear:animated];
  96. // Allow scrolling to collapse the search bar,
  97. // only if we don't want it pinned
  98. if (@available(iOS 11.0, *)) {
  99. self.navigationItem.hidesSearchBarWhenScrolling = !self.pinSearchBar;
  100. }
  101. }
  102. #pragma mark - Private
  103. - (void)debounce:(void(^)())block {
  104. [self.debounceTimer invalidate];
  105. self.debounceTimer = [NSTimer
  106. scheduledTimerWithTimeInterval:self.searchBarDebounceInterval
  107. target:block
  108. selector:@selector(invoke)
  109. userInfo:nil
  110. repeats:NO
  111. ];
  112. }
  113. #pragma mark - Search Bar
  114. #pragma mark UISearchResultsUpdating
  115. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
  116. {
  117. [self.debounceTimer invalidate];
  118. NSString *text = searchController.searchBar.text;
  119. // Only debounce if we want to, and if we have a non-empty string
  120. // Empty string events are sent instantly
  121. if (text.length && self.searchBarDebounceInterval > kFLEXDebounceInstant) {
  122. [self debounce:^{
  123. [self updateSearchResults:text];
  124. }];
  125. } else {
  126. [self updateSearchResults:text];
  127. }
  128. }
  129. #pragma mark UISearchControllerDelegate
  130. - (void)willPresentSearchController:(UISearchController *)searchController {
  131. if (self.automaticallyShowsSearchBarCancelButton) {
  132. [searchController.searchBar setShowsCancelButton:YES animated:YES];
  133. }
  134. }
  135. - (void)willDismissSearchController:(UISearchController *)searchController {
  136. if (self.automaticallyShowsSearchBarCancelButton) {
  137. [searchController.searchBar setShowsCancelButton:NO animated:YES];
  138. }
  139. }
  140. @end