Skip to content

Instantly share code, notes, and snippets.

@Synesso
Created January 18, 2015 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Synesso/d5aa62f1f2c3364bfb21 to your computer and use it in GitHub Desktop.
Save Synesso/d5aa62f1f2c3364bfb21 to your computer and use it in GitHub Desktop.
Simplification of Android's AsyncTask using functionaljava
import android.app.Activity;
import android.os.AsyncTask;
import fj.F;
import fj.function.Effect1;
public class Async<A, B> extends AsyncTask<A, Void, B> {
private final F<A, B> f;
private Effect1<B> e = new Effect1<B>() {
@Override
public void f(B b) {
}
};
public Async(F<A, B> f) {
this.f = f;
}
@Override
protected void onPostExecute(B b) {
e.f(b);
}
@Override
protected final B doInBackground(A... params) {
return f.f(params[0]);
}
public <C> Async<A, C> andThen(final F<B, C> g) {
return new Async<>(new F<A,C>() {
@Override
public C f(A a) {
return g.f(f.f(a));
}
});
}
public Async<A, B> andFinally(final Effect1<B> effect) {
this.e = effect;
return this;
}
public Async<A, B> andFinallyInTheUI(final Activity a, final Effect1<B> effect) {
this.e = new Effect1<B>() {
@Override
public void f(final B b) {
a.runOnUiThread(new Runnable() {
@Override
public void run() {
effect.f(b);
}
});
}
};
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment