feat:adjust the inject timing of panel's initData

This commit is contained in:
pengfei.zhou 2020-03-19 13:35:04 +08:00 committed by osborn
parent 08454d53ea
commit 2b62e8bd3b
18 changed files with 87 additions and 65 deletions

View File

@ -17,6 +17,7 @@ package pub.doric;
import android.animation.AnimatorSet; import android.animation.AnimatorSet;
import android.content.Context; import android.content.Context;
import android.text.TextUtils;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSONBuilder; import com.github.pengfeizhou.jscore.JSONBuilder;
@ -114,17 +115,24 @@ public class DoricContext {
public static DoricContext create(Context context, String script, String source, String extra) { public static DoricContext create(Context context, String script, String source, String extra) {
DoricContext doricContext = DoricContextManager.getInstance().createContext(context, script, source, extra); DoricContext doricContext = DoricContextManager.getInstance().createContext(context, script, source, extra);
doricContext.script = script; doricContext.script = script;
doricContext.extra = extra; doricContext.init(extra);
doricContext.callEntity(DoricConstant.DORIC_ENTITY_CREATE); doricContext.callEntity(DoricConstant.DORIC_ENTITY_CREATE);
return doricContext; return doricContext;
} }
public void init(float width, float height) { public void init(String initData) {
this.extra = initData;
if (!TextUtils.isEmpty(initData)) {
callEntity(DoricConstant.DORIC_ENTITY_INIT, initData);
}
}
public void build(float width, float height) {
this.initParams = new JSONBuilder() this.initParams = new JSONBuilder()
.put("width", width) .put("width", width)
.put("height", height) .put("height", height)
.toJSONObject(); .toJSONObject();
callEntity(DoricConstant.DORIC_ENTITY_INIT, this.initParams, extra); callEntity(DoricConstant.DORIC_ENTITY_BUILD, this.initParams);
} }
public AsyncResult<JSDecoder> callEntity(String methodName, Object... args) { public AsyncResult<JSDecoder> callEntity(String methodName, Object... args) {
@ -186,8 +194,9 @@ public class DoricContext {
this.script = script; this.script = script;
this.mRootNode.setId(""); this.mRootNode.setId("");
getDriver().createContext(mContextId, script, source); getDriver().createContext(mContextId, script, source);
init(this.extra);
callEntity(DoricConstant.DORIC_ENTITY_CREATE); callEntity(DoricConstant.DORIC_ENTITY_CREATE);
callEntity(DoricConstant.DORIC_ENTITY_INIT, this.initParams, extra); callEntity(DoricConstant.DORIC_ENTITY_BUILD, this.initParams);
onShow(); onShow();
} }

View File

@ -61,15 +61,15 @@ public class DoricPanel extends FrameLayout implements LifecycleObserver {
public void config(String script, String alias, String extra) { public void config(String script, String alias, String extra) {
DoricContext doricContext = DoricContext.create(getContext(), script, alias, extra); DoricContext doricContext = DoricContext.create(getContext(), script, alias, extra);
doricContext.onShow();
config(doricContext); config(doricContext);
doricContext.onShow();
} }
public void config(DoricContext doricContext) { public void config(DoricContext doricContext) {
mDoricContext = doricContext; mDoricContext = doricContext;
mDoricContext.getRootNode().setRootView(this); mDoricContext.getRootNode().setRootView(this);
if (getMeasuredWidth() != 0 && getMeasuredHeight() != 0) { if (getMeasuredWidth() != 0 && getMeasuredHeight() != 0) {
mDoricContext.init(DoricUtils.px2dp(getMeasuredWidth()), DoricUtils.px2dp(getMeasuredHeight())); mDoricContext.build(DoricUtils.px2dp(getMeasuredWidth()), DoricUtils.px2dp(getMeasuredHeight()));
} }
} }
@ -95,7 +95,7 @@ public class DoricPanel extends FrameLayout implements LifecycleObserver {
renderedWidth = w; renderedWidth = w;
renderedHeight = h; renderedHeight = h;
} else { } else {
mDoricContext.init(DoricUtils.px2dp(w), DoricUtils.px2dp(h)); mDoricContext.build(DoricUtils.px2dp(w), DoricUtils.px2dp(h));
renderedWidth = w; renderedWidth = w;
renderedHeight = h; renderedHeight = h;
} }

View File

@ -70,4 +70,5 @@ public class DoricConstant {
public static final String DORIC_ENTITY_DESTROY = "__onDestroy__"; public static final String DORIC_ENTITY_DESTROY = "__onDestroy__";
public static final String DORIC_ENTITY_SHOW = "__onShow__"; public static final String DORIC_ENTITY_SHOW = "__onShow__";
public static final String DORIC_ENTITY_HIDDEN = "__onHidden__"; public static final String DORIC_ENTITY_HIDDEN = "__onHidden__";
public static final String DORIC_ENTITY_BUILD = "__build__";
} }

View File

@ -51,7 +51,9 @@ NS_ASSUME_NONNULL_BEGIN
- (DoricAsyncResult *)callEntity:(NSString *)method withArgumentsArray:(NSArray *)args; - (DoricAsyncResult *)callEntity:(NSString *)method withArgumentsArray:(NSArray *)args;
- (void)initContextWithWidth:(CGFloat)width height:(CGFloat)height; - (void)build:(CGSize)size;
- (void)init:(NSString *)initData;
- (void)reload:(NSString *)script; - (void)reload:(NSString *)script;

View File

@ -36,11 +36,11 @@ - (instancetype)initWithScript:(NSString *)script source:(NSString *)source extr
_script = script; _script = script;
_source = source; _source = source;
_initialParams = [@{@"width": @(0), @"height": @(0)} mutableCopy]; _initialParams = [@{@"width": @(0), @"height": @(0)} mutableCopy];
_extra = extra;
[[DoricContextManager instance] createContext:self script:script source:source]; [[DoricContextManager instance] createContext:self script:script source:source];
_headNodes = [NSMutableDictionary new]; _headNodes = [NSMutableDictionary new];
DoricRootNode *rootNode = [[DoricRootNode alloc] initWithContext:self]; DoricRootNode *rootNode = [[DoricRootNode alloc] initWithContext:self];
_rootNode = rootNode; _rootNode = rootNode;
[self init:extra];
[self callEntity:DORIC_ENTITY_CREATE, nil]; [self callEntity:DORIC_ENTITY_CREATE, nil];
} }
return self; return self;
@ -80,20 +80,28 @@ - (DoricAsyncResult *)callEntity:(NSString *)method withArgumentsArray:(NSArray
return [self.driver invokeContextEntity:self.contextId method:method argumentsArray:args]; return [self.driver invokeContextEntity:self.contextId method:method argumentsArray:args];
} }
- (void)initContextWithWidth:(CGFloat)width height:(CGFloat)height { - (void)init:(NSString *)initData {
self.extra = initData;
if (initData) {
[self callEntity:DORIC_ENTITY_INIT, initData, nil];
}
}
- (void)build:(CGSize)size {
[self.initialParams also:^(NSMutableDictionary *it) { [self.initialParams also:^(NSMutableDictionary *it) {
it[@"width"] = @(width); it[@"width"] = @(size.width);
it[@"height"] = @(height); it[@"height"] = @(size.height);
}]; }];
[self callEntity:DORIC_ENTITY_INIT, self.initialParams, self.extra, nil]; [self callEntity:DORIC_ENTITY_BUILD, self.initialParams, nil];
} }
- (void)reload:(NSString *)script { - (void)reload:(NSString *)script {
self.rootNode.viewId = nil; self.rootNode.viewId = nil;
self.script = script; self.script = script;
[self.driver createContext:self.contextId script:script source:self.source]; [self.driver createContext:self.contextId script:script source:self.source];
[self init:self.extra];
[self callEntity:DORIC_ENTITY_CREATE, nil]; [self callEntity:DORIC_ENTITY_CREATE, nil];
[self callEntity:DORIC_ENTITY_INIT, self.initialParams, self.extra, nil]; [self callEntity:DORIC_ENTITY_BUILD, self.initialParams, nil];
[self onShow]; [self onShow];
} }

