-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support custom bitmap loading with a new RequestHandler API #512
Conversation
So you can kind of do this already by wrapping the This isn't a scalable solution though so we should figure out a better way of supporting this use case. We'll talk about it internally here today and evaluate your approach along with some potential others. |
Yeah, considered wrapping Downloader but it felt a bit too hacky given that my custom code is only dealing with local images anyway i.e. wrong place for this kind of abstraction. One thing I like about the 'RequestHandler' approach is the potential for refactoring the built-in BitmapHunters around the same kind of API in the long term. But that's a whole separate discussion. |
Gentle ping :-) Any feedback? |
We're releasing 2.3 today or tomorrow. We wanted to get that out the door
|
For me there are two cases that are not really well served by the existing API:
I'm currently making use of a bit of an over-complicated My only issue is that now the |
@JayH5 I'm not sure point 2 is Picasso's responsibility. That sounds application-level logic to me. |
@JayH5 Probably worth pointing out that the API proposed in this pull request involves passing the Another way to approach point 2 could be to allow queuing URLs around the same target. Very roughly speaking, your code would look like: Picasso.with(context)
.load(local_url).or(remote_url)
.into(imageView); |
Hey folks, just wondering if you had time to discuss this yet? |
I've been thinking about this functionality from a registry approach similar to what you'd have in a library like Gson. The Through this you could register a bunch of custom URI schemes. @Provides @Singleton providePicasso(@Application Context context, OkHttpClient client) {
return new Picasso.Builder(context)
.addUriHandler(new MockUriHandler(context))
.addUriHandler(new ContactsUriHandler(context))
.addUriHandler(new GraphUriHandler(context))
.setDefaultUriHandler(new OkHttpUriHandler(client))
.build();
} final class MockSchemeHandler implements UriHandler {
@Override public boolean canHandleUri(Uri uri) {
return "mock".equals(uri.getScheme());
}
@Override public Result load(Uri uri) {
return // ...
}
} What do you think? |
@JakeWharton Looks good to me. I guess final class Result {
private final Uri sourceUri;
private final Bitmap bitmap;
private final LoadedFrom loadedFrom;
public Result(Uri sourceUri, Bitmap bitmap, LoadedFrom loadedFrom) {...}
public Uri getSourceUri() { ... }
public Bitmap getBitmap() { ... }
public LoadedFrom getLoadedFrom() {...}
} Stuffing this information in a As I mentioned before, I would be great if the You mentioned I can work on a new patch with the proposed API. Let me know if you'd prefer to implement this yourself though. |
Yeah sorry I meant a list of them and calling As to the hunters, I agree. They initially were a function of not having a pluggable system like this. It would be great to kill them and consolidate on the handler's being the source of truth. I know personally I don't have time to work on this for quite some time. Any contribution toward this feature (even a partial one) would be very welcome. |
Ok, I'll probably have some extra cycles to work on this in the next week or so. I'll keep you posted. |
Ok, here's a new patch implementing what I'm calling
I initially built the API around I originally started this as a series of smaller patches but there are too many dependencies between the |
From a quick scan it looks great. Will do a more in-depth review tonight. Thanks for taking the time to do this! |
} | ||
options.inSampleSize = sampleSize; | ||
options.inJustDecodeBounds = false; | ||
throw new IllegalStateException("Unrecognized type of URI: " + request.uri); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This message is a bit off since the full Request
is being used to pick a handler now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed this comment. Yeah, replaced URI with request.
|
||
/** | ||
* Lazily create {@link android.graphics.BitmapFactory.Options} based in given | ||
* {@link com.squareup.picasso.Request}, only instantiating them if needed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Types don't need to be fully qualified since they're imported
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Still looks great after a detailed scan. I had only minor things to say. Overall I'm excited to get this in and released because we're currently using the |
Here's an update patch with all comments addressed. |
LGTM. Once @dnkoutso reviews we'll merge. |
@@ -276,70 +276,17 @@ static void updateThreadName(Request data) { | |||
Thread.currentThread().setName(builder.toString()); | |||
} | |||
|
|||
static BitmapHunter forRequest(Context context, Picasso picasso, Dispatcher dispatcher, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am so glad this is almost dead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
\o/
LGTM. A few tiny nits. |
Thanks for the review @dnkoutso. Branch updated with suggested changes. |
Support custom bitmap loading with a new RequestHandler API
@lucasr thanks for all the work on this one! |
I'm trying to streamline all (or most of) image loading code in Firefox for Android's UI around Picasso. We load images from sources that not supported by Picasso e.g. browser tab thumbnails, app icons from Android's PackageManager, images only accessible through a local Java API or through Gecko, etc.
I really want to avoid having to patch Picasso locally in order support our specific use cases. So, here's an idea: allow Picasso users to extend the library to load bitmaps from non-standard sources. The API docs and tests should hopefully demonstrate the general idea well enough. Let me know if something is not clear.
I'm not entirely happy with the class names (RequestHandler, Decoder, etc) and the API can probably be simplified a bit. Looking for some early feedback here.
Thoughts?