FLEXTableViewController.m 9.8 KB

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