Chromium Code Reviews| Index: ios/clean/chrome/browser/ui/dialogs/dialog_view_controller.mm |
| diff --git a/ios/clean/chrome/browser/ui/dialogs/dialog_view_controller.mm b/ios/clean/chrome/browser/ui/dialogs/dialog_view_controller.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..da33bc43162f9929fefe2b7651642b6d6f53a48d |
| --- /dev/null |
| +++ b/ios/clean/chrome/browser/ui/dialogs/dialog_view_controller.mm |
| @@ -0,0 +1,158 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "ios/clean/chrome/browser/ui/dialogs/dialog_view_controller.h" |
| + |
| +#include "base/logging.h" |
| +#include "components/strings/grit/components_strings.h" |
| +#import "ios/clean/chrome/browser/ui/commands/dialog_commands.h" |
| +#import "ios/clean/chrome/browser/ui/dialogs/dialog_button_item.h" |
| +#import "ios/clean/chrome/browser/ui/dialogs/dialog_text_field_item.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +#if !defined(__has_feature) || !__has_feature(objc_arc) |
| +#error "This file requires ARC support." |
| +#endif |
| + |
| +namespace { |
| +// Typedef the block parameter for UIAlertAction for readability. |
| +typedef void (^AlertActionHandler)(UIAlertAction*); |
| +// Converts DialogButtonStyle to a UIAlertActionStyle. |
| +UIAlertActionStyle GetAlertStyleForDialogButtonStyle(DialogButtonStyle style) { |
| + switch (style) { |
| + case DialogButtonStyle::DEFAULT: |
| + return UIAlertActionStyleDefault; |
| + case DialogButtonStyle::CANCEL: |
| + return UIAlertActionStyleCancel; |
| + case DialogButtonStyle::DESTRUCTIVE: |
| + return UIAlertActionStyleDestructive; |
| + } |
| +} |
| +} |
| + |
| +@interface DialogViewController () |
| + |
| +// The dispatcher used for dismissal; |
| +@property(nonatomic, readonly, strong) id<DialogDismissalCommands> |
| + 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.
|
| + |
| +// Objects provided through the DialogConsumer protocol. |
| +@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
|
| +@property(nonatomic, readonly, strong) NSString* cancelButtonText; |
| +@property(nonatomic, readonly, strong) |
| + NSArray<DialogTextFieldItem*>* textFieldItems; |
| + |
| +// 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.
|
| +@property(nonatomic, readonly, strong) |
| + NSMutableArray<NSString*>* userInputStrings; |
| + |
| +// Creates an AlertActionHandler that sends a DialogDismissalCommand with |tag|. |
| +- (AlertActionHandler)actionForButtonItemTag:(id)tag; |
| +// Adds buttons for each item in |buttonItems|. |
| +- (void)addButtons; |
| +// Adds text fields for each item in |textFieldItems|. |
| +- (void)addTextFields; |
| + |
| +@end |
| + |
| +@implementation DialogViewController |
| + |
| +@synthesize dismissalDispatcher = _dismissalDispatcher; |
| +@synthesize buttonItems = _buttonItems; |
| +@synthesize textFieldItems = _textFieldItems; |
| +@synthesize cancelButtonText = _cancelButtonText; |
| +@synthesize userInputStrings = _userInputStrings; |
| + |
| +- (instancetype)initWithStyle:(UIAlertControllerStyle)style |
| + dispatcher:(id<DialogDismissalCommands>)dispatcher { |
| + DCHECK(dispatcher); |
| + self = [[self class] alertControllerWithTitle:nil |
| + message:nil |
| + preferredStyle:style]; |
| + if (self) { |
| + _dismissalDispatcher = dispatcher; |
| + } |
| + return self; |
| +} |
| + |
| +#pragma mark - Accessors |
| + |
| +- (NSArray<NSString*>*)userInputStrings { |
| + // Early return if text field items haven't been supplied or the text fields |
| + // have not been instantiated. |
| + NSUInteger itemCount = self.textFieldItems.count; |
| + if (!itemCount || itemCount != self.textFields.count) |
| + return nil; |
| + // Lazily create the array and update its contents. |
| + if (!_userInputStrings) |
| + _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
|
| + for (NSUInteger fieldIndex = 0; fieldIndex < itemCount; ++fieldIndex) { |
| + _userInputStrings[fieldIndex] = self.textFields[fieldIndex].text; |
| + } |
| + return _userInputStrings; |
| +} |
| + |
| +#pragma mark - DialogConsumer |
| + |
| +- (void)setDialogTitle:(NSString*)title { |
| + self.title = title; |
| +} |
| + |
| +- (void)setDialogMessage:(NSString*)message { |
| + self.message = message; |
| +} |
| + |
| +- (void)setDialogButtonItems:(NSArray<DialogButtonItem*>*)buttonItems { |
| + _buttonItems = buttonItems; |
| +} |
| + |
| +- (void)setDialogCancelButtonText:(NSString*)cancelButtonText { |
| + _cancelButtonText = cancelButtonText; |
| +} |
| + |
| +- (void)setDialogTextFieldItems:(NSArray<DialogTextFieldItem*>*)textFieldItems { |
| + _textFieldItems = textFieldItems; |
| +} |
| + |
| +#pragma mark - UIViewcontroller |
| + |
| +- (void)viewDidLoad { |
| + DCHECK_GT(self.buttonItems.count, 0U); |
| + [self addButtons]; |
| + [self addTextFields]; |
| +} |
| + |
| +#pragma mark - |
| + |
| +- (AlertActionHandler)actionForButtonItemTag:(id)tag { |
| + __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.
|
| + return ^(UIAlertAction*) { |
| + DialogViewController* strongSelf = weakSelf; |
| + [strongSelf.dismissalDispatcher |
| + dismissDialogWithButtonTag:tag |
| + userInputStrings:strongSelf.userInputStrings]; |
| + }; |
| +} |
| + |
| +- (void)addButtons { |
| + for (DialogButtonItem* item in self.buttonItems) { |
| + AlertActionHandler handler = [self actionForButtonItemTag:item.tag]; |
| + UIAlertActionStyle style = GetAlertStyleForDialogButtonStyle(item.style); |
| + [self addAction:[UIAlertAction actionWithTitle:item.text |
| + style:style |
| + handler:handler]]; |
| + } |
| +} |
| + |
| +- (void)addTextFields { |
| + for (DialogTextFieldItem* item in self.textFieldItems) { |
| + [self addTextFieldWithConfigurationHandler:^(UITextField* textField) { |
| + textField.text = item.defaultText; |
| + textField.placeholder = item.placeholderText; |
| + textField.secureTextEntry = item.secure; |
| + }]; |
| + } |
| +} |
| + |
| +@end |