2019-10-21 09:59:22 +08:00
|
|
|
/*
|
|
|
|
* Copyright [2019] [Doric.Pub]
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2019-07-29 20:23:00 +08:00
|
|
|
//
|
|
|
|
// DoricModalPlugin.m
|
|
|
|
// Doric
|
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2019/7/29.
|
|
|
|
//
|
|
|
|
|
2019-11-20 18:15:04 +08:00
|
|
|
#import <Doric/Doric.h>
|
2019-07-29 20:23:00 +08:00
|
|
|
#import "DoricModalPlugin.h"
|
2019-11-20 17:24:50 +08:00
|
|
|
#import "DoricUtil.h"
|
2019-07-29 20:23:00 +08:00
|
|
|
|
|
|
|
@implementation DoricModalPlugin
|
|
|
|
|
2019-11-20 17:24:50 +08:00
|
|
|
- (void)toast:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
|
2019-07-29 20:23:00 +08:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2019-11-20 18:15:04 +08:00
|
|
|
__block DoricGravity gravity = BOTTOM;
|
|
|
|
[dic[@"gravity"] also:^(NSNumber *it) {
|
|
|
|
gravity = (DoricGravity) [it integerValue];
|
|
|
|
}];
|
2019-11-25 15:16:06 +08:00
|
|
|
ShowToast(dic[@"msg"], gravity);
|
2019-07-29 20:23:00 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-20 20:23:39 +08:00
|
|
|
- (void)alert:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
UIAlertController *alert = [UIAlertController alertControllerWithTitle:dic[@"title"]
|
|
|
|
message:dic[@"msg"]
|
|
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
2019-11-20 20:36:01 +08:00
|
|
|
UIAlertAction *action = [UIAlertAction actionWithTitle:dic[@"okLabel"] ?: NSLocalizedString(@"OK", nil)
|
2019-11-20 20:23:39 +08:00
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *action) {
|
|
|
|
[promise resolve:nil];
|
|
|
|
}];
|
|
|
|
[alert addAction:action];
|
|
|
|
UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
|
|
|
|
[vc presentViewController:alert animated:YES completion:nil];
|
|
|
|
});
|
|
|
|
}
|
2019-11-20 20:36:01 +08:00
|
|
|
|
|
|
|
- (void)confirm:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
UIAlertController *alert = [UIAlertController alertControllerWithTitle:dic[@"title"]
|
|
|
|
message:dic[@"msg"]
|
|
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
UIAlertAction *okAction = [UIAlertAction actionWithTitle:dic[@"okLabel"] ?: NSLocalizedString(@"Ok", nil)
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *action) {
|
|
|
|
[promise resolve:nil];
|
|
|
|
}];
|
|
|
|
[alert addAction:okAction];
|
|
|
|
|
|
|
|
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:dic[@"cancelLabel"] ?: NSLocalizedString(@"Cancel", nil)
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *action) {
|
|
|
|
[promise reject:nil];
|
|
|
|
}];
|
|
|
|
[alert addAction:cancelAction];
|
|
|
|
UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
|
|
|
|
[vc presentViewController:alert animated:YES completion:nil];
|
|
|
|
});
|
|
|
|
}
|
2019-11-20 21:04:27 +08:00
|
|
|
|
|
|
|
- (void)prompt:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
UIAlertController *alert = [UIAlertController alertControllerWithTitle:dic[@"title"]
|
|
|
|
message:dic[@"msg"]
|
|
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
NSString *placeholder = dic[@"defaultText"];
|
|
|
|
NSString *preText = dic[@"text"];
|
|
|
|
[alert addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {
|
|
|
|
if (placeholder.length > 0) {
|
|
|
|
textField.placeholder = placeholder;
|
|
|
|
}
|
|
|
|
if (preText.length > 0) {
|
|
|
|
textField.text = preText;
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
__weak typeof(alert) _alert = alert;
|
|
|
|
UIAlertAction *okAction = [UIAlertAction actionWithTitle:dic[@"okLabel"] ?: NSLocalizedString(@"Ok", nil)
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *action) {
|
|
|
|
__strong typeof(_alert) alert = _alert;
|
|
|
|
[promise resolve:alert.textFields.lastObject.text];
|
|
|
|
}];
|
|
|
|
[alert addAction:okAction];
|
|
|
|
|
|
|
|
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:dic[@"cancelLabel"] ?: NSLocalizedString(@"Cancel", nil)
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *action) {
|
|
|
|
__strong typeof(_alert) alert = _alert;
|
|
|
|
[promise reject:alert.textFields.lastObject.text];
|
|
|
|
}];
|
|
|
|
[alert addAction:cancelAction];
|
|
|
|
|
|
|
|
|
|
|
|
UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
|
|
|
|
[vc presentViewController:alert animated:YES completion:nil];
|
|
|
|
});
|
|
|
|
}
|
2019-07-29 20:23:00 +08:00
|
|
|
@end
|