FLEXTableViewController.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #import "FLEXScopeCarousel.h"
  10. #import "FLEXTableView.h"
  11. #import "FLEXUtility.h"
  12. #import <objc/runtime.h>
  13. @interface Block : NSObject
  14. - (void)invoke;
  15. @end
  16. CGFloat const kFLEXDebounceInstant = 0.f;
  17. CGFloat const kFLEXDebounceFast = 0.05;
  18. CGFloat const kFLEXDebounceForAsyncSearch = 0.15;
  19. CGFloat const kFLEXDebounceForExpensiveIO = 0.5;
  20. @interface FLEXTableViewController ()
  21. @property (nonatomic) NSTimer *debounceTimer;
  22. @property (nonatomic) BOOL didInitiallyRevealSearchBar;
  23. @end
  24. @implementation FLEXTableViewController
  25. @synthesize automaticallyShowsSearchBarCancelButton = _automaticallyShowsSearchBarCancelButton;
  26. #pragma mark - Public
  27. - (id)init {
  28. #if FLEX_AT_LEAST_IOS13_SDK
  29. if (@available(iOS 13.0, *)) {
  30. self = [self initWithStyle:UITableViewStyleInsetGrouped];
  31. } else {
  32. self = [self initWithStyle:UITableViewStyleGrouped];
  33. }
  34. #else
  35. self = [self initWithStyle:UITableViewStyleGrouped];
  36. #endif
  37. return self;
  38. }
  39. - (id)initWithStyle:(UITableViewStyle)style {
  40. self = [super initWithStyle:style];
  41. if (self) {
  42. _searchBarDebounceInterval = kFLEXDebounceFast;
  43. _showSearchBarInitially = YES;
  44. }
  45. return self;
  46. }
  47. - (void)setShowsSearchBar:(BOOL)showsSearchBar {
  48. if (_showsSearchBar == showsSearchBar) return;
  49. _showsSearchBar = showsSearchBar;
  50. UIViewController *results = self.searchResultsController;
  51. self.searchController = [[UISearchController alloc] initWithSearchResultsController:results];
  52. self.searchController.searchBar.placeholder = @"Filter";
  53. self.searchController.searchResultsUpdater = (id)self;
  54. self.searchController.delegate = (id)self;
  55. self.searchController.dimsBackgroundDuringPresentation = NO;
  56. self.searchController.hidesNavigationBarDuringPresentation = NO;
  57. /// Not necessary in iOS 13; remove this when iOS 13 is the minimum deployment target
  58. self.searchController.searchBar.delegate = self;
  59. self.automaticallyShowsSearchBarCancelButton = YES;
  60. #if FLEX_AT_LEAST_IOS13_SDK
  61. if (@available(iOS 13, *)) {
  62. self.searchController.automaticallyShowsScopeBar = NO;
  63. }
  64. #endif
  65. if (@available(iOS 11.0, *)) {
  66. self.navigationItem.searchController = self.searchController;
  67. } else {
  68. self.tableView.tableHeaderView = self.searchController.searchBar;
  69. }
  70. }
  71. - (void)setShowsCarousel:(BOOL)showsCarousel {
  72. if (_showsCarousel == showsCarousel) return;
  73. _showsCarousel = showsCarousel;
  74. _carousel = ({
  75. __weak __typeof(self) weakSelf = self;
  76. FLEXScopeCarousel *carousel = [FLEXScopeCarousel new];
  77. carousel.selectedIndexChangedAction = ^(NSInteger idx) {
  78. __typeof(self) self = weakSelf;
  79. [self updateSearchResults:self.searchText];
  80. };
  81. self.tableView.tableHeaderView = carousel;
  82. [self.tableView layoutIfNeeded];
  83. // UITableView won't update the header size unless you reset the header view
  84. [carousel registerBlockForDynamicTypeChanges:^(FLEXScopeCarousel *carousel) {
  85. __typeof(self) self = weakSelf;
  86. self.tableView.tableHeaderView = carousel;
  87. [self.tableView layoutIfNeeded];
  88. }];
  89. carousel;
  90. });
  91. }
  92. - (NSInteger)selectedScope {
  93. if (self.searchController.searchBar.showsScopeBar) {
  94. return self.searchController.searchBar.selectedScopeButtonIndex;
  95. } else if (self.showsCarousel) {
  96. return self.carousel.selectedIndex;
  97. } else {
  98. return NSNotFound;
  99. }
  100. }
  101. - (NSString *)searchText {
  102. return self.searchController.searchBar.text;
  103. }
  104. - (BOOL)automaticallyShowsSearchBarCancelButton {
  105. #if FLEX_AT_LEAST_IOS13_SDK
  106. if (@available(iOS 13, *)) {
  107. return self.searchController.automaticallyShowsCancelButton;
  108. }
  109. #endif
  110. return _automaticallyShowsSearchBarCancelButton;
  111. }
  112. - (void)setAutomaticallyShowsSearchBarCancelButton:(BOOL)value {
  113. #if FLEX_AT_LEAST_IOS13_SDK
  114. if (@available(iOS 13, *)) {
  115. self.searchController.automaticallyShowsCancelButton = value;
  116. }
  117. #endif
  118. _automaticallyShowsSearchBarCancelButton = value;
  119. }
  120. - (void)updateSearchResults:(NSString *)newText { }
  121. - (void)onBackgroundQueue:(NSArray *(^)(void))backgroundBlock thenOnMainQueue:(void(^)(NSArray *))mainBlock {
  122. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  123. NSArray *items = backgroundBlock();
  124. dispatch_async(dispatch_get_main_queue(), ^{
  125. mainBlock(items);
  126. });
  127. });
  128. }
  129. #pragma mark - View Controller Lifecycle
  130. - (void)viewDidLoad {
  131. [super viewDidLoad];
  132. self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
  133. // On iOS 13, the root view controller shows it's search bar no matter what.
  134. // Turning this off avoids some weird flash the navigation bar does when we
  135. // toggle navigationItem.hidesSearchBarWhenScrolling on and off. The flash
  136. // will still happen on subsequent view controllers, but we can at least
  137. // avoid it for the root view controller
  138. if (@available(iOS 13, *)) {
  139. if (self.navigationController.viewControllers.firstObject == self) {
  140. _showSearchBarInitially = NO;
  141. }
  142. }
  143. }
  144. - (void)viewWillAppear:(BOOL)animated {
  145. [super viewWillAppear:animated];
  146. // When going back, make the search bar reappear instead of hiding
  147. if (@available(iOS 11.0, *)) {
  148. if ((self.pinSearchBar || self.showSearchBarInitially) && !self.didInitiallyRevealSearchBar) {
  149. self.navigationItem.hidesSearchBarWhenScrolling = NO;
  150. }
  151. }
  152. }
  153. - (void)viewDidAppear:(BOOL)animated {
  154. [super viewDidAppear:animated];
  155. // Allow scrolling to collapse the search bar, only if we don't want it pinned
  156. if (@available(iOS 11.0, *)) {
  157. if (self.showSearchBarInitially && !self.pinSearchBar && !self.didInitiallyRevealSearchBar) {
  158. // All this mumbo jumbo is necessary to work around a bug in iOS 13 up to 13.2
  159. // wherein quickly toggling navigationItem.hidesSearchBarWhenScrolling to make
  160. // the search bar appear initially results in a bugged search bar that
  161. // becomes transparent and floats over the screen as you scroll
  162. [UIView animateWithDuration:0.2 animations:^{
  163. self.navigationItem.hidesSearchBarWhenScrolling = YES;
  164. [self.navigationController.view setNeedsLayout];
  165. [self.navigationController.view layoutIfNeeded];
  166. }];
  167. }
  168. }
  169. // We only want to reveal the search bar when the view controller first appears.
  170. self.didInitiallyRevealSearchBar = YES;
  171. }
  172. - (void)willMoveToParentViewController:(UIViewController *)parent {
  173. [super willMoveToParentViewController:parent];
  174. // Reset this since we are re-appearing under a new
  175. // parent view controller and need to show it again
  176. self.didInitiallyRevealSearchBar = NO;
  177. }
  178. - (void)viewDidDisappear:(BOOL)animated {
  179. [super viewDidDisappear:animated];
  180. if (self.searchController.active) {
  181. self.searchController.active = NO;
  182. }
  183. }
  184. #pragma mark - Private
  185. - (void)debounce:(void(^)(void))block {
  186. [self.debounceTimer invalidate];
  187. self.debounceTimer = [NSTimer
  188. scheduledTimerWithTimeInterval:self.searchBarDebounceInterval
  189. target:block
  190. selector:@selector(invoke)
  191. userInfo:nil
  192. repeats:NO
  193. ];
  194. }
  195. #pragma mark - Search Bar
  196. #pragma mark UISearchResultsUpdating
  197. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
  198. {
  199. [self.debounceTimer invalidate];
  200. NSString *text = searchController.searchBar.text;
  201. // Only debounce if we want to, and if we have a non-empty string
  202. // Empty string events are sent instantly
  203. if (text.length && self.searchBarDebounceInterval > kFLEXDebounceInstant) {
  204. [self debounce:^{
  205. [self updateSearchResults:text];
  206. }];
  207. } else {
  208. [self updateSearchResults:text];
  209. }
  210. }
  211. #pragma mark UISearchControllerDelegate
  212. - (void)willPresentSearchController:(UISearchController *)searchController {
  213. // Manually show cancel button for < iOS 13
  214. if (!@available(iOS 13, *) && self.automaticallyShowsSearchBarCancelButton) {
  215. [searchController.searchBar setShowsCancelButton:YES animated:YES];
  216. }
  217. }
  218. - (void)willDismissSearchController:(UISearchController *)searchController {
  219. // Manually hide cancel button for < iOS 13
  220. if (!@available(iOS 13, *) && self.automaticallyShowsSearchBarCancelButton) {
  221. [searchController.searchBar setShowsCancelButton:NO animated:YES];
  222. }
  223. }
  224. #pragma mark UISearchBarDelegate
  225. /// Not necessary in iOS 13; remove this when iOS 13 is the deployment target
  226. - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
  227. [self updateSearchResultsForSearchController:self.searchController];
  228. }
  229. #pragma mark Table view
  230. /// Not having a title in the first section looks weird with a rounded-corner table view style
  231. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  232. if (@available(iOS 13, *)) {
  233. return @" "; // For inset grouped style
  234. }
  235. return nil; // For plain/gropued style
  236. }
  237. @end