Commit b59fb043 authored by Yi Su's avatar Yi Su Committed by Commit Bot

Migrate SearchEngineSettingsCollectionViewController from

CollectionViewController to ChromeTableViewController, and show search
engines' keywords in Settings.

This CL migrates the SearchEngineSettingsCollectionViewController from
CollectionViewController, which is based on MDCCollectionViewController,
to ChromeTableViewController, which is based on UITableViewController.

This CL also enables showing search engines' keywords in Settings for
security considerations.

Bug: 894791, 433824
Change-Id: I0eb6cf7081159d13cf389750f99ca13ee162c9d6
Reviewed-on: https://chromium-review.googlesource.com/c/1344134
Commit-Queue: Yi Su <mrsuyi@chromium.org>
Reviewed-by: default avataredchin <edchin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#609781}
parent 27a03725
...@@ -74,8 +74,8 @@ source_set("settings") { ...@@ -74,8 +74,8 @@ source_set("settings") {
"reauthentication_protocol.h", "reauthentication_protocol.h",
"save_passwords_collection_view_controller.h", "save_passwords_collection_view_controller.h",
"save_passwords_collection_view_controller.mm", "save_passwords_collection_view_controller.mm",
"search_engine_settings_collection_view_controller.h", "search_engine_table_view_controller.h",
"search_engine_settings_collection_view_controller.mm", "search_engine_table_view_controller.mm",
"settings_collection_view_controller.h", "settings_collection_view_controller.h",
"settings_collection_view_controller.mm", "settings_collection_view_controller.mm",
"settings_navigation_controller.h", "settings_navigation_controller.h",
...@@ -285,7 +285,7 @@ source_set("unit_tests") { ...@@ -285,7 +285,7 @@ source_set("unit_tests") {
"privacy_table_view_controller_unittest.mm", "privacy_table_view_controller_unittest.mm",
"reauthentication_module_unittest.mm", "reauthentication_module_unittest.mm",
"save_passwords_collection_view_controller_unittest.mm", "save_passwords_collection_view_controller_unittest.mm",
"search_engine_settings_collection_view_controller_unittest.mm", "search_engine_table_view_controller_unittest.mm",
"settings_navigation_controller_unittest.mm", "settings_navigation_controller_unittest.mm",
"settings_root_collection_view_controller_unittest.mm", "settings_root_collection_view_controller_unittest.mm",
"settings_root_table_view_controller_unittest.mm", "settings_root_table_view_controller_unittest.mm",
......
...@@ -2,27 +2,26 @@ ...@@ -2,27 +2,26 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_UI_SETTINGS_SEARCH_ENGINE_SETTINGS_COLLECTION_VIEW_CONTROLLER_H_ #ifndef IOS_CHROME_BROWSER_UI_SETTINGS_SEARCH_ENGINE_TABLE_VIEW_CONTROLLER_H_
#define IOS_CHROME_BROWSER_UI_SETTINGS_SEARCH_ENGINE_SETTINGS_COLLECTION_VIEW_CONTROLLER_H_ #define IOS_CHROME_BROWSER_UI_SETTINGS_SEARCH_ENGINE_TABLE_VIEW_CONTROLLER_H_
#import "ios/chrome/browser/ui/settings/settings_root_collection_view_controller.h" #import "ios/chrome/browser/ui/settings/settings_root_table_view_controller.h"
namespace ios { namespace ios {
class ChromeBrowserState; class ChromeBrowserState;
} // namespace ios } // namespace ios
// This class is the table view for the Search Engine settings. // This class is the table view for the Search Engine settings.
@interface SearchEngineSettingsCollectionViewController @interface SearchEngineTableViewController : SettingsRootTableViewController
: SettingsRootCollectionViewController
// The designated initializer. |browserState| must not be nil. // The designated initializer. |browserState| must not be nil.
- (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState
NS_DESIGNATED_INITIALIZER; NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithTableViewStyle:(UITableViewStyle)style
- (instancetype)initWithLayout:(UICollectionViewLayout*)layout appBarStyle:
style:(CollectionViewControllerStyle)style (ChromeTableViewControllerStyle)appBarStyle
NS_UNAVAILABLE; NS_UNAVAILABLE;
@end @end
#endif // IOS_CHROME_BROWSER_UI_SETTINGS_SEARCH_ENGINE_SETTINGS_COLLECTION_VIEW_CONTROLLER_H_ #endif // IOS_CHROME_BROWSER_UI_SETTINGS_SEARCH_ENGINE_TABLE_VIEW_CONTROLLER_H_
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#import "ios/chrome/browser/ui/settings/search_engine_settings_collection_view_controller.h" #import "ios/chrome/browser/ui/settings/search_engine_table_view_controller.h"
#include <memory> #include <memory>
...@@ -14,11 +14,9 @@ ...@@ -14,11 +14,9 @@
#include "ios/chrome/browser/browser_state/chrome_browser_state.h" #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#import "ios/chrome/browser/search_engines/search_engine_observer_bridge.h" #import "ios/chrome/browser/search_engines/search_engine_observer_bridge.h"
#include "ios/chrome/browser/search_engines/template_url_service_factory.h" #include "ios/chrome/browser/search_engines/template_url_service_factory.h"
#import "ios/chrome/browser/ui/collection_view/collection_view_model.h" #import "ios/chrome/browser/ui/table_view/cells/table_view_detail_text_item.h"
#import "ios/chrome/browser/ui/settings/cells/settings_text_item.h" #import "ios/chrome/browser/ui/table_view/cells/table_view_text_header_footer_item.h"
#include "ios/chrome/grit/ios_strings.h" #include "ios/chrome/grit/ios_strings.h"
#import "ios/third_party/material_components_ios/src/components/CollectionCells/src/MaterialCollectionCells.h"
#import "ios/third_party/material_components_ios/src/components/Palettes/src/MaterialPalettes.h"
#include "ui/base/l10n/l10n_util_mac.h" #include "ui/base/l10n/l10n_util_mac.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
...@@ -45,11 +43,10 @@ const char kUmaSelectDefaultSearchEngine[] = ...@@ -45,11 +43,10 @@ const char kUmaSelectDefaultSearchEngine[] =
} // namespace } // namespace
@interface SearchEngineSettingsCollectionViewController ()< @interface SearchEngineTableViewController ()<SearchEngineObserving>
SearchEngineObserving>
@end @end
@implementation SearchEngineSettingsCollectionViewController { @implementation SearchEngineTableViewController {
TemplateURLService* _templateURLService; // weak TemplateURLService* _templateURLService; // weak
std::unique_ptr<SearchEngineObserverBridge> _observer; std::unique_ptr<SearchEngineObserverBridge> _observer;
// Prevent unnecessary notifications when we write to the setting. // Prevent unnecessary notifications when we write to the setting.
...@@ -67,9 +64,9 @@ const char kUmaSelectDefaultSearchEngine[] = ...@@ -67,9 +64,9 @@ const char kUmaSelectDefaultSearchEngine[] =
- (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState { - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState {
DCHECK(browserState); DCHECK(browserState);
UICollectionViewLayout* layout = [[MDCCollectionViewFlowLayout alloc] init];
self = self =
[super initWithLayout:layout style:CollectionViewControllerStyleAppBar]; [super initWithTableViewStyle:UITableViewStyleGrouped
appBarStyle:ChromeTableViewControllerStyleWithAppBar];
if (self) { if (self) {
_templateURLService = _templateURLService =
ios::TemplateURLServiceFactory::GetForBrowserState(browserState); ios::TemplateURLServiceFactory::GetForBrowserState(browserState);
...@@ -77,17 +74,23 @@ const char kUmaSelectDefaultSearchEngine[] = ...@@ -77,17 +74,23 @@ const char kUmaSelectDefaultSearchEngine[] =
std::make_unique<SearchEngineObserverBridge>(self, _templateURLService); std::make_unique<SearchEngineObserverBridge>(self, _templateURLService);
_templateURLService->Load(); _templateURLService->Load();
[self setTitle:l10n_util::GetNSString(IDS_IOS_SEARCH_ENGINE_SETTING_TITLE)]; [self setTitle:l10n_util::GetNSString(IDS_IOS_SEARCH_ENGINE_SETTING_TITLE)];
[self setCollectionViewAccessibilityIdentifier:@"Search Engine"];
// TODO(crbug.com/764578): -loadModel should not be called from
// initializer. A possible fix is to move this call to -viewDidLoad.
[self loadModel];
} }
return self; return self;
} }
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loadModel];
}
#pragma mark - ChromeTableViewController
- (void)loadModel { - (void)loadModel {
[super loadModel]; [super loadModel];
CollectionViewModel* model = self.collectionViewModel; TableViewModel* model = self.tableViewModel;
[self loadSearchEngines]; [self loadSearchEngines];
// Add prior search engines. // Add prior search engines.
...@@ -95,11 +98,12 @@ const char kUmaSelectDefaultSearchEngine[] = ...@@ -95,11 +98,12 @@ const char kUmaSelectDefaultSearchEngine[] =
[model addSectionWithIdentifier:SectionIdentifierPriorSearchEngines]; [model addSectionWithIdentifier:SectionIdentifierPriorSearchEngines];
for (TemplateURL* url : _priorSearchEngines) { for (TemplateURL* url : _priorSearchEngines) {
SettingsTextItem* engine = [[SettingsTextItem alloc] TableViewDetailTextItem* engine = [[TableViewDetailTextItem alloc]
initWithType:ItemTypePriorSearchEnginesEngine]; initWithType:ItemTypePriorSearchEnginesEngine];
[engine setText:base::SysUTF16ToNSString(url->short_name())]; engine.text = base::SysUTF16ToNSString(url->short_name());
engine.detailText = base::SysUTF16ToNSString(url->keyword());
if (url == _templateURLService->GetDefaultSearchProvider()) { if (url == _templateURLService->GetDefaultSearchProvider()) {
[engine setAccessoryType:MDCCollectionViewCellAccessoryCheckmark]; [engine setAccessoryType:UITableViewCellAccessoryCheckmark];
} }
[model addItem:engine [model addItem:engine
toSectionWithIdentifier:SectionIdentifierPriorSearchEngines]; toSectionWithIdentifier:SectionIdentifierPriorSearchEngines];
...@@ -110,20 +114,21 @@ const char kUmaSelectDefaultSearchEngine[] = ...@@ -110,20 +114,21 @@ const char kUmaSelectDefaultSearchEngine[] =
if (_customSearchEngines.size() > 0) { if (_customSearchEngines.size() > 0) {
[model addSectionWithIdentifier:SectionIdentifierCustomSearchEngines]; [model addSectionWithIdentifier:SectionIdentifierCustomSearchEngines];
SettingsTextItem* header = [[SettingsTextItem alloc] TableViewTextHeaderFooterItem* header =
initWithType:ItemTypeCustomSearchEnginesEngineHeader]; [[TableViewTextHeaderFooterItem alloc]
initWithType:ItemTypeCustomSearchEnginesEngineHeader];
header.text = l10n_util::GetNSString( header.text = l10n_util::GetNSString(
IDS_IOS_SEARCH_ENGINE_SETTING_CUSTOM_SECTION_HEADER); IDS_IOS_SEARCH_ENGINE_SETTING_CUSTOM_SECTION_HEADER);
header.textColor = [[MDCPalette greyPalette] tint500];
[model setHeader:header [model setHeader:header
forSectionWithIdentifier:SectionIdentifierCustomSearchEngines]; forSectionWithIdentifier:SectionIdentifierCustomSearchEngines];
for (TemplateURL* url : _customSearchEngines) { for (TemplateURL* url : _customSearchEngines) {
SettingsTextItem* engine = [[SettingsTextItem alloc] TableViewDetailTextItem* engine = [[TableViewDetailTextItem alloc]
initWithType:ItemTypeCustomSearchEnginesEngine]; initWithType:ItemTypeCustomSearchEnginesEngine];
[engine setText:base::SysUTF16ToNSString(url->short_name())]; engine.text = base::SysUTF16ToNSString(url->short_name());
engine.detailText = base::SysUTF16ToNSString(url->keyword());
if (url == _templateURLService->GetDefaultSearchProvider()) { if (url == _templateURLService->GetDefaultSearchProvider()) {
[engine setAccessoryType:MDCCollectionViewCellAccessoryCheckmark]; [engine setAccessoryType:UITableViewCellAccessoryCheckmark];
} }
[model addItem:engine [model addItem:engine
toSectionWithIdentifier:SectionIdentifierCustomSearchEngines]; toSectionWithIdentifier:SectionIdentifierCustomSearchEngines];
...@@ -131,25 +136,24 @@ const char kUmaSelectDefaultSearchEngine[] = ...@@ -131,25 +136,24 @@ const char kUmaSelectDefaultSearchEngine[] =
} }
} }
#pragma mark UICollectionViewDelegate #pragma mark UITableViewDelegate
- (void)collectionView:(UICollectionView*)collectionView - (void)tableView:(UITableView*)tableView
didSelectItemAtIndexPath:(NSIndexPath*)indexPath { didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
[super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; [super tableView:tableView didSelectRowAtIndexPath:indexPath];
CollectionViewModel* model = self.collectionViewModel; TableViewModel* model = self.tableViewModel;
// Only handle taps on search engine items. // Only handle taps on search engine items.
CollectionViewItem* selectedItem = [model itemAtIndexPath:indexPath]; TableViewItem* selectedItem = [model itemAtIndexPath:indexPath];
if (selectedItem.type != ItemTypePriorSearchEnginesEngine && if (selectedItem.type != ItemTypePriorSearchEnginesEngine &&
selectedItem.type != ItemTypeCustomSearchEnginesEngine) { selectedItem.type != ItemTypeCustomSearchEnginesEngine) {
return; return;
} }
// Do nothing if the tapped engine was already the default. // Do nothing if the tapped engine was already the default.
SettingsTextItem* selectedTextItem = TableViewDetailTextItem* selectedTextItem =
base::mac::ObjCCastStrict<SettingsTextItem>(selectedItem); base::mac::ObjCCastStrict<TableViewDetailTextItem>(selectedItem);
if (selectedTextItem.accessoryType == if (selectedTextItem.accessoryType == UITableViewCellAccessoryCheckmark) {
MDCCollectionViewCellAccessoryCheckmark) {
return; return;
} }
...@@ -158,42 +162,42 @@ const char kUmaSelectDefaultSearchEngine[] = ...@@ -158,42 +162,42 @@ const char kUmaSelectDefaultSearchEngine[] =
// Iterate through the engines and remove the checkmark from any that have it. // Iterate through the engines and remove the checkmark from any that have it.
if ([model if ([model
hasSectionForSectionIdentifier:SectionIdentifierPriorSearchEngines]) { hasSectionForSectionIdentifier:SectionIdentifierPriorSearchEngines]) {
for (CollectionViewItem* item in for (TableViewItem* item in
[model itemsInSectionWithIdentifier: [model itemsInSectionWithIdentifier:
SectionIdentifierPriorSearchEngines]) { SectionIdentifierPriorSearchEngines]) {
if (item.type != ItemTypePriorSearchEnginesEngine) { if (item.type != ItemTypePriorSearchEnginesEngine) {
continue; continue;
} }
SettingsTextItem* textItem = TableViewDetailTextItem* textItem =
base::mac::ObjCCastStrict<SettingsTextItem>(item); base::mac::ObjCCastStrict<TableViewDetailTextItem>(item);
if (textItem.accessoryType == MDCCollectionViewCellAccessoryCheckmark) { if (textItem.accessoryType == UITableViewCellAccessoryCheckmark) {
textItem.accessoryType = MDCCollectionViewCellAccessoryNone; textItem.accessoryType = UITableViewCellAccessoryNone;
[modifiedItems addObject:textItem]; [modifiedItems addObject:textItem];
} }
} }
} }
if ([model hasSectionForSectionIdentifier: if ([model hasSectionForSectionIdentifier:
SectionIdentifierCustomSearchEngines]) { SectionIdentifierCustomSearchEngines]) {
for (CollectionViewItem* item in for (TableViewItem* item in
[model itemsInSectionWithIdentifier: [model itemsInSectionWithIdentifier:
SectionIdentifierCustomSearchEngines]) { SectionIdentifierCustomSearchEngines]) {
if (item.type != ItemTypeCustomSearchEnginesEngine) { if (item.type != ItemTypeCustomSearchEnginesEngine) {
continue; continue;
} }
SettingsTextItem* textItem = TableViewDetailTextItem* textItem =
base::mac::ObjCCastStrict<SettingsTextItem>(item); base::mac::ObjCCastStrict<TableViewDetailTextItem>(item);
if (textItem.accessoryType == MDCCollectionViewCellAccessoryCheckmark) { if (textItem.accessoryType == UITableViewCellAccessoryCheckmark) {
textItem.accessoryType = MDCCollectionViewCellAccessoryNone; textItem.accessoryType = UITableViewCellAccessoryNone;
[modifiedItems addObject:textItem]; [modifiedItems addObject:textItem];
} }
} }
} }
// Show the checkmark on the new default engine. // Show the checkmark on the new default engine.
SettingsTextItem* newDefaultEngine = TableViewDetailTextItem* newDefaultEngine =
base::mac::ObjCCastStrict<SettingsTextItem>( base::mac::ObjCCastStrict<TableViewDetailTextItem>(
[model itemAtIndexPath:indexPath]); [model itemAtIndexPath:indexPath]);
newDefaultEngine.accessoryType = MDCCollectionViewCellAccessoryCheckmark; newDefaultEngine.accessoryType = UITableViewCellAccessoryCheckmark;
[modifiedItems addObject:newDefaultEngine]; [modifiedItems addObject:newDefaultEngine];
// Set the new engine as the default. // Set the new engine as the default.
...@@ -205,6 +209,7 @@ const char kUmaSelectDefaultSearchEngine[] = ...@@ -205,6 +209,7 @@ const char kUmaSelectDefaultSearchEngine[] =
[model indexInItemTypeForIndexPath:indexPath]]; [model indexInItemTypeForIndexPath:indexPath]];
[self reconfigureCellsForItems:modifiedItems]; [self reconfigureCellsForItems:modifiedItems];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
} }
#pragma mark Internal methods #pragma mark Internal methods
......
...@@ -56,7 +56,7 @@ ...@@ -56,7 +56,7 @@
#import "ios/chrome/browser/ui/settings/material_cell_catalog_view_controller.h" #import "ios/chrome/browser/ui/settings/material_cell_catalog_view_controller.h"
#import "ios/chrome/browser/ui/settings/privacy_table_view_controller.h" #import "ios/chrome/browser/ui/settings/privacy_table_view_controller.h"
#import "ios/chrome/browser/ui/settings/save_passwords_collection_view_controller.h" #import "ios/chrome/browser/ui/settings/save_passwords_collection_view_controller.h"
#import "ios/chrome/browser/ui/settings/search_engine_settings_collection_view_controller.h" #import "ios/chrome/browser/ui/settings/search_engine_table_view_controller.h"
#import "ios/chrome/browser/ui/settings/sync_utils/sync_util.h" #import "ios/chrome/browser/ui/settings/sync_utils/sync_util.h"
#import "ios/chrome/browser/ui/settings/table_cell_catalog_view_controller.h" #import "ios/chrome/browser/ui/settings/table_cell_catalog_view_controller.h"
#import "ios/chrome/browser/ui/settings/utils/pref_backed_boolean.h" #import "ios/chrome/browser/ui/settings/utils/pref_backed_boolean.h"
...@@ -855,7 +855,7 @@ void IdentityObserverBridge::OnPrimaryAccountCleared( ...@@ -855,7 +855,7 @@ void IdentityObserverBridge::OnPrimaryAccountCleared(
[self showSyncGoogleService]; [self showSyncGoogleService];
break; break;
case ItemTypeSearchEngine: case ItemTypeSearchEngine:
controller = [[SearchEngineSettingsCollectionViewController alloc] controller = [[SearchEngineTableViewController alloc]
initWithBrowserState:_browserState]; initWithBrowserState:_browserState];
break; break;
case ItemTypeSavedPasswords: case ItemTypeSavedPasswords:
......
...@@ -29,6 +29,10 @@ ...@@ -29,6 +29,10 @@
// using shouldHideDoneButton to know if it should display the edit button. // using shouldHideDoneButton to know if it should display the edit button.
- (void)updateEditButton; - (void)updateEditButton;
// Reloads the table view model with |loadModel| and then reloads the
// table view data.
- (void)reloadData;
@end @end
// Subclasses of SettingsRootTableViewController should implement the // Subclasses of SettingsRootTableViewController should implement the
......
...@@ -47,6 +47,11 @@ ...@@ -47,6 +47,11 @@
} }
} }
- (void)reloadData {
[self loadModel];
[self.tableView reloadData];
}
#pragma mark - Property #pragma mark - Property
- (UIBarButtonItem*)deleteButton { - (UIBarButtonItem*)deleteButton {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment