fix(android): update props reuse null view object

This commit is contained in:
maxli
2023-10-25 21:29:15 +08:00
committed by siguangli
parent 61c41d8fc5
commit a45eda099f
2 changed files with 14 additions and 17 deletions
@@ -18,13 +18,11 @@ package com.tencent.mtt.hippy.uimanager;
import static com.tencent.renderer.NativeRenderException.ExceptionCode.ADD_CHILD_VIEW_FAILED_ERR;
import static com.tencent.renderer.NativeRenderException.ExceptionCode.REMOVE_CHILD_VIEW_FAILED_ERR;
import static com.tencent.renderer.NativeRenderer.SCREEN_SNAPSHOT_ROOT_ID;
import static com.tencent.renderer.node.RenderNode.FLAG_ALREADY_UPDATED;
import static com.tencent.renderer.node.RenderNode.FLAG_UPDATE_LAYOUT;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.openhippy.pool.BasePool.PoolType;
@@ -35,7 +33,6 @@ import com.tencent.mtt.hippy.annotation.HippyController;
import com.tencent.mtt.hippy.common.HippyArray;
import com.tencent.mtt.hippy.dom.node.NodeProps;
import com.tencent.mtt.hippy.modules.Promise;
import com.tencent.mtt.hippy.utils.UIThreadUtils;
import com.tencent.mtt.hippy.views.custom.HippyCustomPropsController;
import com.tencent.mtt.hippy.views.hippylist.HippyRecyclerViewController;
import com.tencent.mtt.hippy.views.image.HippyImageViewController;
@@ -57,7 +54,6 @@ import com.tencent.mtt.hippy.views.waterfalllist.HippyWaterfallViewController;
import com.tencent.mtt.hippy.views.webview.HippyWebViewController;
import com.tencent.renderer.NativeRender;
import com.tencent.renderer.NativeRenderException;
import com.tencent.renderer.NativeRendererManager;
import com.tencent.renderer.Renderer;
import com.tencent.renderer.node.RenderNode;
import com.tencent.renderer.node.VirtualNode;
@@ -71,7 +67,7 @@ public class ControllerManager {
@NonNull
private final ControllerRegistry mControllerRegistry;
@NonNull
private final ControllerUpdateManger<HippyViewController<?>, View> mControllerUpdateManger;
private final ControllerUpdateManger<HippyViewController<?>> mControllerUpdateManger;
@NonNull
private final Map<Integer, Pool<Integer, View>> mPreCreateViewPools = new HashMap<>();
@NonNull
@@ -38,7 +38,6 @@ import com.tencent.renderer.utils.PropertyUtils;
import com.tencent.renderer.utils.PropertyUtils.PropertyMethodHolder;
import com.tencent.renderer.node.RenderNode;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
@@ -47,7 +46,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class ControllerUpdateManger<T, G> {
public class ControllerUpdateManger<T> {
private static final Map<Class<?>, Map<String, PropertyMethodHolder>> sViewPropsMethodMap = new HashMap<>();
private static final Map<String, PropertyMethodHolder> sComponentPropsMethodMap = new HashMap<>();
@@ -191,12 +190,12 @@ public class ControllerUpdateManger<T, G> {
}
}
private void handleCustomProps(T t, G g, @NonNull String key,
private void handleCustomProps(T t, @Nullable View g, @NonNull String key,
@NonNull Map<String, Object> props) {
boolean hasCustomMethodHolder = false;
if (!(g instanceof View)) {
if (g == null) {
return;
}
boolean hasCustomMethodHolder = false;
Object customProps = props.get(key);
if (mCustomPropsController != null
&& mCustomPropsController instanceof HippyCustomPropsController) {
@@ -214,11 +213,11 @@ public class ControllerUpdateManger<T, G> {
}
if (!hasCustomMethodHolder && t instanceof HippyViewController) {
//noinspection unchecked
((HippyViewController) t).setCustomProp((View) g, key, customProps);
((HippyViewController) t).setCustomProp(g, key, customProps);
}
}
protected void updateProps(@NonNull RenderNode node, @NonNull T controller, @Nullable G view,
protected void updateProps(@NonNull RenderNode node, @NonNull T controller, @Nullable View view,
@Nullable Map<String, Object> props, boolean skipComponentProps) {
if (props == null || props.isEmpty()) {
return;
@@ -238,18 +237,20 @@ public class ControllerUpdateManger<T, G> {
}
PropertyMethodHolder methodHolder = methodHolderMap.get(key);
if (methodHolder != null) {
Object arg = (view == null) ? node.createView(true) : view;
if (arg != null) {
invokePropMethod(controller, arg, props, key, methodHolder);
if (view == null) {
view = node.createView(true);
}
if (view != null) {
invokePropMethod(controller, view, props, key, methodHolder);
}
} else {
// Background color is a property supported by both view and component, if the
// host view of a node has already been created, we need to set this property
// separately on the view, otherwise the background color setting for non
// flattened elements will not take effect.
if (key.equals(NodeProps.BACKGROUND_COLOR) && view instanceof View
if (key.equals(NodeProps.BACKGROUND_COLOR) && view != null
&& !(view instanceof FlatViewGroup)) {
((View) view).setBackgroundColor(
view.setBackgroundColor(
MapUtils.getIntValue(props, NodeProps.BACKGROUND_COLOR,
Color.TRANSPARENT));
} else if (!handleComponentProps(node, key, props, skipComponentProps)) {