From 2290c8cd482888e21f07947c0f9b5b03ff1fa849 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Wed, 20 Nov 2019 20:36:01 +0800 Subject: [PATCH] feat:add confirm of modal for Android and iOS --- .../java/pub/doric/plugin/ModalPlugin.java | 47 +++++++++++++++++++ demo/src/ModalDemo.ts | 31 ++++++++++++ iOS/Pod/Classes/Plugin/DoricModalPlugin.m | 25 +++++++++- iOS/Pod/Classes/Plugin/DoricPromise.h | 4 -- js-framework/src/util/modal.ts | 12 +++++ 5 files changed, 114 insertions(+), 5 deletions(-) diff --git a/Android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java b/Android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java index 518f58ba..1dcb3d37 100644 --- a/Android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java +++ b/Android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java @@ -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())); + } + } } diff --git a/demo/src/ModalDemo.ts b/demo/src/ModalDemo.ts index d395ad0f..f896c2cc 100644 --- a/demo/src/ModalDemo.ts +++ b/demo/src/ModalDemo.ts @@ -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, diff --git a/iOS/Pod/Classes/Plugin/DoricModalPlugin.m b/iOS/Pod/Classes/Plugin/DoricModalPlugin.m index 3f7eb212..38b71192 100644 --- a/iOS/Pod/Classes/Plugin/DoricModalPlugin.m +++ b/iOS/Pod/Classes/Plugin/DoricModalPlugin.m @@ -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 diff --git a/iOS/Pod/Classes/Plugin/DoricPromise.h b/iOS/Pod/Classes/Plugin/DoricPromise.h index 4f0bd10c..26807d07 100644 --- a/iOS/Pod/Classes/Plugin/DoricPromise.h +++ b/iOS/Pod/Classes/Plugin/DoricPromise.h @@ -23,8 +23,6 @@ #import #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 diff --git a/js-framework/src/util/modal.ts b/js-framework/src/util/modal.ts index a20b95b9..5f740c89 100644 --- a/js-framework/src/util/modal.ts +++ b/js-framework/src/util/modal.ts @@ -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) + } + }, } } \ No newline at end of file