OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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/content_settings_collection_view_control
ler.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 10 #include "components/content_settings/core/common/content_settings.h" |
| 11 #include "components/content_settings/core/common/content_settings_types.h" |
| 12 #include "components/prefs/pref_change_registrar.h" |
| 13 #include "components/prefs/pref_service.h" |
| 14 #include "components/strings/grit/components_strings.h" |
| 15 #include "components/translate/core/common/translate_pref_names.h" |
| 16 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 17 #include "ios/chrome/browser/content_settings/host_content_settings_map_factory.
h" |
| 18 #import "ios/chrome/browser/prefs/pref_observer_bridge.h" |
| 19 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item
.h" |
| 20 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 21 #import "ios/chrome/browser/ui/settings/block_popups_collection_view_controller.
h" |
| 22 #import "ios/chrome/browser/ui/settings/settings_navigation_controller.h" |
| 23 #import "ios/chrome/browser/ui/settings/translate_collection_view_controller.h" |
| 24 #import "ios/chrome/browser/ui/settings/utils/content_setting_backed_boolean.h" |
| 25 #include "ios/chrome/grit/ios_strings.h" |
| 26 #import "ios/third_party/material_components_ios/src/components/CollectionCells/
src/MaterialCollectionCells.h" |
| 27 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" |
| 28 #include "ui/base/l10n/l10n_util.h" |
| 29 #include "ui/base/l10n/l10n_util_mac.h" |
| 30 |
| 31 namespace { |
| 32 |
| 33 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 34 SectionIdentifierSettings = kSectionIdentifierEnumZero, |
| 35 }; |
| 36 |
| 37 typedef NS_ENUM(NSInteger, ItemType) { |
| 38 ItemTypeSettingsBlockPopups = kItemTypeEnumZero, |
| 39 ItemTypeSettingsTranslate, |
| 40 }; |
| 41 |
| 42 } // namespace |
| 43 |
| 44 @interface ContentSettingsCollectionViewController ()<PrefObserverDelegate, |
| 45 BooleanObserver> { |
| 46 // Pref observer to track changes to prefs. |
| 47 std::unique_ptr<PrefObserverBridge> _prefObserverBridge; |
| 48 // Registrar for pref changes notifications. |
| 49 PrefChangeRegistrar _prefChangeRegistrar; |
| 50 |
| 51 // The observable boolean that binds to the "Disable Popups" setting state. |
| 52 base::scoped_nsobject<ContentSettingBackedBoolean> _disablePopupsSetting; |
| 53 |
| 54 // Updatable Items |
| 55 base::scoped_nsobject<CollectionViewDetailItem> _blockPopupsDetailItem; |
| 56 base::scoped_nsobject<CollectionViewDetailItem> _translateDetailItem; |
| 57 } |
| 58 |
| 59 // Returns the value for the default setting with ID |settingID|. |
| 60 - (ContentSetting)getContentSetting:(ContentSettingsType)settingID; |
| 61 |
| 62 // Helpers to create collection view items. |
| 63 - (id)blockPopupsItem; |
| 64 - (id)translateItem; |
| 65 |
| 66 @end |
| 67 |
| 68 @implementation ContentSettingsCollectionViewController { |
| 69 ios::ChromeBrowserState* browserState_; // weak |
| 70 } |
| 71 |
| 72 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState { |
| 73 DCHECK(browserState); |
| 74 self = [super initWithStyle:CollectionViewControllerStyleAppBar]; |
| 75 if (self) { |
| 76 browserState_ = browserState; |
| 77 self.title = l10n_util::GetNSString(IDS_IOS_CONTENT_SETTINGS_TITLE); |
| 78 |
| 79 _prefChangeRegistrar.Init(browserState->GetPrefs()); |
| 80 _prefObserverBridge.reset(new PrefObserverBridge(self)); |
| 81 // Register to observe any changes on Perf backed values displayed by the |
| 82 // screen. |
| 83 _prefObserverBridge->ObserveChangesForPreference(prefs::kEnableTranslate, |
| 84 &_prefChangeRegistrar); |
| 85 |
| 86 HostContentSettingsMap* settingsMap = |
| 87 ios::HostContentSettingsMapFactory::GetForBrowserState(browserState); |
| 88 _disablePopupsSetting.reset([[ContentSettingBackedBoolean alloc] |
| 89 initWithHostContentSettingsMap:settingsMap |
| 90 settingID:CONTENT_SETTINGS_TYPE_POPUPS |
| 91 inverted:YES]); |
| 92 [_disablePopupsSetting setObserver:self]; |
| 93 |
| 94 [self loadModel]; |
| 95 } |
| 96 return self; |
| 97 } |
| 98 |
| 99 - (void)dealloc { |
| 100 [_disablePopupsSetting setObserver:nil]; |
| 101 [super dealloc]; |
| 102 } |
| 103 |
| 104 - (instancetype)init { |
| 105 NOTREACHED(); |
| 106 return nil; |
| 107 } |
| 108 |
| 109 - (void)loadModel { |
| 110 [super loadModel]; |
| 111 |
| 112 CollectionViewModel* model = self.collectionViewModel; |
| 113 [model addSectionWithIdentifier:SectionIdentifierSettings]; |
| 114 [model addItem:[self blockPopupsItem] |
| 115 toSectionWithIdentifier:SectionIdentifierSettings]; |
| 116 [model addItem:[self translateItem] |
| 117 toSectionWithIdentifier:SectionIdentifierSettings]; |
| 118 } |
| 119 |
| 120 - (CollectionViewItem*)blockPopupsItem { |
| 121 _blockPopupsDetailItem.reset([[CollectionViewDetailItem alloc] |
| 122 initWithType:ItemTypeSettingsBlockPopups]); |
| 123 NSString* subtitle = [_disablePopupsSetting value] |
| 124 ? l10n_util::GetNSString(IDS_IOS_SETTING_ON) |
| 125 : l10n_util::GetNSString(IDS_IOS_SETTING_OFF); |
| 126 _blockPopupsDetailItem.get().text = |
| 127 l10n_util::GetNSString(IDS_IOS_BLOCK_POPUPS); |
| 128 _blockPopupsDetailItem.get().detailText = subtitle; |
| 129 _blockPopupsDetailItem.get().accessoryType = |
| 130 MDCCollectionViewCellAccessoryDisclosureIndicator; |
| 131 _blockPopupsDetailItem.get().accessibilityTraits |= |
| 132 UIAccessibilityTraitButton; |
| 133 return _blockPopupsDetailItem; |
| 134 } |
| 135 |
| 136 - (CollectionViewItem*)translateItem { |
| 137 _translateDetailItem.reset([[CollectionViewDetailItem alloc] |
| 138 initWithType:ItemTypeSettingsTranslate]); |
| 139 BOOL enabled = browserState_->GetPrefs()->GetBoolean(prefs::kEnableTranslate); |
| 140 NSString* subtitle = enabled ? l10n_util::GetNSString(IDS_IOS_SETTING_ON) |
| 141 : l10n_util::GetNSString(IDS_IOS_SETTING_OFF); |
| 142 _translateDetailItem.get().text = |
| 143 l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING); |
| 144 _translateDetailItem.get().detailText = subtitle; |
| 145 _translateDetailItem.get().accessoryType = |
| 146 MDCCollectionViewCellAccessoryDisclosureIndicator; |
| 147 _translateDetailItem.get().accessibilityTraits |= UIAccessibilityTraitButton; |
| 148 return _translateDetailItem; |
| 149 } |
| 150 |
| 151 - (ContentSetting)getContentSetting:(ContentSettingsType)settingID { |
| 152 return ios::HostContentSettingsMapFactory::GetForBrowserState(browserState_) |
| 153 ->GetDefaultContentSetting(settingID, NULL); |
| 154 } |
| 155 |
| 156 #pragma mark - UICollectionViewDelegate |
| 157 |
| 158 - (void)collectionView:(UICollectionView*)collectionView |
| 159 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
| 160 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; |
| 161 |
| 162 NSInteger itemType = |
| 163 [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 164 switch (itemType) { |
| 165 case ItemTypeSettingsBlockPopups: { |
| 166 base::scoped_nsobject<UIViewController> controller( |
| 167 [[BlockPopupsCollectionViewController alloc] |
| 168 initWithBrowserState:browserState_]); |
| 169 [self.navigationController pushViewController:controller animated:YES]; |
| 170 break; |
| 171 } |
| 172 case ItemTypeSettingsTranslate: { |
| 173 base::scoped_nsobject<UIViewController> controller( |
| 174 [[TranslateCollectionViewController alloc] |
| 175 initWithPrefs:browserState_->GetPrefs()]); |
| 176 [self.navigationController pushViewController:controller animated:YES]; |
| 177 break; |
| 178 } |
| 179 } |
| 180 } |
| 181 |
| 182 #pragma mark - PrefObserverDelegate |
| 183 |
| 184 - (void)onPreferenceChanged:(const std::string&)preferenceName { |
| 185 if (preferenceName == prefs::kEnableTranslate) { |
| 186 BOOL enabled = browserState_->GetPrefs()->GetBoolean(preferenceName); |
| 187 NSString* subtitle = enabled ? l10n_util::GetNSString(IDS_IOS_SETTING_ON) |
| 188 : l10n_util::GetNSString(IDS_IOS_SETTING_OFF); |
| 189 _translateDetailItem.get().detailText = subtitle; |
| 190 [self reconfigureCellsForItems:@[ _translateDetailItem ] |
| 191 inSectionWithIdentifier:SectionIdentifierSettings]; |
| 192 } |
| 193 } |
| 194 |
| 195 #pragma mark - BooleanObserver |
| 196 |
| 197 - (void)booleanDidChange:(id<ObservableBoolean>)observableBoolean { |
| 198 DCHECK_EQ(observableBoolean, _disablePopupsSetting.get()); |
| 199 |
| 200 NSString* subtitle = [_disablePopupsSetting value] |
| 201 ? l10n_util::GetNSString(IDS_IOS_SETTING_ON) |
| 202 : l10n_util::GetNSString(IDS_IOS_SETTING_OFF); |
| 203 // Update the item. |
| 204 _blockPopupsDetailItem.get().detailText = subtitle; |
| 205 |
| 206 // Update the cell. |
| 207 [self reconfigureCellsForItems:@[ _blockPopupsDetailItem ] |
| 208 inSectionWithIdentifier:SectionIdentifierSettings]; |
| 209 } |
| 210 |
| 211 @end |
OLD | NEW |