FLEXNetworkHistoryTableViewController.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 ()
  13. /// Backing model
  14. @property (nonatomic, copy) NSArray *networkTransactions;
  15. @end
  16. @implementation FLEXNetworkHistoryTableViewController
  17. - (instancetype)initWithStyle:(UITableViewStyle)style
  18. {
  19. self = [super initWithStyle:style];
  20. if (self) {
  21. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNewTransactionRecordedNotification:) name:kFLEXNetworkRecorderNewTransactionNotification object:nil];
  22. self.title = @"📡 Network";
  23. }
  24. return self;
  25. }
  26. - (void)dealloc
  27. {
  28. [[NSNotificationCenter defaultCenter] removeObserver:self];
  29. }
  30. - (void)viewDidLoad
  31. {
  32. [super viewDidLoad];
  33. [self.tableView registerClass:[FLEXNetworkTransactionTableViewCell class] forCellReuseIdentifier:kFLEXNetworkTransactionCellIdentifier];
  34. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  35. self.tableView.rowHeight = [FLEXNetworkTransactionTableViewCell preferredCellHeight];
  36. [self updateTransactions];
  37. }
  38. - (void)updateTransactions
  39. {
  40. self.networkTransactions = [[FLEXNetworkRecorder defaultRecorder] networkTransactions];
  41. }
  42. - (void)handleNewTransactionRecordedNotification:(NSNotification *)notification
  43. {
  44. // Note that these notifications may be posted from a background thread.
  45. dispatch_async(dispatch_get_main_queue(), ^{
  46. [self updateTransactions];
  47. [self.tableView reloadData];
  48. });
  49. }
  50. #pragma mark - Table view data source
  51. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  52. {
  53. return 1;
  54. }
  55. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  56. {
  57. return [self.networkTransactions count];
  58. }
  59. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  60. {
  61. FLEXNetworkTransactionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXNetworkTransactionCellIdentifier forIndexPath:indexPath];
  62. cell.transaction = [self.networkTransactions objectAtIndex:indexPath.row];
  63. if (indexPath.row % 2 == 0) {
  64. cell.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
  65. } else {
  66. cell.backgroundColor = [UIColor whiteColor];
  67. }
  68. return cell;
  69. }
  70. @end