[bugfix] Fix calling toDataUrl early and with options

Enable calling toDataUrl as soon as you get a ref to the svg root.
Fix invalidation and rendering with options for width and height.
This commit is contained in:
Mikael Sand
2019-03-11 02:43:06 +02:00
parent 74b0e3d99f
commit 6bd27dfeba
7 changed files with 98 additions and 15 deletions
@@ -101,8 +101,19 @@ public class SvgView extends ReactViewGroup implements ReactCompoundView, ReactC
if (mBitmap == null) {
mBitmap = drawOutput();
}
if (mBitmap != null)
if (mBitmap != null) {
canvas.drawBitmap(mBitmap, 0, 0, null);
if (toDataUrlTask != null) {
toDataUrlTask.run();
toDataUrlTask = null;
}
}
}
private Runnable toDataUrlTask = null;
void setToDataUrlTask(Runnable task) {
toDataUrlTask = task;
}
@Override
@@ -138,6 +149,10 @@ public class SvgView extends ReactViewGroup implements ReactCompoundView, ReactC
private boolean mRendered = false;
int mTintColor = 0;
boolean isRendered() {
return mRendered;
}
private void clearChildCache() {
if (!mRendered) {
return;
@@ -298,7 +313,10 @@ public class SvgView extends ReactViewGroup implements ReactCompoundView, ReactC
getHeight(),
Bitmap.Config.ARGB_8888);
clearChildCache();
drawChildren(new Canvas(bitmap));
clearChildCache();
this.invalidate();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitmap.recycle();
@@ -312,7 +330,10 @@ public class SvgView extends ReactViewGroup implements ReactCompoundView, ReactC
height,
Bitmap.Config.ARGB_8888);
clearChildCache();
drawChildren(new Canvas(bitmap));
clearChildCache();
this.invalidate();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitmap.recycle();
@@ -28,9 +28,19 @@ class SvgViewManager extends ReactViewManager {
private static final String REACT_CLASS = "RNSVGSvgView";
private static final SparseArray<SvgView> mTagToSvgView = new SparseArray<>();
private static final SparseArray<Runnable> mTagToRunnable = new SparseArray<>();
static void setSvgView(int tag, SvgView svg) {
mTagToSvgView.put(tag, svg);
Runnable task = mTagToRunnable.get(tag);
if (task != null) {
task.run();
mTagToRunnable.delete(tag);
}
}
static void runWhenViewIsAvailable(int tag, Runnable task) {
mTagToRunnable.put(tag, task);
}
static @Nullable SvgView getSvgViewByTag(int tag) {
@@ -25,12 +25,33 @@ class SvgViewModule extends ReactContextBaseJavaModule {
return "RNSVGSvgViewManager";
}
@ReactMethod
public void toDataURL(int tag, ReadableMap options, Callback successCallback) {
static public void toDataURL(final int tag, final ReadableMap options, final Callback successCallback, final int attempt) {
SvgView svg = SvgViewManager.getSvgViewByTag(tag);
if (svg != null) {
if (svg == null) {
SvgViewManager.runWhenViewIsAvailable(tag, new Runnable() {
@Override
public void run() {
SvgView svg = SvgViewManager.getSvgViewByTag(tag);
if (svg == null) { // Should never happen
return;
}
svg.setToDataUrlTask(new Runnable() {
@Override
public void run() {
toDataURL(tag, options, successCallback, attempt + 1);
}
});
}
});
} else if (!svg.isRendered()) {
svg.setToDataUrlTask(new Runnable() {
@Override
public void run() {
toDataURL(tag, options, successCallback, attempt + 1);
}
});
} else {
if (options != null) {
successCallback.invoke(
svg.toDataURL(
@@ -43,4 +64,9 @@ class SvgViewModule extends ReactContextBaseJavaModule {
}
}
}
@ReactMethod
public void toDataURL(int tag, ReadableMap options, Callback successCallback) {
toDataURL(tag, options, successCallback, 0);
}
}