feat:add confirm of modal for Android and iOS

This commit is contained in:
pengfei.zhou 2019-11-20 20:36:01 +08:00
parent 3f5099d29a
commit 2290c8cd48
5 changed files with 114 additions and 5 deletions

View File

@ -108,4 +108,51 @@ public class ModalPlugin extends DoricJavaPlugin {
promise.reject(new JavaValue(e.getLocalizedMessage()));
}
}
@DoricMethod(name = "confirm", thread = ThreadMode.UI)
public void confirm(JSDecoder decoder, final DoricPromise promise) {
try {
JSObject jsObject = decoder.decode().asObject();
JSValue titleVal = jsObject.getProperty("title");
JSValue msgVal = jsObject.getProperty("msg");
JSValue okBtn = jsObject.getProperty("okLabel");
JSValue cancelBtn = jsObject.getProperty("cancelLabel");
AlertDialog.Builder builder = new AlertDialog.Builder(getDoricContext().getContext(), R.style.Theme_Doric_Modal_Alert);
if (titleVal.isString()) {
builder.setTitle(titleVal.asString().value());
}
String okLabel = getDoricContext().getContext().getString(android.R.string.ok);
if (okBtn.isString()) {
okLabel = okBtn.asString().value();
}
String cancelLabel = getDoricContext().getContext().getString(android.R.string.cancel);
if (cancelBtn.isString()) {
cancelLabel = cancelBtn.asString().value();
}
builder.setMessage(msgVal.asString().value())
.setPositiveButton(okLabel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
promise.resolve();
}
})
.setNegativeButton(cancelLabel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
promise.reject();
}
});
builder.setCancelable(false);
try {
builder.show();
} catch (Exception e) {
e.printStackTrace();
}
} catch (ArchiveException e) {
e.printStackTrace();
promise.reject(new JavaValue(e.getLocalizedMessage()));
}
}
}

View File

@ -77,6 +77,37 @@ class ModalDemo extends Panel {
})
}
} as IText),
text({
text: "Confirm",
layoutConfig: layoutConfig().w(LayoutSpec.AT_MOST),
textSize: 30,
textColor: Color.WHITE,
bgColor: colors[3],
textAlignment: Gravity.Center,
height: 50,
}),
label('Click me').apply({
width: 200,
height: 50,
bgColor: colors[0],
textSize: 30,
textColor: Color.WHITE,
layoutConfig: layoutConfig().exactly(),
onClick: () => {
modal(context).confirm({
msg: 'This is Confirm.',
title: 'Confirm title',
okLabel: "OkLabel",
cancelLabel: 'CancelLabel',
}).then(
e => {
modal(context).toast('Clicked OK.')
},
e => {
modal(context).toast('Clicked Cancel.')
})
}
} as IText),
]).apply({
layoutConfig: layoutConfig().atmost().h(LayoutSpec.WRAP_CONTENT),
gravity: Gravity.Center,

View File

@ -41,7 +41,7 @@ - (void)alert:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:dic[@"title"]
message:dic[@"msg"]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:dic[@"okLabel"]
UIAlertAction *action = [UIAlertAction actionWithTitle:dic[@"okLabel"] ?: NSLocalizedString(@"OK", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[promise resolve:nil];
@ -51,4 +51,27 @@ - (void)alert:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
[vc presentViewController:alert animated:YES completion:nil];
});
}
- (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];
});
}
@end

View File

@ -23,8 +23,6 @@
#import <Foundation/Foundation.h>
#import "DoricContext.h"
NS_ASSUME_NONNULL_BEGIN
@interface DoricPromise : NSObject
- (instancetype)initWithContext:(DoricContext *)context callbackId:(NSString *)callbackId;
@ -32,5 +30,3 @@ NS_ASSUME_NONNULL_BEGIN
- (void)reject:(id)result;
@end
NS_ASSUME_NONNULL_END

View File

@ -35,5 +35,17 @@ export function modal(context: BridgeContext) {
return context.modal.alert(arg)
}
},
confirm: (arg: string | {
title: string,
msg: string,
okLabel?: string,
cancelLabel?: string,
}) => {
if (typeof arg === 'string') {
return context.modal.confirm({ msg: arg })
} else {
return context.modal.confirm(arg)
}
},
}
}