OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/settings/search_engine_settings_collection_view_c
ontroller.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/ios/weak_nsobject.h" |
| 10 #include "base/mac/foundation_util.h" |
| 11 #include "base/mac/scoped_nsobject.h" |
| 12 #include "base/strings/sys_string_conversions.h" |
| 13 #include "components/search_engines/template_url_service.h" |
| 14 #include "components/search_engines/template_url_service_observer.h" |
| 15 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 16 #include "ios/chrome/browser/search_engines/template_url_service_factory.h" |
| 17 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" |
| 18 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 19 #include "ios/chrome/grit/ios_strings.h" |
| 20 #import "ios/third_party/material_components_ios/src/components/CollectionCells/
src/MaterialCollectionCells.h" |
| 21 #include "ui/base/l10n/l10n_util_mac.h" |
| 22 |
| 23 @interface SearchEngineSettingsCollectionViewController () |
| 24 - (void)onChange; |
| 25 @end |
| 26 |
| 27 namespace { |
| 28 |
| 29 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 30 SectionIdentifierSearchEngines = kSectionIdentifierEnumZero, |
| 31 }; |
| 32 |
| 33 typedef NS_ENUM(NSInteger, ItemType) { |
| 34 ItemTypeSearchEnginesEngine = kItemTypeEnumZero, |
| 35 }; |
| 36 |
| 37 // Observer used to reload the Search Engine collection view once the |
| 38 // TemplateURLService changes, either on first load or due to a |
| 39 // policy change. |
| 40 class SearchEngineObserver : public TemplateURLServiceObserver { |
| 41 public: |
| 42 SearchEngineObserver(SearchEngineSettingsCollectionViewController* owner, |
| 43 TemplateURLService* urlService); |
| 44 ~SearchEngineObserver() override; |
| 45 void OnTemplateURLServiceChanged() override; |
| 46 |
| 47 private: |
| 48 base::WeakNSObject<SearchEngineSettingsCollectionViewController> owner_; |
| 49 TemplateURLService* templateURLService_; // weak |
| 50 }; |
| 51 |
| 52 SearchEngineObserver::SearchEngineObserver( |
| 53 SearchEngineSettingsCollectionViewController* owner, |
| 54 TemplateURLService* urlService) |
| 55 : owner_(owner), templateURLService_(urlService) { |
| 56 templateURLService_->AddObserver(this); |
| 57 } |
| 58 |
| 59 SearchEngineObserver::~SearchEngineObserver() { |
| 60 templateURLService_->RemoveObserver(this); |
| 61 } |
| 62 |
| 63 void SearchEngineObserver::OnTemplateURLServiceChanged() { |
| 64 base::scoped_nsobject<SearchEngineSettingsCollectionViewController> |
| 65 strongOwner([owner_.get() retain]); |
| 66 [strongOwner onChange]; |
| 67 } |
| 68 |
| 69 } // namespace |
| 70 |
| 71 @implementation SearchEngineSettingsCollectionViewController { |
| 72 TemplateURLService* templateURLService_; // weak |
| 73 std::unique_ptr<SearchEngineObserver> observer_; |
| 74 // Prevent unnecessary notifications when we write to the setting. |
| 75 BOOL updatingBackend_; |
| 76 } |
| 77 |
| 78 #pragma mark Initialization |
| 79 |
| 80 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState { |
| 81 DCHECK(browserState); |
| 82 self = [super initWithStyle:CollectionViewControllerStyleAppBar]; |
| 83 if (self) { |
| 84 templateURLService_ = |
| 85 ios::TemplateURLServiceFactory::GetForBrowserState(browserState); |
| 86 observer_.reset(new SearchEngineObserver(self, templateURLService_)); |
| 87 templateURLService_->Load(); |
| 88 [self setTitle:l10n_util::GetNSString(IDS_IOS_SEARCH_ENGINE_SETTING_TITLE)]; |
| 89 [self setCollectionViewAccessibilityIdentifier:@"Search Engine"]; |
| 90 [self loadModel]; |
| 91 } |
| 92 return self; |
| 93 } |
| 94 |
| 95 - (void)loadModel { |
| 96 [super loadModel]; |
| 97 CollectionViewModel* model = self.collectionViewModel; |
| 98 NSArray* values = [self allValues]; |
| 99 |
| 100 // Do not add any sections if there are no search engines. |
| 101 if (![values count]) { |
| 102 return; |
| 103 } |
| 104 |
| 105 [model addSectionWithIdentifier:SectionIdentifierSearchEngines]; |
| 106 for (NSUInteger i = 0; i < values.count; i++) { |
| 107 NSString* value = values[i]; |
| 108 BOOL checked = [value isEqualToString:[self currentValue]]; |
| 109 |
| 110 base::scoped_nsobject<CollectionViewTextItem> engine( |
| 111 [[CollectionViewTextItem alloc] |
| 112 initWithType:ItemTypeSearchEnginesEngine]); |
| 113 [engine setText:value]; |
| 114 if (checked) { |
| 115 [engine setAccessoryType:MDCCollectionViewCellAccessoryCheckmark]; |
| 116 } |
| 117 [model addItem:engine |
| 118 toSectionWithIdentifier:SectionIdentifierSearchEngines]; |
| 119 } |
| 120 } |
| 121 |
| 122 #pragma mark UICollectionViewDelegate |
| 123 |
| 124 - (void)collectionView:(UICollectionView*)collectionView |
| 125 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
| 126 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; |
| 127 CollectionViewModel* model = self.collectionViewModel; |
| 128 |
| 129 // Only handle taps on search engine items. |
| 130 CollectionViewItem* selectedItem = [model itemAtIndexPath:indexPath]; |
| 131 if (selectedItem.type != ItemTypeSearchEnginesEngine) { |
| 132 return; |
| 133 } |
| 134 |
| 135 // Do nothing if the tapped engine was already the default. |
| 136 CollectionViewTextItem* selectedTextItem = |
| 137 base::mac::ObjCCastStrict<CollectionViewTextItem>(selectedItem); |
| 138 if (selectedTextItem.accessoryType == |
| 139 MDCCollectionViewCellAccessoryCheckmark) { |
| 140 return; |
| 141 } |
| 142 |
| 143 // Iterate through the engines and remove the checkmark from any that have it. |
| 144 NSMutableArray* modifiedItems = [NSMutableArray array]; |
| 145 for (CollectionViewItem* item in |
| 146 [model itemsInSectionWithIdentifier:SectionIdentifierSearchEngines]) { |
| 147 if (item.type != ItemTypeSearchEnginesEngine) { |
| 148 continue; |
| 149 } |
| 150 |
| 151 CollectionViewTextItem* textItem = |
| 152 base::mac::ObjCCastStrict<CollectionViewTextItem>(item); |
| 153 if (textItem.accessoryType == MDCCollectionViewCellAccessoryCheckmark) { |
| 154 textItem.accessoryType = MDCCollectionViewCellAccessoryNone; |
| 155 [modifiedItems addObject:textItem]; |
| 156 } |
| 157 } |
| 158 |
| 159 // Show the checkmark on the new default engine. |
| 160 CollectionViewTextItem* newDefaultEngine = |
| 161 base::mac::ObjCCastStrict<CollectionViewTextItem>( |
| 162 [model itemAtIndexPath:indexPath]); |
| 163 newDefaultEngine.accessoryType = MDCCollectionViewCellAccessoryCheckmark; |
| 164 [modifiedItems addObject:newDefaultEngine]; |
| 165 |
| 166 // Set the new engine as the default. |
| 167 [self setValueFromIndex:[model indexInItemTypeForIndexPath:indexPath]]; |
| 168 |
| 169 [self reconfigureCellsForItems:modifiedItems |
| 170 inSectionWithIdentifier:SectionIdentifierSearchEngines]; |
| 171 } |
| 172 |
| 173 #pragma mark Internal methods |
| 174 |
| 175 - (NSArray*)allValues { |
| 176 std::vector<TemplateURL*> urls = templateURLService_->GetTemplateURLs(); |
| 177 NSMutableArray* items = [NSMutableArray arrayWithCapacity:urls.size()]; |
| 178 for (std::vector<TemplateURL*>::const_iterator iter = urls.begin(); |
| 179 iter != urls.end(); ++iter) { |
| 180 [items addObject:base::SysUTF16ToNSString((*iter)->short_name())]; |
| 181 } |
| 182 return items; |
| 183 } |
| 184 |
| 185 - (NSString*)currentValue { |
| 186 return base::SysUTF16ToNSString( |
| 187 templateURLService_->GetDefaultSearchProvider()->short_name()); |
| 188 } |
| 189 |
| 190 - (void)setValueFromIndex:(NSUInteger)index { |
| 191 std::vector<TemplateURL*> urls = templateURLService_->GetTemplateURLs(); |
| 192 DCHECK_GE(index, 0U); |
| 193 DCHECK_LT(index, urls.size()); |
| 194 updatingBackend_ = YES; |
| 195 templateURLService_->SetUserSelectedDefaultSearchProvider(urls[index]); |
| 196 updatingBackend_ = NO; |
| 197 } |
| 198 |
| 199 - (void)onChange { |
| 200 if (!updatingBackend_) |
| 201 [self reloadData]; |
| 202 } |
| 203 |
| 204 @end |
OLD | NEW |