2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import "RCTAlertManager.h"
|
|
|
|
|
2015-04-07 14:36:26 +00:00
|
|
|
#import "RCTAssert.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTLog.h"
|
2015-09-22 17:43:56 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
@interface RCTAlertManager() <UIAlertViewDelegate>
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTAlertManager
|
|
|
|
{
|
|
|
|
NSMutableArray *_alerts;
|
|
|
|
NSMutableArray *_alertCallbacks;
|
|
|
|
NSMutableArray *_alertButtonKeys;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:42:43 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
2015-08-17 14:35:34 +00:00
|
|
|
_alerts = [NSMutableArray new];
|
|
|
|
_alertCallbacks = [NSMutableArray new];
|
|
|
|
_alertButtonKeys = [NSMutableArray new];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-04-20 19:06:02 +00:00
|
|
|
- (dispatch_queue_t)methodQueue
|
|
|
|
{
|
|
|
|
return dispatch_get_main_queue();
|
|
|
|
}
|
|
|
|
|
2015-09-18 15:40:49 +00:00
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
for (UIAlertView *alert in _alerts) {
|
|
|
|
[alert dismissWithClickedButtonIndex:0 animated:YES];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* @param {NSDictionary} args Dictionary of the form
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
* @"message": @"<Alert message>",
|
|
|
|
* @"buttons": @[
|
|
|
|
* @{@"<key1>": @"<title1>"},
|
|
|
|
* @{@"<key2>": @"<cancelButtonTitle>"},
|
|
|
|
* ]
|
|
|
|
* }
|
|
|
|
* The key from the `buttons` dictionary is passed back in the callback on click.
|
|
|
|
* Buttons are displayed in the order they are specified. If "cancel" is used as
|
|
|
|
* the button key, it will be differently highlighted, according to iOS UI conventions.
|
|
|
|
*/
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_METHOD(alertWithArgs:(NSDictionary *)args
|
|
|
|
callback:(RCTResponseSenderBlock)callback)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
|
|
|
NSString *title = args[@"title"];
|
|
|
|
NSString *message = args[@"message"];
|
2015-05-11 23:40:04 +00:00
|
|
|
NSString *type = args[@"type"];
|
2015-02-20 04:10:52 +00:00
|
|
|
NSArray *buttons = args[@"buttons"];
|
|
|
|
|
|
|
|
if (!title && !message) {
|
2015-03-01 23:33:55 +00:00
|
|
|
RCTLogError(@"Must specify either an alert title, or message, or both");
|
2015-02-20 04:10:52 +00:00
|
|
|
return;
|
|
|
|
} else if (buttons.count == 0) {
|
2015-03-01 23:33:55 +00:00
|
|
|
RCTLogError(@"Must have at least one button.");
|
2015-02-20 04:10:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-09-22 17:43:56 +00:00
|
|
|
|
|
|
|
if (RCTRunningInAppExtension()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UIAlertView *alertView = RCTAlertView(title, nil, self, nil, nil);
|
2015-04-18 17:43:20 +00:00
|
|
|
NSMutableArray *buttonKeys = [[NSMutableArray alloc] initWithCapacity:buttons.count];
|
|
|
|
|
2015-05-11 23:40:04 +00:00
|
|
|
if ([type isEqualToString:@"plain-text"]) {
|
|
|
|
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
|
|
|
|
[alertView textFieldAtIndex:0].text = message;
|
|
|
|
} else {
|
|
|
|
alertView.message = message;
|
|
|
|
}
|
|
|
|
|
2015-04-18 17:43:20 +00:00
|
|
|
NSInteger index = 0;
|
|
|
|
for (NSDictionary *button in buttons) {
|
|
|
|
if (button.count != 1) {
|
|
|
|
RCTLogError(@"Button definitions should have exactly one key.");
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-08-24 10:14:33 +00:00
|
|
|
NSString *buttonKey = button.allKeys.firstObject;
|
2015-04-18 17:43:20 +00:00
|
|
|
NSString *buttonTitle = [button[buttonKey] description];
|
|
|
|
[alertView addButtonWithTitle:buttonTitle];
|
|
|
|
if ([buttonKey isEqualToString: @"cancel"]) {
|
|
|
|
alertView.cancelButtonIndex = index;
|
|
|
|
}
|
|
|
|
[buttonKeys addObject:buttonKey];
|
|
|
|
index ++;
|
|
|
|
}
|
2015-03-23 20:28:42 +00:00
|
|
|
|
2015-04-18 17:43:20 +00:00
|
|
|
[_alerts addObject:alertView];
|
2015-06-15 14:53:45 +00:00
|
|
|
[_alertCallbacks addObject:callback ?: ^(__unused id unused) {}];
|
2015-04-18 17:43:20 +00:00
|
|
|
[_alertButtonKeys addObject:buttonKeys];
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-04-18 17:43:20 +00:00
|
|
|
[alertView show];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UIAlertViewDelegate
|
|
|
|
|
|
|
|
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
|
|
|
|
{
|
|
|
|
NSUInteger index = [_alerts indexOfObject:alertView];
|
|
|
|
RCTAssert(index != NSNotFound, @"Dismissed alert was not recognised");
|
2015-03-23 20:28:42 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
RCTResponseSenderBlock callback = _alertCallbacks[index];
|
|
|
|
NSArray *buttonKeys = _alertButtonKeys[index];
|
2015-05-11 23:40:04 +00:00
|
|
|
NSArray *args;
|
|
|
|
|
|
|
|
if (alertView.alertViewStyle == UIAlertViewStylePlainTextInput) {
|
|
|
|
args = @[buttonKeys[buttonIndex], [alertView textFieldAtIndex:0].text];
|
|
|
|
} else {
|
|
|
|
args = @[buttonKeys[buttonIndex]];
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(args);
|
2015-03-23 20:28:42 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
[_alerts removeObjectAtIndex:index];
|
|
|
|
[_alertCallbacks removeObjectAtIndex:index];
|
|
|
|
[_alertButtonKeys removeObjectAtIndex:index];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|