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/physical_web_collection_view_controller.
h" |
| 6 |
| 7 #import <CoreLocation/CoreLocation.h> |
| 8 |
| 9 #include "base/ios/weak_nsobject.h" |
| 10 #import "base/mac/foundation_util.h" |
| 11 #import "base/mac/scoped_nsobject.h" |
| 12 #include "base/metrics/user_metrics.h" |
| 13 #include "components/google/core/browser/google_util.h" |
| 14 #include "components/physical_web/data_source/physical_web_data_source.h" |
| 15 #include "components/prefs/pref_member.h" |
| 16 #include "ios/chrome/browser/application_context.h" |
| 17 #include "ios/chrome/browser/chrome_url_constants.h" |
| 18 #include "ios/chrome/browser/physical_web/physical_web_constants.h" |
| 19 #include "ios/chrome/browser/pref_names.h" |
| 20 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" |
| 21 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item
.h" |
| 22 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
| 23 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item
.h" |
| 24 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" |
| 25 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 26 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
| 27 #import "ios/chrome/browser/ui/settings/settings_utils.h" |
| 28 #include "ios/chrome/common/string_util.h" |
| 29 #include "ios/chrome/grit/ios_chromium_strings.h" |
| 30 #include "ios/chrome/grit/ios_strings.h" |
| 31 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" |
| 32 #include "ui/base/l10n/l10n_util.h" |
| 33 #include "url/gurl.h" |
| 34 |
| 35 namespace { |
| 36 |
| 37 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 38 SectionIdentifierPhysicalWeb = kSectionIdentifierEnumZero, |
| 39 SectionIdentifierLearnMore, |
| 40 }; |
| 41 |
| 42 typedef NS_ENUM(NSInteger, ItemType) { |
| 43 ItemTypePhysicalWebSwitch = kItemTypeEnumZero, |
| 44 ItemTypeLearnMore, |
| 45 }; |
| 46 |
| 47 } // namespace |
| 48 |
| 49 @interface PhysicalWebCollectionViewController () { |
| 50 // Pref for the Physical Web enabled state. |
| 51 IntegerPrefMember _physicalWebEnabled; |
| 52 } |
| 53 |
| 54 // Called when the preference switch is toggled on or off. |
| 55 - (void)physicalWebSwitched:(UISwitch*)switchView; |
| 56 |
| 57 // Updates the Physical Web preference. |
| 58 - (void)updatePhysicalWebEnabled:(BOOL)enabled; |
| 59 @end |
| 60 |
| 61 @implementation PhysicalWebCollectionViewController |
| 62 |
| 63 #pragma mark - Initialization |
| 64 |
| 65 - (instancetype)initWithPrefs:(PrefService*)prefs { |
| 66 self = [super initWithStyle:CollectionViewControllerStyleAppBar]; |
| 67 if (self) { |
| 68 self.title = l10n_util::GetNSString(IDS_IOS_OPTIONS_ENABLE_PHYSICAL_WEB); |
| 69 _physicalWebEnabled.Init(prefs::kIosPhysicalWebEnabled, prefs); |
| 70 [self loadModel]; |
| 71 } |
| 72 return self; |
| 73 } |
| 74 |
| 75 - (instancetype)init { |
| 76 NOTREACHED(); |
| 77 return nil; |
| 78 } |
| 79 |
| 80 - (instancetype)initWithStyle:(CollectionViewControllerStyle)style { |
| 81 NOTREACHED(); |
| 82 return nil; |
| 83 } |
| 84 |
| 85 #pragma mark - Preference switch |
| 86 |
| 87 + (BOOL)shouldEnableForPreferenceState:(int)preferenceState { |
| 88 // In the default (onboarding) state, render the preference as On if the |
| 89 // location app permission is granted. |
| 90 if (preferenceState == physical_web::kPhysicalWebOnboarding) { |
| 91 CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus]; |
| 92 return (authStatus == kCLAuthorizationStatusAuthorizedWhenInUse || |
| 93 authStatus == kCLAuthorizationStatusAuthorizedAlways); |
| 94 } |
| 95 |
| 96 return (preferenceState == physical_web::kPhysicalWebOn); |
| 97 } |
| 98 |
| 99 - (void)physicalWebSwitched:(UISwitch*)switchView { |
| 100 [self updatePhysicalWebEnabled:switchView.isOn]; |
| 101 } |
| 102 |
| 103 - (void)updatePhysicalWebEnabled:(BOOL)enabled { |
| 104 _physicalWebEnabled.SetValue(enabled ? physical_web::kPhysicalWebOn |
| 105 : physical_web::kPhysicalWebOff); |
| 106 |
| 107 physical_web::PhysicalWebDataSource* dataSource = |
| 108 GetApplicationContext()->GetPhysicalWebDataSource(); |
| 109 |
| 110 if (enabled) { |
| 111 base::RecordAction( |
| 112 base::UserMetricsAction("PhysicalWeb.Prefs.FeatureEnabled")); |
| 113 if (dataSource) { |
| 114 dataSource->StartDiscovery(true); |
| 115 } |
| 116 } else { |
| 117 base::RecordAction( |
| 118 base::UserMetricsAction("PhysicalWeb.Prefs.FeatureDisabled")); |
| 119 if (dataSource) { |
| 120 dataSource->StopDiscovery(); |
| 121 } |
| 122 } |
| 123 } |
| 124 |
| 125 #pragma mark - UICollectionViewDataSource |
| 126 |
| 127 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView |
| 128 cellForItemAtIndexPath:(NSIndexPath*)indexPath { |
| 129 UICollectionViewCell* cell = |
| 130 [super collectionView:collectionView cellForItemAtIndexPath:indexPath]; |
| 131 |
| 132 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 133 switch (type) { |
| 134 case ItemTypePhysicalWebSwitch: { |
| 135 CollectionViewSwitchCell* switchCell = |
| 136 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(cell); |
| 137 [switchCell.switchView addTarget:self |
| 138 action:@selector(physicalWebSwitched:) |
| 139 forControlEvents:UIControlEventValueChanged]; |
| 140 break; |
| 141 } |
| 142 } |
| 143 |
| 144 return cell; |
| 145 } |
| 146 |
| 147 #pragma mark - UICollectionViewDelegate |
| 148 |
| 149 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 150 shouldHighlightItemAtIndexPath:(NSIndexPath*)indexPath { |
| 151 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 152 switch (type) { |
| 153 case ItemTypePhysicalWebSwitch: |
| 154 case ItemTypeLearnMore: |
| 155 return NO; |
| 156 default: |
| 157 return [super collectionView:collectionView |
| 158 shouldHighlightItemAtIndexPath:indexPath]; |
| 159 } |
| 160 } |
| 161 |
| 162 #pragma mark - SettingsRootCollectionViewController |
| 163 |
| 164 - (void)loadModel { |
| 165 [super loadModel]; |
| 166 CollectionViewModel* model = self.collectionViewModel; |
| 167 |
| 168 [model addSectionWithIdentifier:SectionIdentifierPhysicalWeb]; |
| 169 |
| 170 NSString* switchLabelText = |
| 171 l10n_util::GetNSString(IDS_IOS_OPTIONS_ENABLE_PHYSICAL_WEB); |
| 172 |
| 173 CollectionViewSwitchItem* switchItem = [[[CollectionViewSwitchItem alloc] |
| 174 initWithType:ItemTypePhysicalWebSwitch] autorelease]; |
| 175 switchItem.text = switchLabelText; |
| 176 switchItem.on = [PhysicalWebCollectionViewController |
| 177 shouldEnableForPreferenceState:_physicalWebEnabled.GetValue()]; |
| 178 [model addItem:switchItem |
| 179 toSectionWithIdentifier:SectionIdentifierPhysicalWeb]; |
| 180 |
| 181 [model addSectionWithIdentifier:SectionIdentifierLearnMore]; |
| 182 |
| 183 NSString* learnMoreText = |
| 184 l10n_util::GetNSString(IDS_IOS_OPTIONS_ENABLE_PHYSICAL_WEB_DETAILS); |
| 185 |
| 186 CollectionViewFooterItem* learnMore = [[[CollectionViewFooterItem alloc] |
| 187 initWithType:ItemTypeLearnMore] autorelease]; |
| 188 learnMore.text = learnMoreText; |
| 189 learnMore.linkURL = GURL(kPhysicalWebLearnMoreURL); |
| 190 learnMore.linkDelegate = self; |
| 191 learnMore.accessibilityTraits = UIAccessibilityTraitButton; |
| 192 [model addItem:learnMore toSectionWithIdentifier:SectionIdentifierLearnMore]; |
| 193 } |
| 194 |
| 195 #pragma mark - MDCCollectionViewStylingDelegate |
| 196 |
| 197 - (MDCCollectionViewCellStyle)collectionView:(UICollectionView*)collectionView |
| 198 cellStyleForSection:(NSInteger)section { |
| 199 NSInteger sectionIdentifier = |
| 200 [self.collectionViewModel sectionIdentifierForSection:section]; |
| 201 switch (sectionIdentifier) { |
| 202 case SectionIdentifierLearnMore: |
| 203 // Display the Learn More footer in the default style with no "card" UI |
| 204 // and no section padding. |
| 205 return MDCCollectionViewCellStyleDefault; |
| 206 default: |
| 207 return self.styler.cellStyle; |
| 208 } |
| 209 } |
| 210 |
| 211 - (CGFloat)collectionView:(UICollectionView*)collectionView |
| 212 cellHeightAtIndexPath:(NSIndexPath*)indexPath { |
| 213 CollectionViewItem* item = |
| 214 [self.collectionViewModel itemAtIndexPath:indexPath]; |
| 215 switch (item.type) { |
| 216 case ItemTypeLearnMore: |
| 217 return [MDCCollectionViewCell |
| 218 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) |
| 219 forItem:item]; |
| 220 default: |
| 221 return MDCCellDefaultOneLineHeight; |
| 222 } |
| 223 } |
| 224 |
| 225 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 226 shouldHideItemBackgroundAtIndexPath:(NSIndexPath*)indexPath { |
| 227 NSInteger sectionIdentifier = |
| 228 [self.collectionViewModel sectionIdentifierForSection:indexPath.section]; |
| 229 switch (sectionIdentifier) { |
| 230 case SectionIdentifierLearnMore: |
| 231 // Display the Learn More footer without any background image or |
| 232 // shadowing. |
| 233 return YES; |
| 234 default: |
| 235 return NO; |
| 236 } |
| 237 } |
| 238 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 239 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { |
| 240 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 241 switch (type) { |
| 242 case ItemTypePhysicalWebSwitch: |
| 243 return YES; |
| 244 default: |
| 245 return NO; |
| 246 } |
| 247 } |
| 248 |
| 249 @end |
OLD | NEW |