FLEXNetworkHistoryTableViewController.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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, copy) NSArray *filteredNetworkTransactions;
  17. @property (nonatomic, strong) UISearchDisplayController *searchController;
  18. @end
  19. @implementation FLEXNetworkHistoryTableViewController
  20. - (instancetype)initWithStyle:(UITableViewStyle)style
  21. {
  22. self = [super initWithStyle:style];
  23. if (self) {
  24. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNewTransactionRecordedNotification:) name:kFLEXNetworkRecorderNewTransactionNotification object:nil];
  25. self.title = @"📡 Network";
  26. }
  27. return self;
  28. }
  29. - (void)dealloc
  30. {
  31. [[NSNotificationCenter defaultCenter] removeObserver:self];
  32. }
  33. - (void)viewDidLoad
  34. {
  35. [super viewDidLoad];
  36. [self.tableView registerClass:[FLEXNetworkTransactionTableViewCell class] forCellReuseIdentifier:kFLEXNetworkTransactionCellIdentifier];
  37. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  38. self.tableView.rowHeight = [FLEXNetworkTransactionTableViewCell preferredCellHeight];
  39. UISearchBar *searchBar = [[UISearchBar alloc] init];
  40. [searchBar sizeToFit];
  41. self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
  42. self.searchController.delegate = self;
  43. self.searchController.searchResultsDataSource = self;
  44. self.searchController.searchResultsDelegate = self;
  45. [self.searchController.searchResultsTableView registerClass:[FLEXNetworkTransactionTableViewCell class] forCellReuseIdentifier:kFLEXNetworkTransactionCellIdentifier];
  46. self.searchController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  47. self.searchController.searchResultsTableView.rowHeight = [FLEXNetworkTransactionTableViewCell preferredCellHeight];
  48. self.tableView.tableHeaderView = self.searchController.searchBar;
  49. [self updateTransactions];
  50. }
  51. - (void)updateTransactions
  52. {
  53. self.networkTransactions = [[FLEXNetworkRecorder defaultRecorder] networkTransactions];
  54. }
  55. - (void)handleNewTransactionRecordedNotification:(NSNotification *)notification
  56. {
  57. [self updateTransactions];
  58. [self.tableView reloadData];
  59. }
  60. #pragma mark - Table view data source
  61. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  62. {
  63. return 1;
  64. }
  65. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  66. {
  67. NSInteger numberOfRows = 0;
  68. if (tableView == self.tableView) {
  69. numberOfRows = [self.networkTransactions count];
  70. } else if (tableView == self.searchController.searchResultsTableView) {
  71. numberOfRows = [self.filteredNetworkTransactions count];
  72. }
  73. return numberOfRows;
  74. }
  75. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  76. {
  77. FLEXNetworkTransactionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXNetworkTransactionCellIdentifier forIndexPath:indexPath];
  78. cell.transaction = [self transactionAtIndexPath:indexPath inTableView:tableView];
  79. if (indexPath.row % 2 == 0) {
  80. cell.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
  81. } else {
  82. cell.backgroundColor = [UIColor whiteColor];
  83. }
  84. return cell;
  85. }
  86. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  87. {
  88. FLEXNetworkTransactionDetailTableViewController *detailViewController = [[FLEXNetworkTransactionDetailTableViewController alloc] init];
  89. detailViewController.transaction = [self transactionAtIndexPath:indexPath inTableView:tableView];
  90. [self.navigationController pushViewController:detailViewController animated:YES];
  91. }
  92. - (FLEXNetworkTransaction *)transactionAtIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView
  93. {
  94. FLEXNetworkTransaction *transaction = nil;
  95. if (tableView == self.tableView) {
  96. transaction = [self.networkTransactions objectAtIndex:indexPath.row];
  97. } else if (tableView == self.searchController.searchResultsTableView) {
  98. transaction = [self.filteredNetworkTransactions objectAtIndex:indexPath.row];
  99. }
  100. return transaction;
  101. }
  102. #pragma mark - Search display delegate
  103. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  104. {
  105. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  106. NSArray *filteredNetworkTransactions = [self.networkTransactions filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(FLEXNetworkTransaction *transaction, NSDictionary *bindings) {
  107. return [[transaction.request.URL absoluteString] rangeOfString:searchString options:NSCaseInsensitiveSearch].length > 0;
  108. }]];
  109. dispatch_async(dispatch_get_main_queue(), ^{
  110. if ([self.searchDisplayController.searchBar.text isEqual:searchString]) {
  111. self.filteredNetworkTransactions = filteredNetworkTransactions;
  112. [self.searchDisplayController.searchResultsTableView reloadData];
  113. }
  114. });
  115. });
  116. // Reload done after the data is filtered asynchronously
  117. return NO;
  118. }
  119. @end