FLEXTableViewController.m 9.1 KB

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