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/contextual_search_collection_view_contro
ller.h" |
| 6 |
| 7 #import "base/mac/foundation_util.h" |
| 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "components/google/core/browser/google_util.h" |
| 10 #include "components/strings/grit/components_strings.h" |
| 11 #include "ios/chrome/browser/application_context.h" |
| 12 #include "ios/chrome/browser/browser_state/chrome_browser_state.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 #import "ios/chrome/browser/ui/contextual_search/touch_to_search_permissions_med
iator.h" |
| 18 #import "ios/chrome/browser/ui/settings/settings_utils.h" |
| 19 #include "ios/chrome/grit/ios_strings.h" |
| 20 #include "ui/base/l10n/l10n_util.h" |
| 21 #include "url/gurl.h" |
| 22 |
| 23 namespace { |
| 24 |
| 25 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 26 SectionIdentifierTouchToSearch = kSectionIdentifierEnumZero, |
| 27 SectionIdentifierFooter, |
| 28 }; |
| 29 |
| 30 typedef NS_ENUM(NSUInteger, ItemType) { |
| 31 ItemTypeTouchToSearchSwitch = kItemTypeEnumZero, |
| 32 ItemTypeFooter, |
| 33 }; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 @interface ContextualSearchCollectionViewController () { |
| 38 // Permissions interface for Touch-to-Search. |
| 39 base::scoped_nsobject<TouchToSearchPermissionsMediator> |
| 40 _touchToSearchPermissions; |
| 41 } |
| 42 |
| 43 // Returns the switch item to use for the touch to search setting. |
| 44 - (CollectionViewSwitchItem*)touchToSearchSwitchItem; |
| 45 |
| 46 // Returns the item to use for the footer. |
| 47 - (CollectionViewFooterItem*)footerItem; |
| 48 |
| 49 // Called when the touch to search switch is toggled. |
| 50 - (void)switchToggled:(id)sender; |
| 51 |
| 52 @end |
| 53 |
| 54 @implementation ContextualSearchCollectionViewController |
| 55 |
| 56 #pragma mark - Initialization |
| 57 |
| 58 - (instancetype)initWithPermissions: |
| 59 (TouchToSearchPermissionsMediator*)touchToSearchPermissions { |
| 60 self = [super initWithStyle:CollectionViewControllerStyleAppBar]; |
| 61 if (self) { |
| 62 self.title = l10n_util::GetNSString(IDS_IOS_CONTEXTUAL_SEARCH_TITLE); |
| 63 _touchToSearchPermissions.reset([touchToSearchPermissions retain]); |
| 64 self.collectionViewAccessibilityIdentifier = @"Contextual Search"; |
| 65 [self loadModel]; |
| 66 } |
| 67 return self; |
| 68 } |
| 69 |
| 70 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState { |
| 71 return [self |
| 72 initWithPermissions:[[[TouchToSearchPermissionsMediator alloc] |
| 73 initWithBrowserState:browserState] autorelease]]; |
| 74 } |
| 75 |
| 76 #pragma mark - SettingsRootCollectionViewController |
| 77 |
| 78 - (void)loadModel { |
| 79 [super loadModel]; |
| 80 CollectionViewModel* model = self.collectionViewModel; |
| 81 |
| 82 [model addSectionWithIdentifier:SectionIdentifierTouchToSearch]; |
| 83 [model addItem:[self touchToSearchSwitchItem] |
| 84 toSectionWithIdentifier:SectionIdentifierTouchToSearch]; |
| 85 |
| 86 [model addSectionWithIdentifier:SectionIdentifierFooter]; |
| 87 [model addItem:[self footerItem] |
| 88 toSectionWithIdentifier:SectionIdentifierFooter]; |
| 89 } |
| 90 |
| 91 - (CollectionViewSwitchItem*)touchToSearchSwitchItem { |
| 92 CollectionViewSwitchItem* item = [[[CollectionViewSwitchItem alloc] |
| 93 initWithType:ItemTypeTouchToSearchSwitch] autorelease]; |
| 94 item.text = l10n_util::GetNSString(IDS_IOS_CONTEXTUAL_SEARCH_TITLE); |
| 95 item.on = |
| 96 ([_touchToSearchPermissions preferenceState] != TouchToSearch::DISABLED); |
| 97 return item; |
| 98 } |
| 99 |
| 100 - (CollectionViewFooterItem*)footerItem { |
| 101 NSString* footerText = |
| 102 [NSString stringWithFormat:@"%@ BEGIN_LINK%@END_LINK", |
| 103 l10n_util::GetNSString( |
| 104 IDS_IOS_CONTEXTUAL_SEARCH_DESCRIPTION), |
| 105 l10n_util::GetNSString(IDS_LEARN_MORE)]; |
| 106 GURL learnMoreURL = google_util::AppendGoogleLocaleParam( |
| 107 GURL(l10n_util::GetStringUTF8(IDS_IOS_CONTEXTUAL_SEARCH_LEARN_MORE_URL)), |
| 108 GetApplicationContext()->GetApplicationLocale()); |
| 109 |
| 110 CollectionViewFooterItem* item = [[[CollectionViewFooterItem alloc] |
| 111 initWithType:ItemTypeFooter] autorelease]; |
| 112 item.text = footerText; |
| 113 item.linkURL = learnMoreURL; |
| 114 item.linkDelegate = self; |
| 115 return item; |
| 116 } |
| 117 |
| 118 - (void)switchToggled:(id)sender { |
| 119 NSIndexPath* switchPath = [self.collectionViewModel |
| 120 indexPathForItemType:ItemTypeTouchToSearchSwitch |
| 121 sectionIdentifier:SectionIdentifierTouchToSearch]; |
| 122 |
| 123 CollectionViewSwitchItem* switchItem = |
| 124 base::mac::ObjCCastStrict<CollectionViewSwitchItem>( |
| 125 [self.collectionViewModel itemAtIndexPath:switchPath]); |
| 126 CollectionViewSwitchCell* switchCell = |
| 127 base::mac::ObjCCastStrict<CollectionViewSwitchCell>( |
| 128 [self.collectionView cellForItemAtIndexPath:switchPath]); |
| 129 |
| 130 // Update the model and the preference with the current value of the switch. |
| 131 DCHECK_EQ(switchCell.switchView, sender); |
| 132 BOOL isOn = switchCell.switchView.isOn; |
| 133 switchItem.on = isOn; |
| 134 TouchToSearch::TouchToSearchPreferenceState state = |
| 135 isOn ? TouchToSearch::ENABLED : TouchToSearch::DISABLED; |
| 136 [_touchToSearchPermissions setPreferenceState:state]; |
| 137 } |
| 138 |
| 139 #pragma mark - UICollectionViewDelegate |
| 140 |
| 141 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView |
| 142 cellForItemAtIndexPath:(NSIndexPath*)indexPath { |
| 143 UICollectionViewCell* cell = |
| 144 [super collectionView:collectionView cellForItemAtIndexPath:indexPath]; |
| 145 NSInteger itemType = |
| 146 [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 147 |
| 148 if (itemType == ItemTypeTouchToSearchSwitch) { |
| 149 // Have the switch send a message on UIControlEventValueChanged. |
| 150 CollectionViewSwitchCell* switchCell = |
| 151 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(cell); |
| 152 [switchCell.switchView addTarget:self |
| 153 action:@selector(switchToggled:) |
| 154 forControlEvents:UIControlEventValueChanged]; |
| 155 } |
| 156 |
| 157 return cell; |
| 158 } |
| 159 |
| 160 #pragma mark - MDCCollectionViewStylingDelegate |
| 161 |
| 162 - (CGFloat)collectionView:(UICollectionView*)collectionView |
| 163 cellHeightAtIndexPath:(NSIndexPath*)indexPath { |
| 164 CollectionViewItem* item = |
| 165 [self.collectionViewModel itemAtIndexPath:indexPath]; |
| 166 switch (item.type) { |
| 167 case ItemTypeFooter: |
| 168 return [MDCCollectionViewCell |
| 169 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) |
| 170 forItem:item]; |
| 171 default: |
| 172 return MDCCellDefaultOneLineHeight; |
| 173 } |
| 174 } |
| 175 |
| 176 - (MDCCollectionViewCellStyle)collectionView:(UICollectionView*)collectionView |
| 177 cellStyleForSection:(NSInteger)section { |
| 178 NSInteger sectionIdentifier = |
| 179 [self.collectionViewModel sectionIdentifierForSection:section]; |
| 180 switch (sectionIdentifier) { |
| 181 case SectionIdentifierFooter: |
| 182 // Display the Learn More footer in the default style with no "card" UI |
| 183 // and no section padding. |
| 184 return MDCCollectionViewCellStyleDefault; |
| 185 default: |
| 186 return self.styler.cellStyle; |
| 187 } |
| 188 } |
| 189 |
| 190 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 191 shouldHideItemBackgroundAtIndexPath:(NSIndexPath*)indexPath { |
| 192 NSInteger sectionIdentifier = |
| 193 [self.collectionViewModel sectionIdentifierForSection:indexPath.section]; |
| 194 switch (sectionIdentifier) { |
| 195 case SectionIdentifierFooter: |
| 196 // Display the Learn More footer without any background image or |
| 197 // shadowing. |
| 198 return YES; |
| 199 default: |
| 200 return NO; |
| 201 } |
| 202 } |
| 203 |
| 204 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 205 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { |
| 206 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 207 switch (type) { |
| 208 case ItemTypeFooter: |
| 209 case ItemTypeTouchToSearchSwitch: |
| 210 return YES; |
| 211 default: |
| 212 return NO; |
| 213 } |
| 214 } |
| 215 |
| 216 @end |
OLD | NEW |