OLD | NEW |
(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/voicesearch_collection_view_controller.h
" |
| 6 |
| 7 #include "base/ios/weak_nsobject.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/mac/foundation_util.h" |
| 10 #include "base/mac/scoped_nsobject.h" |
| 11 #include "base/strings/sys_string_conversions.h" |
| 12 #include "components/prefs/pref_member.h" |
| 13 #include "components/prefs/pref_service.h" |
| 14 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item
.h" |
| 15 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" |
| 16 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 17 #include "ios/chrome/browser/voice/speech_input_locale_config.h" |
| 18 #include "ios/chrome/grit/ios_strings.h" |
| 19 #include "ios/public/provider/chrome/browser/voice/voice_search_prefs.h" |
| 20 #import "ios/third_party/material_components_ios/src/components/CollectionCells/
src/MaterialCollectionCells.h" |
| 21 #include "ui/base/l10n/l10n_util.h" |
| 22 #include "ui/base/l10n/l10n_util_mac.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 27 SectionIdentifierTTS = kSectionIdentifierEnumZero, |
| 28 SectionIdentifierLanguages, |
| 29 }; |
| 30 |
| 31 typedef NS_ENUM(NSInteger, ItemType) { |
| 32 ItemTypeTTSEnabled = kItemTypeEnumZero, |
| 33 ItemTypeLanguagesLanguageOption, |
| 34 }; |
| 35 |
| 36 } // namespace |
| 37 |
| 38 @interface VoicesearchCollectionViewController () { |
| 39 PrefService* _prefs; // weak |
| 40 StringPrefMember _selectedLanguage; |
| 41 BooleanPrefMember _ttsEnabled; |
| 42 } |
| 43 // Updates all cells to check the selected language and uncheck all the other. |
| 44 - (void)markAsCheckedLanguageAtIndex:(NSUInteger)index; |
| 45 |
| 46 // Returns YES if the current language supports TTS. |
| 47 - (BOOL)currentLanguageSupportsTTS; |
| 48 @end |
| 49 |
| 50 @implementation VoicesearchCollectionViewController |
| 51 |
| 52 #pragma mark - Initialization |
| 53 |
| 54 - (instancetype)initWithPrefs:(PrefService*)prefs { |
| 55 self = [super initWithStyle:CollectionViewControllerStyleAppBar]; |
| 56 if (self) { |
| 57 self.title = l10n_util::GetNSString(IDS_IOS_VOICE_SEARCH_SETTING_TITLE); |
| 58 _prefs = prefs; |
| 59 _selectedLanguage.Init(prefs::kVoiceSearchLocale, _prefs); |
| 60 _ttsEnabled.Init(prefs::kVoiceSearchTTS, _prefs); |
| 61 [self loadModel]; |
| 62 } |
| 63 return self; |
| 64 } |
| 65 |
| 66 - (void)loadModel { |
| 67 [super loadModel]; |
| 68 CollectionViewModel* model = self.collectionViewModel; |
| 69 |
| 70 // TTS section. |
| 71 [model addSectionWithIdentifier:SectionIdentifierTTS]; |
| 72 CollectionViewSwitchItem* tts = [[[CollectionViewSwitchItem alloc] |
| 73 initWithType:ItemTypeTTSEnabled] autorelease]; |
| 74 tts.text = l10n_util::GetNSString(IDS_IOS_VOICE_SEARCH_SETTING_TTS); |
| 75 tts.on = _ttsEnabled.GetValue(); |
| 76 tts.enabled = [self currentLanguageSupportsTTS]; |
| 77 [model addItem:tts toSectionWithIdentifier:SectionIdentifierTTS]; |
| 78 |
| 79 // Variables used to populate the languages section. |
| 80 voice::SpeechInputLocaleConfig* localeConfig = |
| 81 voice::SpeechInputLocaleConfig::GetInstance(); |
| 82 const std::vector<voice::SpeechInputLocale>& locales = |
| 83 localeConfig->GetAvailableLocales(); |
| 84 std::string selectedLocaleCode = _selectedLanguage.GetValue(); |
| 85 |
| 86 // Section with the list of voice search languages. |
| 87 [model addSectionWithIdentifier:SectionIdentifierLanguages]; |
| 88 // Add default locale option. Using an empty string for the voice search |
| 89 // locale pref indicates using the default locale. |
| 90 CollectionViewTextItem* defaultItem = [[[CollectionViewTextItem alloc] |
| 91 initWithType:ItemTypeLanguagesLanguageOption] autorelease]; |
| 92 defaultItem.text = |
| 93 l10n_util::GetNSStringF(IDS_IOS_VOICE_SEARCH_SETTINGS_DEFAULT_LOCALE, |
| 94 localeConfig->GetDefaultLocale().display_name); |
| 95 defaultItem.accessoryType = selectedLocaleCode.empty() |
| 96 ? MDCCollectionViewCellAccessoryCheckmark |
| 97 : MDCCollectionViewCellAccessoryNone; |
| 98 [model addItem:defaultItem |
| 99 toSectionWithIdentifier:SectionIdentifierLanguages]; |
| 100 // Add locale list. |
| 101 for (NSUInteger ii = 0; ii < locales.size(); ii++) { |
| 102 voice::SpeechInputLocale locale = locales[ii]; |
| 103 NSString* languageName = base::SysUTF16ToNSString(locale.display_name); |
| 104 BOOL checked = (locale.code == selectedLocaleCode); |
| 105 |
| 106 CollectionViewTextItem* languageItem = [[[CollectionViewTextItem alloc] |
| 107 initWithType:ItemTypeLanguagesLanguageOption] autorelease]; |
| 108 languageItem.text = languageName; |
| 109 languageItem.accessoryType = checked |
| 110 ? MDCCollectionViewCellAccessoryCheckmark |
| 111 : MDCCollectionViewCellAccessoryNone; |
| 112 [model addItem:languageItem |
| 113 toSectionWithIdentifier:SectionIdentifierLanguages]; |
| 114 } |
| 115 } |
| 116 |
| 117 #pragma mark - UICollectionViewDelegate |
| 118 |
| 119 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView |
| 120 cellForItemAtIndexPath:(NSIndexPath*)indexPath { |
| 121 UICollectionViewCell* cell = |
| 122 [super collectionView:collectionView cellForItemAtIndexPath:indexPath]; |
| 123 NSInteger itemType = |
| 124 [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 125 |
| 126 if (itemType == ItemTypeTTSEnabled) { |
| 127 // Have the switch send a message on UIControlEventValueChanged. |
| 128 CollectionViewSwitchCell* switchCell = |
| 129 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(cell); |
| 130 [switchCell.switchView addTarget:self |
| 131 action:@selector(ttsToggled:) |
| 132 forControlEvents:UIControlEventValueChanged]; |
| 133 } |
| 134 |
| 135 return cell; |
| 136 } |
| 137 |
| 138 - (void)collectionView:(UICollectionView*)collectionView |
| 139 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
| 140 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; |
| 141 |
| 142 CollectionViewModel* model = self.collectionViewModel; |
| 143 CollectionViewItem* item = [model itemAtIndexPath:indexPath]; |
| 144 |
| 145 // Language options. |
| 146 if (item.type == ItemTypeLanguagesLanguageOption) { |
| 147 NSInteger index = [model indexInItemTypeForIndexPath:indexPath]; |
| 148 std::string selectedLocaleCode; |
| 149 if (index > 0) { |
| 150 // Fetch selected locale code from locale list if non-default option was |
| 151 // selected. Setting the preference to the empty string denotes using the |
| 152 // default locale. |
| 153 voice::SpeechInputLocaleConfig* localeConfig = |
| 154 voice::SpeechInputLocaleConfig::GetInstance(); |
| 155 DCHECK_LT(static_cast<size_t>(index - 1), |
| 156 localeConfig->GetAvailableLocales().size()); |
| 157 selectedLocaleCode = localeConfig->GetAvailableLocales()[index - 1].code; |
| 158 } |
| 159 _selectedLanguage.SetValue(selectedLocaleCode); |
| 160 |
| 161 // Update the UI. |
| 162 [self markAsCheckedLanguageAtIndex:index]; |
| 163 } |
| 164 } |
| 165 |
| 166 #pragma mark MDCCollectionViewStylingDelegate |
| 167 |
| 168 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 169 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { |
| 170 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 171 switch (type) { |
| 172 case ItemTypeTTSEnabled: |
| 173 return YES; |
| 174 default: |
| 175 return NO; |
| 176 } |
| 177 } |
| 178 |
| 179 #pragma mark - Actions |
| 180 |
| 181 - (void)ttsToggled:(id)sender { |
| 182 NSIndexPath* switchPath = |
| 183 [self.collectionViewModel indexPathForItemType:ItemTypeTTSEnabled |
| 184 sectionIdentifier:SectionIdentifierTTS]; |
| 185 |
| 186 CollectionViewSwitchItem* switchItem = |
| 187 base::mac::ObjCCastStrict<CollectionViewSwitchItem>( |
| 188 [self.collectionViewModel itemAtIndexPath:switchPath]); |
| 189 CollectionViewSwitchCell* switchCell = |
| 190 base::mac::ObjCCastStrict<CollectionViewSwitchCell>( |
| 191 [self.collectionView cellForItemAtIndexPath:switchPath]); |
| 192 |
| 193 // Update the model and the preference with the current value of the switch. |
| 194 DCHECK_EQ(switchCell.switchView, sender); |
| 195 BOOL isOn = switchCell.switchView.isOn; |
| 196 switchItem.on = isOn; |
| 197 _ttsEnabled.SetValue(isOn); |
| 198 } |
| 199 |
| 200 #pragma mark - Private methods |
| 201 |
| 202 - (void)markAsCheckedLanguageAtIndex:(NSUInteger)index { |
| 203 // Update the collection view model with the new selected language. |
| 204 NSArray* languageItems = [self.collectionViewModel |
| 205 itemsInSectionWithIdentifier:SectionIdentifierLanguages]; |
| 206 NSMutableArray* modifiedItems = [NSMutableArray array]; |
| 207 for (NSUInteger ii = 0; ii < [languageItems count]; ++ii) { |
| 208 MDCCollectionViewCellAccessoryType type = |
| 209 (ii == index) ? MDCCollectionViewCellAccessoryCheckmark |
| 210 : MDCCollectionViewCellAccessoryNone; |
| 211 |
| 212 CollectionViewTextItem* textItem = |
| 213 base::mac::ObjCCastStrict<CollectionViewTextItem>( |
| 214 [languageItems objectAtIndex:ii]); |
| 215 if (textItem.accessoryType != type) { |
| 216 textItem.accessoryType = type; |
| 217 [modifiedItems addObject:textItem]; |
| 218 } |
| 219 } |
| 220 |
| 221 // Some languages do not support TTS. Disable the switch for those |
| 222 // languages. |
| 223 NSIndexPath* switchPath = |
| 224 [self.collectionViewModel indexPathForItemType:ItemTypeTTSEnabled |
| 225 sectionIdentifier:SectionIdentifierTTS]; |
| 226 CollectionViewSwitchCell* switchCell = |
| 227 base::mac::ObjCCastStrict<CollectionViewSwitchCell>( |
| 228 [self.collectionView cellForItemAtIndexPath:switchPath]); |
| 229 |
| 230 BOOL enabled = [self currentLanguageSupportsTTS]; |
| 231 BOOL on = enabled && _ttsEnabled.GetValue(); |
| 232 |
| 233 UISwitch* switchView = switchCell.switchView; |
| 234 switchView.enabled = enabled; |
| 235 switchCell.textLabel.textColor = |
| 236 [CollectionViewSwitchCell defaultTextColorForState:switchView.state]; |
| 237 if (on != switchView.isOn) { |
| 238 [switchView setOn:on animated:YES]; |
| 239 } |
| 240 |
| 241 [self reconfigureCellsForItems:modifiedItems |
| 242 inSectionWithIdentifier:SectionIdentifierLanguages]; |
| 243 } |
| 244 |
| 245 - (BOOL)currentLanguageSupportsTTS { |
| 246 voice::SpeechInputLocaleConfig* localeConfig = |
| 247 voice::SpeechInputLocaleConfig::GetInstance(); |
| 248 std::string localeCode = _selectedLanguage.GetValue().empty() |
| 249 ? localeConfig->GetDefaultLocale().code |
| 250 : _selectedLanguage.GetValue(); |
| 251 return localeConfig->IsTextToSpeechEnabledForCode(localeCode); |
| 252 } |
| 253 |
| 254 @end |
OLD | NEW |