FLEXNetworkHistoryTableViewController.m 13 KB

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