|
| 1 | +package org.neotech.app.retainabletasksdemo.activity; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.support.annotation.NonNull; |
| 5 | +import android.support.annotation.Nullable; |
| 6 | +import android.support.design.widget.Snackbar; |
| 7 | +import android.support.v4.app.DialogFragment; |
| 8 | +import android.support.v7.app.AppCompatActivity; |
| 9 | +import android.view.View; |
| 10 | + |
| 11 | +import org.neotech.app.retainabletasksdemo.OnAlertDialogClickListener; |
| 12 | +import org.neotech.app.retainabletasksdemo.ProgressDialog; |
| 13 | +import org.neotech.app.retainabletasksdemo.R; |
| 14 | +import org.neotech.app.retainabletasksdemo.tasks.SimpleTask; |
| 15 | +import org.neotech.library.retainabletasks.*; |
| 16 | +import org.neotech.library.retainabletasks.providers.TaskActivityCompat; |
| 17 | + |
| 18 | +/** |
| 19 | + * This demo activity shows how the lifecycle library from the Google Architecture Library can be |
| 20 | + * used to hook the activity lifecycle calls to the TaskManagerLifeCycleProxy. This example also |
| 21 | + * uses the annotations just like the DemoActivityAnnotations. |
| 22 | + * <p> |
| 23 | + * Created by Rolf Smit on 8-Nov-17. |
| 24 | + */ |
| 25 | +public final class DemoActivityLifeCycleLibrary extends AppCompatActivity implements View.OnClickListener, OnAlertDialogClickListener, TaskManagerOwner { |
| 26 | + |
| 27 | + private static final String TASK_PROGRESS = "progress-dialog"; |
| 28 | + private static final String DIALOG_PROGRESS = "progress-dialog"; |
| 29 | + |
| 30 | + private ProgressDialog progressDialog; |
| 31 | + |
| 32 | + private final TaskManagerLifeCycleProxy taskManagerLifeCycleProxy = new TaskManagerLifeCycleProxy(this); |
| 33 | + |
| 34 | + public DemoActivityLifeCycleLibrary() { |
| 35 | + getLifecycle().addObserver(taskManagerLifeCycleProxy); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 40 | + super.onCreate(savedInstanceState); |
| 41 | + setContentView(R.layout.activity_demo_annotations); |
| 42 | + findViewById(R.id.button_progress_task).setOnClickListener(this); |
| 43 | + |
| 44 | + if (savedInstanceState == null) { |
| 45 | + // After starting this activity directly start the task. |
| 46 | + execute(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onClick(View v) { |
| 52 | + // On click execute the task |
| 53 | + execute(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void onDialogFragmentClick(DialogFragment fragment, int which) { |
| 58 | + getTaskManager().cancel(TASK_PROGRESS); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public TaskManager getTaskManager() { |
| 63 | + return taskManagerLifeCycleProxy.getTaskManager(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Task.Callback onPreAttach(@NonNull Task<?, ?> task) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + public void execute(){ |
| 72 | + if(!getTaskManager().isActive(TASK_PROGRESS)) { |
| 73 | + getTaskManager().execute(new SimpleTask(TASK_PROGRESS)); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @TaskAttach(TASK_PROGRESS) |
| 78 | + public void onAttach(SimpleTask task){ |
| 79 | + // Task attaches, make sure to show the progress dialog and update the progress if needed. |
| 80 | + progressDialog = ProgressDialog.showIfNotShowing(getSupportFragmentManager(), DIALOG_PROGRESS); |
| 81 | + if(task.getLastKnownProgress() != null) { |
| 82 | + progressDialog.setProgress(task.getLastKnownProgress()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @TaskProgress(TASK_PROGRESS) |
| 87 | + public void onProgress(SimpleTask task){ |
| 88 | + progressDialog.setProgress(task.getLastKnownProgress()); |
| 89 | + } |
| 90 | + |
| 91 | + // Now this is cool, we can have a single method handle both the normal onPostExecute and the |
| 92 | + // onCancelled call. |
| 93 | + @TaskPostExecute(TASK_PROGRESS) |
| 94 | + @TaskCancel(TASK_PROGRESS) |
| 95 | + public void onFinish(SimpleTask task){ |
| 96 | + progressDialog.dismiss(); |
| 97 | + if(task.isCancelled()) { |
| 98 | + Snackbar.make(findViewById(android.R.id.content), getString(R.string.toast_task_canceled, getString(R.string.task_progress_dialog)), Snackbar.LENGTH_LONG).show(); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments