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/do_not_track_collection_view_controller.
h" |
| 6 |
| 7 #include "base/mac/foundation_util.h" |
| 8 #include "components/google/core/browser/google_util.h" |
| 9 #include "components/prefs/pref_member.h" |
| 10 #include "ios/chrome/browser/application_context.h" |
| 11 #include "ios/chrome/browser/chrome_url_constants.h" |
| 12 #include "ios/chrome/browser/pref_names.h" |
| 13 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" |
| 14 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item
.h" |
| 15 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item
.h" |
| 16 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 17 #include "ios/chrome/grit/ios_strings.h" |
| 18 #include "ui/base/l10n/l10n_util.h" |
| 19 #include "url/gurl.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 24 SectionIdentifierSwitch = kSectionIdentifierEnumZero, |
| 25 SectionIdentifierFooter, |
| 26 }; |
| 27 |
| 28 typedef NS_ENUM(NSInteger, ItemType) { |
| 29 ItemTypeSwitch = kItemTypeEnumZero, |
| 30 ItemTypeFooter, |
| 31 }; |
| 32 |
| 33 } // namespace |
| 34 |
| 35 @interface DoNotTrackCollectionViewController () { |
| 36 // Pref for whether or not 'Do Not Track' is enabled. |
| 37 BooleanPrefMember _doNotTrackEnabled; |
| 38 } |
| 39 |
| 40 // Returns the item to be used as the preference switch. |
| 41 - (CollectionViewItem*)switchItem; |
| 42 |
| 43 // Returns the item to be used as a footer. |
| 44 - (CollectionViewItem*)footerItem; |
| 45 @end |
| 46 |
| 47 @implementation DoNotTrackCollectionViewController |
| 48 |
| 49 #pragma mark - Initialization |
| 50 |
| 51 - (instancetype)initWithPrefs:(PrefService*)prefs { |
| 52 self = [super initWithStyle:CollectionViewControllerStyleAppBar]; |
| 53 if (self) { |
| 54 self.title = l10n_util::GetNSString(IDS_IOS_OPTIONS_DO_NOT_TRACK_MOBILE); |
| 55 _doNotTrackEnabled.Init(prefs::kEnableDoNotTrack, prefs); |
| 56 self.collectionViewAccessibilityIdentifier = @"Do Not Track"; |
| 57 [self loadModel]; |
| 58 } |
| 59 return self; |
| 60 } |
| 61 |
| 62 - (void)loadModel { |
| 63 [super loadModel]; |
| 64 CollectionViewModel* model = self.collectionViewModel; |
| 65 |
| 66 [model addSectionWithIdentifier:SectionIdentifierSwitch]; |
| 67 [model addItem:[self switchItem] |
| 68 toSectionWithIdentifier:SectionIdentifierSwitch]; |
| 69 |
| 70 [model addSectionWithIdentifier:SectionIdentifierFooter]; |
| 71 [model addItem:[self footerItem] |
| 72 toSectionWithIdentifier:SectionIdentifierFooter]; |
| 73 } |
| 74 |
| 75 - (CollectionViewItem*)switchItem { |
| 76 CollectionViewSwitchItem* item = [[[CollectionViewSwitchItem alloc] |
| 77 initWithType:ItemTypeSwitch] autorelease]; |
| 78 item.text = l10n_util::GetNSString(IDS_IOS_OPTIONS_DO_NOT_TRACK_MOBILE); |
| 79 item.on = _doNotTrackEnabled.GetValue(); |
| 80 return item; |
| 81 } |
| 82 |
| 83 - (CollectionViewItem*)footerItem { |
| 84 NSString* footerText = l10n_util::GetNSString( |
| 85 IDS_IOS_OPTIONS_ENABLE_DO_NOT_TRACK_BUBBLE_TEXT_MOBILE); |
| 86 GURL learnMoreURL = google_util::AppendGoogleLocaleParam( |
| 87 GURL(kDoNotTrackLearnMoreURL), |
| 88 GetApplicationContext()->GetApplicationLocale()); |
| 89 |
| 90 CollectionViewFooterItem* item = [[[CollectionViewFooterItem alloc] |
| 91 initWithType:ItemTypeFooter] autorelease]; |
| 92 item.text = footerText; |
| 93 item.linkURL = learnMoreURL; |
| 94 item.linkDelegate = self; |
| 95 return item; |
| 96 } |
| 97 |
| 98 #pragma mark - Actions |
| 99 |
| 100 - (void)switchToggled:(id)sender { |
| 101 NSIndexPath* switchPath = |
| 102 [self.collectionViewModel indexPathForItemType:ItemTypeSwitch |
| 103 sectionIdentifier:SectionIdentifierSwitch]; |
| 104 |
| 105 CollectionViewSwitchItem* switchItem = |
| 106 base::mac::ObjCCastStrict<CollectionViewSwitchItem>( |
| 107 [self.collectionViewModel itemAtIndexPath:switchPath]); |
| 108 CollectionViewSwitchCell* switchCell = |
| 109 base::mac::ObjCCastStrict<CollectionViewSwitchCell>( |
| 110 [self.collectionView cellForItemAtIndexPath:switchPath]); |
| 111 |
| 112 // Update the model and the preference with the current value of the switch. |
| 113 DCHECK_EQ(switchCell.switchView, sender); |
| 114 BOOL isOn = switchCell.switchView.isOn; |
| 115 switchItem.on = isOn; |
| 116 _doNotTrackEnabled.SetValue(isOn); |
| 117 } |
| 118 |
| 119 #pragma mark - UICollectionViewDelegate |
| 120 |
| 121 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView |
| 122 cellForItemAtIndexPath:(NSIndexPath*)indexPath { |
| 123 UICollectionViewCell* cell = |
| 124 [super collectionView:collectionView cellForItemAtIndexPath:indexPath]; |
| 125 NSInteger itemType = |
| 126 [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 127 |
| 128 if (itemType == ItemTypeSwitch) { |
| 129 // Have the switch send a message on UIControlEventValueChanged. |
| 130 CollectionViewSwitchCell* switchCell = |
| 131 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(cell); |
| 132 [switchCell.switchView addTarget:self |
| 133 action:@selector(switchToggled:) |
| 134 forControlEvents:UIControlEventValueChanged]; |
| 135 } |
| 136 |
| 137 return cell; |
| 138 } |
| 139 |
| 140 #pragma mark - MDCCollectionViewStylingDelegate |
| 141 |
| 142 - (CGFloat)collectionView:(UICollectionView*)collectionView |
| 143 cellHeightAtIndexPath:(NSIndexPath*)indexPath { |
| 144 CollectionViewItem* item = |
| 145 [self.collectionViewModel itemAtIndexPath:indexPath]; |
| 146 switch (item.type) { |
| 147 case ItemTypeFooter: |
| 148 return [MDCCollectionViewCell |
| 149 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) |
| 150 forItem:item]; |
| 151 default: |
| 152 return MDCCellDefaultOneLineHeight; |
| 153 } |
| 154 } |
| 155 |
| 156 - (MDCCollectionViewCellStyle)collectionView:(UICollectionView*)collectionView |
| 157 cellStyleForSection:(NSInteger)section { |
| 158 NSInteger sectionIdentifier = |
| 159 [self.collectionViewModel sectionIdentifierForSection:section]; |
| 160 switch (sectionIdentifier) { |
| 161 case SectionIdentifierFooter: |
| 162 // Display the Learn More footer in the default style with no "card" UI |
| 163 // and no section padding. |
| 164 return MDCCollectionViewCellStyleDefault; |
| 165 default: |
| 166 return self.styler.cellStyle; |
| 167 } |
| 168 } |
| 169 |
| 170 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 171 shouldHideItemBackgroundAtIndexPath:(NSIndexPath*)indexPath { |
| 172 NSInteger sectionIdentifier = |
| 173 [self.collectionViewModel sectionIdentifierForSection:indexPath.section]; |
| 174 switch (sectionIdentifier) { |
| 175 case SectionIdentifierFooter: |
| 176 // Display the Learn More footer without any background image or |
| 177 // shadowing. |
| 178 return YES; |
| 179 default: |
| 180 return NO; |
| 181 } |
| 182 } |
| 183 |
| 184 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 185 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { |
| 186 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 187 switch (type) { |
| 188 case ItemTypeFooter: |
| 189 case ItemTypeSwitch: |
| 190 return YES; |
| 191 default: |
| 192 return NO; |
| 193 } |
| 194 } |
| 195 |
| 196 @end |
OLD | NEW |