OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/handoff_collection_view_controller.h" |
| 6 |
| 7 #import "base/mac/foundation_util.h" |
| 8 #include "components/handoff/pref_names_ios.h" |
| 9 #include "components/prefs/pref_member.h" |
| 10 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 11 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" |
| 12 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item
.h" |
| 13 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item
.h" |
| 14 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 15 #include "ios/chrome/grit/ios_strings.h" |
| 16 #include "ui/base/l10n/l10n_util.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 21 SectionIdentifierSwitch = kSectionIdentifierEnumZero, |
| 22 SectionIdentifierFooter, |
| 23 }; |
| 24 |
| 25 typedef NS_ENUM(NSInteger, ItemType) { |
| 26 ItemTypeSwitch = kItemTypeEnumZero, |
| 27 ItemTypeFooter, |
| 28 }; |
| 29 |
| 30 } // namespace |
| 31 |
| 32 @interface HandoffCollectionViewController () { |
| 33 // Pref for whether Handoff is enabled. |
| 34 BooleanPrefMember _handoffEnabled; |
| 35 } |
| 36 |
| 37 - (void)switchChanged:(UISwitch*)switchView; |
| 38 |
| 39 @end |
| 40 |
| 41 @implementation HandoffCollectionViewController |
| 42 |
| 43 #pragma mark - Initialization |
| 44 |
| 45 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState { |
| 46 DCHECK(browserState); |
| 47 self = [super initWithStyle:CollectionViewControllerStyleAppBar]; |
| 48 if (self) { |
| 49 self.title = l10n_util::GetNSString(IDS_IOS_OPTIONS_CONTINUITY_LABEL); |
| 50 _handoffEnabled.Init(prefs::kIosHandoffToOtherDevices, |
| 51 browserState->GetPrefs()); |
| 52 [self loadModel]; |
| 53 } |
| 54 return self; |
| 55 } |
| 56 |
| 57 #pragma mark - CollectionViewController |
| 58 |
| 59 - (void)loadModel { |
| 60 [super loadModel]; |
| 61 CollectionViewModel* model = self.collectionViewModel; |
| 62 |
| 63 [model addSectionWithIdentifier:SectionIdentifierSwitch]; |
| 64 CollectionViewSwitchItem* switchItem = [[[CollectionViewSwitchItem alloc] |
| 65 initWithType:ItemTypeSwitch] autorelease]; |
| 66 switchItem.text = |
| 67 l10n_util::GetNSString(IDS_IOS_OPTIONS_ENABLE_HANDOFF_TO_OTHER_DEVICES); |
| 68 switchItem.on = _handoffEnabled.GetValue(); |
| 69 [model addItem:switchItem toSectionWithIdentifier:SectionIdentifierSwitch]; |
| 70 |
| 71 // The footer item must currently go into a separate section, to work around a |
| 72 // drawing bug in MDC. |
| 73 // TODO(crbug.com/650424) Use setFooter:forSectionWithIdentifier:. |
| 74 [model addSectionWithIdentifier:SectionIdentifierFooter]; |
| 75 CollectionViewFooterItem* footer = [[[CollectionViewFooterItem alloc] |
| 76 initWithType:ItemTypeFooter] autorelease]; |
| 77 footer.text = l10n_util::GetNSString( |
| 78 IDS_IOS_OPTIONS_ENABLE_HANDOFF_TO_OTHER_DEVICES_DETAILS); |
| 79 [model addItem:footer toSectionWithIdentifier:SectionIdentifierFooter]; |
| 80 } |
| 81 |
| 82 #pragma mark - UICollectionViewDataSource |
| 83 |
| 84 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView |
| 85 cellForItemAtIndexPath:(NSIndexPath*)indexPath { |
| 86 UICollectionViewCell* cell = |
| 87 [super collectionView:collectionView cellForItemAtIndexPath:indexPath]; |
| 88 |
| 89 ItemType itemType = static_cast<ItemType>( |
| 90 [self.collectionViewModel itemTypeForIndexPath:indexPath]); |
| 91 |
| 92 if (itemType == ItemTypeSwitch) { |
| 93 CollectionViewSwitchCell* switchCell = |
| 94 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(cell); |
| 95 [switchCell.switchView addTarget:self |
| 96 action:@selector(switchChanged:) |
| 97 forControlEvents:UIControlEventValueChanged]; |
| 98 } |
| 99 return cell; |
| 100 } |
| 101 |
| 102 #pragma mark - MDCCollectionViewStylingDelegate |
| 103 |
| 104 - (CGFloat)collectionView:(UICollectionView*)collectionView |
| 105 cellHeightAtIndexPath:(NSIndexPath*)indexPath { |
| 106 CollectionViewItem* item = |
| 107 [self.collectionViewModel itemAtIndexPath:indexPath]; |
| 108 if (item.type == ItemTypeFooter) |
| 109 return [MDCCollectionViewCell |
| 110 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) |
| 111 forItem:item]; |
| 112 return MDCCellDefaultOneLineHeight; |
| 113 } |
| 114 |
| 115 - (MDCCollectionViewCellStyle)collectionView:(UICollectionView*)collectionView |
| 116 cellStyleForSection:(NSInteger)section { |
| 117 NSInteger sectionIdentifier = |
| 118 [self.collectionViewModel sectionIdentifierForSection:section]; |
| 119 switch (sectionIdentifier) { |
| 120 case SectionIdentifierFooter: |
| 121 // Display the Learn More footer in the default style with no "card" UI |
| 122 // and no section padding. |
| 123 return MDCCollectionViewCellStyleDefault; |
| 124 default: |
| 125 return self.styler.cellStyle; |
| 126 } |
| 127 } |
| 128 |
| 129 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 130 shouldHideItemBackgroundAtIndexPath:(NSIndexPath*)indexPath { |
| 131 NSInteger sectionIdentifier = |
| 132 [self.collectionViewModel sectionIdentifierForSection:indexPath.section]; |
| 133 switch (sectionIdentifier) { |
| 134 case SectionIdentifierFooter: |
| 135 // Display the Learn More footer without any background image or |
| 136 // shadowing. |
| 137 return YES; |
| 138 default: |
| 139 return NO; |
| 140 } |
| 141 } |
| 142 |
| 143 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 144 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { |
| 145 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 146 switch (type) { |
| 147 case ItemTypeSwitch: |
| 148 return YES; |
| 149 default: |
| 150 return NO; |
| 151 } |
| 152 } |
| 153 |
| 154 #pragma mark - Private |
| 155 |
| 156 - (void)switchChanged:(UISwitch*)switchView { |
| 157 _handoffEnabled.SetValue(switchView.isOn); |
| 158 } |
| 159 |
| 160 @end |
OLD | NEW |