OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/clean/chrome/browser/ui/dialogs/dialog_view_controller.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "components/strings/grit/components_strings.h" |
| 9 #import "ios/clean/chrome/browser/ui/commands/dialog_commands.h" |
| 10 #import "ios/clean/chrome/browser/ui/dialogs/dialog_button_configuration.h" |
| 11 #import "ios/clean/chrome/browser/ui/dialogs/dialog_text_field_configuration.h" |
| 12 #include "ui/base/l10n/l10n_util.h" |
| 13 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 15 #error "This file requires ARC support." |
| 16 #endif |
| 17 |
| 18 namespace { |
| 19 // Typedef the block parameter for UIAlertAction for readability. |
| 20 typedef void (^AlertActionHandler)(UIAlertAction*); |
| 21 // Converts DialogButtonStyle to a UIAlertActionStyle. |
| 22 UIAlertActionStyle GetAlertStyleForDialogButtonStyle(DialogButtonStyle style) { |
| 23 switch (style) { |
| 24 case DialogButtonStyle::DEFAULT: |
| 25 return UIAlertActionStyleDefault; |
| 26 case DialogButtonStyle::CANCEL: |
| 27 return UIAlertActionStyleCancel; |
| 28 case DialogButtonStyle::DESTRUCTIVE: |
| 29 return UIAlertActionStyleDestructive; |
| 30 } |
| 31 } |
| 32 } |
| 33 |
| 34 @interface DialogViewController () |
| 35 |
| 36 // The dispatcher used for dismissal; |
| 37 @property(nonatomic, readonly, strong) id<DialogDismissalCommands> |
| 38 dismissalDispatcher; |
| 39 |
| 40 // Objects provided through the DialogConsumer protocol. |
| 41 @property(nonatomic, readonly, copy) |
| 42 NSArray<DialogButtonConfiguration*>* buttonConfigs; |
| 43 @property(nonatomic, readonly, copy) |
| 44 NSArray<DialogTextFieldConfiguration*>* textFieldConfigs; |
| 45 |
| 46 // The strings corresponding with the text fields. |
| 47 @property(nonatomic, readonly) |
| 48 NSMutableDictionary<id, NSString*>* textFieldValues; |
| 49 |
| 50 // Creates an AlertActionHandler that sends a DialogDismissalCommand with |tag|. |
| 51 - (AlertActionHandler)actionForButtonConfiguration: |
| 52 (DialogButtonConfiguration*)buttonConfig; |
| 53 // Adds buttons for each item in |buttonItems|. |
| 54 - (void)addButtons; |
| 55 // Adds text fields for each item in |textFieldItems|. |
| 56 - (void)addTextFields; |
| 57 |
| 58 @end |
| 59 |
| 60 @implementation DialogViewController |
| 61 |
| 62 @synthesize dismissalDispatcher = _dismissalDispatcher; |
| 63 @synthesize buttonConfigs = _buttonConfigs; |
| 64 @synthesize textFieldConfigs = _textFieldConfigs; |
| 65 @synthesize textFieldValues = _textFieldValues; |
| 66 |
| 67 - (instancetype)initWithStyle:(UIAlertControllerStyle)style |
| 68 dispatcher:(id<DialogDismissalCommands>)dispatcher { |
| 69 DCHECK(dispatcher); |
| 70 self = [[self class] alertControllerWithTitle:nil |
| 71 message:nil |
| 72 preferredStyle:style]; |
| 73 if (self) { |
| 74 _dismissalDispatcher = dispatcher; |
| 75 } |
| 76 return self; |
| 77 } |
| 78 |
| 79 #pragma mark - Accessors |
| 80 |
| 81 - (NSMutableDictionary<id, NSString*>*)textFieldValues { |
| 82 // Early return if text field items haven't been supplied or the text fields |
| 83 // have not been instantiated. |
| 84 NSUInteger textFieldCount = self.textFieldConfigs.count; |
| 85 if (!textFieldCount || textFieldCount != self.textFields.count) |
| 86 return nil; |
| 87 // Lazily create the array and update its contents. |
| 88 if (!_textFieldValues) |
| 89 _textFieldValues = [[NSMutableDictionary<id, NSString*> alloc] init]; |
| 90 for (NSUInteger fieldIndex = 0; fieldIndex < textFieldCount; ++fieldIndex) { |
| 91 _textFieldValues[self.textFieldConfigs[fieldIndex].identifier] = |
| 92 self.textFields[fieldIndex].text; |
| 93 } |
| 94 return _textFieldValues; |
| 95 } |
| 96 |
| 97 #pragma mark - DialogConsumer |
| 98 |
| 99 - (void)setDialogTitle:(NSString*)title { |
| 100 self.title = title; |
| 101 } |
| 102 |
| 103 - (void)setDialogMessage:(NSString*)message { |
| 104 self.message = message; |
| 105 } |
| 106 |
| 107 - (void)setDialogButtonConfigurations: |
| 108 (NSArray<DialogButtonConfiguration*>*)buttonConfigs { |
| 109 _buttonConfigs = buttonConfigs; |
| 110 } |
| 111 |
| 112 - (void)setDialogTextFieldConfigurations: |
| 113 (NSArray<DialogTextFieldConfiguration*>*)textFieldConfigs { |
| 114 _textFieldConfigs = textFieldConfigs; |
| 115 } |
| 116 |
| 117 #pragma mark - UIViewcontroller |
| 118 |
| 119 - (void)viewDidLoad { |
| 120 DCHECK_GT(self.buttonConfigs.count, 0U); |
| 121 [self addButtons]; |
| 122 [self addTextFields]; |
| 123 } |
| 124 |
| 125 #pragma mark - |
| 126 |
| 127 - (AlertActionHandler)actionForButtonConfiguration: |
| 128 (DialogButtonConfiguration*)buttonConfig { |
| 129 return ^(UIAlertAction*) { |
| 130 [self.dismissalDispatcher dismissDialogWithButtonID:buttonConfig.identifier |
| 131 textFieldValues:self.textFieldValues]; |
| 132 }; |
| 133 } |
| 134 |
| 135 - (void)addButtons { |
| 136 for (DialogButtonConfiguration* config in self.buttonConfigs) { |
| 137 AlertActionHandler handler = [self actionForButtonConfiguration:config]; |
| 138 UIAlertActionStyle style = GetAlertStyleForDialogButtonStyle(config.style); |
| 139 [self addAction:[UIAlertAction actionWithTitle:config.text |
| 140 style:style |
| 141 handler:handler]]; |
| 142 } |
| 143 } |
| 144 |
| 145 - (void)addTextFields { |
| 146 for (DialogTextFieldConfiguration* config in self.textFieldConfigs) { |
| 147 [self addTextFieldWithConfigurationHandler:^(UITextField* textField) { |
| 148 textField.text = config.defaultText; |
| 149 textField.placeholder = config.placeholderText; |
| 150 textField.secureTextEntry = config.secure; |
| 151 }]; |
| 152 } |
| 153 } |
| 154 |
| 155 @end |
OLD | NEW |