diff --git a/.gitignore b/.gitignore index ebc6e53..4a36175 100644 --- a/.gitignore +++ b/.gitignore @@ -172,4 +172,5 @@ dmypy.json .pytype/ # Cython debug symbols -cython_debug/ \ No newline at end of file +cython_debug/ +.worktrees/ \ No newline at end of file diff --git a/src/com/yusufcihan/DynamicComponents/DynamicComponents.java b/src/com/yusufcihan/DynamicComponents/DynamicComponents.java index d107347..62fb857 100644 --- a/src/com/yusufcihan/DynamicComponents/DynamicComponents.java +++ b/src/com/yusufcihan/DynamicComponents/DynamicComponents.java @@ -188,6 +188,32 @@ public void run() { } } + @SimpleFunction(description = + "Creates a new dynamic component synchronously in the given container (arrangement/canvas), " + + "registers it internally, and returns the component object directly. " + + "If the ID is empty, a unique ID will be automatically generated." + ) + public Component CreateSync(final AndroidViewComponent in, Object componentName, String id) throws Exception { + String actualId = id; + if (actualId == null || actualId.trim().isEmpty()) { + actualId = GenerateID(); + } + + if (!COMPONENTS.containsKey(actualId)) { + lastUsedId = actualId; + Class mClass = Class.forName(Utils.getClassName(componentName)); + final Constructor mConstructor = mClass.getConstructor(ComponentContainer.class); + Component mComponent = Utils.createInstance(mConstructor, in); + COMPONENT_IDS.put(mComponent, actualId); + COMPONENTS.put(actualId, mComponent); + notifyListenersOfCreation(mComponent, actualId); + ComponentBuilt(mComponent, actualId, mComponent.getClass().getSimpleName()); + return mComponent; + } else { + throw new YailRuntimeError("All component IDs must be unique, the component ID '" + actualId + "' has already used before.", TAG); + } + } + @SimpleFunction(description = "Creates a new dynamic component in given container (arrangement/canvas) and return it without saving it to the " + "created components list, so it won't be attached to an ID. Note that you can't create components " + @@ -321,11 +347,11 @@ public void Move(AndroidViewComponent arrangement, AndroidViewComponent componen "so its ID can be reused for other components that are going to be created later." ) public void Remove(String id) { - Object component = COMPONENTS.get(id); + Component component = COMPONENTS.get(id); if (component == null) { return; } - RemoveComponent((AndroidViewComponent)component); + RemoveComponent(component); COMPONENTS.remove(id); COMPONENT_IDS.remove(component); } @@ -335,24 +361,28 @@ public void Remove(String id) { "But if the given component is dynamically created by this extension, this block will also " + "de-register its ID so its ID can be reused for other components that are going to be created later." ) - public void RemoveComponent(AndroidViewComponent component) { + public void RemoveComponent(Component component) { try { Method mMethod = Utils.getMethod(component, "getView"); if (mMethod != null) { final View mComponent = (View) mMethod.invoke(component); - final ViewGroup mParent = (ViewGroup) mComponent.getParent(); - if (postOnUiThread) { - new Handler(Looper.getMainLooper()).post(new Runnable() { - @Override - public void run() { + if (mComponent != null) { + final ViewGroup mParent = (ViewGroup) mComponent.getParent(); + if (mParent != null) { + if (postOnUiThread) { + new Handler(Looper.getMainLooper()).post(new Runnable() { + @Override + public void run() { + mParent.removeView(mComponent); + } + }); + } else { mParent.removeView(mComponent); } - }); - } else { - mParent.removeView(mComponent); + } } } - final String[] closeMethods = new String[] { "onPause", "onDestroy" }; + final String[] closeMethods = new String[] { "onPause", "onDestroy", "onDelete" }; for (String methodName : closeMethods) { final Method invokeMethod = Utils.getMethod(component, methodName); if (invokeMethod != null)