extract MultiSourceHelper

Reviewed By: andreicoman11

Differential Revision: D3505224
This commit is contained in:
Felix Oghina
2016-07-01 10:46:09 -07:00
committed by Ahmed El-Helw
parent 9a28701bd8
commit 1b77f1a372
3 changed files with 48 additions and 121 deletions
@@ -11,8 +11,8 @@ package com.facebook.react.flat;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.LinkedList;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
@@ -31,6 +31,9 @@ import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.views.image.ImageLoadEvent;
import com.facebook.react.views.image.ImageResizeMode;
import com.facebook.react.views.image.ReactImageView;
import com.facebook.react.views.imagehelper.ImageSource;
import com.facebook.react.views.imagehelper.MultiSourceHelper;
import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult;
/**
* DrawImageWithDrawee is DrawCommand that can draw a local or remote image.
@@ -39,7 +42,7 @@ import com.facebook.react.views.image.ReactImageView;
/* package */ final class DrawImageWithDrawee extends AbstractDrawCommand
implements DrawImage, ControllerListener {
private @Nullable Map<String, Double> mSources;
private final List<ImageSource> mSources = new LinkedList<>();
private @Nullable Context mContext;
private @Nullable DraweeRequestHelper mRequestHelper;
private @Nullable PorterDuffColorFilter mColorFilter;
@@ -53,25 +56,25 @@ import com.facebook.react.views.image.ReactImageView;
@Override
public boolean hasImageRequest() {
return mSources != null && !mSources.isEmpty();
return !mSources.isEmpty();
}
@Override
public void setSource(Context context, @Nullable ReadableArray sources) {
if (mSources == null) {
mSources = new HashMap<>();
}
mSources.clear();
if (sources != null && sources.size() != 0) {
// Optimize for the case where we have just one uri, case in which we don't need the sizes
if (sources.size() == 1) {
mSources.put(sources.getMap(0).getString("uri"), 0.0d);
ReadableMap source = sources.getMap(0);
mSources.add(new ImageSource(context, source.getString("uri")));
} else {
for (int idx = 0; idx < sources.size(); idx++) {
ReadableMap source = sources.getMap(idx);
mSources.put(
mSources.add(new ImageSource(
context,
source.getString("uri"),
source.getDouble("width") * source.getDouble("height"));
source.getDouble("width"),
source.getDouble("height")));
}
}
}
@@ -227,39 +230,30 @@ import com.facebook.react.views.image.ReactImageView;
}
private void computeRequestHelper() {
String[] imageSources = getImageSources();
if (imageSources == null) {
MultiSourceResult multiSource = MultiSourceHelper.getBestSourceForSize(
Math.round(getRight() - getLeft()),
Math.round(getBottom() - getTop()),
mSources);
ImageSource source = multiSource.getBestResult();
ImageSource cachedSource = multiSource.getBestResultInCache();
if (source == null) {
mRequestHelper = null;
return;
}
ImageRequest imageRequest =
ImageRequestHelper.createImageRequest(Assertions.assertNotNull(mContext), imageSources[0]);
ImageRequest imageRequest = ImageRequestHelper.createImageRequest(
Assertions.assertNotNull(mContext),
source.getSource());
ImageRequest cachedImageRequest = null;
if (imageSources.length >= 2 && imageSources[1] != null) {
if (cachedSource != null) {
cachedImageRequest = ImageRequestHelper.createImageRequest(
Assertions.assumeNotNull(mContext),
imageSources[1]);
cachedSource.getSource());
}
mRequestHelper = new
DraweeRequestHelper(Assertions.assertNotNull(imageRequest), cachedImageRequest, this);
}
private @Nullable String[] getImageSources() {
if (mSources == null || mSources.isEmpty()) {
return null;
}
if (hasMultipleSources()) {
final double targetImageSize = (getRight() - getLeft()) * (getBottom() - getTop());
return MultiSourceImageHelper.getImageSourceFromMultipleSources(targetImageSize, mSources);
}
return new String[]{mSources.keySet().iterator().next()};
}
private boolean hasMultipleSources() {
return Assertions.assertNotNull(mSources).size() > 1;
}
private boolean shouldDisplayBorder() {
return mBorderColor != 0 || mBorderRadius >= 0.5f;
}