fresco icon indicating copy to clipboard operation
fresco copied to clipboard

SimpleDraweeView getImageUri

Open pavlospt opened this issue 11 years ago • 11 comments

I think that this is a quite simple task and a logical request.

Can the SimpleDraweeView return the currently loaded image Uri?

Thanks in advance

pavlospt avatar Apr 17 '15 10:04 pavlospt

This is a good question. I've spent a fair amount of time thinking about whether to expose this or not, and concluded that it is better not to do that. Namely, SimpleDraweeView can also accept a DraweeController, in which case it would be hard to get the URI from the controller. The reason for that is that even though the custom controller is usually built by setting an URI and some other additional options, the controller can also be built with multiple URIs set (http://frescolib.org/docs/requesting-multiple-images.html#_) or even with a custom DataSource. In those cases it is not clear what URI should SimpleDraweeView report. So, for the sake of consistency, I've decided not to expose that.

plamenko avatar Apr 18 '15 00:04 plamenko

There could be a case of exposing it only on situations when the simpledraweeview has got the image by explicitly calling setImageUri. On Apr 18, 2015 03:42, "Ognjen Dragoljevic" [email protected] wrote:

Closed #161 https://github.com/facebook/fresco/issues/161.

— Reply to this email directly or view it on GitHub https://github.com/facebook/fresco/issues/161#event-284343551.

pavlospt avatar Apr 18 '15 07:04 pavlospt

Yes, but then someone will build a controller, again "just with the uri", and ask why SimpleDraweeView.getImageURI doesn't work. We can reconsider adding this functionality, but can you give us some more context on why do you need it?

plamenko avatar Apr 18 '15 12:04 plamenko

First of all I checked the multi-images Controllers that you said, and noticed that they are mainly for high-low res and cases where you need a local image till you receive the one from network etc.

The use case I stepped into and made me state this issue, was the following: I had an ArrayList of Uris which I used to feed the SimpleDraweeView, instead of me having to use another variable to keep the Uri that is currently being shown in the SimpleDraweeView, i thought that if there was a method such as "getImageUri()" I could easily detect the Uri being shown and iterate to get the next one when someone clicks on the view.

I don't think that i am the only one out there being in that kind of use case.

As far as the Controller is concerned "getImageUri()" can simply be overrided and return an array of Uris instead of a single one. So there can be 2 distinct methods with the same name but different signature. An array can consist of only one item in case someone uses a Controller and makes a cal to "getImageUri()" while he/she has fed the Controller with a single Uri.

Let me know what you think.

On Sat, Apr 18, 2015 at 3:51 PM, Ognjen Dragoljevic < [email protected]> wrote:

Yes, but then someone will build a controller, again "just with the uri", and ask why SimpleDraweeView.getImageURI doesn't work. We can reconsider adding this functionality, but can you give us some more context on why do you need it?

— Reply to this email directly or view it on GitHub https://github.com/facebook/fresco/issues/161#issuecomment-94163319.

[image: --] Pavlos P. Tournaris [image: http://] http://about.me/pavlospt

Android Developer & Software Engineer

pavlospt avatar Apr 18 '15 13:04 pavlospt

It seems that you have some sort of slide show and to display the next image you need to know what is the currently displayed one. Given that you already need some extra data associated with your SimpleDraweeView, namely the list of the URIs you are iterating over, I see no reason why not having another mCurrentUri or mCurrentIndex field next to that mUris field, wherever it is being stored. What am I missing?

When it comes to controllers, "getImageUri() can simply be overridden", I'm afraid, is not possible. Controller does not know about URIs. All controller knows about is a Supplier<DataSource> that it uses when it needs to get the image(s). DataSource is like a Java Future, except that it can return multiple results. And this is all controller cares about (the images its data source returns). It does not care about how those images get there and what is their source (be it an URI, be it multiple URIs, or be it some completely different source that has nothing to do with URIs).

I know that it seems simple and it is very tempting to just add those methods there, but I strongly believe that this would be a bad design choice in the long run. There is no need to bloat the controller's interface. Narrow interfaces help keeping the design cleaner and make maintenace and refactoring easier in the long run. There has to be a really good reason to add yet another method, (let alone a several of them) to an interface.

Now to get back to your example, even if there is getUri method, I would advise you not to use it for your usecase. It doesn't seem right from a design point of view. For argument sake, suppose that you need some additional state along with the list of uris and the currently selected one. How would you add that? SimpleDraweeView would definitely not be a right place to store that data, so you might as well make your design flexible right now and create a class to store all those things.

That being said, the usecase you explained is not convincing enough to break the ancapsulation and degrade the interfaces.

plamenko avatar Apr 19 '15 13:04 plamenko

Yes my usecase is like a slideshow at this moment! If you think that this is a non-fit usecase for something like this, i won't disagree on it! Glad to have that discussed and thought out!

Have a nice day!

On Sun, Apr 19, 2015 at 4:53 PM, Ognjen Dragoljevic < [email protected]> wrote:

It seems that you have some sort of slide show and to display the next image you need to know what is the currently displayed one. Given that you already need some extra data associated with your SimpleDraweeView, namely the list of the URIs you are iterating over, I see no reason why not having another mCurrentUri or mCurrentIndex field next to that mUris field, wherever it is being stored. What am I missing?

When it comes to controllers, "getImageUri() can simply be overridden", I'm afraid, is not possible. Controller does not know about URIs. All controller knows about is a Supplier that it uses when it needs to get the image(s). DataSource is like a Java Future, except that it can return multiple results. And this is all controller cares about (the images its data source returns). It does not care about how those images get there and what is their source (be it an URI, be it multiple URIs, or be it some completely different source that has nothing to do with URIs).

I know that it seems simple and it is very tempting to just add those methods there, but I strongly believe that this would be a bad design choice in the long run. There is no need to bloat the controller's interface. Narrow interfaces help keeping the design cleaner and make maintenace and refactoring easier in the long run. There has to be a really good reason to add yet another method, (let alone a several of them) to an interface.

Now to get back to your example, even if there is getUri method, I would advise you not to use it for your usecase. It doesn't seem right from a design point of view. For argument sake, suppose that you need some additional state along with the list of uris and the currently selected one. How would you add that? SimpleDraweeView would definitely not be a right place to store that data, so you might as well make your design flexible right now and create a class to store all those things.

That being said, the usecase you explained is not convincing enough to break the ancapsulation and degrade the interfaces.

— Reply to this email directly or view it on GitHub https://github.com/facebook/fresco/issues/161#issuecomment-94278713.

[image: --] Pavlos P. Tournaris [image: http://] http://about.me/pavlospt

Android Developer & Software Engineer

pavlospt avatar Apr 19 '15 14:04 pavlospt

You're welocme.

plamenko avatar Apr 19 '15 14:04 plamenko

how about a case where you have to check if the current displayed image is different then a later available one? Example: profile pictures. Primary profile pictures can change. It would be nice to animate IF such a change exists. To determine if such a change exists, one needs the .getImageURI() method.

dmmh avatar Aug 17 '16 17:08 dmmh

and by design it's generally a bad idea to have only half of getter/ setters

dmmh avatar Aug 17 '16 17:08 dmmh

        if (!user_photo.getImageURI().equals(photos.get(0).USER_PHOTO_ORIGINAL_URL)) {
            user_photo.setImageURI(photos.get(0).USER_PHOTO_ORIGINAL_URL);
            user_photo.startAnimation(AnimationUtils.loadAnimation(CONTEXT, android.R.anim.fade_in));
        } else {
            // let's do nothing
        }

dmmh avatar Aug 17 '16 17:08 dmmh

this also can get fixed by storing everything in local variables, I know. But it's so sloppy

dmmh avatar Aug 17 '16 17:08 dmmh