add androoid blend

This commit is contained in:
pengfei.zhou 2019-07-20 19:08:14 +08:00
parent e85b60d05c
commit e2537c5e9d
5 changed files with 30 additions and 20 deletions

View File

@ -2,7 +2,7 @@ package com.github.penfeizhou.doric;
import android.text.TextUtils; import android.text.TextUtils;
import com.github.penfeizhou.doric.extension.render.ViewNode; import com.github.penfeizhou.doric.widget.ViewNode;
import com.github.penfeizhou.doric.utils.DoricMetaInfo; import com.github.penfeizhou.doric.utils.DoricMetaInfo;
import com.github.penfeizhou.doric.plugin.DoricJavaPlugin; import com.github.penfeizhou.doric.plugin.DoricJavaPlugin;
import com.github.penfeizhou.doric.plugin.ModalPlugin; import com.github.penfeizhou.doric.plugin.ModalPlugin;

View File

@ -1,11 +1,12 @@
package com.github.penfeizhou.doric.widget; package com.github.penfeizhou.doric.widget;
import android.util.SparseArray;
import android.view.ViewGroup; import android.view.ViewGroup;
import com.github.penfeizhou.doric.DoricContext; import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.extension.render.ViewNode;
import com.github.pengfeizhou.jscore.JSArray; import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -16,7 +17,8 @@ import java.util.Map;
* @CreateDate: 2019-07-20 * @CreateDate: 2019-07-20
*/ */
public abstract class GroupNode extends ViewNode<ViewGroup> { public abstract class GroupNode extends ViewNode<ViewGroup> {
private Map<String, ViewNode> mChildren = new HashMap<>(); private Map<String, ViewNode> mChildrenNode = new HashMap<>();
private SparseArray<ViewNode> mIndexInfo = new SparseArray<>();
public GroupNode(DoricContext doricContext) { public GroupNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
@ -26,19 +28,31 @@ public abstract class GroupNode extends ViewNode<ViewGroup> {
public void blend(JSObject jsObject) { public void blend(JSObject jsObject) {
super.blend(jsObject); super.blend(jsObject);
JSArray jsArray = jsObject.getProperty("children").asArray(); JSArray jsArray = jsObject.getProperty("children").asArray();
for (int i = 0; i < jsArray.size(); i++) { int i;
JSObject childObj = jsArray.get(i).asObject(); for (i = 0; i < jsArray.size(); i++) {
JSValue jsValue = jsArray.get(i);
if (!jsValue.isObject()) {
continue;
}
JSObject childObj = jsValue.asObject();
String type = childObj.getProperty("type").asString().value(); String type = childObj.getProperty("type").asString().value();
String id = childObj.getProperty("id").asString().value(); String id = childObj.getProperty("id").asString().value();
ViewNode child = mChildren.get(id); ViewNode child = mChildrenNode.get(id);
if (child == null) { if (child == null) {
child = ViewNode.create(getDoricContext(), id, type); child = ViewNode.create(getDoricContext(), id, type);
mChildren.put(id, child); child.index = i;
} mChildrenNode.put(id, child);
if (getView().getChildAt(i) == null) { mView.addView(child.mView, i);
getView().addView(child.getView()); } else if (i != child.index) {
mView.removeView(child.mView);
mView.addView(child.mView, i);
} }
child.blend(childObj.getProperty("props").asObject()); child.blend(childObj.getProperty("props").asObject());
mIndexInfo.put(i, child);
}
while (i < mView.getChildCount()) {
mView.removeViewAt(mView.getChildCount() - 1);
mChildrenNode.remove(mIndexInfo.get(i).id);
} }
} }
} }

View File

@ -4,7 +4,6 @@ import android.widget.ImageView;
import com.github.penfeizhou.doric.DoricContext; import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.extension.render.DoricNode; import com.github.penfeizhou.doric.extension.render.DoricNode;
import com.github.penfeizhou.doric.extension.render.ViewNode;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
/** /**

View File

@ -4,7 +4,6 @@ import android.widget.TextView;
import com.github.penfeizhou.doric.DoricContext; import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.extension.render.DoricNode; import com.github.penfeizhou.doric.extension.render.DoricNode;
import com.github.penfeizhou.doric.extension.render.ViewNode;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
/** /**

View File

@ -1,4 +1,4 @@
package com.github.penfeizhou.doric.extension.render; package com.github.penfeizhou.doric.widget;
import android.content.Context; import android.content.Context;
import android.view.View; import android.view.View;
@ -17,8 +17,10 @@ import com.github.pengfeizhou.jscore.JSObject;
* @CreateDate: 2019-07-20 * @CreateDate: 2019-07-20
*/ */
public abstract class ViewNode<T extends View> extends DoricComponent { public abstract class ViewNode<T extends View> extends DoricComponent {
private T mView; protected T mView;
private String mId; String id;
int index;
public ViewNode(DoricContext doricContext) { public ViewNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
@ -28,10 +30,6 @@ public abstract class ViewNode<T extends View> extends DoricComponent {
return mView; return mView;
} }
private void setId(String id) {
mId = id;
}
public Context getContext() { public Context getContext() {
return getDoricContext().getContext(); return getDoricContext().getContext();
} }
@ -62,7 +60,7 @@ public abstract class ViewNode<T extends View> extends DoricComponent {
DoricRegistry registry = doricContext.getDriver().getRegistry(); DoricRegistry registry = doricContext.getDriver().getRegistry();
DoricMetaInfo<ViewNode> clz = registry.acquireViewNodeInfo(type); DoricMetaInfo<ViewNode> clz = registry.acquireViewNodeInfo(type);
ViewNode node = clz.createInstance(doricContext); ViewNode node = clz.createInstance(doricContext);
node.setId(id); node.id = id;
return node; return node;
} }
} }