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

Side by Side Diff: ios/chrome/browser/ui/settings/translate_collection_view_controller.mm

Issue 2587023002: Upstream Chrome on iOS source code [8/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 unified diff | Download patch
OLDNEW
(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/translate_collection_view_controller.h"
6
7 #import <Foundation/Foundation.h>
8 #include <memory>
9
10 #include "base/mac/foundation_util.h"
11 #include "components/google/core/browser/google_util.h"
12 #include "components/prefs/pref_member.h"
13 #include "components/prefs/pref_service.h"
14 #include "components/translate/core/browser/translate_prefs.h"
15 #include "components/translate/core/common/translate_pref_names.h"
16 #include "ios/chrome/browser/application_context.h"
17 #import "ios/chrome/browser/translate/chrome_ios_translate_client.h"
18 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom e.h"
19 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item .h"
20 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item .h"
21 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h "
22 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
23 #import "ios/chrome/browser/ui/settings/settings_utils.h"
24 #import "ios/chrome/browser/ui/settings/utils/pref_backed_boolean.h"
25 #include "ios/chrome/grit/ios_chromium_strings.h"
26 #include "ios/chrome/grit/ios_strings.h"
27 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat erialPalettes.h"
28 #import "ios/third_party/material_components_ios/src/components/Snackbar/src/Mat erialSnackbar.h"
29 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF ontLoader.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "url/gurl.h"
32
33 namespace {
34
35 typedef NS_ENUM(NSInteger, SectionIdentifier) {
36 SectionIdentifierTranslate = kSectionIdentifierEnumZero,
37 SectionIdentifierFooter,
38 };
39
40 typedef NS_ENUM(NSInteger, ItemType) {
41 ItemTypeTranslate = kItemTypeEnumZero,
42 ItemTypeResetTranslate,
43 ItemTypeFooter,
44 };
45
46 const char kTranslateLearnMoreUrl[] =
47 "https://support.google.com/chrome/answer/3214105?p=mobile_translate";
48 NSString* const kTranslateSettingsCategory = @"ChromeTranslateSettings";
49
50 } // namespace
51
52 @interface TranslateCollectionViewController ()<BooleanObserver> {
53 // Profile preferences.
54 PrefService* _prefs; // weak
55 base::scoped_nsobject<PrefBackedBoolean> _translationEnabled;
56 // The item related to the switch for the translation setting.
57 base::scoped_nsobject<CollectionViewSwitchItem> _translationItem;
58 }
59
60 @end
61
62 @implementation TranslateCollectionViewController
63
64 #pragma mark - Initialization
65
66 - (instancetype)initWithPrefs:(PrefService*)prefs {
67 DCHECK(prefs);
68 self = [super initWithStyle:CollectionViewControllerStyleAppBar];
69 if (self) {
70 self.title = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING);
71 self.collectionViewAccessibilityIdentifier =
72 @"translate_settings_view_controller";
73 _prefs = prefs;
74 _translationEnabled.reset([[PrefBackedBoolean alloc]
75 initWithPrefService:_prefs
76 prefName:prefs::kEnableTranslate]);
77 [_translationEnabled setObserver:self];
78 [self loadModel];
79 }
80 return self;
81 }
82
83 - (void)dealloc {
84 [_translationEnabled setObserver:nil];
85 [super dealloc];
86 }
87
88 #pragma mark - SettingsRootCollectionViewController
89
90 - (void)loadModel {
91 [super loadModel];
92 CollectionViewModel* model = self.collectionViewModel;
93
94 // Translate Section
95 [model addSectionWithIdentifier:SectionIdentifierTranslate];
96 _translationItem.reset(
97 [[CollectionViewSwitchItem alloc] initWithType:ItemTypeTranslate]);
98 _translationItem.get().text =
99 l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING);
100 _translationItem.get().on = [_translationEnabled value];
101 [model addItem:_translationItem
102 toSectionWithIdentifier:SectionIdentifierTranslate];
103
104 CollectionViewTextItem* resetTranslate = [[[CollectionViewTextItem alloc]
105 initWithType:ItemTypeResetTranslate] autorelease];
106 resetTranslate.text = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING_RESET);
107 resetTranslate.accessibilityTraits |= UIAccessibilityTraitButton;
108 [model addItem:resetTranslate
109 toSectionWithIdentifier:SectionIdentifierTranslate];
110
111 // Footer Section
112 [model addSectionWithIdentifier:SectionIdentifierFooter];
113 CollectionViewFooterItem* footer = [[[CollectionViewFooterItem alloc]
114 initWithType:ItemTypeFooter] autorelease];
115 footer.text = l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING_DESCRIPTION);
116 footer.linkURL = google_util::AppendGoogleLocaleParam(
117 GURL(kTranslateLearnMoreUrl),
118 GetApplicationContext()->GetApplicationLocale());
119 footer.linkDelegate = self;
120 [model addItem:footer toSectionWithIdentifier:SectionIdentifierFooter];
121 }
122
123 #pragma mark UICollectionViewDataSource
124
125 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView
126 cellForItemAtIndexPath:(NSIndexPath*)indexPath {
127 UICollectionViewCell* cell =
128 [super collectionView:collectionView cellForItemAtIndexPath:indexPath];
129 NSInteger itemType =
130 [self.collectionViewModel itemTypeForIndexPath:indexPath];
131
132 switch (itemType) {
133 case ItemTypeTranslate: {
134 CollectionViewSwitchCell* switchCell =
135 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(cell);
136 [switchCell.switchView addTarget:self
137 action:@selector(translateToggled:)
138 forControlEvents:UIControlEventValueChanged];
139 break;
140 }
141 case ItemTypeResetTranslate: {
142 MDCCollectionViewTextCell* textCell =
143 base::mac::ObjCCastStrict<MDCCollectionViewTextCell>(cell);
144 textCell.textLabel.font =
145 [[MDFRobotoFontLoader sharedInstance] mediumFontOfSize:14];
146 break;
147 }
148 default:
149 break;
150 }
151
152 return cell;
153 }
154
155 #pragma mark UICollectionViewDelegate
156 - (void)collectionView:(UICollectionView*)collectionView
157 didSelectItemAtIndexPath:(NSIndexPath*)indexPath {
158 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath];
159
160 NSInteger itemType =
161 [self.collectionViewModel itemTypeForIndexPath:indexPath];
162
163 if (itemType == ItemTypeResetTranslate) {
164 std::unique_ptr<translate::TranslatePrefs> translatePrefs(
165 ChromeIOSTranslateClient::CreateTranslatePrefs(_prefs));
166 translatePrefs->ResetToDefaults();
167 NSString* messageText =
168 l10n_util::GetNSString(IDS_IOS_TRANSLATE_SETTING_RESET_NOTIFICATION);
169 MDCSnackbarMessage* message =
170 [MDCSnackbarMessage messageWithText:messageText];
171 message.category = kTranslateSettingsCategory;
172 [MDCSnackbarManager showMessage:message];
173 }
174 }
175
176 #pragma mark MDCCollectionViewStylingDelegate
177
178 - (MDCCollectionViewCellStyle)collectionView:(UICollectionView*)collectionView
179 cellStyleForSection:(NSInteger)section {
180 NSInteger sectionIdentifier =
181 [self.collectionViewModel sectionIdentifierForSection:section];
182
183 if (sectionIdentifier == SectionIdentifierFooter) {
184 return MDCCollectionViewCellStyleDefault;
185 }
186
187 return self.styler.cellStyle;
188 }
189
190 - (BOOL)collectionView:(UICollectionView*)collectionView
191 shouldHideItemBackgroundAtIndexPath:(NSIndexPath*)indexPath {
192 NSInteger sectionIdentifier =
193 [self.collectionViewModel sectionIdentifierForSection:indexPath.section];
194
195 if (sectionIdentifier == SectionIdentifierFooter) {
196 return YES;
197 }
198
199 return NO;
200 }
201
202 - (CGFloat)collectionView:(UICollectionView*)collectionView
203 cellHeightAtIndexPath:(NSIndexPath*)indexPath {
204 CollectionViewItem* item =
205 [self.collectionViewModel itemAtIndexPath:indexPath];
206
207 if (item.type == ItemTypeFooter)
208 return [MDCCollectionViewCell
209 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds)
210 forItem:item];
211 return MDCCellDefaultOneLineHeight;
212 }
213
214 - (BOOL)collectionView:(UICollectionView*)collectionView
215 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath {
216 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath];
217 switch (type) {
218 case ItemTypeFooter:
219 case ItemTypeTranslate:
220 return YES;
221 default:
222 return NO;
223 }
224 }
225
226 #pragma mark - BooleanObserver
227
228 - (void)booleanDidChange:(id<ObservableBoolean>)observableBoolean {
229 DCHECK_EQ(observableBoolean, _translationEnabled.get());
230
231 // Update the item.
232 _translationItem.get().on = [_translationEnabled value];
233
234 // Update the cell.
235 [self reconfigureCellsForItems:@[ _translationItem ]
236 inSectionWithIdentifier:SectionIdentifierTranslate];
237 }
238
239 #pragma mark - Actions
240
241 - (void)translateToggled:(id)sender {
242 NSIndexPath* switchPath = [self.collectionViewModel
243 indexPathForItemType:ItemTypeTranslate
244 sectionIdentifier:SectionIdentifierTranslate];
245
246 CollectionViewSwitchItem* switchItem =
247 base::mac::ObjCCastStrict<CollectionViewSwitchItem>(
248 [self.collectionViewModel itemAtIndexPath:switchPath]);
249 CollectionViewSwitchCell* switchCell =
250 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(
251 [self.collectionView cellForItemAtIndexPath:switchPath]);
252
253 DCHECK_EQ(switchCell.switchView, sender);
254 BOOL isOn = switchCell.switchView.isOn;
255 switchItem.on = isOn;
256 [_translationEnabled setValue:isOn];
257 }
258
259 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698