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/sync_create_passphrase_collection_view_c
ontroller.h" |
| 6 |
| 7 #import <UIKit/UIKit.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #import "base/mac/foundation_util.h" |
| 11 #include "base/mac/scoped_nsobject.h" |
| 12 #include "base/strings/sys_string_conversions.h" |
| 13 #include "components/strings/grit/components_strings.h" |
| 14 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
| 15 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 16 #import "ios/chrome/browser/ui/settings/cells/byo_textfield_item.h" |
| 17 #include "ios/chrome/grit/ios_strings.h" |
| 18 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" |
| 19 #include "ui/base/l10n/l10n_util_mac.h" |
| 20 |
| 21 using namespace ios_internal::sync_encryption_passphrase; |
| 22 |
| 23 @interface SyncCreatePassphraseCollectionViewController () { |
| 24 base::scoped_nsobject<UITextField> confirmPassphrase_; |
| 25 } |
| 26 // Returns a confirm passphrase item. |
| 27 - (CollectionViewItem*)confirmPassphraseItem; |
| 28 @end |
| 29 |
| 30 @implementation SyncCreatePassphraseCollectionViewController |
| 31 |
| 32 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState { |
| 33 self = [super initWithBrowserState:browserState]; |
| 34 if (self) { |
| 35 self.title = |
| 36 l10n_util::GetNSString(IDS_IOS_SYNC_ENCRYPTION_CREATE_PASSPHRASE); |
| 37 self.headerMessage = nil; |
| 38 self.footerMessage = |
| 39 l10n_util::GetNSString(IDS_IOS_SYNC_ENCRYPTION_PASSPHRASE_INFO), |
| 40 self.processingMessage = |
| 41 l10n_util::GetNSString(IDS_IOS_SYNC_PASSPHRASE_ENCRYPTING); |
| 42 |
| 43 [self loadModel]; |
| 44 } |
| 45 return self; |
| 46 } |
| 47 |
| 48 #pragma mark - View lifecycle |
| 49 |
| 50 - (void)didReceiveMemoryWarning { |
| 51 [super didReceiveMemoryWarning]; |
| 52 if (![self isViewLoaded]) { |
| 53 confirmPassphrase_.reset(); |
| 54 } |
| 55 } |
| 56 |
| 57 - (void)viewDidDisappear:(BOOL)animated { |
| 58 [super viewDidDisappear:animated]; |
| 59 if ([self isMovingFromParentViewController]) { |
| 60 [self unregisterTextField:self.confirmPassphrase]; |
| 61 } |
| 62 } |
| 63 |
| 64 #pragma mark - SettingsRootCollectionViewController |
| 65 |
| 66 - (void)loadModel { |
| 67 [super loadModel]; |
| 68 CollectionViewModel* model = self.collectionViewModel; |
| 69 |
| 70 NSInteger enterPassphraseIndex = |
| 71 [model indexPathForItemType:ItemTypeEnterPassphrase |
| 72 sectionIdentifier:SectionIdentifierPassphrase] |
| 73 .item; |
| 74 [model insertItem:[self confirmPassphraseItem] |
| 75 inSectionWithIdentifier:SectionIdentifierPassphrase |
| 76 atIndex:enterPassphraseIndex + 1]; |
| 77 } |
| 78 |
| 79 #pragma mark - Items |
| 80 |
| 81 - (CollectionViewItem*)confirmPassphraseItem { |
| 82 if (!confirmPassphrase_) { |
| 83 confirmPassphrase_.reset([[UITextField alloc] init]); |
| 84 [confirmPassphrase_ setFont:[MDCTypography body1Font]]; |
| 85 [confirmPassphrase_ setSecureTextEntry:YES]; |
| 86 [confirmPassphrase_ setBackgroundColor:[UIColor clearColor]]; |
| 87 [confirmPassphrase_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; |
| 88 [confirmPassphrase_ setAutocorrectionType:UITextAutocorrectionTypeNo]; |
| 89 [confirmPassphrase_ |
| 90 setPlaceholder:l10n_util::GetNSString( |
| 91 IDS_IOS_SYNC_CONFIRM_PASSPHRASE_LABEL)]; |
| 92 [self registerTextField:confirmPassphrase_]; |
| 93 } |
| 94 |
| 95 BYOTextFieldItem* item = [[[BYOTextFieldItem alloc] |
| 96 initWithType:ItemTypeConfirmPassphrase] autorelease]; |
| 97 item.textField = confirmPassphrase_; |
| 98 return item; |
| 99 } |
| 100 |
| 101 #pragma mark UICollectionViewDelegate |
| 102 |
| 103 - (void)collectionView:(UICollectionView*)collectionView |
| 104 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
| 105 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; |
| 106 NSInteger itemType = |
| 107 [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 108 if (itemType == ItemTypeConfirmPassphrase) { |
| 109 [confirmPassphrase_ becomeFirstResponder]; |
| 110 } |
| 111 } |
| 112 |
| 113 #pragma mark SyncEncryptionPassphraseCollectionViewController |
| 114 |
| 115 - (BOOL)forDecryption { |
| 116 return NO; |
| 117 } |
| 118 |
| 119 - (void)signInPressed { |
| 120 NSString* passphraseText = [self.passphrase text]; |
| 121 NSString* confirmPassphraseText = [confirmPassphrase_ text]; |
| 122 if (![self areAllFieldsFilled]) { |
| 123 [self clearFieldsOnError:l10n_util::GetNSString( |
| 124 IDS_SYNC_EMPTY_PASSPHRASE_ERROR)]; |
| 125 [self reloadData]; |
| 126 return; |
| 127 } else if (![passphraseText isEqualToString:confirmPassphraseText]) { |
| 128 [self clearFieldsOnError:l10n_util::GetNSString( |
| 129 IDS_SYNC_PASSPHRASE_MISMATCH_ERROR)]; |
| 130 [self reloadData]; |
| 131 return; |
| 132 } |
| 133 |
| 134 [super signInPressed]; |
| 135 } |
| 136 |
| 137 - (BOOL)areAllFieldsFilled { |
| 138 return [super areAllFieldsFilled] && [self.confirmPassphrase text].length > 0; |
| 139 } |
| 140 |
| 141 - (void)clearFieldsOnError:(NSString*)errorMessage { |
| 142 [super clearFieldsOnError:errorMessage]; |
| 143 [self.confirmPassphrase setText:@""]; |
| 144 } |
| 145 |
| 146 #pragma mark - UIControl event listener |
| 147 |
| 148 - (void)textFieldDidEndEditing:(id)sender { |
| 149 if (sender == self.passphrase) { |
| 150 [confirmPassphrase_ becomeFirstResponder]; |
| 151 } else if (sender == self.confirmPassphrase) { |
| 152 if ([self areAllFieldsFilled]) { |
| 153 // The right nav bar button is disabled when either of the text fields is |
| 154 // empty. Hitting return when a text field is empty should not cause the |
| 155 // password to be applied. |
| 156 [self signInPressed]; |
| 157 } else { |
| 158 [self clearFieldsOnError:l10n_util::GetNSString( |
| 159 IDS_SYNC_EMPTY_PASSPHRASE_ERROR)]; |
| 160 [self reloadData]; |
| 161 } |
| 162 } |
| 163 } |
| 164 |
| 165 @end |
| 166 |
| 167 @implementation SyncCreatePassphraseCollectionViewController (UsedForTesting) |
| 168 |
| 169 - (UITextField*)confirmPassphrase { |
| 170 return confirmPassphrase_; |
| 171 } |
| 172 |
| 173 @end |
OLD | NEW |