View File

@ -44,7 +44,7 @@ - (void)viewWillLayoutSubviews {
if (self.doricContext && self.renderedWidth != self.view.width && self.renderedHeight != self.view.height) { if (self.doricContext && self.renderedWidth != self.view.width && self.renderedHeight != self.view.height) {
self.renderedWidth = self.view.width; self.renderedWidth = self.view.width;
self.renderedHeight = self.view.height; self.renderedHeight = self.view.height;
[self.doricContext initContextWithWidth:self.renderedWidth height:self.renderedHeight]; [self.doricContext build:CGSizeMake(self.renderedWidth, self.renderedHeight)];
} else { } else {
[self.doricContext.rootNode.view also:^(DoricStackView *it) { [self.doricContext.rootNode.view also:^(DoricStackView *it) {
if (it.width != self.renderedWidth || it.height != self.renderedHeight) { if (it.width != self.renderedWidth || it.height != self.renderedHeight) {

View File

@ -63,3 +63,5 @@ extern NSString *const DORIC_ENTITY_DESTROY;
extern NSString *const DORIC_ENTITY_SHOW; extern NSString *const DORIC_ENTITY_SHOW;
extern NSString *const DORIC_ENTITY_HIDDEN; extern NSString *const DORIC_ENTITY_HIDDEN;
extern NSString *const DORIC_ENTITY_BUILD;

View File

@ -81,3 +81,5 @@
NSString *const DORIC_ENTITY_SHOW = @"__onShow__"; NSString *const DORIC_ENTITY_SHOW = @"__onShow__";
NSString *const DORIC_ENTITY_HIDDEN = @"__onHidden__"; NSString *const DORIC_ENTITY_HIDDEN = @"__onHidden__";
NSString *const DORIC_ENTITY_BUILD = @"__build__";

View File

@ -942,14 +942,10 @@ var Panel = /** @class */ (function () {
Panel.prototype.getInitData = function () { Panel.prototype.getInitData = function () {
return this.__data__; return this.__data__;
}; };
Panel.prototype.__init__ = function (frame, data) { Panel.prototype.__init__ = function (data) {
if (data) { if (data) {
this.__data__ = JSON.parse(data); this.__data__ = JSON.parse(data);
} }
this.__root__.width = frame.width;
this.__root__.height = frame.height;
this.__root__.children.length = 0;
this.build(this.__root__);
}; };
Panel.prototype.__onCreate__ = function () { Panel.prototype.__onCreate__ = function () {
this.onCreate(); this.onCreate();
@ -963,7 +959,10 @@ var Panel = /** @class */ (function () {
Panel.prototype.__onHidden__ = function () { Panel.prototype.__onHidden__ = function () {
this.onHidden(); this.onHidden();
}; };
Panel.prototype.__build__ = function () { Panel.prototype.__build__ = function (frame) {
this.__root__.width = frame.width;
this.__root__.height = frame.height;
this.__root__.children.length = 0;
this.build(this.__root__); this.build(this.__root__);
}; };
Panel.prototype.__response__ = function (viewIds, callbackId) { Panel.prototype.__response__ = function (viewIds, callbackId) {
@ -1143,7 +1142,7 @@ var Panel = /** @class */ (function () {
__decorate$2([ __decorate$2([
NativeCall, NativeCall,
__metadata$2("design:type", Function), __metadata$2("design:type", Function),
__metadata$2("design:paramtypes", [Object, String]), __metadata$2("design:paramtypes", [String]),
__metadata$2("design:returntype", void 0) __metadata$2("design:returntype", void 0)
], Panel.prototype, "__init__", null); ], Panel.prototype, "__init__", null);
__decorate$2([ __decorate$2([
@ -1173,7 +1172,7 @@ var Panel = /** @class */ (function () {
__decorate$2([ __decorate$2([
NativeCall, NativeCall,
__metadata$2("design:type", Function), __metadata$2("design:type", Function),
__metadata$2("design:paramtypes", []), __metadata$2("design:paramtypes", [Object]),
__metadata$2("design:returntype", void 0) __metadata$2("design:returntype", void 0)
], Panel.prototype, "__build__", null); ], Panel.prototype, "__build__", null);
__decorate$2([ __decorate$2([

View File

@ -713,14 +713,10 @@ class Panel {
getInitData() { getInitData() {
return this.__data__; return this.__data__;
} }
__init__(frame, data) { __init__(data) {
if (data) { if (data) {
this.__data__ = JSON.parse(data); this.__data__ = JSON.parse(data);
} }
this.__root__.width = frame.width;
this.__root__.height = frame.height;
this.__root__.children.length = 0;
this.build(this.__root__);
} }
__onCreate__() { __onCreate__() {
this.onCreate(); this.onCreate();
@ -734,7 +730,10 @@ class Panel {
__onHidden__() { __onHidden__() {
this.onHidden(); this.onHidden();
} }
__build__() { __build__(frame) {
this.__root__.width = frame.width;
this.__root__.height = frame.height;
this.__root__.children.length = 0;
this.build(this.__root__); this.build(this.__root__);
} }
__response__(viewIds, callbackId) { __response__(viewIds, callbackId) {
@ -837,7 +836,7 @@ class Panel {
__decorate$2([ __decorate$2([
NativeCall, NativeCall,
__metadata$2("design:type", Function), __metadata$2("design:type", Function),
__metadata$2("design:paramtypes", [Object, String]), __metadata$2("design:paramtypes", [String]),
__metadata$2("design:returntype", void 0) __metadata$2("design:returntype", void 0)
], Panel.prototype, "__init__", null); ], Panel.prototype, "__init__", null);
__decorate$2([ __decorate$2([
@ -867,7 +866,7 @@ __decorate$2([
__decorate$2([ __decorate$2([
NativeCall, NativeCall,
__metadata$2("design:type", Function), __metadata$2("design:type", Function),
__metadata$2("design:paramtypes", []), __metadata$2("design:paramtypes", [Object]),
__metadata$2("design:returntype", void 0) __metadata$2("design:returntype", void 0)
], Panel.prototype, "__build__", null); ], Panel.prototype, "__build__", null);
__decorate$2([ __decorate$2([

View File

@ -2172,14 +2172,10 @@ class Panel {
getInitData() { getInitData() {
return this.__data__; return this.__data__;
} }
__init__(frame, data) { __init__(data) {
if (data) { if (data) {
this.__data__ = JSON.parse(data); this.__data__ = JSON.parse(data);
} }
this.__root__.width = frame.width;
this.__root__.height = frame.height;
this.__root__.children.length = 0;
this.build(this.__root__);
} }
__onCreate__() { __onCreate__() {
this.onCreate(); this.onCreate();
@ -2193,7 +2189,10 @@ class Panel {
__onHidden__() { __onHidden__() {
this.onHidden(); this.onHidden();
} }
__build__() { __build__(frame) {
this.__root__.width = frame.width;
this.__root__.height = frame.height;
this.__root__.children.length = 0;
this.build(this.__root__); this.build(this.__root__);
} }
__response__(viewIds, callbackId) { __response__(viewIds, callbackId) {
@ -2296,7 +2295,7 @@ class Panel {
__decorate$2([ __decorate$2([
NativeCall, NativeCall,
__metadata$2("design:type", Function), __metadata$2("design:type", Function),
__metadata$2("design:paramtypes", [Object, String]), __metadata$2("design:paramtypes", [String]),
__metadata$2("design:returntype", void 0) __metadata$2("design:returntype", void 0)
], Panel.prototype, "__init__", null); ], Panel.prototype, "__init__", null);
__decorate$2([ __decorate$2([
@ -2326,7 +2325,7 @@ __decorate$2([
__decorate$2([ __decorate$2([
NativeCall, NativeCall,
__metadata$2("design:type", Function), __metadata$2("design:type", Function),
__metadata$2("design:paramtypes", []), __metadata$2("design:paramtypes", [Object]),
__metadata$2("design:returntype", void 0) __metadata$2("design:returntype", void 0)
], Panel.prototype, "__build__", null); ], Panel.prototype, "__build__", null);
__decorate$2([ __decorate$2([

2
doric-js/index.d.ts vendored
View File

@ -446,7 +446,7 @@ declare module 'doric/lib/src/widget/layouts' {
space?: number; space?: number;
gravity?: Gravity; gravity?: Gravity;
} }
export class VLayout extends LinearLayout implements VLayout { export class VLayout extends LinearLayout implements IVLayout {
} }
export interface IHLayout extends IView { export interface IHLayout extends IView {
space?: number; space?: number;

View File

@ -81,14 +81,10 @@ export class Panel {
getInitData() { getInitData() {
return this.__data__; return this.__data__;
} }
__init__(frame, data) { __init__(data) {
if (data) { if (data) {
this.__data__ = JSON.parse(data); this.__data__ = JSON.parse(data);
} }
this.__root__.width = frame.width;
this.__root__.height = frame.height;
this.__root__.children.length = 0;
this.build(this.__root__);
} }
__onCreate__() { __onCreate__() {
this.onCreate(); this.onCreate();
@ -102,7 +98,10 @@ export class Panel {
__onHidden__() { __onHidden__() {
this.onHidden(); this.onHidden();
} }
__build__() { __build__(frame) {
this.__root__.width = frame.width;
this.__root__.height = frame.height;
this.__root__.children.length = 0;
this.build(this.__root__); this.build(this.__root__);
} }
__response__(viewIds, callbackId) { __response__(viewIds, callbackId) {
@ -205,7 +204,7 @@ export class Panel {
__decorate([ __decorate([
NativeCall, NativeCall,
__metadata("design:type", Function), __metadata("design:type", Function),
__metadata("design:paramtypes", [Object, String]), __metadata("design:paramtypes", [String]),
__metadata("design:returntype", void 0) __metadata("design:returntype", void 0)
], Panel.prototype, "__init__", null); ], Panel.prototype, "__init__", null);
__decorate([ __decorate([
@ -235,7 +234,7 @@ __decorate([
__decorate([ __decorate([
NativeCall, NativeCall,
__metadata("design:type", Function), __metadata("design:type", Function),
__metadata("design:paramtypes", []), __metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0) __metadata("design:returntype", void 0)
], Panel.prototype, "__build__", null); ], Panel.prototype, "__build__", null);
__decorate([ __decorate([

View File

@ -14,7 +14,7 @@ export interface IVLayout extends IView {
space?: number; space?: number;
gravity?: Gravity; gravity?: Gravity;
} }
export declare class VLayout extends LinearLayout implements VLayout { export declare class VLayout extends LinearLayout implements IVLayout {
} }
export interface IHLayout extends IView { export interface IHLayout extends IView {
space?: number; space?: number;

View File

@ -88,14 +88,10 @@ export abstract class Panel {
} }
@NativeCall @NativeCall
private __init__(frame: Frame, data?: string) { private __init__(data?: string) {
if (data) { if (data) {
this.__data__ = JSON.parse(data) this.__data__ = JSON.parse(data)
} }
this.__root__.width = frame.width
this.__root__.height = frame.height
this.__root__.children.length = 0
this.build(this.__root__)
} }
@NativeCall @NativeCall
@ -119,7 +115,10 @@ export abstract class Panel {
} }
@NativeCall @NativeCall
private __build__() { private __build__(frame: Frame) {
this.__root__.width = frame.width
this.__root__.height = frame.height
this.__root__.children.length = 0
this.build(this.__root__) this.build(this.__root__)
} }

View File

@ -39,7 +39,7 @@ export interface IVLayout extends IView {
gravity?: Gravity gravity?: Gravity
} }
export class VLayout extends LinearLayout implements VLayout { export class VLayout extends LinearLayout implements IVLayout {
} }

View File

@ -2230,14 +2230,10 @@ class Panel {
getInitData() { getInitData() {
return this.__data__; return this.__data__;
} }
__init__(frame, data) { __init__(data) {
if (data) { if (data) {
this.__data__ = JSON.parse(data); this.__data__ = JSON.parse(data);
} }
this.__root__.width = frame.width;
this.__root__.height = frame.height;
this.__root__.children.length = 0;
this.build(this.__root__);
} }
__onCreate__() { __onCreate__() {
this.onCreate(); this.onCreate();
@ -2251,7 +2247,10 @@ class Panel {
__onHidden__() { __onHidden__() {
this.onHidden(); this.onHidden();
} }
__build__() { __build__(frame) {
this.__root__.width = frame.width;
this.__root__.height = frame.height;
this.__root__.children.length = 0;
this.build(this.__root__); this.build(this.__root__);
} }
__response__(viewIds, callbackId) { __response__(viewIds, callbackId) {
@ -2354,7 +2353,7 @@ class Panel {
__decorate$2([ __decorate$2([
NativeCall, NativeCall,
__metadata$2("design:type", Function), __metadata$2("design:type", Function),
__metadata$2("design:paramtypes", [Object, String]), __metadata$2("design:paramtypes", [String]),
__metadata$2("design:returntype", void 0) __metadata$2("design:returntype", void 0)
], Panel.prototype, "__init__", null); ], Panel.prototype, "__init__", null);
__decorate$2([ __decorate$2([
@ -2384,7 +2383,7 @@ __decorate$2([
__decorate$2([ __decorate$2([
NativeCall, NativeCall,
__metadata$2("design:type", Function), __metadata$2("design:type", Function),
__metadata$2("design:paramtypes", []), __metadata$2("design:paramtypes", [Object]),
__metadata$2("design:returntype", void 0) __metadata$2("design:returntype", void 0)
], Panel.prototype, "__build__", null); ], Panel.prototype, "__build__", null);
__decorate$2([ __decorate$2([

File diff suppressed because one or more lines are too long