[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 @ReactMethod
public void toDataURL(int tag, ReadableMap options, Callback successCallback) { public void toDataURL(int tag, ReadableMap options, Callback successCallback) {
SvgView svg = SvgViewManager.getSvgViewByTag(tag); SvgView svg = SvgViewManager.getSvgViewByTag(tag);
if (svg != null) { if (svg != null) {
successCallback.invoke( if (options != null) {
svg.toDataURL( successCallback.invoke(
options.getInt("width"), svg.toDataURL(
options.getInt("height") 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; return;
} }
const handle = findNodeHandle(this.root); const handle = findNodeHandle(this.root);
if (options) { RNSVGSvgViewManager.toDataURL(handle, options, callback);
RNSVGSvgViewManager.toDataURL(handle, options, callback);
} else {
RNSVGSvgViewManager.toDataURL(handle, callback);
}
}; };
render() { 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(meetOrSlice, RNSVGVBMOS)
RCT_EXPORT_VIEW_PROPERTY(tintColor, UIColor) 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) RCT_EXPORT_METHOD(toDataURL:(nonnull NSNumber *)reactTag options:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback)
{ {
[self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *,UIView *> *viewRegistry) { [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *,UIView *> *viewRegistry) {
__kindof UIView *view = viewRegistry[reactTag]; __kindof UIView *view = viewRegistry[reactTag];
if ([view isKindOfClass:[RNSVGSvgView class]]) { if ([view isKindOfClass:[RNSVGSvgView class]]) {
RNSVGSvgView *svg = view; RNSVGSvgView *svg = view;
id width = [options objectForKey:@"width"]; if (options == nil) {
id height = [options objectForKey:@"height"]; RNSVGSvgView *svg = view;
if (![width isKindOfClass:NSNumber.class] || callback(@[[svg getDataURL]]);
![height isKindOfClass:NSNumber.class]) { } else {
RCTLogError(@"Invalid width or height given to toDataURL"); id width = [options objectForKey:@"width"];
return; id height = [options objectForKey:@"height"];
} if (![width isKindOfClass:NSNumber.class] ||
NSNumber* w = width; ![height isKindOfClass:NSNumber.class]) {
NSInteger wi = (NSInteger)[w intValue]; RCTLogError(@"Invalid width or height given to toDataURL");
NSNumber* h = height; return;
NSInteger hi = (NSInteger)[h intValue]; }
NSNumber* w = width;
NSInteger wi = (NSInteger)[w intValue];
NSNumber* h = height;
NSInteger hi = (NSInteger)[h intValue];
CGSize bounds = CGSizeMake(wi, hi); CGSize bounds = CGSizeMake(wi, hi);
callback(@[[svg getDataURLwithBounds:bounds]]); callback(@[[svg getDataURLwithBounds:bounds]]);
}
} else { } else {
RCTLogError(@"Invalid svg returned frin registry, expecting RNSVGSvgView, got: %@", view); RCTLogError(@"Invalid svg returned frin registry, expecting RNSVGSvgView, got: %@", view);
} }