FLEXNetworkHistoryTableViewController.m 11 KB

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