Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/ui/alert_coordinator/alert_coordinator.h" | 5 #import "ios/chrome/browser/ui/alert_coordinator/alert_coordinator.h" |
| 6 | 6 |
| 7 #import "base/ios/weak_nsobject.h" | 7 #include "base/logging.h" |
| 8 #import "base/mac/scoped_block.h" | |
| 9 #import "base/mac/scoped_nsobject.h" | |
| 10 #include "ui/base/l10n/l10n_util.h" | 8 #include "ui/base/l10n/l10n_util.h" |
| 11 #include "ui/strings/grit/ui_strings.h" | 9 #include "ui/strings/grit/ui_strings.h" |
| 12 | 10 |
| 11 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 12 #error "This file requires ARC support." | |
| 13 #endif | |
| 14 | |
| 13 @interface AlertCoordinator () { | 15 @interface AlertCoordinator () { |
| 14 // Variables backing properties of the same name. | 16 // Variable backing properties of the same name. |
|
noyau (Ping after 24h)
2016/12/12 15:55:58
This comment is not true anymore.
stkhapugin
2016/12/12 16:39:50
It is true, the property is just in a category so
| |
| 15 base::scoped_nsobject<UIAlertController> _alertController; | 17 UIAlertController* _alertController; |
| 16 base::scoped_nsobject<NSString> _message; | |
| 17 base::mac::ScopedBlock<ProceduralBlock> _cancelAction; | |
| 18 base::mac::ScopedBlock<ProceduralBlock> _startAction; | |
| 19 base::mac::ScopedBlock<ProceduralBlock> _noInteractionAction; | |
| 20 base::mac::ScopedBlock<ProceduralBlock> _rawCancelAction; | |
| 21 | |
| 22 // Title for the alert. | 18 // Title for the alert. |
| 23 base::scoped_nsobject<NSString> _title; | 19 NSString* _title; |
| 24 } | 20 } |
| 25 | 21 |
| 26 // Redefined to readwrite. | 22 // Redefined to readwrite. |
| 27 @property(nonatomic, readwrite, getter=isVisible) BOOL visible; | 23 @property(nonatomic, readwrite, getter=isVisible) BOOL visible; |
| 28 | 24 |
| 29 // Cancel action passed using the public API. | 25 // Cancel action passed using the public API. |
| 30 // It will called from the overridden block stored in the |cancelAction| | 26 // It will called from the overridden block stored in the |cancelAction| |
| 31 // property. | 27 // property. |
| 32 @property(nonatomic, copy) ProceduralBlock rawCancelAction; | 28 @property(nonatomic, copy) ProceduralBlock rawCancelAction; |
| 33 | 29 |
| 34 // Called when the alert is dismissed to perform cleanup. | 30 // Called when the alert is dismissed to perform cleanup. |
| 35 - (void)alertDismissed; | 31 - (void)alertDismissed; |
| 36 | 32 |
| 37 - (UIAlertController*)alertControllerWithTitle:(NSString*)title | 33 - (UIAlertController*)alertControllerWithTitle:(NSString*)title |
| 38 message:(NSString*)message; | 34 message:(NSString*)message; |
| 39 @end | 35 @end |
| 40 | 36 |
| 41 @implementation AlertCoordinator | 37 @implementation AlertCoordinator |
| 42 | 38 |
| 43 @synthesize visible = _visible; | 39 @synthesize visible = _visible; |
| 44 @synthesize cancelButtonAdded = _cancelButtonAdded; | 40 @synthesize cancelButtonAdded = _cancelButtonAdded; |
| 41 @synthesize cancelAction = _cancelAction; | |
| 42 @synthesize startAction = _startAction; | |
| 43 @synthesize noInteractionAction = _noInteractionAction; | |
| 44 @synthesize rawCancelAction = _rawCancelAction; | |
| 45 @synthesize message = _message; | |
| 45 | 46 |
| 46 - (instancetype)initWithBaseViewController:(UIViewController*)viewController { | 47 - (instancetype)initWithBaseViewController:(UIViewController*)viewController { |
| 47 NOTREACHED(); | 48 NOTREACHED(); |
| 48 return nil; | 49 return nil; |
| 49 } | 50 } |
| 50 | 51 |
| 51 - (instancetype)initWithBaseViewController:(UIViewController*)viewController | 52 - (instancetype)initWithBaseViewController:(UIViewController*)viewController |
| 52 title:(NSString*)title | 53 title:(NSString*)title |
| 53 message:(NSString*)message { | 54 message:(NSString*)message { |
| 54 self = [super initWithBaseViewController:viewController]; | 55 self = [super initWithBaseViewController:viewController]; |
| 55 if (self) { | 56 if (self) { |
| 56 _title.reset([title copy]); | 57 _title = [title copy]; |
| 57 _message.reset([message copy]); | 58 _message = [message copy]; |
| 58 } | 59 } |
| 59 return self; | 60 return self; |
| 60 } | 61 } |
| 61 | 62 |
| 62 #pragma mark - Public Methods. | 63 #pragma mark - Public Methods. |
| 63 | 64 |
| 64 - (void)addItemWithTitle:(NSString*)title | 65 - (void)addItemWithTitle:(NSString*)title |
| 65 action:(ProceduralBlock)actionBlock | 66 action:(ProceduralBlock)actionBlock |
| 66 style:(UIAlertActionStyle)style { | 67 style:(UIAlertActionStyle)style { |
| 67 if (self.visible || | 68 if (self.visible || |
| 68 (style == UIAlertActionStyleCancel && self.cancelButtonAdded)) { | 69 (style == UIAlertActionStyleCancel && self.cancelButtonAdded)) { |
| 69 return; | 70 return; |
| 70 } | 71 } |
| 71 | 72 |
| 72 if (style == UIAlertActionStyleCancel) | 73 if (style == UIAlertActionStyleCancel) |
| 73 _cancelButtonAdded = YES; | 74 _cancelButtonAdded = YES; |
| 74 | 75 |
| 75 base::WeakNSObject<AlertCoordinator> weakSelf(self); | 76 __weak AlertCoordinator* weakSelf = self; |
| 76 | 77 |
| 77 UIAlertAction* alertAction = | 78 UIAlertAction* alertAction = |
| 78 [UIAlertAction actionWithTitle:title | 79 [UIAlertAction actionWithTitle:title |
| 79 style:style | 80 style:style |
| 80 handler:^(UIAlertAction*) { | 81 handler:^(UIAlertAction*) { |
| 81 [weakSelf setNoInteractionAction:nil]; | 82 [weakSelf setNoInteractionAction:nil]; |
| 82 if (actionBlock) | 83 if (actionBlock) |
| 83 actionBlock(); | 84 actionBlock(); |
| 84 [weakSelf alertDismissed]; | 85 [weakSelf alertDismissed]; |
| 85 }]; | 86 }]; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 113 self.startAction(); | 114 self.startAction(); |
| 114 | 115 |
| 115 [self.baseViewController presentViewController:self.alertController | 116 [self.baseViewController presentViewController:self.alertController |
| 116 animated:YES | 117 animated:YES |
| 117 completion:nil]; | 118 completion:nil]; |
| 118 self.visible = YES; | 119 self.visible = YES; |
| 119 } | 120 } |
| 120 | 121 |
| 121 - (void)stop { | 122 - (void)stop { |
| 122 if (_noInteractionAction) { | 123 if (_noInteractionAction) { |
| 123 _noInteractionAction.get()(); | 124 _noInteractionAction(); |
| 124 _noInteractionAction.reset(); | 125 _noInteractionAction = nil; |
| 125 } | 126 } |
| 126 [[_alertController presentingViewController] | 127 [[_alertController presentingViewController] |
| 127 dismissViewControllerAnimated:NO | 128 dismissViewControllerAnimated:NO |
| 128 completion:nil]; | 129 completion:nil]; |
| 129 [self alertDismissed]; | 130 [self alertDismissed]; |
| 130 } | 131 } |
| 131 | 132 |
| 132 #pragma mark - Property Implementation. | 133 #pragma mark - Property Implementation. |
| 133 | 134 |
| 134 - (UIAlertController*)alertController { | 135 - (UIAlertController*)alertController { |
| 135 if (!_alertController) { | 136 if (!_alertController) { |
| 136 UIAlertController* alert = | 137 UIAlertController* alert = |
| 137 [self alertControllerWithTitle:_title message:_message]; | 138 [self alertControllerWithTitle:_title message:_message]; |
| 138 | 139 |
| 139 if (alert) | 140 if (alert) |
| 140 _alertController.reset([alert retain]); | 141 _alertController = alert; |
| 141 } | 142 } |
| 142 return _alertController; | 143 return _alertController; |
| 143 } | 144 } |
| 144 | 145 |
| 145 - (NSString*)message { | 146 - (NSString*)message { |
| 146 return _message; | 147 return _message; |
| 147 } | 148 } |
| 148 | 149 |
| 149 - (void)setMessage:(NSString*)message { | |
| 150 _message.reset([message copy]); | |
| 151 } | |
| 152 | |
| 153 - (ProceduralBlock)cancelAction { | |
| 154 return _cancelAction; | |
| 155 } | |
| 156 | |
| 157 - (void)setCancelAction:(ProceduralBlock)cancelAction { | 150 - (void)setCancelAction:(ProceduralBlock)cancelAction { |
| 158 base::WeakNSObject<AlertCoordinator> weakSelf(self); | 151 __weak AlertCoordinator* weakSelf = self; |
| 159 | 152 |
| 160 self.rawCancelAction = cancelAction; | 153 self.rawCancelAction = cancelAction; |
| 161 | 154 |
| 162 _cancelAction.reset([^{ | 155 _cancelAction = [^{ |
| 163 base::scoped_nsobject<AlertCoordinator> strongSelf([weakSelf retain]); | 156 AlertCoordinator* strongSelf = weakSelf; |
| 164 [strongSelf setNoInteractionAction:nil]; | 157 [strongSelf setNoInteractionAction:nil]; |
| 165 if ([strongSelf rawCancelAction]) { | 158 if ([strongSelf rawCancelAction]) { |
| 166 [strongSelf rawCancelAction](); | 159 [strongSelf rawCancelAction](); |
| 167 } | 160 } |
| 168 } copy]); | 161 } copy]; |
| 169 } | |
| 170 | |
| 171 - (ProceduralBlock)startAction { | |
| 172 return _startAction; | |
| 173 } | |
| 174 | |
| 175 - (void)setStartAction:(ProceduralBlock)startAction { | |
| 176 _startAction.reset([startAction copy]); | |
| 177 } | |
| 178 | |
| 179 - (ProceduralBlock)noInteractionAction { | |
| 180 return _noInteractionAction; | |
| 181 } | |
| 182 | |
| 183 - (void)setNoInteractionAction:(ProceduralBlock)noInteractionAction { | |
| 184 _noInteractionAction.reset([noInteractionAction copy]); | |
| 185 } | |
| 186 | |
| 187 - (ProceduralBlock)rawCancelAction { | |
| 188 return _rawCancelAction; | |
| 189 } | |
| 190 | |
| 191 - (void)setRawCancelAction:(ProceduralBlock)rawCancelAction { | |
| 192 _rawCancelAction.reset([rawCancelAction copy]); | |
| 193 } | 162 } |
| 194 | 163 |
| 195 #pragma mark - Private Methods. | 164 #pragma mark - Private Methods. |
| 196 | 165 |
| 197 - (void)alertDismissed { | 166 - (void)alertDismissed { |
| 198 self.visible = NO; | 167 self.visible = NO; |
| 199 _cancelButtonAdded = NO; | 168 _cancelButtonAdded = NO; |
| 200 _alertController.reset(); | 169 _alertController = nil; |
| 201 _cancelAction.reset(); | 170 _cancelAction = nil; |
| 202 _noInteractionAction.reset(); | 171 _noInteractionAction = nil; |
| 203 } | 172 } |
| 204 | 173 |
| 205 - (UIAlertController*)alertControllerWithTitle:(NSString*)title | 174 - (UIAlertController*)alertControllerWithTitle:(NSString*)title |
| 206 message:(NSString*)message { | 175 message:(NSString*)message { |
| 207 return | 176 return |
| 208 [UIAlertController alertControllerWithTitle:title | 177 [UIAlertController alertControllerWithTitle:title |
| 209 message:message | 178 message:message |
| 210 preferredStyle:UIAlertControllerStyleAlert]; | 179 preferredStyle:UIAlertControllerStyleAlert]; |
| 211 } | 180 } |
| 212 | 181 |
| 213 @end | 182 @end |
| OLD | NEW |