You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
7.3 KiB

  1. //
  2. // FLEXNetworkTransactionTableViewCell.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2/8/15.
  6. // Copyright (c) 2015 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXNetworkTransactionTableViewCell.h"
  9. #import "FLEXNetworkTransaction.h"
  10. #import "FLEXUtility.h"
  11. #import "FLEXResources.h"
  12. NSString *const kFLEXNetworkTransactionCellIdentifier = @"kFLEXNetworkTransactionCellIdentifier";
  13. @interface FLEXNetworkTransactionTableViewCell ()
  14. @property (nonatomic, strong) UIImageView *thumbnailImageView;
  15. @property (nonatomic, strong) UILabel *nameLabel;
  16. @property (nonatomic, strong) UILabel *pathLabel;
  17. @property (nonatomic, strong) UILabel *transactionDetailsLabel;
  18. @end
  19. @implementation FLEXNetworkTransactionTableViewCell
  20. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  21. {
  22. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  23. if (self) {
  24. self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  25. self.nameLabel = [[UILabel alloc] init];
  26. self.nameLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  27. [self.contentView addSubview:self.nameLabel];
  28. self.pathLabel = [[UILabel alloc] init];
  29. self.pathLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  30. self.pathLabel.textColor = [UIColor colorWithWhite:0.4 alpha:1.0];
  31. [self.contentView addSubview:self.pathLabel];
  32. self.thumbnailImageView = [[UIImageView alloc] init];
  33. self.thumbnailImageView.layer.borderColor = [[UIColor blackColor] CGColor];
  34. self.thumbnailImageView.layer.borderWidth = 1.0;
  35. self.thumbnailImageView.contentMode = UIViewContentModeScaleAspectFit;
  36. [self.contentView addSubview:self.thumbnailImageView];
  37. self.transactionDetailsLabel = [[UILabel alloc] init];
  38. self.transactionDetailsLabel.font = [FLEXUtility defaultFontOfSize:10.0];
  39. self.transactionDetailsLabel.textColor = [UIColor colorWithWhite:0.65 alpha:1.0];
  40. [self.contentView addSubview:self.transactionDetailsLabel];
  41. }
  42. return self;
  43. }
  44. - (void)setTransaction:(FLEXNetworkTransaction *)transaction
  45. {
  46. if (_transaction != transaction) {
  47. _transaction = transaction;
  48. [self setNeedsLayout];
  49. }
  50. }
  51. - (void)layoutSubviews
  52. {
  53. [super layoutSubviews];
  54. const CGFloat kVerticalPadding = 8.0;
  55. const CGFloat kLeftPadding = 10.0;
  56. const CGFloat kImageDimension = 32.0;
  57. CGFloat thumbnailOriginY = round((self.contentView.bounds.size.height - kImageDimension) / 2.0);
  58. self.thumbnailImageView.frame = CGRectMake(kLeftPadding, thumbnailOriginY, kImageDimension, kImageDimension);
  59. self.thumbnailImageView.image = self.transaction.responseThumbnail;
  60. CGFloat textOriginX = CGRectGetMaxX(self.thumbnailImageView.frame) + kLeftPadding;
  61. CGFloat availableTextWidth = self.contentView.bounds.size.width - textOriginX;
  62. self.nameLabel.text = [self nameLabelText];
  63. CGSize nameLabelPreferredSize = [self.nameLabel sizeThatFits:CGSizeMake(availableTextWidth, CGFLOAT_MAX)];
  64. self.nameLabel.frame = CGRectMake(textOriginX, kVerticalPadding, availableTextWidth, nameLabelPreferredSize.height);
  65. self.nameLabel.textColor = (self.transaction.error || [FLEXUtility isErrorStatusCodeFromURLResponse:self.transaction.response]) ? [UIColor redColor] : [UIColor blackColor];
  66. self.pathLabel.text = [self pathLabelText];
  67. CGSize pathLabelPreferredSize = [self.pathLabel sizeThatFits:CGSizeMake(availableTextWidth, CGFLOAT_MAX)];
  68. CGFloat pathLabelOriginY = ceil((self.contentView.bounds.size.height - pathLabelPreferredSize.height) / 2.0);
  69. self.pathLabel.frame = CGRectMake(textOriginX, pathLabelOriginY, availableTextWidth, pathLabelPreferredSize.height);
  70. self.transactionDetailsLabel.text = [self transactionDetailsLabelText];
  71. CGSize transactionLabelPreferredSize = [self.transactionDetailsLabel sizeThatFits:CGSizeMake(availableTextWidth, CGFLOAT_MAX)];
  72. CGFloat transactionDetailsOriginX = textOriginX;
  73. CGFloat transactionDetailsLabelOriginY = CGRectGetMaxY(self.contentView.bounds) - kVerticalPadding - transactionLabelPreferredSize.height;
  74. CGFloat transactionDetailsLabelWidth = self.contentView.bounds.size.width - transactionDetailsOriginX;
  75. self.transactionDetailsLabel.frame = CGRectMake(transactionDetailsOriginX, transactionDetailsLabelOriginY, transactionDetailsLabelWidth, transactionLabelPreferredSize.height);
  76. }
  77. - (NSString *)nameLabelText
  78. {
  79. NSURL *url = self.transaction.request.URL;
  80. NSString *name = [url lastPathComponent];
  81. if ([name length] == 0) {
  82. name = @"/";
  83. }
  84. NSString *query = [url query];
  85. if (query) {
  86. name = [name stringByAppendingFormat:@"?%@", query];
  87. }
  88. return name;
  89. }
  90. - (NSString *)pathLabelText
  91. {
  92. NSURL *url = self.transaction.request.URL;
  93. NSMutableArray<NSString *> *mutablePathComponents = [[url pathComponents] mutableCopy];
  94. if ([mutablePathComponents count] > 0) {
  95. [mutablePathComponents removeLastObject];
  96. }
  97. NSString *path = [url host];
  98. for (NSString *pathComponent in mutablePathComponents) {
  99. path = [path stringByAppendingPathComponent:pathComponent];
  100. }
  101. return path;
  102. }
  103. - (NSString *)transactionDetailsLabelText
  104. {
  105. NSMutableArray<NSString *> *detailComponents = [NSMutableArray array];
  106. NSString *timestamp = [[self class] timestampStringFromRequestDate:self.transaction.startTime];
  107. if ([timestamp length] > 0) {
  108. [detailComponents addObject:timestamp];
  109. }
  110. // Omit method for GET (assumed as default)
  111. NSString *httpMethod = self.transaction.request.HTTPMethod;
  112. if ([httpMethod length] > 0) {
  113. [detailComponents addObject:httpMethod];
  114. }
  115. if (self.transaction.transactionState == FLEXNetworkTransactionStateFinished || self.transaction.transactionState == FLEXNetworkTransactionStateFailed) {
  116. NSString *statusCodeString = [FLEXUtility statusCodeStringFromURLResponse:self.transaction.response];
  117. if ([statusCodeString length] > 0) {
  118. [detailComponents addObject:statusCodeString];
  119. }
  120. if (self.transaction.receivedDataLength > 0) {
  121. NSString *responseSize = [NSByteCountFormatter stringFromByteCount:self.transaction.receivedDataLength countStyle:NSByteCountFormatterCountStyleBinary];
  122. [detailComponents addObject:responseSize];
  123. }
  124. NSString *totalDuration = [FLEXUtility stringFromRequestDuration:self.transaction.duration];
  125. NSString *latency = [FLEXUtility stringFromRequestDuration:self.transaction.latency];
  126. NSString *duration = [NSString stringWithFormat:@"%@ (%@)", totalDuration, latency];
  127. [detailComponents addObject:duration];
  128. } else {
  129. // Unstarted, Awaiting Response, Receiving Data, etc.
  130. NSString *state = [FLEXNetworkTransaction readableStringFromTransactionState:self.transaction.transactionState];
  131. [detailComponents addObject:state];
  132. }
  133. return [detailComponents componentsJoinedByString:@" ・ "];
  134. }
  135. + (NSString *)timestampStringFromRequestDate:(NSDate *)date
  136. {
  137. static NSDateFormatter *dateFormatter = nil;
  138. static dispatch_once_t onceToken;
  139. dispatch_once(&onceToken, ^{
  140. dateFormatter = [[NSDateFormatter alloc] init];
  141. dateFormatter.dateFormat = @"HH:mm:ss";
  142. });
  143. return [dateFormatter stringFromDate:date];
  144. }
  145. + (CGFloat)preferredCellHeight
  146. {
  147. return 65.0;
  148. }
  149. @end