FLEXNetworkTransactionCell.m 7.2 KB

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