feat:add loadCallback for image network loading callback
This commit is contained in:
parent
5eddc44eb1
commit
38823700a2
@ -19,6 +19,7 @@ import android.graphics.drawable.Drawable;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
@ -30,6 +31,7 @@ import com.bumptech.glide.request.target.Target;
|
||||
import pub.doric.DoricContext;
|
||||
import pub.doric.extension.bridge.DoricPlugin;
|
||||
|
||||
import com.github.pengfeizhou.jscore.JSONBuilder;
|
||||
import com.github.pengfeizhou.jscore.JSValue;
|
||||
|
||||
/**
|
||||
@ -39,6 +41,8 @@ import com.github.pengfeizhou.jscore.JSValue;
|
||||
*/
|
||||
@DoricPlugin(name = "Image")
|
||||
public class ImageNode extends ViewNode<ImageView> {
|
||||
private String loadCallbackId = "";
|
||||
|
||||
public ImageNode(DoricContext doricContext) {
|
||||
super(doricContext);
|
||||
}
|
||||
@ -50,21 +54,32 @@ public class ImageNode extends ViewNode<ImageView> {
|
||||
|
||||
@Override
|
||||
protected void blend(ImageView view, String name, JSValue prop) {
|
||||
if ("imageUrl".equals(name)) {
|
||||
switch (name) {
|
||||
case "imageUrl":
|
||||
Glide.with(getContext()).load(prop.asString().value())
|
||||
.listener(new RequestListener<Drawable>() {
|
||||
@Override
|
||||
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
|
||||
if (!TextUtils.isEmpty(loadCallbackId)) {
|
||||
callJSResponse(loadCallbackId);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
|
||||
if (!TextUtils.isEmpty(loadCallbackId)) {
|
||||
callJSResponse(loadCallbackId, new JSONBuilder()
|
||||
.put("width", resource.getIntrinsicWidth())
|
||||
.put("height", resource.getIntrinsicHeight())
|
||||
.toJSONObject());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.into(view);
|
||||
} else if ("scaleType".equals(name)) {
|
||||
break;
|
||||
case "scaleType":
|
||||
int scaleType = prop.asNumber().toInt();
|
||||
switch (scaleType) {
|
||||
case 1:
|
||||
@ -77,8 +92,13 @@ public class ImageNode extends ViewNode<ImageView> {
|
||||
view.setScaleType(ImageView.ScaleType.FIT_XY);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
case "loadCallback":
|
||||
this.loadCallbackId = prop.asString().value();
|
||||
break;
|
||||
default:
|
||||
super.blend(view, name, prop);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -57,6 +57,8 @@ public class TextNode extends ViewNode<TextView> {
|
||||
case "textAlignment":
|
||||
view.setGravity(prop.asNumber().toInt() | Gravity.CENTER_VERTICAL);
|
||||
break;
|
||||
case "numberOfLines":
|
||||
break;
|
||||
default:
|
||||
super.blend(view, name, prop);
|
||||
break;
|
||||
|
@ -25,6 +25,9 @@ class ImageDemo extends Panel {
|
||||
},
|
||||
scaleType: ScaleType.ScaleToFill,
|
||||
layoutConfig: layoutConfig().exactly(),
|
||||
loadCallback: (ret) => {
|
||||
log('loadCallback', ret)
|
||||
}
|
||||
}),
|
||||
label('ScaleAspectFit'),
|
||||
image({
|
||||
|
@ -24,6 +24,10 @@
|
||||
#import "Doric.h"
|
||||
#import <SDWebImage/SDWebImage.h>
|
||||
|
||||
@interface DoricImageNode ()
|
||||
@property(nonatomic, copy) NSString *loadCallbackId;
|
||||
@end
|
||||
|
||||
@implementation DoricImageNode
|
||||
|
||||
- (UIImageView *)build {
|
||||
@ -37,7 +41,19 @@ - (void)blendView:(UIImageView *)view forPropName:(NSString *)name propValue:(id
|
||||
__weak typeof(self) _self = self;
|
||||
[view sd_setImageWithURL:[NSURL URLWithString:prop] completed:^(UIImage *_Nullable image, NSError *_Nullable error, SDImageCacheType cacheType, NSURL *_Nullable imageURL) {
|
||||
__strong typeof(_self) self = _self;
|
||||
if (error) {
|
||||
if (self.loadCallbackId.length > 0) {
|
||||
[self callJSResponse:self.loadCallbackId, nil];
|
||||
}
|
||||
} else {
|
||||
if (self.loadCallbackId.length > 0) {
|
||||
[self callJSResponse:self.loadCallbackId,
|
||||
@{@"width": @(image.size.width), @"height": @(image.size.height)},
|
||||
nil];
|
||||
}
|
||||
[self requestLayout];
|
||||
}
|
||||
|
||||
}];
|
||||
} else if ([@"scaleType" isEqualToString:name]) {
|
||||
switch ([prop integerValue]) {
|
||||
@ -54,6 +70,8 @@ - (void)blendView:(UIImageView *)view forPropName:(NSString *)name propValue:(id
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if ([@"loadCallback" isEqualToString:name]) {
|
||||
self.loadCallbackId = prop;
|
||||
} else {
|
||||
[super blendView:view forPropName:name propValue:prop];
|
||||
}
|
||||
|
@ -51,6 +51,7 @@ export enum ScaleType {
|
||||
export interface IImage extends IView {
|
||||
imageUrl?: string
|
||||
scaleType?: ScaleType
|
||||
loadCallback?: (image: { width: number; height: number } | undefined) => void
|
||||
}
|
||||
|
||||
export class Image extends View implements IImage {
|
||||
@ -59,4 +60,7 @@ export class Image extends View implements IImage {
|
||||
|
||||
@Property
|
||||
scaleType?: ScaleType
|
||||
|
||||
@Property
|
||||
loadCallback?: (image: { width: number; height: number } | undefined) => void
|
||||
}
|
Reference in New Issue
Block a user