package com.webimageloader.loader;

import android.graphics.Bitmap;
import com.webimageloader.util.InputSupplier;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Future;

public class LoaderWork {
    private final Loader.Listener listener;
    private final List<Future<?>> futures;

    public LoaderWork(Loader.Listener listener) {
        this.listener = listener;
        this.futures = new ArrayList<Future<?>>();
    }

    public void cancel() {
        synchronized (futures) {
            for (Future<?> future : futures) {
                future.cancel(false);
            }
        }
    }

    public void start(List<Loader> loaderChain, LoaderRequest request) {
        Iterator<Loader> it = loaderChain.iterator();
        Loader loader = it.next();

        loader.load(new Manager(it, listener), request);
    }

    public class Manager {
        private Iterator<Loader> chain;
        private Loader.Listener listener;

        private Loader nextLoader;

        Manager(Iterator<Loader> chain, Loader.Listener listener) {
            this.chain = chain;
            this.listener = listener;

            if (chain.hasNext()) {
                nextLoader = chain.next();
            }
        }

        public void addFuture(Future<?> future) {
            synchronized (futures) {
                futures.add(future);
            }
        }

        public void next(LoaderRequest request) {
            // Load using old listener
            next(request, listener);
        }

        public void next(LoaderRequest request, Loader.Listener listener) {
            Manager nextManager = new Manager(chain, listener);
            nextLoader.load(nextManager, request);
        }

        public void deliverStream(InputSupplier is, Metadata metadata) {
            listener.onStreamLoaded(is, metadata);
        }

        public void deliverBitmap(Bitmap b, Metadata metadata) {
            listener.onBitmapLoaded(b, metadata);
        }

        public void deliverError(Throwable t) {
            listener.onError(t);
        }

        public void deliverNotMotified(Metadata metadata) {
            listener.onNotModified(metadata);
        }
    }
}

@@ -337,16 +337,65 @@
 tures;%0A%0A
+    private volatile boolean cancelled = false;%0A%0A
     publ
@@ -551,24 +551,51 @@
  cancel() %7B%0A
+        cancelled = true;%0A%0A
         sync
@@ -1338,32 +1338,112 @@
    %7D%0A        %7D%0A%0A
+        public boolean isCancelled() %7B%0A            return cancelled;%0A        %7D%0A%0A
         public v
@@ -1780,32 +1780,66 @@
 ner listener) %7B%0A
+            if (!cancelled) %7B%0A    
             Mana
@@ -1890,32 +1890,36 @@
 r);%0A            
+    
 nextLoader.load(
@@ -1937,24 +1937,38 @@
 , request);%0A
+            %7D%0A
         %7D%0A%0A 
@@ -2031,32 +2031,66 @@
 ata metadata) %7B%0A
+            if (!cancelled) %7B%0A    
             list
@@ -2116,32 +2116,46 @@
 (is, metadata);%0A
+            %7D%0A
         %7D%0A%0A     
@@ -2206,32 +2206,66 @@
 ata metadata) %7B%0A
+            if (!cancelled) %7B%0A    
             list
@@ -2290,32 +2290,46 @@
 d(b, metadata);%0A
+            %7D%0A
         %7D%0A%0A     
@@ -2367,24 +2367,58 @@
 owable t) %7B%0A
+            if (!cancelled) %7B%0A    
             
@@ -2434,24 +2434,38 @@
 onError(t);%0A
+            %7D%0A
         %7D%0A%0A 
@@ -2515,32 +2515,66 @@
 ata metadata) %7B%0A
+            if (!cancelled) %7B%0A    
             list
@@ -2595,32 +2595,46 @@
 fied(metadata);%0A
+            %7D%0A
         %7D%0A    %7D%0A

Check cancellation status in loader mananger