FLEXTableViewController.m 9.5 KB

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