Skip to content

Commit

Permalink
fix(android): should create view if has custom props
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 authored and siguangli committed Oct 30, 2023
1 parent f672a44 commit b93d175
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ class ExampleAPIProvider : HippyAPIProvider {
* register View controller for JavaScript
*/
override fun getControllers(): List<Class<out HippyViewController<*>>> {
return emptyList()
return arrayListOf(ExampleCustomPropsController::class.java)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Tencent is pleased to support the open source community by making Hippy
* available.
* Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.openhippy.example

import android.view.View
import com.tencent.mtt.hippy.annotation.HippyController
import com.tencent.mtt.hippy.annotation.HippyControllerProps
import com.tencent.mtt.hippy.common.HippyMap
import com.tencent.mtt.hippy.utils.LogUtils
import com.tencent.mtt.hippy.views.custom.HippyCustomPropsController

@HippyController(name = HippyCustomPropsController.CLASS_NAME)
class ExampleCustomPropsController : HippyCustomPropsController() {

val TAG = "ExampleCustomPropsController"

@HippyControllerProps(name = "pageParams", defaultType = HippyControllerProps.MAP)
fun setDtPageParams(view: View, params: HippyMap?) {
LogUtils.d(TAG, "setDtPageParams id " + view.id + ", params " + params)
}

@HippyControllerProps(name = "elementParams", defaultType = HippyControllerProps.MAP)
fun setDtElementParams(view: View, params: HippyMap?) {
LogUtils.d(TAG, "setDtElementParams id " + view.id + ", params " + params)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,8 @@ private void invokePropMethod(@NonNull Object obj, @NonNull Object arg1,
}
}

private void handleCustomProps(T t, @Nullable View g, @NonNull String key,
@NonNull Map<String, Object> props) {
if (g == null) {
return;
}
boolean hasCustomMethodHolder = false;
Object customProps = props.get(key);
@Nullable
private PropertyMethodHolder getCustomPropsMethodHolder(@NonNull String key) {
if (mCustomPropsController != null
&& mCustomPropsController instanceof HippyCustomPropsController) {
Class<?> cls = mCustomPropsController.getClass();
Expand All @@ -205,16 +200,22 @@ private void handleCustomProps(T t, @Nullable View g, @NonNull String key,
methodHolderMap = new HashMap<>();
findViewPropsMethod(cls, methodHolderMap);
}
PropertyMethodHolder methodHolder = methodHolderMap.get(key);
return methodHolderMap.get(key);
}
return null;
}

private void handleCustomProps(T t, @Nullable View view, @NonNull String key,
@NonNull Map<String, Object> props, @Nullable PropertyMethodHolder methodHolder) {
if (view != null) {
Object customProps = props.get(key);
if (methodHolder != null) {
invokePropMethod(mCustomPropsController, g, props, key, methodHolder);
hasCustomMethodHolder = true;
invokePropMethod(mCustomPropsController, view, props, key, methodHolder);
} else if (t instanceof HippyViewController) {
//noinspection unchecked
((HippyViewController) t).setCustomProp(view, key, customProps);
}
}
if (!hasCustomMethodHolder && t instanceof HippyViewController) {
//noinspection unchecked
((HippyViewController) t).setCustomProp(g, key, customProps);
}
}

protected void updateProps(@NonNull RenderNode node, @NonNull T controller, @Nullable View view,
Expand Down Expand Up @@ -254,7 +255,14 @@ protected void updateProps(@NonNull RenderNode node, @NonNull T controller, @Nul
MapUtils.getIntValue(props, NodeProps.BACKGROUND_COLOR,
Color.TRANSPARENT));
} else if (!handleComponentProps(node, key, props, skipComponentProps)) {
handleCustomProps(controller, view, key, props);
PropertyMethodHolder customMethodHolder = getCustomPropsMethodHolder(key);
if (customMethodHolder != null && view == null) {
// If the host has a custom attribute that needs to be processed, this element cannot be
// flattened, otherwise, if the view is empty, custom attributes will not be passed through
// to custom props controller.
view = node.createView(true);
}
handleCustomProps(controller, view, key, props, customMethodHolder);
}
}
}
Expand Down

0 comments on commit b93d175

Please sign in to comment.