Package 

Class RequestFutureTarget

  • All Implemented Interfaces:
    com.mobwith.imgmodule.manager.LifecycleListener , com.mobwith.imgmodule.request.FutureTarget , com.mobwith.imgmodule.request.RequestListener , com.mobwith.imgmodule.request.target.Target , java.util.concurrent.Future

    
    public class RequestFutureTarget<R>
     implements FutureTarget<R>, RequestListener<R>
                        

    A java.util.concurrent.Future implementation for ImageModule that can be used to load resources in a blocking manner on background threads.

    Note - Unlike most targets, RequestFutureTargets can be used once and only once. Attempting to reuse a RequestFutureTarget will probably result in undesirable behavior or exceptions. Instead of reusing objects of this class, the pattern should be:

    {@code * FutureTargettarget = null;
     * RequestManager requestManager = ImageModule.with(context);
     * try {
     *   target = requestManager
     *      .downloadOnly()
     *      .load(model)
     *      .submit();
     *   File downloadedFile = target.get();
     *   // ... do something with the file (usually throws IOException)
     * } catch (ExecutionException | InterruptedException | IOException e) {
     *   // ... bug reporting or recovery
     * } finally {
     *   // make sure to cancel pending operations and free resources
     *   if (target != null) {
     *     target.cancel(true); // mayInterruptIfRunning
     *   }
     * }
     * }
    The cancel call will cancel pending operations and make sure that any resources used are recycled.
    • Constructor Detail

      • RequestFutureTarget

        RequestFutureTarget(int width, int height)
        Constructor for a RequestFutureTarget.
    • Method Detail

      • getRequest

        @Nullable() synchronized Request getRequest()

        Retrieves the current request for this target, should not be called outside of ImageModule.

      • setRequest

         synchronized void setRequest(@Nullable() Request request)

        Sets the current request for this target to retain, should not be called outside of ImageModule.

      • cancel

         boolean cancel(boolean mayInterruptIfRunning)
      • isDone

         synchronized boolean isDone()
      • onStop

         void onStop()

        Callback for when onStop} or } is called.

      • onLoadFailed

         synchronized boolean onLoadFailed(@Nullable() ImageModuleException e, Object model, Target<R> target, boolean isFirstResource)

        Called when an exception occurs during a load, immediately before . Will only be called if we currently want to display an imagefor the given model in the given target. It is recommended to create a single instance peractivity/fragment rather than instantiate a new object for each call to {@code * ImageModule.with(fragment/activity).load()} to avoid object churn.

        It is not safe to reload this or a different model in this callback. If you need to do souse error instead.

        Although you can't start an entirely new load, it is safe to change what is displayed in the Target at this point, as long as you return {@code true} from the method to prevent onLoadFailed from being called.

        For threading guarantees, see the class comment.

        For example:

        {@code * public boolean onLoadFailed(Exception e, T model, Target target, boolean isFirstResource) { * target.setPlaceholder(R.drawable.a_specific_error_for_my_exception); * return true; // Prevent onLoadFailed from being called on the Target. * } * }
        Parameters:
        e - The maybe {@code null} exception containing information about why the request failed.
        model - The model we were trying to load when the exception occurred.
        target - The Target we were trying to load the image into.
        isFirstResource - {@code true} if this exception is for the first resource to load.
      • onResourceReady

         synchronized boolean onResourceReady(R resource, Object model, Target<R> target, DataSource dataSource, boolean isFirstResource)

        Called when a load completes successfully, immediately before .

        For threading guarantees, see the class comment.

        Parameters:
        resource - The resource that was loaded for the target.
        model - The specific model that was used to load the image.
        target - The target the model was loaded into.
        dataSource - The DataSource the resource was loaded from.
        isFirstResource - {@code true} if this is the first resource to in this load to be loadedinto the target.