
package com.uwetrottmann.androidutils;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;

import java.util.List;

/**
 * A generic {@link AsyncTaskLoader} loading any {@link List} or single object
 * of things (beware for e.g. Cursors you need to override onReleaseResources in
 * a meaningful way). It takes care of delivering and resetting results, so you
 * only have to implement <code>loadInBackground()</code>.
 */
public abstract class GenericSimpleLoader<T> extends AsyncTaskLoader<T> {

    protected T mItems;

    public GenericSimpleLoader(Context context) {
        super(context);
    }

    /**
     * Called when there is new data to deliver to the client. The super class
     * will take care of delivering it; the implementation here just adds a
     * little more logic.
     */
    @Override
    public void deliverResult(T items) {
        if (isReset()) {
            // An async query came in while the loader is stopped. We
            // don't need the result.
            if (items != null) {
                onReleaseResources(items);
            }
        }
        T oldItems = mItems;
        mItems = items;

        if (isStarted()) {
            // If the Loader is currently started, we can immediately
            // deliver its results.
            super.deliverResult(items);
        }

        if (oldItems != null) {
            onReleaseResources(oldItems);
        }
    }

    @Override
    protected void onStartLoading() {
        if (mItems != null) {
            deliverResult(mItems);
        } else {
            forceLoad();
        }
    }

    /**
     * Handles a request to stop the Loader.
     */
    @Override
    protected void onStopLoading() {
        // Attempt to cancel the current load task if possible.
        cancelLoad();
    }

    /**
     * Handles a request to cancel a load.
     */
    @Override
    public void onCanceled(T items) {
        super.onCanceled(items);

        onReleaseResources(items);
    }

    /**
     * Handles a request to completely reset the Loader.
     */
    @Override
    protected void onReset() {
        super.onReset();

        // Ensure the loader is stopped
        onStopLoading();

        // At this point we can release resources
        if (mItems != null) {
            onReleaseResources(mItems);
            mItems = null;
        }
    }

    /**
     * Helper function to take care of releasing resources associated with an
     * actively loaded data set.
     */
    protected void onReleaseResources(T items) {
        // For simple items there is nothing to do. For something
        // like a Cursor, we would close it here.
    }

}

@@ -117,17 +117,16 @@
 Loader;%0A
-%0A
 import j
@@ -186,96 +186,399 @@
 er%7D 
-loading any %7B@link List%7D or single object%0A * of thi
+suitable for loading any single object or %7B@link List%7D of%0A * objects. It takes care of delivering and resetting results, so you only have to implement%0A * %3Ccode%3EloadInBackground()%3C/code%3E.%0A *%0A * %3Cp%3ETo automatically reload data on content cha
 ng
+e
 s 
-(beware for e.g. Cursors you need
+implement some form of content observer which%0A * just calls this loaders %7B@link #onContentChanged()%7D method if data has changed.%0A *%0A * %3Cp%3EMake sure
  to 
@@ -586,16 +586,24 @@
 verride 
+%7B@link #
 onReleas
@@ -616,14 +616,12 @@
 rces
+%7D
  in
-%0A *
  a m
@@ -637,126 +637,218 @@
  way
-). It takes care of delivering and resetting results, so you%0A * only have to implement %3Ccode%3EloadInBackground()%3C/code%3E
+ to clean up resources%0A * associated with the actively loaded data set. If you have to use a %7B@link%0A * android.database.Cursor%7D you should probably use %7B@link android.support.v4.content.CursorLoader%7D%0A * instead
 .%0A *
@@ -940,18 +940,17 @@
 ected T 
-mI
+i
 tems;%0A%0A 
@@ -1115,23 +1115,16 @@
 er class
-%0A     *
  will ta
@@ -1125,32 +1125,39 @@
 ill take care of
+%0A     *
  delivering it; 
@@ -1191,23 +1191,16 @@
 t adds a
-%0A     *
  little 
@@ -1265,17 +1265,20 @@
 esult(T 
-i
+newI
 tems) %7B%0A
@@ -1426,17 +1426,20 @@
     if (
-i
+newI
 tems != 
@@ -1473,33 +1473,36 @@
 eleaseResources(
-i
+newI
 tems);%0A         
@@ -1537,18 +1537,22 @@
 Items = 
-mI
+this.i
 tems;%0A  
@@ -1561,18 +1561,25 @@
     
-mI
+this.i
 tems = 
-i
+newI
 tems
@@ -1746,17 +1746,20 @@
 rResult(
-i
+newI
 tems);%0A 
@@ -1915,34 +1915,33 @@
 ) %7B%0A        if (
-mI
+i
 tems != null) %7B%0A
@@ -1966,18 +1966,17 @@
 rResult(
-mI
+i
 tems);%0A 
@@ -1987,13 +1987,59 @@
    %7D
- else
+%0A        if (takeContentChanged() %7C%7C items == null)
  %7B%0A 
@@ -2407,34 +2407,33 @@
     
-super.onCanceled(items);%0A%0A
+if (items != null) %7B%0A    
     
@@ -2459,24 +2459,34 @@
 ces(items);%0A
+        %7D%0A
     %7D%0A%0A    /
@@ -2752,18 +2752,17 @@
     if (
-mI
+i
 tems != 
@@ -2800,18 +2800,17 @@
 sources(
-mI
+i
 tems);%0A 
@@ -2824,10 +2824,9 @@
     
-mI
+i
 tems
@@ -2936,23 +2936,16 @@
  with an
-%0A     *
  activel
@@ -2953,24 +2953,31 @@
  loaded data
+%0A     *
  set.%0A     *
@@ -3149,11 +3149,10 @@
 .%0A    %7D%0A
-%0A
 %7D%0A

Clean up GenericSimpleLoader, support observers.