FLEXNetworkHistoryTableViewController.m 5.2 KB

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