[android] Make SvgView.drawChildren synchronized

Fix race-condition
https://github.com/react-native-community/react-native-svg/issues/948
Refactor toDataUrl
This commit is contained in:
Mikael Sand
2019-02-27 21:09:50 +02:00
parent fdd8f93dac
commit 0b1f53698b
3 changed files with 29 additions and 47 deletions
@@ -26,27 +26,21 @@ class SvgViewModule extends ReactContextBaseJavaModule {
}
@ReactMethod
public void toDataURL(int tag, Callback successCallback) {
SvgView svg = SvgViewManager.getSvgViewByTag(tag);
if (svg != null) {
successCallback.invoke(svg.toDataURL());
}
}
@ReactMethod
public void toDataURL(int tag, ReadableMap options, Callback successCallback) {
SvgView svg = SvgViewManager.getSvgViewByTag(tag);
if (svg != null) {
successCallback.invoke(
svg.toDataURL(
options.getInt("width"),
options.getInt("height")
)
);
if (options != null) {
successCallback.invoke(
svg.toDataURL(
options.getInt("width"),
options.getInt("height")
)
);
} else {
successCallback.invoke(svg.toDataURL());
}
}
}
}
+1 -5
View File
@@ -54,11 +54,7 @@ export default class Svg extends Shape {
return;
}
const handle = findNodeHandle(this.root);
if (options) {
RNSVGSvgViewManager.toDataURL(handle, options, callback);
} else {
RNSVGSvgViewManager.toDataURL(handle, callback);
}
RNSVGSvgViewManager.toDataURL(handle, options, callback);
};
render() {
+18 -26
View File
@@ -30,39 +30,31 @@ RCT_EXPORT_VIEW_PROPERTY(align, NSString)
RCT_EXPORT_VIEW_PROPERTY(meetOrSlice, RNSVGVBMOS)
RCT_EXPORT_VIEW_PROPERTY(tintColor, UIColor)
RCT_EXPORT_METHOD(toDataURL:(nonnull NSNumber *)reactTag callback:(RCTResponseSenderBlock)callback)
{
[self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *,UIView *> *viewRegistry) {
__kindof UIView *view = viewRegistry[reactTag];
if ([view isKindOfClass:[RNSVGSvgView class]]) {
RNSVGSvgView *svg = view;
callback(@[[svg getDataURL]]);
} else {
RCTLogError(@"Invalid svg returned frin registry, expecting RNSVGSvgView, got: %@", view);
}
}];
}
RCT_EXPORT_METHOD(toDataURL:(nonnull NSNumber *)reactTag options:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback)
{
[self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *,UIView *> *viewRegistry) {
__kindof UIView *view = viewRegistry[reactTag];
if ([view isKindOfClass:[RNSVGSvgView class]]) {
RNSVGSvgView *svg = view;
id width = [options objectForKey:@"width"];
id height = [options objectForKey:@"height"];
if (![width isKindOfClass:NSNumber.class] ||
![height isKindOfClass:NSNumber.class]) {
RCTLogError(@"Invalid width or height given to toDataURL");
return;
}
NSNumber* w = width;
NSInteger wi = (NSInteger)[w intValue];
NSNumber* h = height;
NSInteger hi = (NSInteger)[h intValue];
if (options == nil) {
RNSVGSvgView *svg = view;
callback(@[[svg getDataURL]]);
} else {
id width = [options objectForKey:@"width"];
id height = [options objectForKey:@"height"];
if (![width isKindOfClass:NSNumber.class] ||
![height isKindOfClass:NSNumber.class]) {
RCTLogError(@"Invalid width or height given to toDataURL");
return;
}
NSNumber* w = width;
NSInteger wi = (NSInteger)[w intValue];
NSNumber* h = height;
NSInteger hi = (NSInteger)[h intValue];
CGSize bounds = CGSizeMake(wi, hi);
callback(@[[svg getDataURLwithBounds:bounds]]);
CGSize bounds = CGSizeMake(wi, hi);
callback(@[[svg getDataURLwithBounds:bounds]]);
}
} else {
RCTLogError(@"Invalid svg returned frin registry, expecting RNSVGSvgView, got: %@", view);
}