NetworkConnectionViewController.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // Non-google3 relative import to support building with Xcode.
  15. #import "NetworkConnectionViewController.h"
  16. #import "../Models/PerfLogger.h"
  17. #import "../Views/NetworkConnectionView.h"
  18. #import "NetworkConnectionViewController+Accessibility.h"
  19. @interface NetworkConnectionViewController () <NetworkConnectionViewDelegate>
  20. // Models
  21. @property(nonatomic, strong) id<NetworkConnection> connection;
  22. @property(nonatomic, copy) NSString *connectionTitle;
  23. // Views
  24. @property(nonatomic) UILabel *endpointLabel;
  25. @property(nonatomic, weak) NetworkConnectionView *connectionView;
  26. @end
  27. @implementation NetworkConnectionViewController
  28. #pragma mark - Initialization
  29. - (nonnull instancetype)initWithNetworkConnection:(nonnull id<NetworkConnection>)connection
  30. title:(nonnull NSString *)title {
  31. self = [super initWithNibName:nil bundle:nil];
  32. if (self) {
  33. self.connection = connection;
  34. self.connectionTitle = title;
  35. }
  36. return self;
  37. }
  38. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  39. NSAssert(NO, @"Not a valid initializer.");
  40. return nil;
  41. }
  42. #pragma mark - View life cycle
  43. - (void)loadView {
  44. UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  45. view.backgroundColor = [UIColor whiteColor];
  46. view.translatesAutoresizingMaskIntoConstraints = NO;
  47. self.view = view;
  48. [self createViewTree];
  49. [self constrainViews];
  50. }
  51. #pragma mark - Private methods
  52. - (void)createViewTree {
  53. NetworkConnectionView *connectionView = [[NetworkConnectionView alloc] init];
  54. connectionView.translatesAutoresizingMaskIntoConstraints = NO;
  55. connectionView.title = self.connectionTitle;
  56. AccessibilityItem *item = [NetworkConnectionViewController
  57. statusLabelAccessibilityItemWithConnectionName:self.connectionTitle];
  58. connectionView.connectionStatusLabel.accessibilityIdentifier = item.accessibilityID;
  59. connectionView.connectionStatusLabel.accessibilityLabel = item.accessibilityLabel;
  60. [self.view addSubview:connectionView];
  61. connectionView.delegate = self;
  62. self.connectionView = connectionView;
  63. }
  64. - (void)constrainViews {
  65. NSDictionary *viewsDict = NSDictionaryOfVariableBindings(_connectionView);
  66. NSArray *horizontal = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[_connectionView]-|"
  67. options:0
  68. metrics:nil
  69. views:viewsDict];
  70. [self.view addConstraints:horizontal];
  71. NSArray *vertical = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_connectionView]-|"
  72. options:0
  73. metrics:nil
  74. views:viewsDict];
  75. [self.view addConstraints:vertical];
  76. }
  77. /**
  78. * Determines if the autopush endpoint should be used.
  79. *
  80. * @return BOOL stating if the autopush endpoint should be used.
  81. */
  82. - (BOOL)shouldUseAutoPushEndpoint {
  83. BOOL autopushEndpoint = NO;
  84. #ifdef FPR_AUTOPUSH_ENDPOINT
  85. autopushEndpoint = YES;
  86. #endif
  87. return autopushEndpoint;
  88. }
  89. - (UILabel *)endpointLabel {
  90. if (!_endpointLabel) {
  91. _endpointLabel = [[UILabel alloc] init];
  92. _endpointLabel.translatesAutoresizingMaskIntoConstraints = NO;
  93. _endpointLabel.textAlignment = NSTextAlignmentRight;
  94. _endpointLabel.accessibilityLabel = @"Endpoint";
  95. if ([self shouldUseAutoPushEndpoint]) {
  96. _endpointLabel.text = @"autopush";
  97. } else {
  98. _endpointLabel.text = @"prod";
  99. }
  100. }
  101. return _endpointLabel;
  102. }
  103. - (void)updateUIForOperationCompleted {
  104. PerfLog(@"Operation completed - update UI");
  105. self.connectionView.networkCallButton.enabled = YES;
  106. [self.connectionView setProgress:1.0 animated:YES];
  107. }
  108. #pragma mark - NetworkConnectionViewDelegate
  109. - (void)networkConnectionViewDidTapRequestButton:(NetworkConnectionView *)connectionView {
  110. [self.connectionView setProgress:0.f animated:NO];
  111. self.connectionView.networkCallButton.enabled = NO;
  112. self.connectionView.connectionStatus = ConnectionStatus_NA;
  113. self.connectionView.progressViewColor = [UIColor blueColor];
  114. dispatch_after(0.f, dispatch_get_main_queue(), ^{
  115. [self.connectionView setProgress:0.5f animated:YES];
  116. });
  117. __weak NetworkConnectionViewController *weakSelf = self;
  118. PerfLog(@"Start perform network request");
  119. [self.connection
  120. makeNetworkRequestWithSuccessCallback:^{
  121. PerfLog(@"Network operation completed with success");
  122. dispatch_async(dispatch_get_main_queue(), ^{
  123. [weakSelf updateUIForOperationCompleted];
  124. weakSelf.connectionView.progressViewColor = [UIColor greenColor];
  125. self.connectionView.connectionStatus = ConnectionStatus_Success;
  126. PerfLog(@"Label text changed to success");
  127. });
  128. }
  129. failureCallback:^(NSError *error) {
  130. dispatch_async(dispatch_get_main_queue(), ^{
  131. PerfLog(@"Network operation completed with fail: %@", error.localizedDescription);
  132. [weakSelf updateUIForOperationCompleted];
  133. weakSelf.connectionView.progressViewColor = [UIColor redColor];
  134. self.connectionView.connectionStatus = ConnectionStatus_Fail;
  135. PerfLog(@"Label text changed to success");
  136. });
  137. }];
  138. }
  139. @end