Chromium Code Reviews| Index: ios/chrome/browser/ui/alert_coordinator/alert_controller_coordinator.mm |
| diff --git a/ios/chrome/browser/ui/alert_coordinator/alert_controller_coordinator.mm b/ios/chrome/browser/ui/alert_coordinator/alert_controller_coordinator.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..51532ebfadf0670172cd1b7f9fe4f7567ffa9619 |
| --- /dev/null |
| +++ b/ios/chrome/browser/ui/alert_coordinator/alert_controller_coordinator.mm |
| @@ -0,0 +1,142 @@ |
| +// Copyright 2016 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/chrome/browser/ui/alert_coordinator/alert_controller_coordinator.h" |
| + |
| +#import "base/ios/weak_nsobject.h" |
| +#import "base/mac/scoped_nsobject.h" |
| +#import "ios/chrome/browser/ui/alert_coordinator/alert_controller_coordinator+subclassing.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/strings/grit/ui_strings.h" |
| + |
| +@interface AlertControllerCoordinator () { |
| + // Variables backing properties of the same name. |
| + base::scoped_nsobject<UIAlertController> _alertController; |
| + base::scoped_nsobject<NSString> _message; |
| + |
| + // Whether a cancel has been added. |
| + BOOL _cancelButtonAdded; |
| +} |
| + |
| +// Redefined to readwrite. |
| +@property(nonatomic, readwrite, getter=isVisible) BOOL visible; |
| +// Lazy initializer to create the |_alert|. |
| +@property(nonatomic, readonly) UIAlertController* alertController; |
| + |
| +@end |
| + |
| +@implementation AlertControllerCoordinator |
| + |
| +@synthesize visible = _visible; |
| + |
| +#pragma mark - Object Lifecycle. |
| + |
| +- (void)dealloc { |
| + [self stop]; |
| + [super dealloc]; |
| +} |
| + |
| +#pragma mark - Public Methods. |
| + |
| +- (void)addItemWithTitle:(NSString*)title |
| + action:(ProceduralBlock)actionBlock |
| + destructive:(BOOL)isDestructive { |
| + if (self.visible) { |
| + return; |
| + } |
| + UIAlertActionStyle style = UIAlertActionStyleDefault; |
| + if (isDestructive) |
| + style = UIAlertActionStyleDestructive; |
| + |
| + base::WeakNSObject<AlertControllerCoordinator> weakSelf(self); |
| + [self.alertController |
| + addAction:[UIAlertAction actionWithTitle:title |
| + style:style |
| + handler:^(UIAlertAction*) { |
| + [weakSelf alertDismissed]; |
| + actionBlock(); |
|
lpromero
2016/07/06 09:03:59
DCHECK actionBlock, or only execute if non-nil.
gambard
2016/07/07 08:57:43
Done.
|
| + }]]; |
| +} |
| + |
| +- (void)start { |
| + if (![self shouldStart]) { |
| + return; |
| + } |
| + |
| + [self.baseViewController presentViewController:self.alertController |
| + animated:YES |
| + completion:nil]; |
| + self.visible = YES; |
| +} |
| + |
| +- (void)stop { |
| + [_alertController dismissViewControllerAnimated:NO completion:nil]; |
| + self.visible = NO; |
| + _cancelButtonAdded = NO; |
| + _alertController.reset(); |
|
lpromero
2016/07/06 09:03:59
These 3 lines are -alertDismissed.
gambard
2016/07/07 08:57:43
Done.
|
| +} |
| + |
| +- (void)addCancelButton { |
|
lpromero
2016/07/06 09:03:59
Implement by calling to addItemWithTitle:action:st
gambard
2016/07/07 08:57:43
Acknowledged.
|
| + if (_cancelButtonAdded) |
| + return; |
| + |
| + base::WeakNSObject<AlertControllerCoordinator> weakSelf(self); |
| + UIAlertAction* cancelAction = |
| + [UIAlertAction actionWithTitle:l10n_util::GetNSString(IDS_APP_CANCEL) |
| + style:UIAlertActionStyleCancel |
| + handler:^(UIAlertAction*) { |
| + [weakSelf alertDismissed]; |
| + }]; |
| + |
| + [_alertController addAction:cancelAction]; |
| +} |
| + |
| +- (NSUInteger)actionCount { |
| + return [_alertController actions].count; |
| +} |
| + |
| +#pragma mark - Property Implementation. |
| + |
| +- (UIAlertController*)alertController { |
| + if (!_alertController) { |
| + _alertController.reset([[self createAlertController] retain]); |
|
lpromero
2016/07/06 09:03:59
If renamed newAlertController, you need to remove
gambard
2016/07/07 08:57:43
Acknowledged.
|
| + if ([self shouldCreateCancelButtonAtCreation]) |
| + [self addCancelButton]; |
| + } |
| + return _alertController; |
| +} |
| + |
| +- (NSString*)message { |
| + return _message; |
| +} |
| + |
| +- (void)setMessage:(NSString*)message { |
| + _message.reset([message copy]); |
| +} |
| + |
| +#pragma mark - Private Methods. |
| + |
| +- (void)alertDismissed { |
| + self.visible = NO; |
| + _cancelButtonAdded = NO; |
| + _alertController.reset(); |
| +} |
| + |
| +#pragma mark - Subclass Methods. |
| + |
| +- (BOOL)shouldStart { |
| + // Should be subclassed. |
| + return NO; |
| +} |
| + |
| +- (UIAlertController*)createAlertController { |
| + // Should be subclassed. |
| + return nil; |
| +} |
| + |
| +- (BOOL)shouldCreateCancelButtonAtCreation { |
| + return NO; |
| +} |
| + |
| +@end |