FLEXNetworkHistoryTableViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 () <UISearchResultsUpdating, UISearchControllerDelegate>
  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. @property (nonatomic, assign) BOOL isPresentingSearch;
  23. @property (nonatomic, strong) UISearchController *searchController;
  24. @end
  25. @implementation FLEXNetworkHistoryTableViewController
  26. - (instancetype)initWithStyle:(UITableViewStyle)style
  27. {
  28. self = [super initWithStyle:style];
  29. if (self) {
  30. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNewTransactionRecordedNotification:) name:kFLEXNetworkRecorderNewTransactionNotification object:nil];
  31. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTransactionUpdatedNotification:) name:kFLEXNetworkRecorderTransactionUpdatedNotification object:nil];
  32. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTransactionsClearedNotification:) name:kFLEXNetworkRecorderTransactionsClearedNotification object:nil];
  33. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetworkObserverEnabledStateChangedNotification:) name:kFLEXNetworkObserverEnabledStateChangedNotification object:nil];
  34. self.title = @"📡 Network";
  35. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStylePlain target:self action:@selector(settingsButtonTapped:)];
  36. }
  37. return self;
  38. }
  39. - (void)dealloc
  40. {
  41. [[NSNotificationCenter defaultCenter] removeObserver:self];
  42. }
  43. - (void)viewDidLoad
  44. {
  45. [super viewDidLoad];
  46. [self.tableView registerClass:[FLEXNetworkTransactionTableViewCell class] forCellReuseIdentifier:kFLEXNetworkTransactionCellIdentifier];
  47. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  48. self.tableView.rowHeight = [FLEXNetworkTransactionTableViewCell preferredCellHeight];
  49. self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  50. self.searchController.delegate = self;
  51. self.searchController.searchResultsUpdater = self;
  52. self.searchController.dimsBackgroundDuringPresentation = NO;
  53. self.tableView.tableHeaderView = self.searchController.searchBar;
  54. [self updateTransactions];
  55. }
  56. - (void)settingsButtonTapped:(id)sender
  57. {
  58. FLEXNetworkSettingsTableViewController *settingsViewController = [[FLEXNetworkSettingsTableViewController alloc] init];
  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 *)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 *)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 - Notification Handlers
  136. - (void)handleNewTransactionRecordedNotification:(NSNotification *)notification
  137. {
  138. [self tryUpdateTransactions];
  139. }
  140. - (void)tryUpdateTransactions
  141. {
  142. // Let the previous row insert animation finish before starting a new one to avoid stomping.
  143. // We'll try calling the method again when the insertion completes, and we properly no-op if there haven't been changes.
  144. if (self.rowInsertInProgress) {
  145. return;
  146. }
  147. if (self.searchController.isActive) {
  148. [self updateTransactions];
  149. [self updateSearchResults];
  150. return;
  151. }
  152. NSInteger existingRowCount = [self.networkTransactions count];
  153. [self updateTransactions];
  154. NSInteger newRowCount = [self.networkTransactions count];
  155. NSInteger addedRowCount = newRowCount - existingRowCount;
  156. if (addedRowCount != 0 && !self.isPresentingSearch) {
  157. // Insert animation if we're at the top.
  158. if (self.tableView.contentOffset.y <= 0.0 && addedRowCount > 0) {
  159. [CATransaction begin];
  160. self.rowInsertInProgress = YES;
  161. [CATransaction setCompletionBlock:^{
  162. self.rowInsertInProgress = NO;
  163. [self tryUpdateTransactions];
  164. }];
  165. NSMutableArray *indexPathsToReload = [NSMutableArray array];
  166. for (NSInteger row = 0; row < addedRowCount; row++) {
  167. [indexPathsToReload addObject:[NSIndexPath indexPathForRow:row inSection:0]];
  168. }
  169. [self.tableView insertRowsAtIndexPaths:indexPathsToReload withRowAnimation:UITableViewRowAnimationAutomatic];
  170. [CATransaction commit];
  171. } else {
  172. // Maintain the user's position if they've scrolled down.
  173. CGSize existingContentSize = self.tableView.contentSize;
  174. [self.tableView reloadData];
  175. CGFloat contentHeightChange = self.tableView.contentSize.height - existingContentSize.height;
  176. self.tableView.contentOffset = CGPointMake(self.tableView.contentOffset.x, self.tableView.contentOffset.y + contentHeightChange);
  177. }
  178. }
  179. }
  180. - (void)handleTransactionUpdatedNotification:(NSNotification *)notification
  181. {
  182. [self updateBytesReceived];
  183. [self updateFilteredBytesReceived];
  184. FLEXNetworkTransaction *transaction = notification.userInfo[kFLEXNetworkRecorderUserInfoTransactionKey];
  185. // Update both the main table view and search table view if needed.
  186. for (FLEXNetworkTransactionTableViewCell *cell in [self.tableView visibleCells]) {
  187. if ([cell.transaction isEqual:transaction]) {
  188. // Using -[UITableView reloadRowsAtIndexPaths:withRowAnimation:] is overkill here and kicks off a lot of
  189. // work that can make the table view somewhat unresponseive when lots of updates are streaming in.
  190. // We just need to tell the cell that it needs to re-layout.
  191. [cell setNeedsLayout];
  192. break;
  193. }
  194. }
  195. [self updateFirstSectionHeader];
  196. }
  197. - (void)handleTransactionsClearedNotification:(NSNotification *)notification
  198. {
  199. [self updateTransactions];
  200. [self.tableView reloadData];
  201. }
  202. - (void)handleNetworkObserverEnabledStateChangedNotification:(NSNotification *)notification
  203. {
  204. // Update the header, which displays a warning when network debugging is disabled
  205. [self updateFirstSectionHeader];
  206. }
  207. #pragma mark - Table view data source
  208. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  209. {
  210. return 1;
  211. }
  212. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  213. {
  214. return self.searchController.isActive ? [self.filteredNetworkTransactions count] : [self.networkTransactions count];
  215. }
  216. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  217. {
  218. return [self headerText];
  219. }
  220. - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
  221. {
  222. if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
  223. UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
  224. headerView.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:14.0];
  225. headerView.textLabel.textColor = [UIColor whiteColor];
  226. headerView.contentView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:1.0];
  227. }
  228. }
  229. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  230. {
  231. FLEXNetworkTransactionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXNetworkTransactionCellIdentifier forIndexPath:indexPath];
  232. cell.transaction = [self transactionAtIndexPath:indexPath inTableView:tableView];
  233. // Since we insert from the top, assign background colors bottom up to keep them consistent for each transaction.
  234. NSInteger totalRows = [tableView numberOfRowsInSection:indexPath.section];
  235. if ((totalRows - indexPath.row) % 2 == 0) {
  236. cell.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
  237. } else {
  238. cell.backgroundColor = [UIColor whiteColor];
  239. }
  240. return cell;
  241. }
  242. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  243. {
  244. FLEXNetworkTransactionDetailTableViewController *detailViewController = [[FLEXNetworkTransactionDetailTableViewController alloc] init];
  245. detailViewController.transaction = [self transactionAtIndexPath:indexPath inTableView:tableView];
  246. [self.navigationController pushViewController:detailViewController animated:YES];
  247. }
  248. #pragma mark - Menu Actions
  249. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
  250. {
  251. return YES;
  252. }
  253. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  254. {
  255. return action == @selector(copy:);
  256. }
  257. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  258. {
  259. if (action == @selector(copy:)) {
  260. FLEXNetworkTransaction *transaction = [self transactionAtIndexPath:indexPath inTableView:tableView];
  261. NSString *requestURLString = transaction.request.URL.absoluteString ?: @"";
  262. [[UIPasteboard generalPasteboard] setString:requestURLString];
  263. }
  264. }
  265. - (FLEXNetworkTransaction *)transactionAtIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView
  266. {
  267. return self.searchController.isActive ? self.filteredNetworkTransactions[indexPath.row] : self.networkTransactions[indexPath.row];
  268. }
  269. #pragma mark - UISearchResultsUpdating
  270. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
  271. {
  272. [self updateSearchResults];
  273. }
  274. - (void)updateSearchResults
  275. {
  276. NSString *searchString = self.searchController.searchBar.text;
  277. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  278. NSArray *filteredNetworkTransactions = [self.networkTransactions filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(FLEXNetworkTransaction *transaction, NSDictionary *bindings) {
  279. return [[transaction.request.URL absoluteString] rangeOfString:searchString options:NSCaseInsensitiveSearch].length > 0;
  280. }]];
  281. dispatch_async(dispatch_get_main_queue(), ^{
  282. if ([self.searchController.searchBar.text isEqual:searchString]) {
  283. self.filteredNetworkTransactions = filteredNetworkTransactions;
  284. [self.tableView reloadData];
  285. }
  286. });
  287. });
  288. }
  289. #pragma mark - UISearchControllerDelegate
  290. - (void)willPresentSearchController:(UISearchController *)searchController
  291. {
  292. self.isPresentingSearch = YES;
  293. }
  294. - (void)didPresentSearchController:(UISearchController *)searchController
  295. {
  296. self.isPresentingSearch = NO;
  297. }
  298. - (void)willDismissSearchController:(UISearchController *)searchController
  299. {
  300. [self.tableView reloadData];
  301. }
  302. @end