feat:add Android alert

This commit is contained in:
pengfei.zhou 2019-11-20 20:16:55 +08:00
parent 1345d75a7f
commit 9a6f2d7f9f
5 changed files with 112 additions and 2 deletions

View File

@ -15,10 +15,14 @@
*/
package pub.doric.plugin;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;
import pub.doric.DoricContext;
import pub.doric.R;
import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.extension.bridge.DoricMethod;
import pub.doric.extension.bridge.DoricPromise;
@ -29,6 +33,7 @@ import com.github.pengfeizhou.jscore.ArchiveException;
import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue;
import com.github.pengfeizhou.jscore.JavaValue;
/**
* @Description: Doric
@ -68,4 +73,39 @@ public class ModalPlugin extends DoricJavaPlugin {
e.printStackTrace();
}
}
@DoricMethod(name = "alert", thread = ThreadMode.UI)
public void alert(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");
AlertDialog.Builder builder = new AlertDialog.Builder(getDoricContext().getContext(), R.style.Theme_Doric_Modal_Alert);
if (titleVal.isString()) {
builder.setTitle(titleVal.asString().value());
}
String btnTitle = getDoricContext().getContext().getString(android.R.string.ok);
if (okBtn.isString()) {
btnTitle = okBtn.asString().value();
}
builder.setMessage(msgVal.asString().value())
.setPositiveButton(btnTitle, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
promise.resolve();
}
});
builder.setCancelable(false);
try {
builder.show();
} catch (Exception e) {
e.printStackTrace();
}
} catch (ArchiveException e) {
e.printStackTrace();
promise.reject(new JavaValue(e.getLocalizedMessage()));
}
}
}

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Doric.Modal" parent="android:Theme.Material.Light.Dialog.Alert" />
</resources>

View File

@ -0,0 +1,15 @@
<resources>
<style name="Theme.Doric.Modal" parent="android:style/Theme.Holo.Light.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
</style>
<style name="Theme.Doric.Modal.Alert" parent="Theme.Doric.Modal" />
<style name="Theme.Doric.Modal.Confirm" parent="Theme.Doric.Modal" />
<style name="Theme.Doric.Modal.Prompt" parent="Theme.Doric.Modal" />
<style name="Theme.Doric.Modal.Prompt.EditText" parent="Theme.AppCompat.Light" />
</resources>

View File

@ -51,6 +51,32 @@ class ModalDemo extends Panel {
modal(context).toast('This is a toast.', Gravity.Center)
}
} as IText),
text({
text: "Alert",
layoutConfig: layoutConfig().w(LayoutSpec.AT_MOST),
textSize: 30,
textColor: Color.WHITE,
bgColor: colors[2],
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).alert({
msg: 'This is alert.',
title: 'Alert title',
okLabel: "OkLabel"
}).then(e => {
modal(context).toast('Clicked OK.')
})
}
} as IText),
]).apply({
layoutConfig: layoutConfig().atmost().h(LayoutSpec.WRAP_CONTENT),
gravity: Gravity.Center,

View File

@ -1,8 +1,21 @@
/*
* 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.
*/
import { BridgeContext } from "../runtime/global";
import { Gravity } from "./gravity";
export function modal(context: BridgeContext) {
return {
toast: (msg: string, gravity: Gravity = Gravity.Bottom) => {
@ -11,5 +24,16 @@ export function modal(context: BridgeContext) {
gravity: gravity.toModel(),
})
},
alert: (arg: string | {
title: string,
msg: string,
okLabel?: string,
}) => {
if (typeof arg === 'string') {
return context.modal.alert({ msg: arg })
} else {
return context.modal.alert(arg)
}
},
}
}