| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/ui/settings/translate_collection_view_controller.h" | 5 #import "ios/chrome/browser/ui/settings/translate_collection_view_controller.h" |
| 6 | 6 |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/mac/foundation_util.h" | 10 #include "base/mac/foundation_util.h" |
| 11 #include "base/mac/scoped_nsobject.h" | |
| 12 #include "components/google/core/browser/google_util.h" | 11 #include "components/google/core/browser/google_util.h" |
| 13 #include "components/prefs/pref_member.h" | 12 #include "components/prefs/pref_member.h" |
| 14 #include "components/prefs/pref_service.h" | 13 #include "components/prefs/pref_service.h" |
| 15 #include "components/translate/core/browser/translate_pref_names.h" | 14 #include "components/translate/core/browser/translate_pref_names.h" |
| 16 #include "components/translate/core/browser/translate_prefs.h" | 15 #include "components/translate/core/browser/translate_prefs.h" |
| 17 #include "ios/chrome/browser/application_context.h" | 16 #include "ios/chrome/browser/application_context.h" |
| 18 #import "ios/chrome/browser/translate/chrome_ios_translate_client.h" | 17 #import "ios/chrome/browser/translate/chrome_ios_translate_client.h" |
| 19 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" | 18 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" |
| 20 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item
.h" | 19 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item
.h" |
| 21 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item
.h" | 20 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item
.h" |
| 22 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" | 21 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" |
| 23 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | 22 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 24 #import "ios/chrome/browser/ui/settings/settings_utils.h" | 23 #import "ios/chrome/browser/ui/settings/settings_utils.h" |
| 25 #import "ios/chrome/browser/ui/settings/utils/pref_backed_boolean.h" | 24 #import "ios/chrome/browser/ui/settings/utils/pref_backed_boolean.h" |
| 26 #include "ios/chrome/browser/ui/uikit_ui_util.h" | 25 #include "ios/chrome/browser/ui/uikit_ui_util.h" |
| 27 #include "ios/chrome/grit/ios_chromium_strings.h" | 26 #include "ios/chrome/grit/ios_chromium_strings.h" |
| 28 #include "ios/chrome/grit/ios_strings.h" | 27 #include "ios/chrome/grit/ios_strings.h" |
| 29 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" | 28 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" |
| 30 #import "ios/third_party/material_components_ios/src/components/Snackbar/src/Mat
erialSnackbar.h" | 29 #import "ios/third_party/material_components_ios/src/components/Snackbar/src/Mat
erialSnackbar.h" |
| 31 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" | 30 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" |
| 32 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
| 33 #include "url/gurl.h" | 32 #include "url/gurl.h" |
| 34 | 33 |
| 34 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 35 #error "This file requires ARC support." |
| 36 #endif |
| 37 |
| 35 namespace { | 38 namespace { |
| 36 | 39 |
| 37 typedef NS_ENUM(NSInteger, SectionIdentifier) { | 40 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 38 SectionIdentifierTranslate = kSectionIdentifierEnumZero, | 41 SectionIdentifierTranslate = kSectionIdentifierEnumZero, |
| 39 SectionIdentifierFooter, | 42 SectionIdentifierFooter, |
| 40 }; | 43 }; |
| 41 | 44 |
| 42 typedef NS_ENUM(NSInteger, ItemType) { | 45 typedef NS_ENUM(NSInteger, ItemType) { |
| 43 ItemTypeTranslate = kItemTypeEnumZero, | 46 ItemTypeTranslate = kItemTypeEnumZero, |
| 44 ItemTypeResetTranslate, | 47 ItemTypeResetTranslate, |
| 45 ItemTypeFooter, | 48 ItemTypeFooter, |
| 46 }; | 49 }; |
| 47 | 50 |
| 48 const char kTranslateLearnMoreUrl[] = | 51 const char kTranslateLearnMoreUrl[] = |
| 49 "https://support.google.com/chrome/answer/3214105?p=mobile_translate"; | 52 "https://support.google.com/chrome/answer/3214105?p=mobile_translate"; |
| 50 NSString* const kTranslateSettingsCategory = @"ChromeTranslateSettings"; | 53 NSString* const kTranslateSettingsCategory = @"ChromeTranslateSettings"; |
| 51 | 54 |
| 52 } // namespace | 55 } // namespace |
| 53 | 56 |
| 54 @interface TranslateCollectionViewController ()<BooleanObserver> { | 57 @interface TranslateCollectionViewController ()<BooleanObserver> { |
| 55 // Profile preferences. | 58 // Profile preferences. |
| 56 PrefService* _prefs; // weak | 59 PrefService* _prefs; // weak |
| 57 base::scoped_nsobject<PrefBackedBoolean> _translationEnabled; | 60 PrefBackedBoolean* _translationEnabled; |
| 58 // The item related to the switch for the translation setting. | 61 // The item related to the switch for the translation setting. |
| 59 base::scoped_nsobject<CollectionViewSwitchItem> _translationItem; | 62 CollectionViewSwitchItem* _translationItem; |
| 60 } | 63 } |
| 61 | 64 |
| 62 @end | 65 @end |
| 63 | 66 |
| 64 @implementation TranslateCollectionViewController | 67 @implementation TranslateCollectionViewController |
| 65 | 68 |
| 66 #pragma mark - Initialization | 69 #pragma mark - Initialization |
| 67 | 70 |
| 68 - (instancetype)initWithPrefs:(PrefService*)prefs { | 71 - (instancetype)initWithPrefs:(PrefService*)prefs { |
| 69 DCHECK(prefs); | 72 DCHECK(prefs); |
| 70 self = [super initWithStyle:CollectionViewControllerStyleAppBar]; | 73 self = [super initWithStyle:CollectionViewControllerStyleAppBar]; |
| 71 if (self) { | 74 if (self) { |
| 72 self.title = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING); | 75 self.title = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING); |
| 73 self.collectionViewAccessibilityIdentifier = | 76 self.collectionViewAccessibilityIdentifier = |
| 74 @"translate_settings_view_controller"; | 77 @"translate_settings_view_controller"; |
| 75 _prefs = prefs; | 78 _prefs = prefs; |
| 76 _translationEnabled.reset([[PrefBackedBoolean alloc] | 79 _translationEnabled = |
| 77 initWithPrefService:_prefs | 80 [[PrefBackedBoolean alloc] initWithPrefService:_prefs |
| 78 prefName:prefs::kEnableTranslate]); | 81 prefName:prefs::kEnableTranslate]; |
| 79 [_translationEnabled setObserver:self]; | 82 [_translationEnabled setObserver:self]; |
| 80 [self loadModel]; | 83 [self loadModel]; |
| 81 } | 84 } |
| 82 return self; | 85 return self; |
| 83 } | 86 } |
| 84 | 87 |
| 85 - (void)dealloc { | 88 - (void)dealloc { |
| 86 [_translationEnabled setObserver:nil]; | 89 [_translationEnabled setObserver:nil]; |
| 87 [super dealloc]; | |
| 88 } | 90 } |
| 89 | 91 |
| 90 #pragma mark - SettingsRootCollectionViewController | 92 #pragma mark - SettingsRootCollectionViewController |
| 91 | 93 |
| 92 - (void)loadModel { | 94 - (void)loadModel { |
| 93 [super loadModel]; | 95 [super loadModel]; |
| 94 CollectionViewModel* model = self.collectionViewModel; | 96 CollectionViewModel* model = self.collectionViewModel; |
| 95 | 97 |
| 96 // Translate Section | 98 // Translate Section |
| 97 [model addSectionWithIdentifier:SectionIdentifierTranslate]; | 99 [model addSectionWithIdentifier:SectionIdentifierTranslate]; |
| 98 _translationItem.reset( | 100 _translationItem = |
| 99 [[CollectionViewSwitchItem alloc] initWithType:ItemTypeTranslate]); | 101 [[CollectionViewSwitchItem alloc] initWithType:ItemTypeTranslate]; |
| 100 _translationItem.get().text = | 102 _translationItem.text = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING); |
| 101 l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING); | 103 _translationItem.on = [_translationEnabled value]; |
| 102 _translationItem.get().on = [_translationEnabled value]; | |
| 103 [model addItem:_translationItem | 104 [model addItem:_translationItem |
| 104 toSectionWithIdentifier:SectionIdentifierTranslate]; | 105 toSectionWithIdentifier:SectionIdentifierTranslate]; |
| 105 | 106 |
| 106 CollectionViewTextItem* resetTranslate = [[[CollectionViewTextItem alloc] | 107 CollectionViewTextItem* resetTranslate = |
| 107 initWithType:ItemTypeResetTranslate] autorelease]; | 108 [[CollectionViewTextItem alloc] initWithType:ItemTypeResetTranslate]; |
| 108 resetTranslate.text = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING_RESET); | 109 resetTranslate.text = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING_RESET); |
| 109 resetTranslate.accessibilityTraits |= UIAccessibilityTraitButton; | 110 resetTranslate.accessibilityTraits |= UIAccessibilityTraitButton; |
| 110 resetTranslate.textFont = [MDCTypography body2Font]; | 111 resetTranslate.textFont = [MDCTypography body2Font]; |
| 111 [model addItem:resetTranslate | 112 [model addItem:resetTranslate |
| 112 toSectionWithIdentifier:SectionIdentifierTranslate]; | 113 toSectionWithIdentifier:SectionIdentifierTranslate]; |
| 113 | 114 |
| 114 // Footer Section | 115 // Footer Section |
| 115 [model addSectionWithIdentifier:SectionIdentifierFooter]; | 116 [model addSectionWithIdentifier:SectionIdentifierFooter]; |
| 116 CollectionViewFooterItem* footer = [[[CollectionViewFooterItem alloc] | 117 CollectionViewFooterItem* footer = |
| 117 initWithType:ItemTypeFooter] autorelease]; | 118 [[CollectionViewFooterItem alloc] initWithType:ItemTypeFooter]; |
| 118 footer.text = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING_DESCRIPTION); | 119 footer.text = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING_DESCRIPTION); |
| 119 footer.linkURL = google_util::AppendGoogleLocaleParam( | 120 footer.linkURL = google_util::AppendGoogleLocaleParam( |
| 120 GURL(kTranslateLearnMoreUrl), | 121 GURL(kTranslateLearnMoreUrl), |
| 121 GetApplicationContext()->GetApplicationLocale()); | 122 GetApplicationContext()->GetApplicationLocale()); |
| 122 footer.linkDelegate = self; | 123 footer.linkDelegate = self; |
| 123 [model addItem:footer toSectionWithIdentifier:SectionIdentifierFooter]; | 124 [model addItem:footer toSectionWithIdentifier:SectionIdentifierFooter]; |
| 124 } | 125 } |
| 125 | 126 |
| 126 #pragma mark UICollectionViewDataSource | 127 #pragma mark UICollectionViewDataSource |
| 127 | 128 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 case ItemTypeTranslate: | 217 case ItemTypeTranslate: |
| 217 return YES; | 218 return YES; |
| 218 default: | 219 default: |
| 219 return NO; | 220 return NO; |
| 220 } | 221 } |
| 221 } | 222 } |
| 222 | 223 |
| 223 #pragma mark - BooleanObserver | 224 #pragma mark - BooleanObserver |
| 224 | 225 |
| 225 - (void)booleanDidChange:(id<ObservableBoolean>)observableBoolean { | 226 - (void)booleanDidChange:(id<ObservableBoolean>)observableBoolean { |
| 226 DCHECK_EQ(observableBoolean, _translationEnabled.get()); | 227 DCHECK_EQ(observableBoolean, _translationEnabled); |
| 227 | 228 |
| 228 // Update the item. | 229 // Update the item. |
| 229 _translationItem.get().on = [_translationEnabled value]; | 230 _translationItem.on = [_translationEnabled value]; |
| 230 | 231 |
| 231 // Update the cell. | 232 // Update the cell. |
| 232 [self reconfigureCellsForItems:@[ _translationItem ]]; | 233 [self reconfigureCellsForItems:@[ _translationItem ]]; |
| 233 } | 234 } |
| 234 | 235 |
| 235 #pragma mark - Actions | 236 #pragma mark - Actions |
| 236 | 237 |
| 237 - (void)translateToggled:(id)sender { | 238 - (void)translateToggled:(id)sender { |
| 238 NSIndexPath* switchPath = [self.collectionViewModel | 239 NSIndexPath* switchPath = [self.collectionViewModel |
| 239 indexPathForItemType:ItemTypeTranslate | 240 indexPathForItemType:ItemTypeTranslate |
| 240 sectionIdentifier:SectionIdentifierTranslate]; | 241 sectionIdentifier:SectionIdentifierTranslate]; |
| 241 | 242 |
| 242 CollectionViewSwitchItem* switchItem = | 243 CollectionViewSwitchItem* switchItem = |
| 243 base::mac::ObjCCastStrict<CollectionViewSwitchItem>( | 244 base::mac::ObjCCastStrict<CollectionViewSwitchItem>( |
| 244 [self.collectionViewModel itemAtIndexPath:switchPath]); | 245 [self.collectionViewModel itemAtIndexPath:switchPath]); |
| 245 CollectionViewSwitchCell* switchCell = | 246 CollectionViewSwitchCell* switchCell = |
| 246 base::mac::ObjCCastStrict<CollectionViewSwitchCell>( | 247 base::mac::ObjCCastStrict<CollectionViewSwitchCell>( |
| 247 [self.collectionView cellForItemAtIndexPath:switchPath]); | 248 [self.collectionView cellForItemAtIndexPath:switchPath]); |
| 248 | 249 |
| 249 DCHECK_EQ(switchCell.switchView, sender); | 250 DCHECK_EQ(switchCell.switchView, sender); |
| 250 BOOL isOn = switchCell.switchView.isOn; | 251 BOOL isOn = switchCell.switchView.isOn; |
| 251 switchItem.on = isOn; | 252 switchItem.on = isOn; |
| 252 [_translationEnabled setValue:isOn]; | 253 [_translationEnabled setValue:isOn]; |
| 253 } | 254 } |
| 254 | 255 |
| 255 @end | 256 @end |
| OLD | NEW |