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_item.h" | |
11 #import "ios/clean/chrome/browser/ui/dialogs/dialog_text_field_item.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; | |
marq (ping after 24h)
2017/06/14 10:52:16
anti-nit: Convention so far has just been to name
kkhorimoto
2017/06/23 06:24:18
Acknowledged.
| |
39 | |
40 // Objects provided through the DialogConsumer protocol. | |
41 @property(nonatomic, readonly, strong) NSArray<DialogButtonItem*>* buttonItems; | |
marq (ping after 24h)
2017/06/14 10:52:16
Why not readwrite, copy for all of these?
kkhorimoto
2017/06/23 06:24:18
Moved to copy, but I left as readonly to discourag
| |
42 @property(nonatomic, readonly, strong) NSString* cancelButtonText; | |
43 @property(nonatomic, readonly, strong) | |
44 NSArray<DialogTextFieldItem*>* textFieldItems; | |
45 | |
46 // The strings corresponding with the text fields. | |
marq (ping after 24h)
2017/06/14 10:52:16
Maybe use a dictionary rather than depending on in
kkhorimoto
2017/06/23 06:24:18
Done.
| |
47 @property(nonatomic, readonly, strong) | |
48 NSMutableArray<NSString*>* userInputStrings; | |
49 | |
50 // Creates an AlertActionHandler that sends a DialogDismissalCommand with |tag|. | |
51 - (AlertActionHandler)actionForButtonItemTag:(id)tag; | |
52 // Adds buttons for each item in |buttonItems|. | |
53 - (void)addButtons; | |
54 // Adds text fields for each item in |textFieldItems|. | |
55 - (void)addTextFields; | |
56 | |
57 @end | |
58 | |
59 @implementation DialogViewController | |
60 | |
61 @synthesize dismissalDispatcher = _dismissalDispatcher; | |
62 @synthesize buttonItems = _buttonItems; | |
63 @synthesize textFieldItems = _textFieldItems; | |
64 @synthesize cancelButtonText = _cancelButtonText; | |
65 @synthesize userInputStrings = _userInputStrings; | |
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 - (NSArray<NSString*>*)userInputStrings { | |
82 // Early return if text field items haven't been supplied or the text fields | |
83 // have not been instantiated. | |
84 NSUInteger itemCount = self.textFieldItems.count; | |
85 if (!itemCount || itemCount != self.textFields.count) | |
86 return nil; | |
87 // Lazily create the array and update its contents. | |
88 if (!_userInputStrings) | |
89 _userInputStrings = [[NSMutableArray alloc] initWithCapacity:itemCount]; | |
marq (ping after 24h)
2017/06/14 10:52:16
Newline after brace-less if().
kkhorimoto
2017/06/23 06:24:18
Is this part of the style guide? I don't mind add
| |
90 for (NSUInteger fieldIndex = 0; fieldIndex < itemCount; ++fieldIndex) { | |
91 _userInputStrings[fieldIndex] = self.textFields[fieldIndex].text; | |
92 } | |
93 return _userInputStrings; | |
94 } | |
95 | |
96 #pragma mark - DialogConsumer | |
97 | |
98 - (void)setDialogTitle:(NSString*)title { | |
99 self.title = title; | |
100 } | |
101 | |
102 - (void)setDialogMessage:(NSString*)message { | |
103 self.message = message; | |
104 } | |
105 | |
106 - (void)setDialogButtonItems:(NSArray<DialogButtonItem*>*)buttonItems { | |
107 _buttonItems = buttonItems; | |
108 } | |
109 | |
110 - (void)setDialogCancelButtonText:(NSString*)cancelButtonText { | |
111 _cancelButtonText = cancelButtonText; | |
112 } | |
113 | |
114 - (void)setDialogTextFieldItems:(NSArray<DialogTextFieldItem*>*)textFieldItems { | |
115 _textFieldItems = textFieldItems; | |
116 } | |
117 | |
118 #pragma mark - UIViewcontroller | |
119 | |
120 - (void)viewDidLoad { | |
121 DCHECK_GT(self.buttonItems.count, 0U); | |
122 [self addButtons]; | |
123 [self addTextFields]; | |
124 } | |
125 | |
126 #pragma mark - | |
127 | |
128 - (AlertActionHandler)actionForButtonItemTag:(id)tag { | |
129 __weak DialogViewController* weakSelf = self; | |
marq (ping after 24h)
2017/06/14 10:52:16
You don't need to do the weak/strong dance for dia
kkhorimoto
2017/06/23 06:24:18
Done.
| |
130 return ^(UIAlertAction*) { | |
131 DialogViewController* strongSelf = weakSelf; | |
132 [strongSelf.dismissalDispatcher | |
133 dismissDialogWithButtonTag:tag | |
134 userInputStrings:strongSelf.userInputStrings]; | |
135 }; | |
136 } | |
137 | |
138 - (void)addButtons { | |
139 for (DialogButtonItem* item in self.buttonItems) { | |
140 AlertActionHandler handler = [self actionForButtonItemTag:item.tag]; | |
141 UIAlertActionStyle style = GetAlertStyleForDialogButtonStyle(item.style); | |
142 [self addAction:[UIAlertAction actionWithTitle:item.text | |
143 style:style | |
144 handler:handler]]; | |
145 } | |
146 } | |
147 | |
148 - (void)addTextFields { | |
149 for (DialogTextFieldItem* item in self.textFieldItems) { | |
150 [self addTextFieldWithConfigurationHandler:^(UITextField* textField) { | |
151 textField.text = item.defaultText; | |
152 textField.placeholder = item.placeholderText; | |
153 textField.secureTextEntry = item.secure; | |
154 }]; | |
155 } | |
156 } | |
157 | |
158 @end | |
OLD | NEW |