You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
9.1 KiB

  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. #pragma mark - Private
  179. - (void)debounce:(void(^)(void))block {
  180. [self.debounceTimer invalidate];
  181. self.debounceTimer = [NSTimer
  182. scheduledTimerWithTimeInterval:self.searchBarDebounceInterval
  183. target:block
  184. selector:@selector(invoke)
  185. userInfo:nil
  186. repeats:NO
  187. ];
  188. }
  189. #pragma mark - Search Bar
  190. #pragma mark UISearchResultsUpdating
  191. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
  192. {
  193. [self.debounceTimer invalidate];
  194. NSString *text = searchController.searchBar.text;
  195. // Only debounce if we want to, and if we have a non-empty string
  196. // Empty string events are sent instantly
  197. if (text.length && self.searchBarDebounceInterval > kFLEXDebounceInstant) {
  198. [self debounce:^{
  199. [self updateSearchResults:text];
  200. }];
  201. } else {
  202. [self updateSearchResults:text];
  203. }
  204. }
  205. #pragma mark UISearchControllerDelegate
  206. - (void)willPresentSearchController:(UISearchController *)searchController {
  207. // Manually show cancel button for < iOS 13
  208. if (!@available(iOS 13, *) && self.automaticallyShowsSearchBarCancelButton) {
  209. [searchController.searchBar setShowsCancelButton:YES animated:YES];
  210. }
  211. }
  212. - (void)willDismissSearchController:(UISearchController *)searchController {
  213. // Manually hide cancel button for < iOS 13
  214. if (!@available(iOS 13, *) && self.automaticallyShowsSearchBarCancelButton) {
  215. [searchController.searchBar setShowsCancelButton:NO animated:YES];
  216. }
  217. }
  218. #pragma mark UISearchBarDelegate
  219. /// Not necessary in iOS 13; remove this when iOS 13 is the deployment target
  220. - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
  221. [self updateSearchResultsForSearchController:self.searchController];
  222. }
  223. #pragma mark Table view
  224. /// Not having a title in the first section looks weird with a rounded-corner table view style
  225. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  226. if (@available(iOS 13, *)) {
  227. return @" "; // For inset grouped style
  228. }
  229. return nil; // For plain/gropued style
  230. }
  231. @end