[iOS]🐻[FRE] Create shared overlay view

This CL creates an overlay view which can be shared among different UI.

Bug: 1189836
Change-Id: I4f14eb429b9e1a39d1a5c23a7fca1037f020ad1a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2840454
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Auto-Submit: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: Mark Cogan <marq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#875090}
GitOrigin-RevId: 6fcb47684b505e5955a5f153ae48de115f06a576
diff --git a/chrome/browser/ui/elements/BUILD.gn b/chrome/browser/ui/elements/BUILD.gn
index bcbbf2c..63aa420 100644
--- a/chrome/browser/ui/elements/BUILD.gn
+++ b/chrome/browser/ui/elements/BUILD.gn
@@ -82,6 +82,8 @@
   sources = [
     "activity_overlay_coordinator.h",
     "activity_overlay_coordinator.mm",
+    "activity_overlay_view.h",
+    "activity_overlay_view.mm",
     "activity_overlay_view_controller.h",
     "activity_overlay_view_controller.mm",
     "chrome_activity_overlay_coordinator.h",
diff --git a/chrome/browser/ui/elements/activity_overlay_view.h b/chrome/browser/ui/elements/activity_overlay_view.h
new file mode 100644
index 0000000..bb4bea4
--- /dev/null
+++ b/chrome/browser/ui/elements/activity_overlay_view.h
@@ -0,0 +1,23 @@
+// Copyright 2021 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef IOS_CHROME_BROWSER_UI_ELEMENTS_ACTIVITY_OVERLAY_VIEW_H_
+#define IOS_CHROME_BROWSER_UI_ELEMENTS_ACTIVITY_OVERLAY_VIEW_H_
+
+#import <UIKit/UIKit.h>
+
+// View with a translucent background and an activity indicator in the middle.
+@interface ActivityOverlayView : UIView
+
+- (instancetype)init NS_DESIGNATED_INITIALIZER;
+
+- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE;
+- (instancetype)initWithCoder:(NSCoder*)coder NS_UNAVAILABLE;
+
+// The activity indicator of the view.
+@property(nonatomic, strong, readonly) UIActivityIndicatorView* indicator;
+
+@end
+
+#endif  // IOS_CHROME_BROWSER_UI_ELEMENTS_ACTIVITY_OVERLAY_VIEW_H_
diff --git a/chrome/browser/ui/elements/activity_overlay_view.mm b/chrome/browser/ui/elements/activity_overlay_view.mm
new file mode 100644
index 0000000..78915c5
--- /dev/null
+++ b/chrome/browser/ui/elements/activity_overlay_view.mm
@@ -0,0 +1,37 @@
+// Copyright 2021 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import "ios/chrome/browser/ui/elements/activity_overlay_view.h"
+
+#import "ios/chrome/browser/ui/util/uikit_ui_util.h"
+#import "ios/chrome/common/ui/colors/semantic_color_names.h"
+#import "ios/chrome/common/ui/util/constraints_ui_util.h"
+
+#if !defined(__has_feature) || !__has_feature(objc_arc)
+#error "This file requires ARC support."
+#endif
+
+@implementation ActivityOverlayView
+
+- (instancetype)init {
+  self = [super initWithFrame:CGRectZero];
+  if (self) {
+    _indicator = GetLargeUIActivityIndicatorView();
+    _indicator.translatesAutoresizingMaskIntoConstraints = NO;
+  }
+  return self;
+}
+
+- (void)didMoveToSuperview {
+  if (self.subviews.count == 0) {
+    // This is the first time the view is used, finish setting everything up.
+    self.backgroundColor = [UIColor colorNamed:kScrimBackgroundColor];
+    [self addSubview:_indicator];
+    AddSameCenterConstraints(self, _indicator);
+    [_indicator startAnimating];
+  }
+  [super didMoveToSuperview];
+}
+
+@end
diff --git a/chrome/browser/ui/elements/activity_overlay_view_controller.mm b/chrome/browser/ui/elements/activity_overlay_view_controller.mm
index dfd0c25..52f324b 100644
--- a/chrome/browser/ui/elements/activity_overlay_view_controller.mm
+++ b/chrome/browser/ui/elements/activity_overlay_view_controller.mm
@@ -4,36 +4,16 @@
 
 #import "ios/chrome/browser/ui/elements/activity_overlay_view_controller.h"
 
-#import <MaterialComponents/MaterialActivityIndicator.h>
-
-#import "ios/chrome/browser/ui/material_components/activity_indicator.h"
-#import "ios/chrome/common/ui/util/constraints_ui_util.h"
+#import "ios/chrome/browser/ui/elements/activity_overlay_view.h"
 
 #if !defined(__has_feature) || !__has_feature(objc_arc)
 #error "This file requires ARC support."
 #endif
 
-namespace {
-// Size of the activity indicator.
-const CGFloat kActivityIndicatorSize = 48;
-// Alpha of the presented view's background.
-const CGFloat kBackgroundAlpha = 0.5;
-}
-
 @implementation ActivityOverlayViewController
 
-- (void)viewDidLoad {
-  [super viewDidLoad];
-  self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:kBackgroundAlpha];
-
-  MDCActivityIndicator* activityIndicator = [[MDCActivityIndicator alloc]
-      initWithFrame:CGRectMake(0, 0, kActivityIndicatorSize,
-                               kActivityIndicatorSize)];
-  activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;
-  [self.view addSubview:activityIndicator];
-  AddSameCenterConstraints(self.view, activityIndicator);
-  activityIndicator.cycleColors = ActivityIndicatorBrandedCycleColors();
-  [activityIndicator startAnimating];
+- (void)loadView {
+  self.view = [[ActivityOverlayView alloc] init];
 }
 
 @end
diff --git a/chrome/browser/ui/first_run/BUILD.gn b/chrome/browser/ui/first_run/BUILD.gn
index d9b6766..fdcb53d 100644
--- a/chrome/browser/ui/first_run/BUILD.gn
+++ b/chrome/browser/ui/first_run/BUILD.gn
@@ -97,6 +97,7 @@
     "//ios/chrome/browser/ui/authentication/views",
     "//ios/chrome/browser/ui/commands",
     "//ios/chrome/browser/ui/elements",
+    "//ios/chrome/browser/ui/elements:elements_internal",
     "//ios/chrome/browser/ui/fancy_ui",
     "//ios/chrome/browser/ui/icons",
     "//ios/chrome/browser/ui/material_components",
diff --git a/chrome/browser/ui/first_run/signin_screen_view_controller.mm b/chrome/browser/ui/first_run/signin_screen_view_controller.mm
index 356256d..f4d741c 100644
--- a/chrome/browser/ui/first_run/signin_screen_view_controller.mm
+++ b/chrome/browser/ui/first_run/signin_screen_view_controller.mm
@@ -4,31 +4,22 @@
 
 #import "ios/chrome/browser/ui/first_run/signin_screen_view_controller.h"
 
-#import <MaterialComponents/MaterialActivityIndicator.h>
 
 #import "ios/chrome/browser/ui/authentication/views/identity_button_control.h"
-#import "ios/chrome/browser/ui/material_components/activity_indicator.h"
-#import "ios/chrome/common/ui/colors/semantic_color_names.h"
+#import "ios/chrome/browser/ui/elements/activity_overlay_view.h"
 #import "ios/chrome/common/ui/util/constraints_ui_util.h"
 
 #if !defined(__has_feature) || !__has_feature(objc_arc)
 #error "This file requires ARC support."
 #endif
 
-namespace {
-// Size of the activity indicator.
-const CGFloat kActivityIndicatorSize = 48;
-}  // namespace
-
 @interface SigninScreenViewController ()
 
 // Button controlling the display of the selected identity.
 @property(nonatomic, strong) IdentityButtonControl* identityControl;
 
 // Scrim displayed above the view when the UI is disabled.
-@property(nonatomic, strong) UIView* scrimView;
-// Activity indicator for the scrim view.
-@property(nonatomic, strong) MDCActivityIndicator* activityIndicator;
+@property(nonatomic, strong) ActivityOverlayView* overlay;
 
 @end
 
@@ -57,21 +48,12 @@
 
 #pragma mark - Properties
 
-- (UIView*)scrimView {
-  if (!_scrimView) {
-    _scrimView = [[UIView alloc] initWithFrame:CGRectZero];
-    _scrimView.translatesAutoresizingMaskIntoConstraints = NO;
-    _scrimView.backgroundColor = [UIColor colorNamed:kScrimBackgroundColor];
-
-    self.activityIndicator = [[MDCActivityIndicator alloc]
-        initWithFrame:CGRectMake(0, 0, kActivityIndicatorSize,
-                                 kActivityIndicatorSize)];
-    self.activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;
-    [self.view addSubview:self.activityIndicator];
-    AddSameCenterConstraints(_scrimView, self.activityIndicator);
-    self.activityIndicator.cycleColors = ActivityIndicatorBrandedCycleColors();
+- (ActivityOverlayView*)overlay {
+  if (!_overlay) {
+    _overlay = [[ActivityOverlayView alloc] init];
+    _overlay.translatesAutoresizingMaskIntoConstraints = NO;
   }
-  return _scrimView;
+  return _overlay;
 }
 
 #pragma mark - SignInScreenConsumer
@@ -95,23 +77,22 @@
 
 - (void)setUIEnabled:(BOOL)UIEnabled {
   if (UIEnabled) {
-    [self.scrimView removeFromSuperview];
-    self.scrimView = nil;
+    [self.overlay removeFromSuperview];
   } else {
-    [self.view addSubview:self.scrimView];
-    AddSameConstraints(self.view, self.scrimView);
-    [self.activityIndicator startAnimating];
+    [self.view addSubview:self.overlay];
+    AddSameConstraints(self.view, self.overlay);
+    [self.overlay.indicator startAnimating];
   }
 }
 
 #pragma mark - AuthenticationFlowDelegate
 
 - (void)didPresentDialog {
-  [self.activityIndicator stopAnimating];
+  [self.overlay.indicator stopAnimating];
 }
 
 - (void)didDismissDialog {
-  [self.activityIndicator startAnimating];
+  [self.overlay.indicator startAnimating];
 }
 
 #pragma mark - Private