/*
* ____.____ __.____ ___ _____
* | | |/ _| | \ / _ \ ______ ______
* | | < | | / / /_\ \\____ \\____ \
* /\__| | | \| | / / | \ |_> > |_> >
* \________|____|__ \______/ \____|__ / __/| __/
* \/ \/|__| |__|
*
* Copyright (c) 2014-2015 Paul "Marunjar" Pretsch
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
package org.voidsink.anewjkuapp.base;
import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;
/**
* Loader which extends AsyncTaskLoaders and handles caveats as pointed out in http://code.google.com/p/android/issues/detail?id=14944.
*
* Based on CursorLoader.java in the Fragment compatibility package
*
* @param <D> data type
*/
public abstract class BaseAsyncTaskLoader<D> extends AsyncTaskLoader<D> {
private D mData;
public BaseAsyncTaskLoader(Context context) {
super(context);
}
@Override
public void deliverResult(D data) {
if (isReset()) {
// An async query came in while the loader is stopped
return;
}
mData = data;
super.deliverResult(data);
}
@Override
protected void onStartLoading() {
if (mData != null) {
deliverResult(mData);
}
if (takeContentChanged() || mData == null) {
forceLoad();
}
}
@Override
protected void onStopLoading() {
// Attempt to cancel the current load task if possible.
cancelLoad();
}
@Override
protected void onReset() {
super.onReset();
// Ensure the loader is stopped
onStopLoading();
mData = null;
}
}