Skip to content

Commit

Permalink
Added steadyKey
Browse files Browse the repository at this point in the history
  • Loading branch information
dasfuu authored and JakeWharton committed Oct 29, 2014
1 parent 2ce8847 commit a250816
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
34 changes: 27 additions & 7 deletions picasso/src/main/java/com/squareup/picasso/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public final class Request {
* This is mutually exclusive with {@link #uri}.
*/
public final int resourceId;
/**
* Optional stable key for this request to be used instead of the URI or resource ID when
* caching. Two requests with the same value are considered to be for the same resource.
*/
public final String stableKey;
/** List of custom transformations to be applied after the built-in transformations. */
public final List<Transformation> transformations;
/** Target image width for resizing. */
Expand Down Expand Up @@ -78,12 +83,13 @@ public final class Request {
/** The priority of this request. */
public final Priority priority;

private Request(Uri uri, int resourceId, List<Transformation> transformations, int targetWidth,
int targetHeight, boolean centerCrop, boolean centerInside, float rotationDegrees,
float rotationPivotX, float rotationPivotY, boolean hasRotationPivot, Bitmap.Config config,
Priority priority) {
private Request(Uri uri, int resourceId, String stableKey, List<Transformation> transformations,
int targetWidth, int targetHeight, boolean centerCrop, boolean centerInside,
float rotationDegrees, float rotationPivotX, float rotationPivotY, boolean hasRotationPivot,
Bitmap.Config config, Priority priority) {
this.uri = uri;
this.resourceId = resourceId;
this.stableKey = stableKey;
if (transformations == null) {
this.transformations = null;
} else {
Expand Down Expand Up @@ -113,6 +119,9 @@ private Request(Uri uri, int resourceId, List<Transformation> transformations, i
sb.append(' ').append(transformation.key());
}
}
if (stableKey != null) {
sb.append(" stableKey(").append(stableKey).append(')');
}
if (targetWidth > 0) {
sb.append(" resize(").append(targetWidth).append(',').append(targetHeight).append(')');
}
Expand Down Expand Up @@ -180,6 +189,7 @@ public Builder buildUpon() {
public static final class Builder {
private Uri uri;
private int resourceId;
private String stableKey;
private int targetWidth;
private int targetHeight;
private boolean centerCrop;
Expand Down Expand Up @@ -210,6 +220,7 @@ public Builder(int resourceId) {
private Builder(Request request) {
uri = request.uri;
resourceId = request.resourceId;
stableKey = request.stableKey;
targetWidth = request.targetWidth;
targetHeight = request.targetHeight;
centerCrop = request.centerCrop;
Expand Down Expand Up @@ -265,6 +276,15 @@ public Builder setResourceId(int resourceId) {
return this;
}

/**
* Set the stable key to be used instead of the URI or resource ID when caching.
* Two requests with the same value are considered to be for the same resource.
*/
public Builder stableKey(String stableKey) {
this.stableKey = stableKey;
return this;
}

/**
* Resize the image to the specified size in pixels.
* Use 0 as desired dimension to resize keeping aspect ratio.
Expand Down Expand Up @@ -407,9 +427,9 @@ public Request build() {
if (priority == null) {
priority = Priority.NORMAL;
}
return new Request(uri, resourceId, transformations, targetWidth, targetHeight, centerCrop,
centerInside, rotationDegrees, rotationPivotX, rotationPivotY, hasRotationPivot, config,
priority);
return new Request(uri, resourceId, stableKey, transformations, targetWidth, targetHeight,
centerCrop, centerInside, rotationDegrees, rotationPivotX, rotationPivotY,
hasRotationPivot, config, priority);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,15 @@ public RequestCreator config(Bitmap.Config config) {
return this;
}

/**
* Sets the stable key for this request to be used instead of the URI or resource ID when
* caching. Two requests with the same value are considered to be for the same resource.
*/
public RequestCreator stableKey(String stableKey) {
data.stableKey(stableKey);
return this;
}

/**
* Set the priority of this request.
* <p>
Expand Down
5 changes: 4 additions & 1 deletion picasso/src/main/java/com/squareup/picasso/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ static String createKey(Request data) {
}

static String createKey(Request data, StringBuilder builder) {
if (data.uri != null) {
if (data.stableKey != null) {
builder.ensureCapacity(data.stableKey.length() + KEY_PADDING);
builder.append(data.stableKey);
} else if (data.uri != null) {
String path = data.uri.toString();
builder.ensureCapacity(path.length() + KEY_PADDING);
builder.append(path);
Expand Down
14 changes: 14 additions & 0 deletions picasso/src/test/java/com/squareup/picasso/RequestCreatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import static com.squareup.picasso.RemoteViewsAction.AppWidgetAction;
import static com.squareup.picasso.RemoteViewsAction.NotificationAction;
import static com.squareup.picasso.TestUtils.BITMAP_1;
import static com.squareup.picasso.TestUtils.STABLE_1;
import static com.squareup.picasso.TestUtils.STABLE_URI_KEY_1;
import static com.squareup.picasso.TestUtils.TRANSFORM_REQUEST_ANSWER;
import static com.squareup.picasso.TestUtils.URI_1;
import static com.squareup.picasso.TestUtils.URI_KEY_1;
Expand Down Expand Up @@ -745,4 +747,16 @@ public void nullKeyTransformationInvalid() {
} catch (IllegalArgumentException ignored) {
}
}

@Test public void imageViewActionWithStableyKey() throws Exception {
new RequestCreator(picasso, URI_1, 0).stableKey(STABLE_1).into(mockImageViewTarget());
verify(picasso).enqueueAndSubmit(actionCaptor.capture());
assertThat(actionCaptor.getValue().getKey()).isEqualTo(STABLE_URI_KEY_1);
}

@Test public void imageViewActionWithStableKeyNull() throws Exception {
new RequestCreator(picasso, URI_1, 0).stableKey(null).into(mockImageViewTarget());
verify(picasso).enqueueAndSubmit(actionCaptor.capture());
assertThat(actionCaptor.getValue().getKey()).isEqualTo(URI_KEY_1);
}
}
2 changes: 2 additions & 0 deletions picasso/src/test/java/com/squareup/picasso/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ class TestUtils {
};
static final Uri URI_1 = Uri.parse("http://example.com/1.png");
static final Uri URI_2 = Uri.parse("http://example.com/2.png");
static final String STABLE_1 = "stableExampleKey1";
static final String URI_KEY_1 = createKey(new Request.Builder(URI_1).build());
static final String URI_KEY_2 = createKey(new Request.Builder(URI_2).build());
static final String STABLE_URI_KEY_1 = createKey(new Request.Builder(URI_1).stableKey(STABLE_1).build());
static final Bitmap VIDEO_THUMBNAIL_1 = Bitmap.createBitmap(10, 10, null);
static final Bitmap IMAGE_THUMBNAIL_1 = Bitmap.createBitmap(20, 20, null);
static final Bitmap BITMAP_1 = Bitmap.createBitmap(10, 10, null);
Expand Down

0 comments on commit a250816

Please sign in to comment.