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.

383 lines
14 KiB

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