Last active
January 3, 2017 23:00
-
-
Save jbruchanov/630b5a702342a358f4c4e909503c554c to your computer and use it in GitHub Desktop.
ArbiterTask
This file contains hidden or 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.smallplanet.android.planned; | |
import android.os.AsyncTask; | |
import android.support.annotation.NonNull; | |
/** | |
* Created by JBruchanov on 03/01/2017. | |
*/ | |
public class ArbiterTask<T> extends AsyncTask<Void, Void, T> { | |
interface Delegate { | |
void showProgressBar(); | |
void hideProgressBar(); | |
boolean isActive(); | |
} | |
interface Action<T> { | |
T run() throws Exception; | |
} | |
interface ResultAction<T> { | |
void run(T result) throws Exception; | |
} | |
private Delegate mDelegate; | |
private Throwable mThrowable; | |
private Action<T> mBackgroundAction; | |
private ResultAction<T> mResultAction; | |
private ResultAction<Throwable> mErrorAction; | |
private boolean mShouldCallIfInactive = false; | |
public ArbiterTask(@NonNull Delegate delegate) { | |
mDelegate = delegate; | |
} | |
@Override | |
protected void onPreExecute() { | |
mDelegate.showProgressBar(); | |
} | |
@Override | |
protected final T doInBackground(Void... params) { | |
try { | |
return mBackgroundAction.run(); | |
} catch (Throwable t) { | |
mThrowable = t; | |
return null; | |
} | |
} | |
@Override | |
protected final void onPostExecute(T result) { | |
mDelegate.hideProgressBar(); | |
if (mShouldCallIfInactive || mDelegate.isActive()) { | |
try { | |
if (mThrowable != null) { | |
mErrorAction.run(mThrowable); | |
} else { | |
mResultAction.run(result); | |
} | |
} catch (Exception e) { | |
try { | |
mErrorAction.run(e); | |
} catch (Throwable t) { | |
t.printStackTrace(); | |
throw new Error(t);//exception in error handler | |
} | |
} | |
} | |
} | |
public static <T> ArbiterTaskBuilder<T> newTask(@NonNull Delegate delegate) { | |
return new ArbiterTaskBuilder<>(delegate); | |
} | |
public static class ArbiterTaskBuilder<T> { | |
private Delegate mDelegate; | |
private Action<T> mBackgroundFunction; | |
private ResultAction<T> mResultAction; | |
private ResultAction<Throwable> mErrorAction; | |
private boolean mShouldCallIfInactive = false; | |
protected ArbiterTaskBuilder(Delegate delegate) { | |
mDelegate = delegate; | |
} | |
public ArbiterTaskBuilder<T> onBackground(Action<T> action) { | |
mBackgroundFunction = action; | |
return this; | |
} | |
public ArbiterTaskBuilder<T> onError(ResultAction<Throwable> errAction) { | |
mErrorAction = errAction; | |
return this; | |
} | |
public ArbiterTaskBuilder<T> onPost(ResultAction<T> resultAction) { | |
mResultAction = resultAction; | |
return this; | |
} | |
public ArbiterTaskBuilder<T> shouldCallOnPostIfInactive(boolean shouldCallIfInactive) { | |
mShouldCallIfInactive = shouldCallIfInactive; | |
return this; | |
} | |
public ArbiterTask<T> start() { | |
//null checks | |
ArbiterTask<T> arbiter = new ArbiterTask<>(mDelegate); | |
arbiter.mBackgroundAction = mBackgroundFunction; | |
arbiter.mResultAction = mResultAction; | |
arbiter.mErrorAction = mErrorAction; | |
arbiter.mShouldCallIfInactive = mShouldCallIfInactive; | |
arbiter.execute(); | |
return arbiter; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment