vendredi 18 août 2017

Is it possible to store the return from the Glide library for use with a proxy pattern?

I'm trying to build a static method to return images for an activity which I've set to update programatically to display details on a given item (hills in Scotland). The plan is that this will include an image which I'm using Glide to retrieve from Google Maps. As it's possible that my user will be flicking between different hills, it could be quite resource and data intensive, so I was hoping to use a proxy pattern to call images when needed, and cache those already seen so they persist through the app lifecycle.

The code I've written so far is below, my main question is what kind of object will Glide be returning, and will it be possible to store it into a HashMap? Use of Object is a placeholder until I can find what I should replace it with.

I've read that Glide caches images automatically (http://ift.tt/2icAiKT), however I'm also wanting to write in code which offers a placeholder image for users with no data connection, since it's expected that the application will be used remotely. I'm not certain it's possible to check Glide's cache for a specific image, so I'd end up checking for the cached image that wasn't present, and then trying to download the image with a poor connection, causing issues for the user (maybe?)

private static HashMap<String, Object> munroImageMap = new HashMap<String, Object>();

public static void getMunroImage(Context context, String munroName, double latitude, double longitude, ImageView view) {

    Object imageObject = munroImageMap.get(munroName);

    if (imageObject != null){
        //view.setImageResource(Object);
    } else if (imageObject == null /*&& user has data connection*/) {
        String googleStreetViewImage =
                "http://ift.tt/1KlUCxN" +
                        latitude + ","
                        + longitude
                        + "&heading=151.78&pitch=-0.76&key="
                        + context.getString(R.string.google_services_api_key);

        // If possible, I'd like to put the image into the HashMap before placing it into the view.
        Glide.with(context).load(googleStreetViewImage).into(view);
    } else {
        /*view.setImageResource(placeholder image);*/
    }
}

Thanks for any help! And I'm very happy to be told this is a poor solution if there's a better way I could handle this.

Aucun commentaire:

Enregistrer un commentaire