prepare viewnode

This commit is contained in:
pengfei.zhou 2019-07-20 14:29:44 +08:00
parent ef5bb17380
commit 920053c0f5
12 changed files with 139 additions and 33 deletions

View File

@ -4,7 +4,7 @@ import android.content.Context;
import com.github.penfeizhou.doric.async.AsyncResult; import com.github.penfeizhou.doric.async.AsyncResult;
import com.github.penfeizhou.doric.plugin.DoricJavaPlugin; import com.github.penfeizhou.doric.plugin.DoricJavaPlugin;
import com.github.penfeizhou.doric.extension.bridge.DoricPluginInfo; import com.github.penfeizhou.doric.utils.DoricMetaInfo;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import java.util.HashMap; import java.util.HashMap;
@ -64,11 +64,11 @@ public class DoricContext {
}); });
} }
public DoricJavaPlugin obtainPlugin(DoricPluginInfo doricPluginInfo) { public DoricJavaPlugin obtainPlugin(DoricMetaInfo<DoricJavaPlugin> doricMetaInfo) {
DoricJavaPlugin plugin = mPluginMap.get(doricPluginInfo.getName()); DoricJavaPlugin plugin = mPluginMap.get(doricMetaInfo.getName());
if (plugin == null) { if (plugin == null) {
plugin = doricPluginInfo.createPlugin(this); plugin = doricMetaInfo.createInstance(this);
mPluginMap.put(doricPluginInfo.getName(), plugin); mPluginMap.put(doricMetaInfo.getName(), plugin);
} }
return plugin; return plugin;
} }

View File

