[iOS] Add a new section for the migration to account button

Screenshot: https://screenshot.googleplex.com/8TUPupDtACEm4g3.png
Bug: 1482269
Change-Id: I2073942c2ad9fc0a3d52cc9f0bf9f4d5d70ef410
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5522297
Reviewed-by: Tommy Martino <tmartino@chromium.org>
Commit-Queue: Vidhan Jain <vidhanj@google.com>
Cr-Commit-Position: refs/heads/main@{#1298074}
diff --git a/ios/chrome/browser/ui/autofill/autofill_profile_edit_table_view_constants.h b/ios/chrome/browser/ui/autofill/autofill_profile_edit_table_view_constants.h
index c4252db..1d7d02f 100644
--- a/ios/chrome/browser/ui/autofill/autofill_profile_edit_table_view_constants.h
+++ b/ios/chrome/browser/ui/autofill/autofill_profile_edit_table_view_constants.h
@@ -19,6 +19,7 @@
   AutofillProfileDetailsSectionIdentifierAddress,
   AutofillProfileDetailsSectionIdentifierPhoneEmail,
   AutofillProfileDetailsSectionIdentifierButton,
+  AutofillProfileDetailsSectionIdentifierMigrationButton,
   AutofillProfileDetailsSectionIdentifierErrorFooter,
   AutofillProfileDetailsSectionIdentifierFooter,
 };
diff --git a/ios/chrome/browser/ui/autofill/autofill_profile_edit_table_view_controller.mm b/ios/chrome/browser/ui/autofill/autofill_profile_edit_table_view_controller.mm
index fed1328f..dd78ee0e 100644
--- a/ios/chrome/browser/ui/autofill/autofill_profile_edit_table_view_controller.mm
+++ b/ios/chrome/browser/ui/autofill/autofill_profile_edit_table_view_controller.mm
@@ -320,7 +320,8 @@
   CHECK(_settingsView);
   TableViewModel* model = _controller.tableViewModel;
 
-  if (self.accountProfile && _userEmail != nil) {
+  if (self.accountProfile) {
+    CHECK(_userEmail);
     [model
         addSectionWithIdentifier:AutofillProfileDetailsSectionIdentifierFooter];
     [model setFooter:[self footerItem]
diff --git a/ios/chrome/browser/ui/settings/autofill/BUILD.gn b/ios/chrome/browser/ui/settings/autofill/BUILD.gn
index eb4ee6ce..b11d66bea 100644
--- a/ios/chrome/browser/ui/settings/autofill/BUILD.gn
+++ b/ios/chrome/browser/ui/settings/autofill/BUILD.gn
@@ -34,6 +34,7 @@
     "//components/autofill/core/browser",
     "//components/autofill/core/common",
     "//components/autofill/ios/browser",
+    "//components/autofill/ios/common",
     "//components/password_manager/core/common:features",
     "//components/prefs",
     "//components/strings",
diff --git a/ios/chrome/browser/ui/settings/autofill/autofill_settings_profile_edit_table_view_controller.mm b/ios/chrome/browser/ui/settings/autofill/autofill_settings_profile_edit_table_view_controller.mm
index 3c51ef0f..c8aaee1 100644
--- a/ios/chrome/browser/ui/settings/autofill/autofill_settings_profile_edit_table_view_controller.mm
+++ b/ios/chrome/browser/ui/settings/autofill/autofill_settings_profile_edit_table_view_controller.mm
@@ -4,7 +4,9 @@
 
 #import "ios/chrome/browser/ui/settings/autofill/autofill_settings_profile_edit_table_view_controller.h"
 
+#import "base/feature_list.h"
 #import "base/strings/sys_string_conversions.h"
+#import "components/autofill/ios/common/features.h"
 #import "ios/chrome/browser/shared/public/commands/snackbar_commands.h"
 #import "ios/chrome/browser/shared/ui/symbols/symbols.h"
 #import "ios/chrome/browser/shared/ui/table_view/cells/table_view_text_item.h"
@@ -83,10 +85,18 @@
 
   TableViewModel* model = self.tableViewModel;
   if (_showMigrateToAccountSection) {
+    AutofillProfileDetailsSectionIdentifier section =
+        AutofillProfileDetailsSectionIdentifierFields;
+    if (base::FeatureList::IsEnabled(
+            kAutofillDynamicallyLoadsFieldsForAddressInput)) {
+      section = AutofillProfileDetailsSectionIdentifierMigrationButton;
+      [model addSectionWithIdentifier:
+                 AutofillProfileDetailsSectionIdentifierMigrationButton];
+    }
     [model addItem:[self migrateToAccountRecommendationItem]
-        toSectionWithIdentifier:AutofillProfileDetailsSectionIdentifierFields];
+        toSectionWithIdentifier:section];
     [model addItem:[self migrateToAccountButtonItem]
-        toSectionWithIdentifier:AutofillProfileDetailsSectionIdentifierFields];
+        toSectionWithIdentifier:section];
   }
 
   [self.handler loadFooterForSettings];
@@ -123,6 +133,14 @@
 
   [self loadModel];
   [self.handler reconfigureCells];
+  if (_showMigrateToAccountSection &&
+      base::FeatureList::IsEnabled(
+          kAutofillDynamicallyLoadsFieldsForAddressInput)) {
+    [self reconfigureCellsForItems:
+              [self.tableViewModel
+                  itemsInSectionWithIdentifier:
+                      AutofillProfileDetailsSectionIdentifierMigrationButton]];
+  }
 }
 
 #pragma mark - UITableViewDataSource
