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/bandwidth_management_collection_view_con
troller.h" |
| 6 |
| 7 #include "base/mac/foundation_util.h" |
| 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "components/prefs/pref_change_registrar.h" |
| 10 #include "components/prefs/pref_service.h" |
| 11 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 12 #include "ios/chrome/browser/pref_names.h" |
| 13 #import "ios/chrome/browser/prefs/pref_observer_bridge.h" |
| 14 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" |
| 15 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item
.h" |
| 16 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item
.h" |
| 17 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 18 #import "ios/chrome/browser/ui/settings/dataplan_usage_collection_view_controlle
r.h" |
| 19 #import "ios/chrome/browser/ui/settings/settings_utils.h" |
| 20 #include "ios/chrome/grit/ios_chromium_strings.h" |
| 21 #include "ios/chrome/grit/ios_strings.h" |
| 22 #import "ios/third_party/material_components_ios/src/components/CollectionCells/
src/MaterialCollectionCells.h" |
| 23 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" |
| 24 #include "ui/base/l10n/l10n_util.h" |
| 25 #include "ui/base/l10n/l10n_util_mac.h" |
| 26 #include "url/gurl.h" |
| 27 |
| 28 namespace { |
| 29 |
| 30 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 31 SectionIdentifierActions = kSectionIdentifierEnumZero, |
| 32 SectionIdentifierFooter, |
| 33 }; |
| 34 |
| 35 typedef NS_ENUM(NSInteger, ItemType) { |
| 36 ItemTypePreload = kItemTypeEnumZero, |
| 37 ItemTypeFooter, |
| 38 }; |
| 39 |
| 40 } // namespace |
| 41 |
| 42 @interface BandwidthManagementCollectionViewController ()< |
| 43 PrefObserverDelegate> { |
| 44 ios::ChromeBrowserState* _browserState; // weak |
| 45 |
| 46 // Pref observer to track changes to prefs. |
| 47 std::unique_ptr<PrefObserverBridge> _prefObserverBridge; |
| 48 // Registrar for pref changes notifications. |
| 49 PrefChangeRegistrar _prefChangeRegistrarApplicationContext; |
| 50 |
| 51 // Updatable Items |
| 52 base::scoped_nsobject<CollectionViewDetailItem> _preloadWebpagesDetailItem; |
| 53 } |
| 54 |
| 55 // Helpers to create collection view items. |
| 56 - (CollectionViewItem*)preloadWebpagesItem; |
| 57 - (CollectionViewItem*)footerItem; |
| 58 @end |
| 59 |
| 60 @implementation BandwidthManagementCollectionViewController |
| 61 |
| 62 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState { |
| 63 self = [super initWithStyle:CollectionViewControllerStyleAppBar]; |
| 64 if (self) { |
| 65 self.title = l10n_util::GetNSString(IDS_IOS_BANDWIDTH_MANAGEMENT_SETTINGS); |
| 66 self.collectionViewAccessibilityIdentifier = @"Bandwidth Management"; |
| 67 _browserState = browserState; |
| 68 |
| 69 _prefChangeRegistrarApplicationContext.Init(_browserState->GetPrefs()); |
| 70 _prefObserverBridge.reset(new PrefObserverBridge(self)); |
| 71 // Register to observe any changes on Perf backed values displayed by the |
| 72 // screen. |
| 73 _prefObserverBridge->ObserveChangesForPreference( |
| 74 prefs::kNetworkPredictionEnabled, |
| 75 &_prefChangeRegistrarApplicationContext); |
| 76 _prefObserverBridge->ObserveChangesForPreference( |
| 77 prefs::kNetworkPredictionWifiOnly, |
| 78 &_prefChangeRegistrarApplicationContext); |
| 79 |
| 80 [self loadModel]; |
| 81 } |
| 82 return self; |
| 83 } |
| 84 |
| 85 - (void)loadModel { |
| 86 [super loadModel]; |
| 87 |
| 88 CollectionViewModel* model = self.collectionViewModel; |
| 89 [model addSectionWithIdentifier:SectionIdentifierActions]; |
| 90 [model addItem:[self preloadWebpagesItem] |
| 91 toSectionWithIdentifier:SectionIdentifierActions]; |
| 92 |
| 93 // The footer item must currently go into a separate section, to work around a |
| 94 // drawing bug in MDC. |
| 95 [model addSectionWithIdentifier:SectionIdentifierFooter]; |
| 96 [model addItem:[self footerItem] |
| 97 toSectionWithIdentifier:SectionIdentifierFooter]; |
| 98 } |
| 99 |
| 100 - (CollectionViewItem*)preloadWebpagesItem { |
| 101 NSString* detailText = [DataplanUsageCollectionViewController |
| 102 currentLabelForPreference:_browserState->GetPrefs() |
| 103 basePref:prefs::kNetworkPredictionEnabled |
| 104 wifiPref:prefs::kNetworkPredictionWifiOnly]; |
| 105 _preloadWebpagesDetailItem.reset( |
| 106 [[CollectionViewDetailItem alloc] initWithType:ItemTypePreload]); |
| 107 |
| 108 _preloadWebpagesDetailItem.get().text = |
| 109 l10n_util::GetNSString(IDS_IOS_OPTIONS_PRELOAD_WEBPAGES); |
| 110 _preloadWebpagesDetailItem.get().detailText = detailText; |
| 111 _preloadWebpagesDetailItem.get().accessoryType = |
| 112 MDCCollectionViewCellAccessoryDisclosureIndicator; |
| 113 _preloadWebpagesDetailItem.get().accessibilityTraits |= |
| 114 UIAccessibilityTraitButton; |
| 115 return _preloadWebpagesDetailItem; |
| 116 } |
| 117 |
| 118 - (CollectionViewItem*)footerItem { |
| 119 CollectionViewFooterItem* item = [[[CollectionViewFooterItem alloc] |
| 120 initWithType:ItemTypeFooter] autorelease]; |
| 121 |
| 122 item.text = l10n_util::GetNSString( |
| 123 IDS_IOS_BANDWIDTH_MANAGEMENT_DESCRIPTION_LEARN_MORE); |
| 124 item.linkURL = |
| 125 GURL(l10n_util::GetStringUTF8(IDS_IOS_BANDWIDTH_MANAGEMENT_LEARN_URL)); |
| 126 item.linkDelegate = self; |
| 127 item.accessibilityTraits |= UIAccessibilityTraitButton; |
| 128 return item; |
| 129 } |
| 130 |
| 131 #pragma mark UICollectionViewDelegate |
| 132 |
| 133 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 134 shouldHighlightItemAtIndexPath:(NSIndexPath*)indexPath { |
| 135 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 136 if (type == ItemTypeFooter) { |
| 137 return NO; |
| 138 } |
| 139 return [super collectionView:collectionView |
| 140 shouldHighlightItemAtIndexPath:indexPath]; |
| 141 } |
| 142 |
| 143 - (void)collectionView:(UICollectionView*)collectionView |
| 144 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
| 145 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; |
| 146 |
| 147 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 148 if (type == ItemTypePreload) { |
| 149 NSString* preloadTitle = |
| 150 l10n_util::GetNSString(IDS_IOS_OPTIONS_PRELOAD_WEBPAGES); |
| 151 base::scoped_nsobject<UIViewController> controller( |
| 152 [[DataplanUsageCollectionViewController alloc] |
| 153 initWithPrefs:_browserState->GetPrefs() |
| 154 basePref:prefs::kNetworkPredictionEnabled |
| 155 wifiPref:prefs::kNetworkPredictionWifiOnly |
| 156 title:preloadTitle]); |
| 157 [self.navigationController pushViewController:controller animated:YES]; |
| 158 } |
| 159 } |
| 160 |
| 161 #pragma mark - MDCCollectionViewStylingDelegate |
| 162 |
| 163 - (MDCCollectionViewCellStyle)collectionView:(UICollectionView*)collectionView |
| 164 cellStyleForSection:(NSInteger)section { |
| 165 NSInteger sectionIdentifier = |
| 166 [self.collectionViewModel sectionIdentifierForSection:section]; |
| 167 switch (sectionIdentifier) { |
| 168 case SectionIdentifierFooter: |
| 169 // Display the Learn More footer in the default style with no "card" UI |
| 170 // and no section padding. |
| 171 return MDCCollectionViewCellStyleDefault; |
| 172 default: |
| 173 return self.styler.cellStyle; |
| 174 } |
| 175 } |
| 176 |
| 177 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 178 shouldHideItemBackgroundAtIndexPath:(NSIndexPath*)indexPath { |
| 179 NSInteger sectionIdentifier = |
| 180 [self.collectionViewModel sectionIdentifierForSection:indexPath.section]; |
| 181 switch (sectionIdentifier) { |
| 182 case SectionIdentifierFooter: |
| 183 // Display the Learn More footer without any background image or |
| 184 // shadowing. |
| 185 return YES; |
| 186 default: |
| 187 return NO; |
| 188 } |
| 189 } |
| 190 |
| 191 - (CGFloat)collectionView:(UICollectionView*)collectionView |
| 192 cellHeightAtIndexPath:(NSIndexPath*)indexPath { |
| 193 CollectionViewItem* item = |
| 194 [self.collectionViewModel itemAtIndexPath:indexPath]; |
| 195 |
| 196 if (item.type == ItemTypeFooter) |
| 197 return [MDCCollectionViewCell |
| 198 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) |
| 199 forItem:item]; |
| 200 return MDCCellDefaultOneLineHeight; |
| 201 } |
| 202 |
| 203 #pragma mark - PrefObserverDelegate |
| 204 |
| 205 - (void)onPreferenceChanged:(const std::string&)preferenceName { |
| 206 if (preferenceName == prefs::kNetworkPredictionEnabled || |
| 207 preferenceName == prefs::kNetworkPredictionWifiOnly) { |
| 208 NSString* detailText = [DataplanUsageCollectionViewController |
| 209 currentLabelForPreference:_browserState->GetPrefs() |
| 210 basePref:prefs::kNetworkPredictionEnabled |
| 211 wifiPref:prefs::kNetworkPredictionWifiOnly]; |
| 212 |
| 213 _preloadWebpagesDetailItem.get().detailText = detailText; |
| 214 |
| 215 [self reconfigureCellsForItems:@[ _preloadWebpagesDetailItem ] |
| 216 inSectionWithIdentifier:SectionIdentifierActions]; |
| 217 } |
| 218 } |
| 219 |
| 220 @end |
OLD | NEW |