Index: ios/chrome/browser/ui/settings/bandwidth_management_collection_view_controller.mm |
diff --git a/ios/chrome/browser/ui/settings/bandwidth_management_collection_view_controller.mm b/ios/chrome/browser/ui/settings/bandwidth_management_collection_view_controller.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b75d9d52d6c181039d9d8f0e732fb76aa9f7df2b |
--- /dev/null |
+++ b/ios/chrome/browser/ui/settings/bandwidth_management_collection_view_controller.mm |
@@ -0,0 +1,220 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#import "ios/chrome/browser/ui/settings/bandwidth_management_collection_view_controller.h" |
+ |
+#include "base/mac/foundation_util.h" |
+#include "base/mac/scoped_nsobject.h" |
+#include "components/prefs/pref_change_registrar.h" |
+#include "components/prefs/pref_service.h" |
+#include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
+#include "ios/chrome/browser/pref_names.h" |
+#import "ios/chrome/browser/prefs/pref_observer_bridge.h" |
+#import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrome.h" |
+#import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item.h" |
+#import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item.h" |
+#import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
+#import "ios/chrome/browser/ui/settings/dataplan_usage_collection_view_controller.h" |
+#import "ios/chrome/browser/ui/settings/settings_utils.h" |
+#include "ios/chrome/grit/ios_chromium_strings.h" |
+#include "ios/chrome/grit/ios_strings.h" |
+#import "ios/third_party/material_components_ios/src/components/CollectionCells/src/MaterialCollectionCells.h" |
+#import "ios/third_party/material_components_ios/src/components/Palettes/src/MaterialPalettes.h" |
+#include "ui/base/l10n/l10n_util.h" |
+#include "ui/base/l10n/l10n_util_mac.h" |
+#include "url/gurl.h" |
+ |
+namespace { |
+ |
+typedef NS_ENUM(NSInteger, SectionIdentifier) { |
+ SectionIdentifierActions = kSectionIdentifierEnumZero, |
+ SectionIdentifierFooter, |
+}; |
+ |
+typedef NS_ENUM(NSInteger, ItemType) { |
+ ItemTypePreload = kItemTypeEnumZero, |
+ ItemTypeFooter, |
+}; |
+ |
+} // namespace |
+ |
+@interface BandwidthManagementCollectionViewController ()< |
+ PrefObserverDelegate> { |
+ ios::ChromeBrowserState* _browserState; // weak |
+ |
+ // Pref observer to track changes to prefs. |
+ std::unique_ptr<PrefObserverBridge> _prefObserverBridge; |
+ // Registrar for pref changes notifications. |
+ PrefChangeRegistrar _prefChangeRegistrarApplicationContext; |
+ |
+ // Updatable Items |
+ base::scoped_nsobject<CollectionViewDetailItem> _preloadWebpagesDetailItem; |
+} |
+ |
+// Helpers to create collection view items. |
+- (CollectionViewItem*)preloadWebpagesItem; |
+- (CollectionViewItem*)footerItem; |
+@end |
+ |
+@implementation BandwidthManagementCollectionViewController |
+ |
+- (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState { |
+ self = [super initWithStyle:CollectionViewControllerStyleAppBar]; |
+ if (self) { |
+ self.title = l10n_util::GetNSString(IDS_IOS_BANDWIDTH_MANAGEMENT_SETTINGS); |
+ self.collectionViewAccessibilityIdentifier = @"Bandwidth Management"; |
+ _browserState = browserState; |
+ |
+ _prefChangeRegistrarApplicationContext.Init(_browserState->GetPrefs()); |
+ _prefObserverBridge.reset(new PrefObserverBridge(self)); |
+ // Register to observe any changes on Perf backed values displayed by the |
+ // screen. |
+ _prefObserverBridge->ObserveChangesForPreference( |
+ prefs::kNetworkPredictionEnabled, |
+ &_prefChangeRegistrarApplicationContext); |
+ _prefObserverBridge->ObserveChangesForPreference( |
+ prefs::kNetworkPredictionWifiOnly, |
+ &_prefChangeRegistrarApplicationContext); |
+ |
+ [self loadModel]; |
+ } |
+ return self; |
+} |
+ |
+- (void)loadModel { |
+ [super loadModel]; |
+ |
+ CollectionViewModel* model = self.collectionViewModel; |
+ [model addSectionWithIdentifier:SectionIdentifierActions]; |
+ [model addItem:[self preloadWebpagesItem] |
+ toSectionWithIdentifier:SectionIdentifierActions]; |
+ |
+ // The footer item must currently go into a separate section, to work around a |
+ // drawing bug in MDC. |
+ [model addSectionWithIdentifier:SectionIdentifierFooter]; |
+ [model addItem:[self footerItem] |
+ toSectionWithIdentifier:SectionIdentifierFooter]; |
+} |
+ |
+- (CollectionViewItem*)preloadWebpagesItem { |
+ NSString* detailText = [DataplanUsageCollectionViewController |
+ currentLabelForPreference:_browserState->GetPrefs() |
+ basePref:prefs::kNetworkPredictionEnabled |
+ wifiPref:prefs::kNetworkPredictionWifiOnly]; |
+ _preloadWebpagesDetailItem.reset( |
+ [[CollectionViewDetailItem alloc] initWithType:ItemTypePreload]); |
+ |
+ _preloadWebpagesDetailItem.get().text = |
+ l10n_util::GetNSString(IDS_IOS_OPTIONS_PRELOAD_WEBPAGES); |
+ _preloadWebpagesDetailItem.get().detailText = detailText; |
+ _preloadWebpagesDetailItem.get().accessoryType = |
+ MDCCollectionViewCellAccessoryDisclosureIndicator; |
+ _preloadWebpagesDetailItem.get().accessibilityTraits |= |
+ UIAccessibilityTraitButton; |
+ return _preloadWebpagesDetailItem; |
+} |
+ |
+- (CollectionViewItem*)footerItem { |
+ CollectionViewFooterItem* item = [[[CollectionViewFooterItem alloc] |
+ initWithType:ItemTypeFooter] autorelease]; |
+ |
+ item.text = l10n_util::GetNSString( |
+ IDS_IOS_BANDWIDTH_MANAGEMENT_DESCRIPTION_LEARN_MORE); |
+ item.linkURL = |
+ GURL(l10n_util::GetStringUTF8(IDS_IOS_BANDWIDTH_MANAGEMENT_LEARN_URL)); |
+ item.linkDelegate = self; |
+ item.accessibilityTraits |= UIAccessibilityTraitButton; |
+ return item; |
+} |
+ |
+#pragma mark UICollectionViewDelegate |
+ |
+- (BOOL)collectionView:(UICollectionView*)collectionView |
+ shouldHighlightItemAtIndexPath:(NSIndexPath*)indexPath { |
+ NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
+ if (type == ItemTypeFooter) { |
+ return NO; |
+ } |
+ return [super collectionView:collectionView |
+ shouldHighlightItemAtIndexPath:indexPath]; |
+} |
+ |
+- (void)collectionView:(UICollectionView*)collectionView |
+ didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
+ [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; |
+ |
+ NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
+ if (type == ItemTypePreload) { |
+ NSString* preloadTitle = |
+ l10n_util::GetNSString(IDS_IOS_OPTIONS_PRELOAD_WEBPAGES); |
+ base::scoped_nsobject<UIViewController> controller( |
+ [[DataplanUsageCollectionViewController alloc] |
+ initWithPrefs:_browserState->GetPrefs() |
+ basePref:prefs::kNetworkPredictionEnabled |
+ wifiPref:prefs::kNetworkPredictionWifiOnly |
+ title:preloadTitle]); |
+ [self.navigationController pushViewController:controller animated:YES]; |
+ } |
+} |
+ |
+#pragma mark - MDCCollectionViewStylingDelegate |
+ |
+- (MDCCollectionViewCellStyle)collectionView:(UICollectionView*)collectionView |
+ cellStyleForSection:(NSInteger)section { |
+ NSInteger sectionIdentifier = |
+ [self.collectionViewModel sectionIdentifierForSection:section]; |
+ switch (sectionIdentifier) { |
+ case SectionIdentifierFooter: |
+ // Display the Learn More footer in the default style with no "card" UI |
+ // and no section padding. |
+ return MDCCollectionViewCellStyleDefault; |
+ default: |
+ return self.styler.cellStyle; |
+ } |
+} |
+ |
+- (BOOL)collectionView:(UICollectionView*)collectionView |
+ shouldHideItemBackgroundAtIndexPath:(NSIndexPath*)indexPath { |
+ NSInteger sectionIdentifier = |
+ [self.collectionViewModel sectionIdentifierForSection:indexPath.section]; |
+ switch (sectionIdentifier) { |
+ case SectionIdentifierFooter: |
+ // Display the Learn More footer without any background image or |
+ // shadowing. |
+ return YES; |
+ default: |
+ return NO; |
+ } |
+} |
+ |
+- (CGFloat)collectionView:(UICollectionView*)collectionView |
+ cellHeightAtIndexPath:(NSIndexPath*)indexPath { |
+ CollectionViewItem* item = |
+ [self.collectionViewModel itemAtIndexPath:indexPath]; |
+ |
+ if (item.type == ItemTypeFooter) |
+ return [MDCCollectionViewCell |
+ cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) |
+ forItem:item]; |
+ return MDCCellDefaultOneLineHeight; |
+} |
+ |
+#pragma mark - PrefObserverDelegate |
+ |
+- (void)onPreferenceChanged:(const std::string&)preferenceName { |
+ if (preferenceName == prefs::kNetworkPredictionEnabled || |
+ preferenceName == prefs::kNetworkPredictionWifiOnly) { |
+ NSString* detailText = [DataplanUsageCollectionViewController |
+ currentLabelForPreference:_browserState->GetPrefs() |
+ basePref:prefs::kNetworkPredictionEnabled |
+ wifiPref:prefs::kNetworkPredictionWifiOnly]; |
+ |
+ _preloadWebpagesDetailItem.get().detailText = detailText; |
+ |
+ [self reconfigureCellsForItems:@[ _preloadWebpagesDetailItem ] |
+ inSectionWithIdentifier:SectionIdentifierActions]; |
+ } |
+} |
+ |
+@end |