@ -2,7 +2,8 @@ package com.github.penfeizhou.doric;
import android.text.TextUtils; import android.text.TextUtils;
import com.github.penfeizhou.doric.extension.bridge.DoricPluginInfo; import com.github.penfeizhou.doric.render.ViewNode;
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;
@ -19,7 +20,8 @@ import java.util.concurrent.ConcurrentHashMap;
*/ */
public class DoricRegistry { public class DoricRegistry {
private static Map<String, String> bundles = new ConcurrentHashMap<>(); private static Map<String, String> bundles = new ConcurrentHashMap<>();
private Map<String, DoricPluginInfo> pluginInfoMap = new HashMap<>(); private Map<String, DoricMetaInfo<DoricJavaPlugin>> pluginInfoMap = new HashMap<>();
private Map<String, DoricMetaInfo<ViewNode>> nodeInfoMap = new HashMap<>();
private static Set<DoricLibrary> doricLibraries = new HashSet<>(); private static Set<DoricLibrary> doricLibraries = new HashSet<>();
private static void initRegistry(DoricRegistry doricRegistry) { private static void initRegistry(DoricRegistry doricRegistry) {
@ -43,16 +45,27 @@ public class DoricRegistry {
} }
public void registerNativePlugin(Class<? extends DoricJavaPlugin> pluginClass) { public void registerNativePlugin(Class<? extends DoricJavaPlugin> pluginClass) {
DoricPluginInfo doricPluginInfo = new DoricPluginInfo(pluginClass); DoricMetaInfo<DoricJavaPlugin> doricMetaInfo = new DoricMetaInfo<>(pluginClass);
if (!TextUtils.isEmpty(doricPluginInfo.getName())) { if (!TextUtils.isEmpty(doricMetaInfo.getName())) {
pluginInfoMap.put(doricPluginInfo.getName(), doricPluginInfo); pluginInfoMap.put(doricMetaInfo.getName(), doricMetaInfo);
} }
} }
public DoricPluginInfo acquirePluginInfo(String name) { public DoricMetaInfo<DoricJavaPlugin> acquirePluginInfo(String name) {
return pluginInfoMap.get(name); return pluginInfoMap.get(name);
} }
public void registerViewNode(Class<? extends ViewNode> pluginClass) {
DoricMetaInfo<ViewNode> doricMetaInfo = new DoricMetaInfo<>(pluginClass);
if (!TextUtils.isEmpty(doricMetaInfo.getName())) {
nodeInfoMap.put(doricMetaInfo.getName(), doricMetaInfo);
}
}
public DoricMetaInfo<ViewNode> acquireViewNodeInfo(String name) {
return nodeInfoMap.get(name);
}
public String acquireJSBundle(String name) { public String acquireJSBundle(String name) {
return bundles.get(name); return bundles.get(name);
} }

View File

@ -1,21 +1,17 @@
package com.github.penfeizhou.doric.extension.bridge; package com.github.penfeizhou.doric.extension.bridge;
import android.text.TextUtils;
import com.github.penfeizhou.doric.DoricContext; import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.DoricRegistry; import com.github.penfeizhou.doric.DoricRegistry;
import com.github.penfeizhou.doric.async.AsyncResult; import com.github.penfeizhou.doric.async.AsyncResult;
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.DoricContextManager; import com.github.penfeizhou.doric.DoricContextManager;
import com.github.penfeizhou.doric.utils.DoricLog; import com.github.penfeizhou.doric.utils.DoricLog;
import com.github.penfeizhou.doric.utils.DoricMetaInfo;
import com.github.penfeizhou.doric.utils.DoricUtils; import com.github.penfeizhou.doric.utils.DoricUtils;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JavaValue; import com.github.pengfeizhou.jscore.JavaValue;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
/** /**
@ -33,7 +29,7 @@ public class DoricBridgeExtension {
public JavaValue callNative(String contextId, String module, String methodName, final String callbackId, final JSDecoder jsDecoder) { public JavaValue callNative(String contextId, String module, String methodName, final String callbackId, final JSDecoder jsDecoder) {
final DoricContext context = DoricContextManager.getContext(contextId); final DoricContext context = DoricContextManager.getContext(contextId);
DoricPluginInfo pluginInfo = mRegistry.acquirePluginInfo(module); DoricMetaInfo<DoricJavaPlugin> pluginInfo = mRegistry.acquirePluginInfo(module);
if (pluginInfo == null) { if (pluginInfo == null) {
DoricLog.e("Cannot find plugin class:%s", module); DoricLog.e("Cannot find plugin class:%s", module);
return new JavaValue(false); return new JavaValue(false);

View File

@ -15,5 +15,5 @@ import java.lang.annotation.Target;
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface DoricPlugin { public @interface DoricPlugin {
String name() default ""; String name();
} }

View File

@ -1,20 +1,15 @@
package com.github.penfeizhou.doric.plugin; package com.github.penfeizhou.doric.plugin;
import com.github.penfeizhou.doric.DoricContext; import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.utils.DoricComponent;
/** /**
* @Description: Doric * @Description: Doric
* @Author: pengfei.zhou * @Author: pengfei.zhou
* @CreateDate: 2019-07-18 * @CreateDate: 2019-07-18
*/ */
public abstract class DoricJavaPlugin { public abstract class DoricJavaPlugin extends DoricComponent {
private final DoricContext doricContext;
public DoricJavaPlugin(DoricContext doricContext) { public DoricJavaPlugin(DoricContext doricContext) {
this.doricContext = doricContext; super(doricContext);
}
public DoricContext getDoricContext() {
return doricContext;
} }
} }

View File

@ -0,0 +1,19 @@
package com.github.penfeizhou.doric.render;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @Description: com.github.penfeizhou.doric.render
* @Author: pengfei.zhou
* @CreateDate: 2019-07-20
*/
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DoricNode {
String name();
}

View File

@ -0,0 +1,14 @@
package com.github.penfeizhou.doric.render;
import com.github.pengfeizhou.jscore.JSValue;
/**
* @Description: com.github.penfeizhou.doric.render
* @Author: pengfei.zhou
* @CreateDate: 2019-07-20
*/
public interface IShader {
ViewNode create(String type);
boolean blend(ViewNode node, String propertyName, JSValue property);
}

View File

@ -0,0 +1,15 @@
package com.github.penfeizhou.doric.render;
import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.utils.DoricComponent;
/**
* @Description: Render
* @Author: pengfei.zhou
* @CreateDate: 2019-07-20
*/
public abstract class ViewNode extends DoricComponent {
public ViewNode(DoricContext doricContext) {
super(doricContext);
}
}

View File

@ -0,0 +1,20 @@
package com.github.penfeizhou.doric.utils;
import com.github.penfeizhou.doric.DoricContext;
/**
* @Description: com.github.penfeizhou.doric.utils
* @Author: pengfei.zhou
* @CreateDate: 2019-07-20
*/
public abstract class DoricComponent {
private final DoricContext doricContext;
public DoricComponent(DoricContext doricContext) {
this.doricContext = doricContext;
}
public DoricContext getDoricContext() {
return doricContext;
}
}

View File

@ -1,10 +1,10 @@
package com.github.penfeizhou.doric.extension.bridge; package com.github.penfeizhou.doric.utils;
import android.text.TextUtils; import android.text.TextUtils;
import com.github.penfeizhou.doric.DoricContext; import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.plugin.DoricJavaPlugin; import com.github.penfeizhou.doric.extension.bridge.DoricMethod;
import com.github.penfeizhou.doric.utils.DoricLog; import com.github.penfeizhou.doric.extension.bridge.DoricPlugin;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -16,14 +16,14 @@ import java.util.concurrent.ConcurrentHashMap;
* @Author: pengfei.zhou * @Author: pengfei.zhou
* @CreateDate: 2019-07-18 * @CreateDate: 2019-07-18
*/ */
public class DoricPluginInfo { public class DoricMetaInfo<T extends DoricComponent> {
private Constructor<? extends DoricJavaPlugin> pluginConstructor; private Constructor<? extends T> pluginConstructor;
private Map<String, Method> methodMap = new ConcurrentHashMap<>(); private Map<String, Method> methodMap = new ConcurrentHashMap<>();
private String name; private String name;
public DoricPluginInfo(Class<? extends DoricJavaPlugin> pluginClass) { public DoricMetaInfo(Class<? extends T> pluginClass) {
try { try {
this.pluginConstructor = pluginClass.getDeclaredConstructor(DoricContext.class); this.pluginConstructor = pluginClass.getDeclaredConstructor(DoricContext.class);
DoricPlugin doricPlugin = pluginClass.getAnnotation(DoricPlugin.class); DoricPlugin doricPlugin = pluginClass.getAnnotation(DoricPlugin.class);
@ -48,7 +48,7 @@ public class DoricPluginInfo {
return name; return name;
} }
public DoricJavaPlugin createPlugin(DoricContext doricContext) { public T createInstance(DoricContext doricContext) {
try { try {
return pluginConstructor.newInstance(doricContext); return pluginConstructor.newInstance(doricContext);
} catch (Exception e) { } catch (Exception e) {

View File

@ -0,0 +1,17 @@
package com.github.penfeizhou.doric.widget;
import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.render.DoricNode;
import com.github.penfeizhou.doric.render.ViewNode;
/**
* @Description: com.github.penfeizhou.doric.widget
* @Author: pengfei.zhou
* @CreateDate: 2019-07-20
*/
@DoricNode(name = "Image")
public class ImageNode extends ViewNode {
public ImageNode(DoricContext doricContext) {
super(doricContext);
}
}

View File

@ -0,0 +1,17 @@
package com.github.penfeizhou.doric.widget;
import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.render.DoricNode;
import com.github.penfeizhou.doric.render.ViewNode;
/**
* @Description: widget
* @Author: pengfei.zhou
* @CreateDate: 2019-07-20
*/
@DoricNode(name = "Text")
public class TextNode extends ViewNode {
public TextNode(DoricContext doricContext) {
super(doricContext);
}
}