FLEXNetworkHistoryTableViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. //
  2. // FLEXNetworkHistoryTableViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2/8/15.
  6. // Copyright (c) 2015 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXNetworkHistoryTableViewController.h"
  9. #import "FLEXNetworkTransaction.h"
  10. #import "FLEXNetworkTransactionTableViewCell.h"
  11. #import "FLEXNetworkRecorder.h"
  12. #import "FLEXNetworkTransactionDetailTableViewController.h"
  13. #import "FLEXNetworkObserver.h"
  14. #import "FLEXNetworkSettingsTableViewController.h"
  15. @interface FLEXNetworkHistoryTableViewController () <UISearchDisplayDelegate>
  16. /// Backing model
  17. @property (nonatomic, copy) NSArray *networkTransactions;
  18. @property (nonatomic, assign) long long bytesReceived;
  19. @property (nonatomic, copy) NSArray *filteredNetworkTransactions;
  20. @property (nonatomic, assign) long long filteredBytesReceived;
  21. @property (nonatomic, assign) BOOL rowInsertInProgress;
  22. #pragma clang diagnostic push
  23. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  24. @property (nonatomic, strong) UISearchDisplayController *searchController;
  25. #pragma clang diagnostic pop
  26. @end
  27. @implementation FLEXNetworkHistoryTableViewController
  28. - (instancetype)initWithStyle:(UITableViewStyle)style
  29. {
  30. self = [super initWithStyle:style];
  31. if (self) {
  32. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNewTransactionRecordedNotification:) name:kFLEXNetworkRecorderNewTransactionNotification object:nil];
  33. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTransactionUpdatedNotification:) name:kFLEXNetworkRecorderTransactionUpdatedNotification object:nil];
  34. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTransactionsClearedNotification:) name:kFLEXNetworkRecorderTransactionsClearedNotification object:nil];
  35. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetworkObserverEnabledStateChangedNotification:) name:kFLEXNetworkObserverEnabledStateChangedNotification object:nil];
  36. self.title = @"📡 Network";
  37. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStylePlain target:self action:@selector(settingsButtonTapped:)];
  38. }
  39. return self;
  40. }
  41. - (void)dealloc
  42. {
  43. [[NSNotificationCenter defaultCenter] removeObserver:self];
  44. }
  45. - (void)viewDidLoad
  46. {
  47. [super viewDidLoad];
  48. [self.tableView registerClass:[FLEXNetworkTransactionTableViewCell class] forCellReuseIdentifier:kFLEXNetworkTransactionCellIdentifier];
  49. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  50. self.tableView.rowHeight = [FLEXNetworkTransactionTableViewCell preferredCellHeight];
  51. UISearchBar *searchBar = [[UISearchBar alloc] init];
  52. [searchBar sizeToFit];
  53. #pragma clang diagnostic push
  54. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  55. self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
  56. #pragma clang diagnostic pop
  57. self.searchController.delegate = self;
  58. self.searchController.searchResultsDataSource = self;
  59. self.searchController.searchResultsDelegate = self;
  60. [self.searchController.searchResultsTableView registerClass:[FLEXNetworkTransactionTableViewCell class] forCellReuseIdentifier:kFLEXNetworkTransactionCellIdentifier];
  61. self.searchController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  62. self.searchController.searchResultsTableView.rowHeight = [FLEXNetworkTransactionTableViewCell preferredCellHeight];
  63. self.tableView.tableHeaderView = self.searchController.searchBar;
  64. [self updateTransactions];
  65. }
  66. - (void)settingsButtonTapped:(id)sender
  67. {
  68. FLEXNetworkSettingsTableViewController *settingsViewController = [[FLEXNetworkSettingsTableViewController alloc] init];
  69. settingsViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(settingsViewControllerDoneTapped:)];
  70. settingsViewController.title = @"Network Debugging Settings";
  71. UINavigationController *wrapperNavigationController = [[UINavigationController alloc] initWithRootViewController:settingsViewController];
  72. [self presentViewController:wrapperNavigationController animated:YES completion:nil];
  73. }
  74. - (void)settingsViewControllerDoneTapped:(id)sender
  75. {
  76. [self dismissViewControllerAnimated:YES completion:nil];
  77. }
  78. - (void)updateTransactions
  79. {
  80. self.networkTransactions = [[FLEXNetworkRecorder defaultRecorder] networkTransactions];
  81. }
  82. - (void)setNetworkTransactions:(NSArray *)networkTransactions
  83. {
  84. if (![_networkTransactions isEqual:networkTransactions]) {
  85. _networkTransactions = networkTransactions;
  86. [self updateBytesReceived];
  87. [self updateFilteredBytesReceived];
  88. }
  89. }
  90. - (void)updateBytesReceived
  91. {
  92. long long bytesReceived = 0;
  93. for (FLEXNetworkTransaction *transaction in self.networkTransactions) {
  94. bytesReceived += transaction.receivedDataLength;
  95. }
  96. self.bytesReceived = bytesReceived;
  97. [self updateFirstSectionHeaderInTableView:self.tableView];
  98. }
  99. - (void)setFilteredNetworkTransactions:(NSArray *)filteredNetworkTransactions
  100. {
  101. if (![_filteredNetworkTransactions isEqual:filteredNetworkTransactions]) {
  102. _filteredNetworkTransactions = filteredNetworkTransactions;
  103. [self updateFilteredBytesReceived];
  104. }
  105. }
  106. - (void)updateFilteredBytesReceived
  107. {
  108. long long filteredBytesReceived = 0;
  109. for (FLEXNetworkTransaction *transaction in self.filteredNetworkTransactions) {
  110. filteredBytesReceived += transaction.receivedDataLength;
  111. }
  112. self.filteredBytesReceived = filteredBytesReceived;
  113. [self updateFirstSectionHeaderInTableView:self.searchController.searchResultsTableView];
  114. }
  115. - (void)updateFirstSectionHeaderInTableView:(UITableView *)tableView
  116. {
  117. UIView *view = [tableView headerViewForSection:0];
  118. if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
  119. UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
  120. headerView.textLabel.text = [self headerTextForTableView:tableView];
  121. [headerView setNeedsLayout];
  122. }
  123. }
  124. - (NSString *)headerTextForTableView:(UITableView *)tableView
  125. {
  126. NSString *headerText = nil;
  127. if ([FLEXNetworkObserver isEnabled]) {
  128. long long bytesReceived = 0;
  129. NSInteger totalRequests = 0;
  130. if (tableView == self.tableView) {
  131. bytesReceived = self.bytesReceived;
  132. totalRequests = [self.networkTransactions count];
  133. } else if (tableView == self.searchController.searchResultsTableView) {
  134. bytesReceived = self.filteredBytesReceived;
  135. totalRequests = [self.filteredNetworkTransactions count];
  136. }
  137. NSString *byteCountText = [NSByteCountFormatter stringFromByteCount:bytesReceived countStyle:NSByteCountFormatterCountStyleBinary];
  138. NSString *requestsText = totalRequests == 1 ? @"Request" : @"Requests";
  139. headerText = [NSString stringWithFormat:@"%ld %@ (%@ received)", (long)totalRequests, requestsText, byteCountText];
  140. } else {
  141. headerText = @"⚠️ Debugging Disabled (Enable in Settings)";
  142. }
  143. return headerText;
  144. }
  145. #pragma mark - Notification Handlers
  146. - (void)handleNewTransactionRecordedNotification:(NSNotification *)notification
  147. {
  148. [self tryUpdateTransactions];
  149. }
  150. - (void)tryUpdateTransactions
  151. {
  152. // Let the previous row insert animation finish before starting a new one to avoid stomping.
  153. // We'll try calling the method again when the insertion completes, and we properly no-op if there haven't been changes.
  154. if (self.rowInsertInProgress) {
  155. return;
  156. }
  157. NSInteger existingRowCount = [self.networkTransactions count];
  158. [self updateTransactions];
  159. NSInteger newRowCount = [self.networkTransactions count];
  160. NSInteger addedRowCount = newRowCount - existingRowCount;
  161. if (addedRowCount != 0) {
  162. // Insert animation if we're at the top.
  163. if (self.tableView.contentOffset.y <= 0.0 && addedRowCount > 0) {
  164. [CATransaction begin];
  165. self.rowInsertInProgress = YES;
  166. [CATransaction setCompletionBlock:^{
  167. self.rowInsertInProgress = NO;
  168. [self tryUpdateTransactions];
  169. }];
  170. NSMutableArray *indexPathsToReload = [NSMutableArray array];
  171. for (NSInteger row = 0; row < addedRowCount; row++) {
  172. [indexPathsToReload addObject:[NSIndexPath indexPathForRow:row inSection:0]];
  173. }
  174. [self.tableView insertRowsAtIndexPaths:indexPathsToReload withRowAnimation:UITableViewRowAnimationAutomatic];
  175. [CATransaction commit];
  176. } else {
  177. // Maintain the user's position if they've scrolled down.
  178. CGSize existingContentSize = self.tableView.contentSize;
  179. [self.tableView reloadData];
  180. CGFloat contentHeightChange = self.tableView.contentSize.height - existingContentSize.height;
  181. self.tableView.contentOffset = CGPointMake(self.tableView.contentOffset.x, self.tableView.contentOffset.y + contentHeightChange);
  182. }
  183. if (self.searchController.isActive) {
  184. [self updateSearchResultsWithSearchString:self.searchController.searchBar.text];
  185. }
  186. }
  187. }
  188. - (void)handleTransactionUpdatedNotification:(NSNotification *)notification
  189. {
  190. [self updateBytesReceived];
  191. [self updateFilteredBytesReceived];
  192. FLEXNetworkTransaction *transaction = [notification.userInfo objectForKey:kFLEXNetworkRecorderUserInfoTransactionKey];
  193. NSArray *tableViews = @[self.tableView];
  194. if (self.searchController.searchResultsTableView) {
  195. tableViews = [tableViews arrayByAddingObject:self.searchController.searchResultsTableView];
  196. }
  197. // Update both the main table view and search table view if needed.
  198. for (UITableView *tableView in tableViews) {
  199. for (FLEXNetworkTransactionTableViewCell *cell in [tableView visibleCells]) {
  200. if ([cell.transaction isEqual:transaction]) {
  201. // Using -[UITableView reloadRowsAtIndexPaths:withRowAnimation:] is overkill here and kicks off a lot of
  202. // work that can make the table view somewhat unresponseive when lots of updates are streaming in.
  203. // We just need to tell the cell that it needs to re-layout.
  204. [cell setNeedsLayout];
  205. break;
  206. }
  207. }
  208. [self updateFirstSectionHeaderInTableView:tableView];
  209. }
  210. }
  211. - (void)handleTransactionsClearedNotification:(NSNotification *)notification
  212. {
  213. [self updateTransactions];
  214. [self.tableView reloadData];
  215. [self.searchController.searchResultsTableView reloadData];
  216. }
  217. - (void)handleNetworkObserverEnabledStateChangedNotification:(NSNotification *)notification
  218. {
  219. // Update the header, which displays a warning when network debugging is disabled
  220. [self updateFirstSectionHeaderInTableView:self.tableView];
  221. }
  222. #pragma mark - Table view data source
  223. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  224. {
  225. return 1;
  226. }
  227. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  228. {
  229. NSInteger numberOfRows = 0;
  230. if (tableView == self.tableView) {
  231. numberOfRows = [self.networkTransactions count];
  232. } else if (tableView == self.searchController.searchResultsTableView) {
  233. numberOfRows = [self.filteredNetworkTransactions count];
  234. }
  235. return numberOfRows;
  236. }
  237. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  238. {
  239. return [self headerTextForTableView:tableView];
  240. }
  241. - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
  242. {
  243. if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
  244. UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
  245. headerView.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:14.0];
  246. headerView.textLabel.textColor = [UIColor whiteColor];
  247. headerView.contentView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:1.0];
  248. }
  249. }
  250. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  251. {
  252. FLEXNetworkTransactionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXNetworkTransactionCellIdentifier forIndexPath:indexPath];
  253. cell.transaction = [self transactionAtIndexPath:indexPath inTableView:tableView];
  254. // Since we insert from the top, assign background colors bottom up to keep them consistent for each transaction.
  255. NSInteger totalRows = [tableView numberOfRowsInSection:indexPath.section];
  256. if ((totalRows - indexPath.row) % 2 == 0) {
  257. cell.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
  258. } else {
  259. cell.backgroundColor = [UIColor whiteColor];
  260. }
  261. return cell;
  262. }
  263. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  264. {
  265. FLEXNetworkTransactionDetailTableViewController *detailViewController = [[FLEXNetworkTransactionDetailTableViewController alloc] init];
  266. detailViewController.transaction = [self transactionAtIndexPath:indexPath inTableView:tableView];
  267. [self.navigationController pushViewController:detailViewController animated:YES];
  268. }
  269. #pragma mark - Menu Actions
  270. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
  271. {
  272. return YES;
  273. }
  274. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  275. {
  276. return action == @selector(copy:);
  277. }
  278. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  279. {
  280. if (action == @selector(copy:)) {
  281. FLEXNetworkTransaction *transaction = [self transactionAtIndexPath:indexPath inTableView:tableView];
  282. NSString *requestURLString = transaction.request.URL.absoluteString ?: @"";
  283. [[UIPasteboard generalPasteboard] setString:requestURLString];
  284. }
  285. }
  286. - (FLEXNetworkTransaction *)transactionAtIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView
  287. {
  288. FLEXNetworkTransaction *transaction = nil;
  289. if (tableView == self.tableView) {
  290. transaction = [self.networkTransactions objectAtIndex:indexPath.row];
  291. } else if (tableView == self.searchController.searchResultsTableView) {
  292. transaction = [self.filteredNetworkTransactions objectAtIndex:indexPath.row];
  293. }
  294. return transaction;
  295. }
  296. #pragma mark - Search display delegate
  297. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  298. {
  299. [self updateSearchResultsWithSearchString:searchString];
  300. // Reload done after the data is filtered asynchronously
  301. return NO;
  302. }
  303. - (void)updateSearchResultsWithSearchString:(NSString *)searchString
  304. {
  305. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  306. NSArray *filteredNetworkTransactions = [self.networkTransactions filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(FLEXNetworkTransaction *transaction, NSDictionary *bindings) {
  307. return [[transaction.request.URL absoluteString] rangeOfString:searchString options:NSCaseInsensitiveSearch].length > 0;
  308. }]];
  309. dispatch_async(dispatch_get_main_queue(), ^{
  310. if ([self.searchController.searchBar.text isEqual:searchString]) {
  311. self.filteredNetworkTransactions = filteredNetworkTransactions;
  312. [self.searchController.searchResultsTableView reloadData];
  313. }
  314. });
  315. });
  316. }
  317. @end