NetworkConnectionView.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
  50. _progressView.translatesAutoresizingMaskIntoConstraints = NO;
  51. _progressView.trackTintColor = [UIColor lightGrayColor];
  52. _progressView.progressTintColor = [UIColor blueColor];
  53. }
  54. return _progressView;
  55. }
  56. - (UIButton *)networkCallButton {
  57. if (!_networkCallButton) {
  58. _networkCallButton = [[UIButton alloc] init];
  59. _networkCallButton.translatesAutoresizingMaskIntoConstraints = NO;
  60. [_networkCallButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  61. [_networkCallButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
  62. _networkCallButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
  63. _networkCallButton.contentEdgeInsets =
  64. UIEdgeInsetsMake(kEdgeInsetsTop, kEdgeInsetsLeft, kEdgeInsetsBottom, kEdgeInsetsRight);
  65. _networkCallButton.layer.cornerRadius = 3.0f;
  66. _networkCallButton.layer.borderColor = [[UIColor blackColor] CGColor];
  67. _networkCallButton.layer.borderWidth = 1.0f;
  68. [_networkCallButton setTitle:@"Make a network request" forState:UIControlStateNormal];
  69. [_networkCallButton addTarget:self
  70. action:@selector(makeNetworkRequest:)
  71. forControlEvents:UIControlEventTouchDown];
  72. }
  73. return _networkCallButton;
  74. }
  75. - (UILabel *)connectionStatusLabel {
  76. if (!_connectionStatusLabel) {
  77. _connectionStatusLabel = [[UILabel alloc] init];
  78. _connectionStatusLabel.translatesAutoresizingMaskIntoConstraints = NO;
  79. _connectionStatusLabel.text = [self stringForStatus:_connectionStatus];
  80. _connectionStatusLabel.textAlignment = NSTextAlignmentRight;
  81. }
  82. return _connectionStatusLabel;
  83. }
  84. - (void)makeNetworkRequest:(UIButton *)button {
  85. [self.delegate networkConnectionViewDidTapRequestButton:self];
  86. }
  87. - (void)createViewTree {
  88. [self addSubview:self.networkCallButton];
  89. [self addSubview:self.progressView];
  90. [self addSubview:self.connectionStatusLabel];
  91. }
  92. #pragma mark - Public methods
  93. - (void)setProgress:(float)progress animated:(BOOL)animated {
  94. [self.progressView setProgress:progress animated:animated];
  95. }
  96. #pragma mark - View hierarchy methods
  97. - (void)constrainViews {
  98. NSDictionary<NSString *, UIView *> *viewsDictionary =
  99. NSDictionaryOfVariableBindings(_networkCallButton, _progressView, _connectionStatusLabel);
  100. [self addConstraintsString:@"V:|-0-[_networkCallButton(40)]-[_progressView(2)]"
  101. forViewsBinding:viewsDictionary];
  102. [self addConstraintsString:@"H:|-5-[_networkCallButton]-4-[_connectionStatusLabel(90)]-5-|"
  103. forViewsBinding:viewsDictionary];
  104. [self addConstraintsString:@"H:|-0-[_progressView]-0-|" forViewsBinding:viewsDictionary];
  105. [NSLayoutConstraint activateConstraints:@[
  106. [_connectionStatusLabel.topAnchor constraintEqualToAnchor:_networkCallButton.topAnchor],
  107. [_connectionStatusLabel.bottomAnchor constraintEqualToAnchor:_networkCallButton.bottomAnchor],
  108. ]];
  109. }
  110. - (void)addConstraintsString:(NSString *)string forViewsBinding:(NSDictionary *)viewsBinding {
  111. NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:string
  112. options:kNilOptions
  113. metrics:nil
  114. views:viewsBinding];
  115. [self addConstraints:constraints];
  116. }
  117. - (void)updateConstraints {
  118. [super updateConstraints];
  119. if (self.constraints.count == 0) {
  120. [self constrainViews];
  121. }
  122. }
  123. - (void)didMoveToSuperview {
  124. [super didMoveToSuperview];
  125. if (self.superview != nil && self.subviews.count == 0) {
  126. [self createViewTree];
  127. }
  128. }
  129. #pragma mark - Private methods
  130. - (NSString *)stringForStatus:(PerfConnectionStatus)status {
  131. NSString *statusString = nil;
  132. switch (status) {
  133. case ConnectionStatus_NA:
  134. statusString = @"N/A";
  135. break;
  136. case ConnectionStatus_Fail:
  137. statusString = @"Fail";
  138. break;
  139. case ConnectionStatus_Success:
  140. statusString = @"Success";
  141. break;
  142. default:
  143. NSAssert(NO, @"Unsupported status");
  144. break;
  145. }
  146. return statusString;
  147. }
  148. @end