Created
January 18, 2015 06:56
-
-
Save Synesso/d5aa62f1f2c3364bfb21 to your computer and use it in GitHub Desktop.
Simplification of Android's AsyncTask using functionaljava
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
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