Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Unified Diff: ios/chrome/browser/ui/settings/handoff_collection_view_controller.mm

Issue 2589583003: Upstream Chrome on iOS source code [7/11]. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ios/chrome/browser/ui/settings/handoff_collection_view_controller.mm
diff --git a/ios/chrome/browser/ui/settings/handoff_collection_view_controller.mm b/ios/chrome/browser/ui/settings/handoff_collection_view_controller.mm
new file mode 100644
index 0000000000000000000000000000000000000000..8fcca0939ced9304ffa912899d0fd20619c707d7
--- /dev/null
+++ b/ios/chrome/browser/ui/settings/handoff_collection_view_controller.mm
@@ -0,0 +1,160 @@
+// Copyright 2016 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/handoff_collection_view_controller.h"
+
+#import "base/mac/foundation_util.h"
+#include "components/handoff/pref_names_ios.h"
+#include "components/prefs/pref_member.h"
+#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
+#import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrome.h"
+#import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item.h"
+#import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item.h"
+#import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
+#include "ios/chrome/grit/ios_strings.h"
+#include "ui/base/l10n/l10n_util.h"
+
+namespace {
+
+typedef NS_ENUM(NSInteger, SectionIdentifier) {
+ SectionIdentifierSwitch = kSectionIdentifierEnumZero,
+ SectionIdentifierFooter,
+};
+
+typedef NS_ENUM(NSInteger, ItemType) {
+ ItemTypeSwitch = kItemTypeEnumZero,
+ ItemTypeFooter,
+};
+
+} // namespace
+
+@interface HandoffCollectionViewController () {
+ // Pref for whether Handoff is enabled.
+ BooleanPrefMember _handoffEnabled;
+}
+
+- (void)switchChanged:(UISwitch*)switchView;
+
+@end
+
+@implementation HandoffCollectionViewController
+
+#pragma mark - Initialization
+
+- (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState {
+ DCHECK(browserState);
+ self = [super initWithStyle:CollectionViewControllerStyleAppBar];
+ if (self) {
+ self.title = l10n_util::GetNSString(IDS_IOS_OPTIONS_CONTINUITY_LABEL);
+ _handoffEnabled.Init(prefs::kIosHandoffToOtherDevices,
+ browserState->GetPrefs());
+ [self loadModel];
+ }
+ return self;
+}
+
+#pragma mark - CollectionViewController
+
+- (void)loadModel {
+ [super loadModel];
+ CollectionViewModel* model = self.collectionViewModel;
+
+ [model addSectionWithIdentifier:SectionIdentifierSwitch];
+ CollectionViewSwitchItem* switchItem = [[[CollectionViewSwitchItem alloc]
+ initWithType:ItemTypeSwitch] autorelease];
+ switchItem.text =
+ l10n_util::GetNSString(IDS_IOS_OPTIONS_ENABLE_HANDOFF_TO_OTHER_DEVICES);
+ switchItem.on = _handoffEnabled.GetValue();
+ [model addItem:switchItem toSectionWithIdentifier:SectionIdentifierSwitch];
+
+ // The footer item must currently go into a separate section, to work around a
+ // drawing bug in MDC.
+ // TODO(crbug.com/650424) Use setFooter:forSectionWithIdentifier:.
+ [model addSectionWithIdentifier:SectionIdentifierFooter];
+ CollectionViewFooterItem* footer = [[[CollectionViewFooterItem alloc]
+ initWithType:ItemTypeFooter] autorelease];
+ footer.text = l10n_util::GetNSString(
+ IDS_IOS_OPTIONS_ENABLE_HANDOFF_TO_OTHER_DEVICES_DETAILS);
+ [model addItem:footer toSectionWithIdentifier:SectionIdentifierFooter];
+}
+
+#pragma mark - UICollectionViewDataSource
+
+- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView
+ cellForItemAtIndexPath:(NSIndexPath*)indexPath {
+ UICollectionViewCell* cell =
+ [super collectionView:collectionView cellForItemAtIndexPath:indexPath];
+
+ ItemType itemType = static_cast<ItemType>(
+ [self.collectionViewModel itemTypeForIndexPath:indexPath]);
+
+ if (itemType == ItemTypeSwitch) {
+ CollectionViewSwitchCell* switchCell =
+ base::mac::ObjCCastStrict<CollectionViewSwitchCell>(cell);
+ [switchCell.switchView addTarget:self
+ action:@selector(switchChanged:)
+ forControlEvents:UIControlEventValueChanged];
+ }
+ return cell;
+}
+
+#pragma mark - MDCCollectionViewStylingDelegate
+
+- (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;
+}
+
+- (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;
+ }
+}
+
+- (BOOL)collectionView:(UICollectionView*)collectionView
+ hidesInkViewAtIndexPath:(NSIndexPath*)indexPath {
+ NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath];
+ switch (type) {
+ case ItemTypeSwitch:
+ return YES;
+ default:
+ return NO;
+ }
+}
+
+#pragma mark - Private
+
+- (void)switchChanged:(UISwitch*)switchView {
+ _handoffEnabled.SetValue(switchView.isOn);
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698