Skip to content

Instantly share code, notes, and snippets.

@rodgarcialima
Last active August 23, 2018 18:27
Show Gist options
  • Save rodgarcialima/95474a53f87501ab5fc8c208d9f90f29 to your computer and use it in GitHub Desktop.
Save rodgarcialima/95474a53f87501ab5fc8c208d9f90f29 to your computer and use it in GitHub Desktop.
AsyncTask
package rodgarcialima.task;
public class AsyncTaskActivity {
private void download() {
new Task1<Integer, List<Product>>(
top -> { // receive 10 here
Response<List<Product>> response = apiService.getProducts(top).execute();
if (response.isSuccessful()) {
return response.body();
}
return new ArrayList<>();
},
result -> {
productsAdapter = new ProductsAdapter(result, MainActivity.this);
recyclerView.setAdapter(productsAdapter);
},
error -> Toast.makeText(MainActivity.this, "Error: " + error.getMessage(), Toast.LENGTH_SHORT).show()
).execute(10); // return top 10 products
}
}
package rodgarcialima.task;
import android.os.AsyncTask;
public class Task1<Param, Result> extends AsyncTask<Param, Void, Result> {
private TaskExecutor<Param, Result> taskExecutor;
private TaskResult<Result> taskResult;
private TaskError taskError;
private Throwable throwable = null;
public Task1(TaskExecutor<Param, Result> taskExecutor,
TaskResult<Result> taskResult,
TaskError taskError) {
this.taskExecutor = taskExecutor;
this.taskResult = taskResult;
this.taskError = taskError;
}
@Override
protected Result doInBackground(Param[] params) {
Result res = null;
try {
res = this.taskExecutor.onExecute(params[0]);
} catch (Throwable throwable) {
this.throwable = throwable;
}
return res;
}
@Override
protected void onPostExecute(Result result) {
// If we have an error
if (this.throwable != null) {
this.taskError.onError(this.throwable);
return;
}
// All good
this.taskResult.onResult(result);
}
}
package rodgarcialima.task;
import android.os.AsyncTask;
public class Task2<Param, Progress, Result> extends AsyncTask<Param, Progress, Result> {
private TaskExecutor<Param, Result> taskExecutor;
private TaskProgress<Progress> taskProgress;
private TaskResult<Result> taskResult;
private TaskError taskError;
private Throwable throwable = null;
public Task2(TaskExecutor<Param, Result> taskExecutor,
TaskProgress<Progress> taskProgress,
TaskResult<Result> taskResult,
TaskError taskError) {
this.taskExecutor = taskExecutor;
this.taskProgress = taskProgress;
this.taskResult = taskResult;
this.taskError = taskError;
}
@Override
protected Result doInBackground(Param[] params) {
Result res = null;
try {
res = this.taskExecutor.onExecute(params[0]);
} catch (Throwable throwable) {
this.throwable = throwable;
}
return res;
}
@Override
protected void onPostExecute(Result result) {
// If we have an error
if (this.throwable != null) {
this.taskError.onError(this.throwable);
return;
}
// All good
this.taskResult.onResult(result);
}
@Override
protected void onProgressUpdate(Progress[] progress) {
if (this.taskProgress != null) {
this.taskProgress.onProgress(progress[0]);
}
}
}
package rodgarcialima.task;
public interface TaskError {
void onError(Throwable error);
}
package rodgarcialima.task;
public interface TaskExecutor<Param, Result> {
Result onExecute(Param param) throws Throwable;
}
package rodgarcialima.task;
public interface TaskProgress<T> {
void onProgress(T progress);
}
package rodgarcialima.task;
public interface TaskResult<Result> {
void onResult(Result result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment