FLEXNetworkHistoryTableViewController.m 12 KB

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