feat:add toast bridge

This commit is contained in:
pengfei.zhou 2019-11-20 17:24:50 +08:00
parent 38823700a2
commit d495043dc9
9 changed files with 96 additions and 7 deletions

View File

@ -25,6 +25,7 @@ import pub.doric.utils.ThreadMode;
import com.github.pengfeizhou.jscore.ArchiveException;
import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSObject;
/**
* @Description: Doric
@ -41,7 +42,10 @@ public class ModalPlugin extends DoricJavaPlugin {
@DoricMethod(name = "toast", thread = ThreadMode.UI)
public void toast(JSDecoder decoder, DoricPromise promise) {
try {
Toast.makeText(getDoricContext().getContext(), decoder.string(), Toast.LENGTH_SHORT).show();
JSObject jsObject = decoder.decode().asObject();
Toast.makeText(getDoricContext().getContext(),
jsObject.getProperty("msg").asString().value(),
Toast.LENGTH_SHORT).show();
} catch (ArchiveException e) {
e.printStackTrace();
}

View File

@ -7,4 +7,5 @@ export default [
'src/LayoutDemo',
'src/EffectsDemo',
'src/ImageDemo',
'src/ModalDemo',
]

37
demo/src/ModalDemo.ts Normal file
View File

@ -0,0 +1,37 @@
import { Group, Panel, List, text, gravity, Color, Stack, LayoutSpec, list, NativeCall, listItem, log, vlayout, Gravity, hlayout, Text, scroller, layoutConfig, image, IView, IVLayout, ScaleType, IText, modal } from "doric";
import { colors, label } from "./utils";
@Entry
class ModalDemo extends Panel {
build(rootView: Group): void {
scroller(vlayout([
text({
text: "Modal",
layoutConfig: layoutConfig().w(LayoutSpec.AT_MOST),
textSize: 30,
textColor: Color.WHITE,
bgColor: colors[1],
textAlignment: gravity().center(),
height: 50,
}),
label('toast'),
label('Click me').apply({
width: 200,
height: 50,
bgColor: colors[0],
textSize: 30,
textColor: Color.WHITE,
layoutConfig: layoutConfig().exactly(),
onClick: () => {
modal(context).toast('This is a toast.')
}
} as IText),
]).apply({
layoutConfig: layoutConfig().atmost().h(LayoutSpec.WRAP_CONTENT),
gravity: gravity().center(),
space: 10,
} as IVLayout)).apply({
layoutConfig: layoutConfig().atmost(),
}).in(rootView)
}
}

View File

@ -21,15 +21,13 @@
//
#import "DoricModalPlugin.h"
#import "DoricRegistry.h"
#import "DoricUtil.h"
@implementation DoricModalPlugin
- (void)toast:(NSString *)message withPromise:(DoricPromise *)promise {
- (void)toast:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"toast:%@", message);
[promise resolve:@"Resolved"];
showToast(dic[@"msg"]);
});
}

View File

@ -35,3 +35,8 @@ NSBundle *_Nonnull DoricBundle(void);
#ifndef DC_UNLOCK
#define DC_UNLOCK(lock) dispatch_semaphore_signal(lock);
#endif
void showToastInView(NSString *_Nonnull text, UIView *_Nonnull superView);
void showToast(NSString *_Nonnull text);

View File

@ -22,6 +22,7 @@
#import "DoricUtil.h"
#import "DoricContext.h"
#import "UIView+Doric.h"
void DoricLog(NSString *_Nonnull format, ...) {
va_list args;
@ -45,3 +46,32 @@ void DoricLog(NSString *_Nonnull format, ...) {
NSURL *url = [bundle URLForResource:@"Doric" withExtension:@"bundle"];
return [NSBundle bundleWithURL:url];
}
void showToastInView(NSString *text, UIView *superView) {
if (!superView) {
return;
}
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:20.f];
label.text = text;
label.textAlignment = NSTextAlignmentCenter;
label.layer.masksToBounds = YES;
label.backgroundColor = [UIColor grayColor];
label.textColor = [UIColor whiteColor];
[label sizeToFit];
label.width += 30;
label.height += 10;
label.layer.cornerRadius = label.height / 2;
label.bottom = superView.height - 20;
label.centerX = superView.width / 2;
[superView addSubview:label];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[label removeFromSuperview];
});
}
void showToast(NSString *text) {
showToastInView(text, [UIApplication sharedApplication].windows.lastObject);
}

View File

@ -28,3 +28,4 @@ export * from './src/util/gravity'
export * from './src/util/candies'
export * from './src/vm/mvvm'
export * from './src/runtime/global'
export * from './src/util/modal'

View File

@ -15,8 +15,10 @@
*/
export * from 'reflect-metadata'
export type BridgeContext = { [index: string]: { [index: string]: (args?: any) => Promise<any> } }
declare global {
const context: { [index: string]: { [index: string]: (args?: any) => Promise<any> } };
const context: BridgeContext
function Entry(constructor: { new(...args: any[]): {} }): any
}
export { }

View File

@ -0,0 +1,11 @@
import { BridgeContext } from "../runtime/global";
export function modal(context: BridgeContext) {
return {
toast: (msg: string) => {
context.modal.toast({ msg })
},
}
}