Last active
August 23, 2018 18:27
-
-
Save rodgarcialima/95474a53f87501ab5fc8c208d9f90f29 to your computer and use it in GitHub Desktop.
AsyncTask
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 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 | |
} | |
} |
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 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); | |
} | |
} |
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 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]); | |
} | |
} | |
} |
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 rodgarcialima.task; | |
public interface TaskError { | |
void onError(Throwable error); | |
} |
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 rodgarcialima.task; | |
public interface TaskExecutor<Param, Result> { | |
Result onExecute(Param param) throws Throwable; | |
} |
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 rodgarcialima.task; | |
public interface TaskProgress<T> { | |
void onProgress(T progress); | |
} |
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 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