Created
April 24, 2014 14:43
-
-
Save fernandocamargoai/11257230 to your computer and use it in GitHub Desktop.
LiveQueryLoader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.meubilhete.model.dao.loader; | |
import android.content.Context; | |
import android.os.Handler; | |
import android.support.v4.content.AsyncTaskLoader; | |
import android.util.Log; | |
import com.couchbase.lite.CouchbaseLiteException; | |
import com.couchbase.lite.LiveQuery; | |
import com.couchbase.lite.QueryEnumerator; | |
public abstract class LiveQueryLoader<T> extends AsyncTaskLoader<T> implements LiveQuery.ChangeListener { | |
private T object; | |
private QueryEnumerator rows; | |
private LiveQuery liveQuery; | |
private Handler handler; | |
public LiveQueryLoader(Context context, LiveQuery liveQuery){ | |
super(context); | |
this.liveQuery = liveQuery; | |
this.liveQuery.addChangeListener(this); | |
Log.w(this.getClass().getName(), "new()"); | |
} | |
@Override | |
protected void onStartLoading() { | |
Log.w(this.getClass().getName(), "onStartLoading()"); | |
if(handler == null){ | |
handler = new Handler(); | |
} | |
if (object != null) { | |
Log.w(this.getClass().getName(), "onStartLoading() - deliverResult()"); | |
deliverResult(object); | |
} | |
if (object == null || rows.isStale()) { | |
Log.w(this.getClass().getName(), "onStartLoading() - forceLoad()"); | |
forceLoad(); | |
} | |
} | |
@Override | |
public T loadInBackground() { | |
Log.w(this.getClass().getName(), "loadInBackground()"); | |
try { | |
liveQuery.waitForRows(); | |
rows = liveQuery.getRows(); | |
object = loadObjectFromRows(rows); | |
return object; | |
} catch (CouchbaseLiteException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
protected abstract T loadObjectFromRows(QueryEnumerator rows); | |
@Override | |
public void changed(LiveQuery.ChangeEvent event) { | |
Log.w(this.getClass().getName(), "changed()"); | |
rows = event.getRows(); | |
object = loadObjectFromRows(rows); | |
handler.post(new Runnable() { | |
@Override | |
public void run() { | |
deliverResult(object); | |
} | |
}); | |
} | |
@Override | |
protected void onStopLoading() { | |
Log.w(this.getClass().getName(), "onStopLoading()"); | |
// Attempt to cancel the current load task if possible. | |
cancelLoad(); | |
} | |
@Override | |
public void onCanceled(T data) { | |
Log.w(this.getClass().getName(), "onCanceled()"); | |
liveQuery.stop(); | |
} | |
@Override | |
protected void onReset() { | |
Log.w(this.getClass().getName(), "onReset()"); | |
super.onReset(); | |
// Ensure the loader is stopped | |
onStopLoading(); | |
liveQuery.removeChangeListener(this); | |
liveQuery.stop(); | |
object = null; | |
rows = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment