package droidkit.sqlite;

import android.content.AsyncTaskLoader;
import android.content.Context;
import android.database.ContentObserver;
import android.support.annotation.NonNull;

import droidkit.concurrent.MainQueue;
import droidkit.io.IOUtils;
import droidkit.log.Logger;

/**
 * @author Daniel Serdyukov
 */
public class SQLiteLoader<D> extends AsyncTaskLoader<SQLiteResult<D>> {

    private final ContentObserver mObserver =
            new ContentObserver(MainQueue.getHandler()) {
                @Override
                public boolean deliverSelfNotifications() {
                    return true;
                }

                @Override
                public void onChange(boolean selfChange) {
                    onContentChanged();
                }
            };

    private final SQLiteQuery<D> mQuery;

    private SQLiteResult<D> mResult;

    public SQLiteLoader(@NonNull Context context, @NonNull Class<D> type) {
        this(context, SQLite.with(context).where(type));
    }

    public SQLiteLoader(@NonNull Context context, @NonNull SQLiteQuery<D> query) {
        super(context);
        mQuery = query;
    }

    @Override
    public SQLiteResult<D> loadInBackground() {
        final SQLiteResult<D> result = mQuery.all();
        getContext().getContentResolver().registerContentObserver(mQuery.getUri(), true, mObserver);
        return result;
    }

    @Override
    public void deliverResult(SQLiteResult<D> result) {
        if (isReset()) {
            if (result != null) {
                IOUtils.closeQuietly(result);
            }
            return;
        }
        final SQLiteResult<D> oldResult = mResult;
        mResult = result;
        if (isStarted()) {
            super.deliverResult(result);
        }
        if (oldResult != null && oldResult != result) {
            IOUtils.closeQuietly(result);
        }
    }

    @Override
    protected void onStartLoading() {
        if (mResult != null) {
            deliverResult(mResult);
        }
        if (takeContentChanged() || mResult == null) {
            forceLoad();
        }
    }

    @Override
    protected void onStopLoading() {
        getContext().getContentResolver().unregisterContentObserver(mObserver);
        cancelLoad();
    }

    @Override
    protected void onReset() {
        onStopLoading();
        if (mResult != null) {
            IOUtils.closeQuietly(mResult);
        }
        mResult = null;
    }

}

@@ -245,36 +245,8 @@
 ils;
-%0Aimport droidkit.log.Logger;
 %0A%0A/*
@@ -2127,88 +2127,8 @@
 ) %7B%0A
-        getContext().getContentResolver().unregisterContentObserver(mObserver);%0A
     
@@ -2218,24 +2218,104 @@
 pLoading();%0A
+        getContext().getContentResolver().unregisterContentObserver(mObserver);%0A
         if (

fix SQLiteLoader content cobserving issue