NetworkConnectionView.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "NetworkConnectionView.h"
  15. /** Edge insets used by internal subviews. */
  16. static const CGFloat kEdgeInsetsTop = 10.0f;
  17. static const CGFloat kEdgeInsetsBottom = 10.0f;
  18. static const CGFloat kEdgeInsetsLeft = 20.0f;
  19. static const CGFloat kEdgeInsetsRight = 20.0f;
  20. @interface NetworkConnectionView () {
  21. PerfConnectionStatus _connectionStatus;
  22. }
  23. @property(nonatomic) UIProgressView *progressView;
  24. @end
  25. @implementation NetworkConnectionView
  26. #pragma mark - Initialization
  27. - (instancetype)initWithCoder:(NSCoder *)coder {
  28. NSAssert(NO, @"Not a valid initializer.");
  29. return nil;
  30. }
  31. #pragma mark - Properties
  32. - (void)setConnectionStatus:(PerfConnectionStatus)connectionStatus {
  33. _connectionStatus = connectionStatus;
  34. self.connectionStatusLabel.text = [self stringForStatus:connectionStatus];
  35. }
  36. - (PerfConnectionStatus)connectionStatus {
  37. return _connectionStatus;
  38. }
  39. - (void)setTitle:(NSString *)title {
  40. _title = title;
  41. [self.networkCallButton setTitle:title forState:UIControlStateNormal];
  42. }
  43. - (void)setProgressViewColor:(UIColor *)progressViewColor {
  44. _progressViewColor = progressViewColor;
  45. self.progressView.progressTintColor = progressViewColor;
  46. }
  47. - (UIProgressView *)progressView {
  48. if (!_progressView) {
  49. #if TARGET_OS_TV
  50. _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
  51. #else
  52. _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
  53. #endif
  54. _progressView.translatesAutoresizingMaskIntoConstraints = NO;
  55. _progressView.trackTintColor = [UIColor lightGrayColor];
  56. _progressView.progressTintColor = [UIColor blueColor];
  57. }
  58. return _progressView;
  59. }
  60. - (UIButton *)networkCallButton {
  61. if (!_networkCallButton) {
  62. _networkCallButton = [[UIButton alloc] init];
  63. _networkCallButton.translatesAutoresizingMaskIntoConstraints = NO;
  64. [_networkCallButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  65. [_networkCallButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
  66. _networkCallButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
  67. _networkCallButton.contentEdgeInsets =
  68. UIEdgeInsetsMake(kEdgeInsetsTop, kEdgeInsetsLeft, kEdgeInsetsBottom, kEdgeInsetsRight);
  69. _networkCallButton.layer.cornerRadius = 3.0f;
  70. _networkCallButton.layer.borderColor = [[UIColor blackColor] CGColor];
  71. _networkCallButton.layer.borderWidth = 1.0f;
  72. [_networkCallButton setTitle:@"Make a network request" forState:UIControlStateNormal];
  73. [_networkCallButton addTarget:self
  74. action:@selector(makeNetworkRequest:)
  75. forControlEvents:UIControlEventTouchDown];
  76. }
  77. return _networkCallButton;
  78. }
  79. - (UILabel *)connectionStatusLabel {
  80. if (!_connectionStatusLabel) {
  81. _connectionStatusLabel = [[UILabel alloc] init];
  82. _connectionStatusLabel.translatesAutoresizingMaskIntoConstraints = NO;
  83. _connectionStatusLabel.text = [self stringForStatus:_connectionStatus];
  84. _connectionStatusLabel.textAlignment = NSTextAlignmentRight;
  85. }
  86. return _connectionStatusLabel;
  87. }
  88. - (void)makeNetworkRequest:(UIButton *)button {
  89. [self.delegate networkConnectionViewDidTapRequestButton:self];
  90. }
  91. - (void)createViewTree {
  92. [self addSubview:self.networkCallButton];
  93. [self addSubview:self.progressView];
  94. [self addSubview:self.connectionStatusLabel];
  95. }
  96. #pragma mark - Public methods
  97. - (void)setProgress:(float)progress animated:(BOOL)animated {
  98. [self.progressView setProgress:progress animated:animated];
  99. }
  100. #pragma mark - View hierarchy methods
  101. - (void)constrainViews {
  102. NSDictionary<NSString *, UIView *> *viewsDictionary =
  103. NSDictionaryOfVariableBindings(_networkCallButton, _progressView, _connectionStatusLabel);
  104. [self addConstraintsString:@"V:|-0-[_networkCallButton(40)]-[_progressView(2)]"
  105. forViewsBinding:viewsDictionary];
  106. [self addConstraintsString:@"H:|-5-[_networkCallButton]-4-[_connectionStatusLabel(90)]-5-|"
  107. forViewsBinding:viewsDictionary];
  108. [self addConstraintsString:@"H:|-0-[_progressView]-0-|" forViewsBinding:viewsDictionary];
  109. [NSLayoutConstraint activateConstraints:@[
  110. [_connectionStatusLabel.topAnchor constraintEqualToAnchor:_networkCallButton.topAnchor],
  111. [_connectionStatusLabel.bottomAnchor constraintEqualToAnchor:_networkCallButton.bottomAnchor],
  112. ]];
  113. }
  114. - (void)addConstraintsString:(NSString *)string forViewsBinding:(NSDictionary *)viewsBinding {
  115. NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:string
  116. options:kNilOptions
  117. metrics:nil
  118. views:viewsBinding];
  119. [self addConstraints:constraints];
  120. }
  121. - (void)updateConstraints {
  122. [super updateConstraints];
  123. if (self.constraints.count == 0) {
  124. [self constrainViews];
  125. }
  126. }
  127. - (void)didMoveToSuperview {
  128. [super didMoveToSuperview];
  129. if (self.superview != nil && self.subviews.count == 0) {
  130. [self createViewTree];
  131. }
  132. }
  133. #pragma mark - Private methods
  134. - (NSString *)stringForStatus:(PerfConnectionStatus)status {
  135. NSString *statusString = nil;
  136. switch (status) {
  137. case ConnectionStatus_NA:
  138. statusString = @"N/A";
  139. break;
  140. case ConnectionStatus_Fail:
  141. statusString = @"Fail";
  142. break;
  143. case ConnectionStatus_Success:
  144. statusString = @"Success";
  145. break;
  146. default:
  147. NSAssert(NO, @"Unsupported status");
  148. break;
  149. }
  150. return statusString;
  151. }
  152. @end