FLEXNetworkHistoryTableViewController.m 12 KB

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