FLEXNetworkHistoryTableViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. @interface FLEXNetworkHistoryTableViewController () <UISearchDisplayDelegate>
  15. /// Backing model
  16. @property (nonatomic, copy) NSArray *networkTransactions;
  17. @property (nonatomic, assign) long long bytesReceived;
  18. @property (nonatomic, copy) NSArray *filteredNetworkTransactions;
  19. @property (nonatomic, assign) long long filteredBytesReceived;
  20. @property (nonatomic, strong) UISearchDisplayController *searchController;
  21. @end
  22. @implementation FLEXNetworkHistoryTableViewController
  23. - (instancetype)initWithStyle:(UITableViewStyle)style
  24. {
  25. self = [super initWithStyle:style];
  26. if (self) {
  27. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNewTransactionRecordedNotification:) name:kFLEXNetworkRecorderNewTransactionNotification object:nil];
  28. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTransactionUpdatedNotification:) name:kFLEXNetworkRecorderTransactionUpdatedNotification object:nil];
  29. self.title = @"📡 Network";
  30. }
  31. return self;
  32. }
  33. - (void)dealloc
  34. {
  35. [[NSNotificationCenter defaultCenter] removeObserver:self];
  36. }
  37. - (void)viewDidLoad
  38. {
  39. [super viewDidLoad];
  40. [self.tableView registerClass:[FLEXNetworkTransactionTableViewCell class] forCellReuseIdentifier:kFLEXNetworkTransactionCellIdentifier];
  41. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  42. self.tableView.rowHeight = [FLEXNetworkTransactionTableViewCell preferredCellHeight];
  43. UISearchBar *searchBar = [[UISearchBar alloc] init];
  44. [searchBar sizeToFit];
  45. self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
  46. self.searchController.delegate = self;
  47. self.searchController.searchResultsDataSource = self;
  48. self.searchController.searchResultsDelegate = self;
  49. [self.searchController.searchResultsTableView registerClass:[FLEXNetworkTransactionTableViewCell class] forCellReuseIdentifier:kFLEXNetworkTransactionCellIdentifier];
  50. self.searchController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  51. self.searchController.searchResultsTableView.rowHeight = [FLEXNetworkTransactionTableViewCell preferredCellHeight];
  52. self.tableView.tableHeaderView = self.searchController.searchBar;
  53. [self updateTransactions];
  54. }
  55. - (void)updateTransactions
  56. {
  57. self.networkTransactions = [[FLEXNetworkRecorder defaultRecorder] networkTransactions];
  58. }
  59. - (void)setNetworkTransactions:(NSArray *)networkTransactions
  60. {
  61. if (![_networkTransactions isEqual:networkTransactions]) {
  62. _networkTransactions = networkTransactions;
  63. [self updateBytesReceived];
  64. [self updateFilteredBytesReceived];
  65. }
  66. }
  67. - (void)updateBytesReceived
  68. {
  69. long long bytesReceived = 0;
  70. for (FLEXNetworkTransaction *transaction in self.networkTransactions) {
  71. bytesReceived += transaction.receivedDataLength;
  72. }
  73. self.bytesReceived = bytesReceived;
  74. [self updateFirstSectionHeaderInTableView:self.tableView];
  75. }
  76. - (void)setFilteredNetworkTransactions:(NSArray *)filteredNetworkTransactions
  77. {
  78. if (![_filteredNetworkTransactions isEqual:filteredNetworkTransactions]) {
  79. _filteredNetworkTransactions = filteredNetworkTransactions;
  80. [self updateFilteredBytesReceived];
  81. }
  82. }
  83. - (void)updateFilteredBytesReceived
  84. {
  85. long long filteredBytesReceived = 0;
  86. for (FLEXNetworkTransaction *transaction in self.filteredNetworkTransactions) {
  87. filteredBytesReceived += transaction.receivedDataLength;
  88. }
  89. self.filteredBytesReceived = filteredBytesReceived;
  90. [self updateFirstSectionHeaderInTableView:self.searchController.searchResultsTableView];
  91. }
  92. - (void)updateFirstSectionHeaderInTableView:(UITableView *)tableView
  93. {
  94. UIView *view = [tableView headerViewForSection:0];
  95. if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
  96. UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
  97. headerView.textLabel.text = [self headerTextForTableView:tableView];
  98. [headerView setNeedsLayout];
  99. }
  100. }
  101. - (NSString *)headerTextForTableView:(UITableView *)tableView
  102. {
  103. NSString *headerText = nil;
  104. if ([FLEXNetworkObserver isEnabled]) {
  105. long long bytesReceived = 0;
  106. NSInteger totalRequests = 0;
  107. if (tableView == self.tableView) {
  108. bytesReceived = self.bytesReceived;
  109. totalRequests = [self.networkTransactions count];
  110. } else if (tableView == self.searchController.searchResultsTableView) {
  111. bytesReceived = self.filteredBytesReceived;
  112. totalRequests = [self.filteredNetworkTransactions count];
  113. }
  114. NSString *byteCountText = [NSByteCountFormatter stringFromByteCount:bytesReceived countStyle:NSByteCountFormatterCountStyleBinary];
  115. NSString *requestsText = totalRequests == 1 ? @"Request" : @"Requests";
  116. headerText = [NSString stringWithFormat:@"%ld %@ (%@ received)", (long)totalRequests, requestsText, byteCountText];
  117. } else {
  118. headerText = @"⚠️ Debugging Disabled (Enable in Settings)";
  119. }
  120. return headerText;
  121. }
  122. - (void)handleNewTransactionRecordedNotification:(NSNotification *)notification
  123. {
  124. NSInteger existingRowCount = [self.networkTransactions count];
  125. [self updateTransactions];
  126. NSInteger newRowCount = [self.networkTransactions count];
  127. NSInteger addedRowCount = newRowCount - existingRowCount;
  128. if (self.tableView.contentOffset.y <= 0.0 && addedRowCount > 0) {
  129. // Insert animation if we're at the top.
  130. NSMutableArray *indexPathsToReload = [NSMutableArray array];
  131. for (NSInteger row = 0; row < addedRowCount; row++) {
  132. [indexPathsToReload addObject:[NSIndexPath indexPathForRow:row inSection:0]];
  133. }
  134. [self.tableView insertRowsAtIndexPaths:indexPathsToReload withRowAnimation:UITableViewRowAnimationAutomatic];
  135. } else {
  136. // Maintain the user's position if they've scrolled down.
  137. CGSize existingContentSize = self.tableView.contentSize;
  138. [self.tableView reloadData];
  139. CGFloat contentHeightChange = self.tableView.contentSize.height - existingContentSize.height;
  140. self.tableView.contentOffset = CGPointMake(self.tableView.contentOffset.x, self.tableView.contentOffset.y + contentHeightChange);
  141. }
  142. if (self.searchController.isActive) {
  143. [self updateSearchResultsWithSearchString:self.searchController.searchBar.text];
  144. }
  145. }
  146. - (void)handleTransactionUpdatedNotification:(NSNotification *)notification
  147. {
  148. [self updateBytesReceived];
  149. [self updateFilteredBytesReceived];
  150. FLEXNetworkTransaction *transaction = [notification.userInfo objectForKey:kFLEXNetworkRecorderUserInfoTransactionKey];
  151. NSArray *tableViews = @[self.tableView];
  152. if (self.searchController.searchResultsTableView) {
  153. tableViews = [tableViews arrayByAddingObject:self.searchController.searchResultsTableView];
  154. }
  155. // Update both the main table view and search table view if needed.
  156. for (UITableView *tableView in tableViews) {
  157. for (FLEXNetworkTransactionTableViewCell *cell in [tableView visibleCells]) {
  158. if ([cell.transaction isEqual:transaction]) {
  159. NSIndexPath *indexPath = [tableView indexPathForCell:cell];
  160. if (indexPath) {
  161. [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  162. }
  163. break;
  164. }
  165. }
  166. [self updateFirstSectionHeaderInTableView:tableView];
  167. }
  168. }
  169. #pragma mark - Table view data source
  170. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  171. {
  172. return 1;
  173. }
  174. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  175. {
  176. NSInteger numberOfRows = 0;
  177. if (tableView == self.tableView) {
  178. numberOfRows = [self.networkTransactions count];
  179. } else if (tableView == self.searchController.searchResultsTableView) {
  180. numberOfRows = [self.filteredNetworkTransactions count];
  181. }
  182. return numberOfRows;
  183. }
  184. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  185. {
  186. return [self headerTextForTableView:tableView];
  187. }
  188. - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
  189. {
  190. if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
  191. UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
  192. headerView.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:14.0];
  193. headerView.textLabel.textColor = [UIColor whiteColor];
  194. headerView.contentView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:1.0];
  195. }
  196. }
  197. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  198. {
  199. FLEXNetworkTransactionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXNetworkTransactionCellIdentifier forIndexPath:indexPath];
  200. cell.transaction = [self transactionAtIndexPath:indexPath inTableView:tableView];
  201. // Since we insert from the top, assign background colors bottom up to keep them consistent for each transaction.
  202. NSInteger totalRows = [tableView numberOfRowsInSection:indexPath.section];
  203. if ((totalRows - indexPath.row) % 2 == 0) {
  204. cell.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
  205. } else {
  206. cell.backgroundColor = [UIColor whiteColor];
  207. }
  208. return cell;
  209. }
  210. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  211. {
  212. FLEXNetworkTransactionDetailTableViewController *detailViewController = [[FLEXNetworkTransactionDetailTableViewController alloc] init];
  213. detailViewController.transaction = [self transactionAtIndexPath:indexPath inTableView:tableView];
  214. [self.navigationController pushViewController:detailViewController animated:YES];
  215. }
  216. - (FLEXNetworkTransaction *)transactionAtIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView
  217. {
  218. FLEXNetworkTransaction *transaction = nil;
  219. if (tableView == self.tableView) {
  220. transaction = [self.networkTransactions objectAtIndex:indexPath.row];
  221. } else if (tableView == self.searchController.searchResultsTableView) {
  222. transaction = [self.filteredNetworkTransactions objectAtIndex:indexPath.row];
  223. }
  224. return transaction;
  225. }
  226. #pragma mark - Search display delegate
  227. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  228. {
  229. [self updateSearchResultsWithSearchString:searchString];
  230. // Reload done after the data is filtered asynchronously
  231. return NO;
  232. }
  233. - (void)updateSearchResultsWithSearchString:(NSString *)searchString
  234. {
  235. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  236. NSArray *filteredNetworkTransactions = [self.networkTransactions filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(FLEXNetworkTransaction *transaction, NSDictionary *bindings) {
  237. return [[transaction.request.URL absoluteString] rangeOfString:searchString options:NSCaseInsensitiveSearch].length > 0;
  238. }]];
  239. dispatch_async(dispatch_get_main_queue(), ^{
  240. if ([self.searchDisplayController.searchBar.text isEqual:searchString]) {
  241. self.filteredNetworkTransactions = filteredNetworkTransactions;
  242. [self.searchDisplayController.searchResultsTableView reloadData];
  243. }
  244. });
  245. });
  246. }
  247. @end