feat:add loadCallback for image network loading callback

This commit is contained in:
pengfei.zhou 2019-11-20 16:03:07 +08:00
parent 5eddc44eb1
commit 38823700a2
5 changed files with 76 additions and 29 deletions

View File

@ -19,6 +19,7 @@ import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView; import android.widget.ImageView;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
@ -30,6 +31,7 @@ import com.bumptech.glide.request.target.Target;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
/** /**
@ -39,6 +41,8 @@ import com.github.pengfeizhou.jscore.JSValue;
*/ */
@DoricPlugin(name = "Image") @DoricPlugin(name = "Image")
public class ImageNode extends ViewNode<ImageView> { public class ImageNode extends ViewNode<ImageView> {
private String loadCallbackId = "";
public ImageNode(DoricContext doricContext) { public ImageNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
} }
@ -50,21 +54,32 @@ public class ImageNode extends ViewNode<ImageView> {
@Override @Override
protected void blend(ImageView view, String name, JSValue prop) { protected void blend(ImageView view, String name, JSValue prop) {
if ("imageUrl".equals(name)) { switch (name) {
case "imageUrl":
Glide.with(getContext()).load(prop.asString().value()) Glide.with(getContext()).load(prop.asString().value())
.listener(new RequestListener<Drawable>() { .listener(new RequestListener<Drawable>() {
@Override @Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
if (!TextUtils.isEmpty(loadCallbackId)) {
callJSResponse(loadCallbackId);
}
return false; return false;
} }
@Override @Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { 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; return false;
} }
}) })
.into(view); .into(view);
} else if ("scaleType".equals(name)) { break;
case "scaleType":
int scaleType = prop.asNumber().toInt(); int scaleType = prop.asNumber().toInt();
switch (scaleType) { switch (scaleType) {
case 1: case 1:
@ -77,8 +92,13 @@ public class ImageNode extends ViewNode<ImageView> {
view.setScaleType(ImageView.ScaleType.FIT_XY); view.setScaleType(ImageView.ScaleType.FIT_XY);
break; break;
} }
} else { break;
case "loadCallback":
this.loadCallbackId = prop.asString().value();
break;
default:
super.blend(view, name, prop); super.blend(view, name, prop);
break;
} }
} }
} }

View File

@ -57,6 +57,8 @@ public class TextNode extends ViewNode<TextView> {
case "textAlignment": case "textAlignment":
view.setGravity(prop.asNumber().toInt() | Gravity.CENTER_VERTICAL); view.setGravity(prop.asNumber().toInt() | Gravity.CENTER_VERTICAL);
break; break;
case "numberOfLines":
break;
default: default:
super.blend(view, name, prop); super.blend(view, name, prop);
break; break;

View File

@ -25,6 +25,9 @@ class ImageDemo extends Panel {
}, },
scaleType: ScaleType.ScaleToFill, scaleType: ScaleType.ScaleToFill,
layoutConfig: layoutConfig().exactly(), layoutConfig: layoutConfig().exactly(),
loadCallback: (ret) => {
log('loadCallback', ret)
}
}), }),
label('ScaleAspectFit'), label('ScaleAspectFit'),
image({ image({

View File

@ -24,6 +24,10 @@
#import "Doric.h" #import "Doric.h"
#import <SDWebImage/SDWebImage.h> #import <SDWebImage/SDWebImage.h>
@interface DoricImageNode ()
@property(nonatomic, copy) NSString *loadCallbackId;
@end
@implementation DoricImageNode @implementation DoricImageNode
- (UIImageView *)build { - (UIImageView *)build {
@ -37,7 +41,19 @@ - (void)blendView:(UIImageView *)view forPropName:(NSString *)name propValue:(id
__weak typeof(self) _self = self; __weak typeof(self) _self = self;
[view sd_setImageWithURL:[NSURL URLWithString:prop] completed:^(UIImage *_Nullable image, NSError *_Nullable error, SDImageCacheType cacheType, NSURL *_Nullable imageURL) { [view sd_setImageWithURL:[NSURL URLWithString:prop] completed:^(UIImage *_Nullable image, NSError *_Nullable error, SDImageCacheType cacheType, NSURL *_Nullable imageURL) {
__strong typeof(_self) self = _self; __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]; [self requestLayout];
}
}]; }];
} else if ([@"scaleType" isEqualToString:name]) { } else if ([@"scaleType" isEqualToString:name]) {
switch ([prop integerValue]) { switch ([prop integerValue]) {
@ -54,6 +70,8 @@ - (void)blendView:(UIImageView *)view forPropName:(NSString *)name propValue:(id
break; break;
} }
} }
} else if ([@"loadCallback" isEqualToString:name]) {
self.loadCallbackId = prop;
} else { } else {
[super blendView:view forPropName:name propValue:prop]; [super blendView:view forPropName:name propValue:prop];
} }

View File

@ -51,6 +51,7 @@ export enum ScaleType {
export interface IImage extends IView { export interface IImage extends IView {
imageUrl?: string imageUrl?: string
scaleType?: ScaleType scaleType?: ScaleType
loadCallback?: (image: { width: number; height: number } | undefined) => void
} }
export class Image extends View implements IImage { export class Image extends View implements IImage {
@ -59,4 +60,7 @@ export class Image extends View implements IImage {
@Property @Property
scaleType?: ScaleType scaleType?: ScaleType
@Property
loadCallback?: (image: { width: number; height: number } | undefined) => void
} }