@@ -246,34 +264,55 @@
   [self
       performBatchTableViewUpdates:^{
         TableViewModel* model = weakSelf.tableViewModel;
-        NSIndexPath* indexPathForMigrateRecommendationItem = [model
-            indexPathForItemType:
-                AutofillProfileDetailsItemTypeMigrateToAccountRecommendation
-               sectionIdentifier:AutofillProfileDetailsSectionIdentifierFields];
-        NSIndexPath* indexPathForMigrateButton = [model
-            indexPathForItemType:
-                AutofillProfileDetailsItemTypeMigrateToAccountButton
-               sectionIdentifier:AutofillProfileDetailsSectionIdentifierFields];
+        if (base::FeatureList::IsEnabled(
+                kAutofillDynamicallyLoadsFieldsForAddressInput)) {
+          [self removeSectionWithIdentifier:
+                    AutofillProfileDetailsSectionIdentifierMigrationButton
+                           withRowAnimation:UITableViewRowAnimationFade];
+        } else {
+          NSIndexPath* indexPathForMigrateRecommendationItem = [model
+              indexPathForItemType:
+                  AutofillProfileDetailsItemTypeMigrateToAccountRecommendation
+                 sectionIdentifier:
+                     AutofillProfileDetailsSectionIdentifierFields];
+          NSIndexPath* indexPathForMigrateButton =
+              [model indexPathForItemType:
+                         AutofillProfileDetailsItemTypeMigrateToAccountButton
+                        sectionIdentifier:
+                            AutofillProfileDetailsSectionIdentifierFields];
 
-        [model removeItemWithType:
-                   AutofillProfileDetailsItemTypeMigrateToAccountRecommendation
-            fromSectionWithIdentifier:
-                AutofillProfileDetailsSectionIdentifierFields];
-        [model removeItemWithType:
-                   AutofillProfileDetailsItemTypeMigrateToAccountButton
-            fromSectionWithIdentifier:
-                AutofillProfileDetailsSectionIdentifierFields];
+          [model removeItemWithType:
+                     AutofillProfileDetailsItemTypeMigrateToAccountRecommendation
+              fromSectionWithIdentifier:
+                  AutofillProfileDetailsSectionIdentifierFields];
+          [model removeItemWithType:
+                     AutofillProfileDetailsItemTypeMigrateToAccountButton
+              fromSectionWithIdentifier:
+                  AutofillProfileDetailsSectionIdentifierFields];
 
-        [weakSelf.tableView
-            deleteRowsAtIndexPaths:@[
-              indexPathForMigrateRecommendationItem, indexPathForMigrateButton
-            ]
-                  withRowAnimation:UITableViewRowAnimationAutomatic];
+          [weakSelf.tableView
+              deleteRowsAtIndexPaths:@[
+                indexPathForMigrateRecommendationItem, indexPathForMigrateButton
+              ]
+                    withRowAnimation:UITableViewRowAnimationAutomatic];
+        }
       }
                         completion:onCompletion];
   _showMigrateToAccountSection = NO;
 }
 
+// Removes the given section if it exists.
+- (void)removeSectionWithIdentifier:(NSInteger)sectionIdentifier
+                   withRowAnimation:(UITableViewRowAnimation)animation {
+  TableViewModel* model = self.tableViewModel;
+  if ([model hasSectionForSectionIdentifier:sectionIdentifier]) {
+    NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier];
+    [model removeSectionWithIdentifier:sectionIdentifier];
+    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:section]
+                  withRowAnimation:animation];
+  }
+}
+
 - (void)showPostMigrationToast {
   CHECK(self.snackbarCommandsHandler);
   CHECK(_userEmail.length)