Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/waterfall image module #4071

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
this.listView.scrollToIndex({ index, animation: true });
}

onScroll(obj) {

Check failure on line 238 in driver/js/examples/hippy-react-demo/src/components/WaterfallView/index.jsx

View workflow job for this annotation

GitHub Actions / frontend_build_tests (16.x)

'obj' is defined but never used. Allowed unused args must match /^_.+/u

Check failure on line 238 in driver/js/examples/hippy-react-demo/src/components/WaterfallView/index.jsx

View workflow job for this annotation

GitHub Actions / frontend_build_tests (17.x)

'obj' is defined but never used. Allowed unused args must match /^_.+/u

}

Expand Down Expand Up @@ -372,7 +372,6 @@
getItemKey={this.getItemKey}
getItemStyle={this.getItemStyle}
getHeaderStyle={this.getHeaderStyle}
contentInset={contentInset}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.tencent.mtt.hippy.modules.nativemodules.image;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import androidx.annotation.NonNull;
Expand All @@ -25,6 +26,7 @@
import com.tencent.mtt.hippy.modules.Promise;
import com.tencent.mtt.hippy.modules.nativemodules.HippyNativeModuleBase;
import com.tencent.mtt.hippy.runtime.builtins.JSObject;
import com.tencent.mtt.hippy.utils.LogUtils;
import com.tencent.vfs.ResourceDataHolder;
import com.tencent.vfs.VfsManager;
import com.tencent.vfs.VfsManager.FetchResourceCallback;
Expand All @@ -33,6 +35,7 @@
@HippyNativeModule(name = "ImageLoaderModule")
public class ImageLoaderModule extends HippyNativeModuleBase {

private final String TAG = "ImageLoaderModule";
private final VfsManager mVfsManager;
private static final String ERROR_KEY_MESSAGE = "message";

Expand Down Expand Up @@ -64,6 +67,41 @@ private HashMap<String, String> generateRequestParams() {
return requestParams;
}

private void onFetchFailed(final String url, final Promise promise, @NonNull final ResourceDataHolder dataHolder) {
String message =
dataHolder.errorMessage != null ? dataHolder.errorMessage : "";
String errorMsg = "Fetch image failed, url=" + url + ", msg=" + message;
JSObject jsObject = new JSObject();
jsObject.set(ERROR_KEY_MESSAGE, errorMsg);
promise.reject(jsObject);
}

private void handleFetchResult(final String url, final Promise promise, @NonNull final ResourceDataHolder dataHolder) {
byte[] bytes = dataHolder.getBytes();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should place after resultCode check

Bitmap bitmap = dataHolder.bitmap;
LogUtils.d(TAG, "handleFetchResult: url " + url + ", result " + dataHolder.resultCode);
if (dataHolder.resultCode == ResourceDataHolder.RESOURCE_LOAD_SUCCESS_CODE) {
if (bitmap != null && !bitmap.isRecycled()) {
LogUtils.d(TAG, "handleFetchResult: url " + url
+ ", bitmap width " + bitmap.getWidth() + ", bitmap height " + bitmap.getHeight());
JSObject jsObject = new JSObject();
jsObject.set("width", bitmap.getWidth());
jsObject.set("height", bitmap.getHeight());
promise.resolve(jsObject);
} else if (bytes != null && bytes.length > 0) {
decodeImageData(url, bytes, promise);
} else {
if (TextUtils.isEmpty(dataHolder.errorMessage)) {
dataHolder.errorMessage = "Invalid image data or bitmap!";
}
onFetchFailed(url, promise, dataHolder);
}
} else {
onFetchFailed(url, promise, dataHolder);
}
dataHolder.recycle();
}

@HippyMethod(name = "getSize")
public void getSize(final String url, final Promise promise) {
if (TextUtils.isEmpty(url)) {
Expand All @@ -76,20 +114,7 @@ public void getSize(final String url, final Promise promise) {
new FetchResourceCallback() {
@Override
public void onFetchCompleted(@NonNull final ResourceDataHolder dataHolder) {
byte[] bytes = dataHolder.getBytes();
if (dataHolder.resultCode
!= ResourceDataHolder.RESOURCE_LOAD_SUCCESS_CODE || bytes == null
|| bytes.length <= 0) {
String message =
dataHolder.errorMessage != null ? dataHolder.errorMessage : "";
String errorMsg = "Fetch image failed, url=" + url + ", msg=" + message;
JSObject jsObject = new JSObject();
jsObject.set(ERROR_KEY_MESSAGE, errorMsg);
promise.reject(jsObject);
} else {
decodeImageData(url, bytes, promise);
}
dataHolder.recycle();
handleFetchResult(url, promise, dataHolder);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,10 @@ public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
lp.leftMargin = 0;
return;
}
ViewHolder vh = lp.mViewHolder;
int margin = mItemSpacing;
HippyRecyclerListAdapter adapter = (HippyRecyclerListAdapter) parent.getAdapter();
if (lp.isFullSpan()) {
margin = 0;
} else if (vh != null) {
int headerCount = adapter.hasHeader() ? 1 : 0;
if (!adapter.hasBannerView() && vh.getLayoutPosition() < (mNumberOfColumns + headerCount)) {
margin = 0;
}
}
if (HippyListUtils.isVerticalLayout(parent)) {
lp.bottomMargin = mItemSpacing;
lp.topMargin = margin;
} else {
lp.rightMargin = mItemSpacing;
lp.leftMargin = margin;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import com.tencent.mtt.hippy.views.hippylist.HippyRecyclerListAdapter;
import com.tencent.mtt.hippy.views.hippylist.PullRefreshContainer;
import com.tencent.renderer.node.ListItemRenderNode;
import com.tencent.renderer.node.PullFooterRenderNode;
import com.tencent.renderer.node.PullHeaderRenderNode;
import com.tencent.renderer.node.RenderNode;
import com.tencent.renderer.node.WaterfallItemRenderNode;

Expand Down Expand Up @@ -63,22 +61,22 @@ public void layoutDecoratedWithMargins(@NonNull View child, int left, int top, i
(StaggeredGridLayoutManager.LayoutParams) child.getLayoutParams();
final int spanIndex = lp.getSpanIndex();
final boolean isFullSpan = lp.isFullSpan();
int lf = left;
int rt = right;
if (isFullSpan) {
child.layout(left, top, right, bottom);
} else {
int lf = mRecyclerView.getPaddingLeft() + spanIndex * (lp.width
lf = mRecyclerView.getPaddingLeft() + spanIndex * (lp.width
+ mItemDecoration.getColumnSpacing());
int rt = mRecyclerView.getPaddingLeft() + (spanIndex + 1) * lp.width
rt = mRecyclerView.getPaddingLeft() + (spanIndex + 1) * lp.width
+ spanIndex * mItemDecoration.getColumnSpacing();
child.layout(lf, top, rt, bottom);
}
super.layoutDecoratedWithMargins(child, lf, top, rt, bottom);
int size;
if (child instanceof PullRefreshContainer) {
size = getOrientation() == RecyclerView.VERTICAL ? bottom - top : right - left;
} else {
size = getOrientation() == RecyclerView.VERTICAL ? lp.height
+ mItemDecoration.getItemSpacing()
: lp.width + mItemDecoration.getItemSpacing();
size = getOrientation() == RecyclerView.VERTICAL ? lp.height : lp.width;
}
RenderNode childNode = RenderManager.getRenderNode(child);
if (childNode instanceof WaterfallItemRenderNode) {
Expand Down Expand Up @@ -162,11 +160,7 @@ private int computeScrollOffset() {
private int getChildSize(@NonNull ListItemRenderNode child) {
Integer size = itemSizeMaps.get(child.getId());
if (size == null) {
if (child.isPullFooter() || child.isPullHeader()) {
size = getItemSizeFromAdapter(child);
} else {
size = getItemSizeFromAdapter(child) + mItemDecoration.getItemSpacing();
}
size = getItemSizeFromAdapter(child);
}
return Math.max(size, 0);
}
Expand Down Expand Up @@ -280,6 +274,7 @@ int getItemSizeFromAdapter(ListItemRenderNode node) {
ItemLayoutParams layoutInfo = (ItemLayoutParams) adapter;
resetLayoutParams();
layoutInfo.getItemLayoutParams(node, ITEM_LAYOUT_PARAMS);
LogUtils.d(TAG, "getItemSizeFromAdapter id " + node.getId() + ", height " + ITEM_LAYOUT_PARAMS.height);
if (getOrientation() == RecyclerView.VERTICAL) {
if (ITEM_LAYOUT_PARAMS.height >= 0) {
int size = ITEM_LAYOUT_PARAMS.height + ITEM_LAYOUT_PARAMS.bottomMargin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.tencent.mtt.hippy.views.list.IRecycleItemTypeChange;
import com.tencent.mtt.hippy.views.refresh.HippyPullFooterView;
import com.tencent.mtt.hippy.views.refresh.HippyPullHeaderView;
import com.tencent.mtt.hippy.views.waterfall.HippyWaterfallItemView;
import com.tencent.mtt.hippy.views.waterfall.HippyWaterfallView;
import com.tencent.renderer.node.ListItemRenderNode;
import com.tencent.renderer.node.PullFooterRenderNode;
import com.tencent.renderer.node.PullHeaderRenderNode;
Expand Down Expand Up @@ -223,7 +225,11 @@ protected void setLayoutParams(View itemView, int position) {
childLp.height = isVertical ? childNode.getHeight() : MATCH_PARENT;
childLp.width = isVertical ? MATCH_PARENT : childNode.getWidth();
} else {
childLp.height = childNode.getHeight();
if ((itemView instanceof HippyWaterfallItemView) && (hippyRecyclerView instanceof HippyWaterfallView)) {
childLp.height = childNode.getHeight() + ((HippyWaterfallView) hippyRecyclerView).getItemSpacing();
} else {
childLp.height = childNode.getHeight();
}
childLp.width = childNode.getWidth();
}
itemView.setLayoutParams(childLp);
Expand Down Expand Up @@ -342,6 +348,9 @@ private int getRenderNodeHeight(@NonNull ListItemRenderNode childNode) {
}
return 0;
}
if ((childNode instanceof WaterfallItemRenderNode) && (hippyRecyclerView instanceof HippyWaterfallView)) {
return childNode.getHeight() + ((HippyWaterfallView) hippyRecyclerView).getItemSpacing();
}
return childNode.getHeight();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public boolean onFling(int velocityX, int velocityY) {
return isOverPulling();
}
});
setOverScrollMode(OVER_SCROLL_NEVER);
}

public void setColumnSpacing(int columnSpacing) {
Expand All @@ -46,6 +47,14 @@ public void setColumnSpacing(int columnSpacing) {
}
}

public int getItemSpacing() {
RecyclerView.ItemDecoration decoration = getItemDecorationAt(0);
if (decoration instanceof HippyGridSpacesItemDecoration) {
return ((HippyGridSpacesItemDecoration) decoration).getItemSpacing();
}
return 0;
}

public void setItemSpacing(int itemSpacing) {
RecyclerView.ItemDecoration decoration = getItemDecorationAt(0);
if (decoration instanceof HippyGridSpacesItemDecoration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void draw(@NonNull Canvas canvas) {
drawGif(canvas, mImageHolder.getGifMovie());
} else {
Bitmap bitmap = mImageHolder.getBitmap();
if (bitmap != null) {
if (bitmap != null && !bitmap.isRecycled()) {
drawBitmap(canvas, bitmap);
}
}
Expand Down
Loading