2019-12-04 13:29:26 +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.
|
|
|
|
*/
|
|
|
|
//
|
|
|
|
// DoricModalPlugin.m
|
|
|
|
// Doric
|
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2019/7/29.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "DoricModalPlugin.h"
|
|
|
|
#import "DoricUtil.h"
|
2019-12-14 15:04:34 +08:00
|
|
|
#import "DoricExtensions.h"
|
2019-12-04 13:29:26 +08:00
|
|
|
|
|
|
|
@implementation DoricModalPlugin
|
|
|
|
|
|
|
|
- (void)toast:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
|
2021-02-01 15:31:24 +08:00
|
|
|
__weak typeof(self) _self = self;
|
|
|
|
[self.doricContext dispatchToMainQueue:^{
|
|
|
|
__strong typeof(_self) self = _self;
|
2020-04-03 10:19:13 +08:00
|
|
|
__block DoricGravity gravity = DoricGravityBottom;
|
2019-12-04 13:29:26 +08:00
|
|
|
[dic[@"gravity"] also:^(NSNumber *it) {
|
|
|
|
gravity = (DoricGravity) [it integerValue];
|
|
|
|
}];
|
2020-04-26 11:21:19 +08:00
|
|
|
if (self.doricContext.vc) {
|
|
|
|
ShowToastInVC(self.doricContext.vc, dic[@"msg"], gravity);
|
|
|
|
} else {
|
|
|
|
ShowToast(dic[@"msg"], gravity);
|
|
|
|
}
|
2021-02-01 15:31:24 +08:00
|
|
|
}];
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)alert:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
|
2021-02-01 15:31:24 +08:00
|
|
|
__weak typeof(self) _self = self;
|
|
|
|
[self.doricContext dispatchToMainQueue:^{
|
|
|
|
__strong typeof(_self) self = _self;
|
2019-12-04 13:29:26 +08:00
|
|
|
UIAlertController *alert = [UIAlertController alertControllerWithTitle:dic[@"title"]
|
|
|
|
message:dic[@"msg"]
|
|
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
UIAlertAction *action = [UIAlertAction actionWithTitle:dic[@"okLabel"] ?: NSLocalizedString(@"OK", nil)
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *action) {
|
|
|
|
[promise resolve:nil];
|
|
|
|
}];
|
|
|
|
[alert addAction:action];
|
2020-04-21 18:52:54 +08:00
|
|
|
[self.doricContext.vc presentViewController:alert animated:YES completion:nil];
|
2021-02-01 15:31:24 +08:00
|
|
|
}];
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)confirm:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
|
2021-02-01 15:31:24 +08:00
|
|
|
__weak typeof(self) _self = self;
|
|
|
|
[self.doricContext dispatchToMainQueue:^{
|
|
|
|
__strong typeof(_self) self = _self;
|
2019-12-04 13:29:26 +08:00
|
|
|
UIAlertController *alert = [UIAlertController alertControllerWithTitle:dic[@"title"]
|
|
|
|
message:dic[@"msg"]
|
|
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
|
|
|
|
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:dic[@"cancelLabel"] ?: NSLocalizedString(@"Cancel", nil)
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *action) {
|
|
|
|
[promise reject:nil];
|
|
|
|
}];
|
|
|
|
[alert addAction:cancelAction];
|
2021-02-01 15:31:24 +08:00
|
|
|
|
2020-04-22 12:35:37 +08:00
|
|
|
UIAlertAction *okAction = [UIAlertAction actionWithTitle:dic[@"okLabel"] ?: NSLocalizedString(@"Ok", nil)
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *action) {
|
|
|
|
[promise resolve:nil];
|
|
|
|
}];
|
|
|
|
[alert addAction:okAction];
|
2021-02-01 15:31:24 +08:00
|
|
|
|
2020-04-21 18:52:54 +08:00
|
|
|
[self.doricContext.vc presentViewController:alert animated:YES completion:nil];
|
2021-02-01 15:31:24 +08:00
|
|
|
}];
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)prompt:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
|
2021-02-01 15:31:24 +08:00
|
|
|
__weak typeof(self) _self = self;
|
|
|
|
[self.doricContext dispatchToMainQueue:^{
|
|
|
|
__strong typeof(_self) self = _self;
|
2019-12-04 13:29:26 +08:00
|
|
|
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 *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];
|
|
|
|
|
2020-04-22 12:35:37 +08:00
|
|
|
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];
|
2019-12-04 13:29:26 +08:00
|
|
|
|
2020-04-21 18:52:54 +08:00
|
|
|
[self.doricContext.vc presentViewController:alert animated:YES completion:nil];
|
2021-02-01 15:31:24 +08:00
|
|
|
}];
|
2019-12-04 13:29:26 +08:00
|
|
|
}
|
|
|
|
@end
|