FLEXTableViewController.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 (@available(iOS 13.0, *)) {
  23. self = [self initWithStyle:UITableViewStyleInsetGrouped];
  24. } else {
  25. self = [self initWithStyle:UITableViewStyleGrouped];
  26. }
  27. return self;
  28. }
  29. - (id)initWithStyle:(UITableViewStyle)style {
  30. self = [super initWithStyle:style];
  31. if (self) {
  32. self.searchBarDebounceInterval = kFLEXDebounceFast;
  33. }
  34. return self;
  35. }
  36. - (void)setShowsSearchBar:(BOOL)showsSearchBar {
  37. if (_showsSearchBar == showsSearchBar) return;
  38. _showsSearchBar = showsSearchBar;
  39. UIViewController *results = self.searchResultsController;
  40. self.searchController = [[UISearchController alloc] initWithSearchResultsController:results];
  41. self.searchController.searchBar.placeholder = @"Filter";
  42. self.searchController.searchResultsUpdater = (id)self;
  43. self.searchController.delegate = (id)self;
  44. self.searchController.dimsBackgroundDuringPresentation = NO;
  45. self.searchController.hidesNavigationBarDuringPresentation = NO;
  46. if (@available(iOS 11.0, *)) {
  47. self.navigationItem.searchController = self.searchController;
  48. } else {
  49. self.tableView.tableHeaderView = self.searchController.searchBar;
  50. }
  51. }
  52. - (NSInteger)selectedScope {
  53. return self.searchController.searchBar.selectedScopeButtonIndex;
  54. }
  55. - (NSString *)searchText {
  56. return self.searchController.searchBar.text;
  57. }
  58. - (void)setAutomaticallyShowsSearchBarCancelButton:(BOOL)autoShowCancel {
  59. if (@available(iOS 13, *)) {
  60. self.searchController.automaticallyShowsCancelButton = autoShowCancel;
  61. } else {
  62. _automaticallyShowsSearchBarCancelButton = autoShowCancel;
  63. }
  64. }
  65. - (void)updateSearchResults:(NSString *)newText { }
  66. - (void)onBackgroundQueue:(NSArray *(^)())backgroundBlock thenOnMainQueue:(void(^)(NSArray *))mainBlock {
  67. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  68. NSArray *items = backgroundBlock();
  69. dispatch_async(dispatch_get_main_queue(), ^{
  70. mainBlock(items);
  71. });
  72. });
  73. }
  74. #pragma mark - View Controller Lifecycle
  75. - (void)viewDidLoad {
  76. [super viewDidLoad];
  77. self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
  78. }
  79. - (void)viewWillAppear:(BOOL)animated {
  80. [super viewWillAppear:animated];
  81. // Make the search bar re-appear instead of hiding
  82. if (@available(iOS 11.0, *)) {
  83. self.navigationItem.hidesSearchBarWhenScrolling = NO;
  84. }
  85. }
  86. - (void)viewDidAppear:(BOOL)animated {
  87. [super viewDidAppear:animated];
  88. // Allow scrolling to collapse the search bar,
  89. // only if we don't want it pinned
  90. if (@available(iOS 11.0, *)) {
  91. self.navigationItem.hidesSearchBarWhenScrolling = !self.pinSearchBar;
  92. }
  93. }
  94. #pragma mark - Private
  95. - (void)debounce:(void(^)())block {
  96. [self.debounceTimer invalidate];
  97. self.debounceTimer = [NSTimer
  98. scheduledTimerWithTimeInterval:self.searchBarDebounceInterval
  99. target:block
  100. selector:@selector(invoke)
  101. userInfo:nil
  102. repeats:NO
  103. ];
  104. }
  105. #pragma mark - Search Bar
  106. #pragma mark UISearchResultsUpdating
  107. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
  108. {
  109. [self.debounceTimer invalidate];
  110. NSString *text = searchController.searchBar.text;
  111. // Only debounce if we want to, and if we have a non-empty string
  112. // Empty string events are sent instantly
  113. if (text.length && self.searchBarDebounceInterval > kFLEXDebounceInstant) {
  114. [self debounce:^{
  115. [self updateSearchResults:text];
  116. }];
  117. } else {
  118. [self updateSearchResults:text];
  119. }
  120. }
  121. #pragma mark UISearchControllerDelegate
  122. - (void)willPresentSearchController:(UISearchController *)searchController {
  123. if (self.automaticallyShowsSearchBarCancelButton) {
  124. [searchController.searchBar setShowsCancelButton:YES animated:YES];
  125. }
  126. }
  127. - (void)willDismissSearchController:(UISearchController *)searchController {
  128. if (self.automaticallyShowsSearchBarCancelButton) {
  129. [searchController.searchBar setShowsCancelButton:NO animated:YES];
  130. }
  131. }
  132. @end