eltos / SimpleDialogFragments

An Android library to create dialogs with ease and handle user interaction reliably, using fragments and material design.
Apache License 2.0
119 stars 17 forks source link

Add progress dialog #71

Closed eltos closed 3 years ago

eltos commented 3 years ago

closes #69


Wiki page draft:

Progress dialog

extends CustomViewDialog

A dialog showing a progress

Building dialogs

The progress is indeterminate by default, but can be updated either before the dialog is shown or while it is being shown. In addition, an AsyncTask can easily be connected to the dialog, such that the progress and state is automatically synced with the task.
By default, a cancel button is shown, but the dialog can not be dismissed by (accidentally) clicking outside it or via the back button. This button can be removed via .neut(null).
The dialog is meant to be dismissed by calling .dismiss(), but an auto-dismiss option exists.

Example

SimpleProgressDialog.bar()
                    .title(R.string.login)
                    .msg(R.string.creating_user_profile_wait)
                    .show(this, PROGRESS_DIALOG);
// use this to access the dialog after rotation changes
FragmentManager fm = getSupportFragmentManager();
SimpleProgressDialog dialog = (SimpleProgressDialog) 
    fm.findFragmentByTag(PROGRESS_DIALOG);

dialog.updateProgress(13, 100);
dialog.updateInfoText("Working…");
dialog.dismiss();

More examples can be found in the testApp

Available customizations

See also CustomViewDialog

Updating the progress

These methods can be called either before the dialog is shown or afterwards.

Progress .updateProgress(int progress)
.updateProgress(int progress, int max)
.updateMax(int max)
.updateSecondaryProgress(int progress)
.updateIndeterminate()
.updateFinished()

Text .updateProgressText(String text) for a short text, e.g. 5% or 8MB (by default, this shows the progress unless manually set)
.updateInfoText(String text) for a longer text, e.g. "Loading data…" or "5 seconds left"

Working with AsyncTask

Derive a task from SimpleProgressTask.

static class MyProgressTask extends SimpleProgressTask<Void, Integer, Void> {
    @Override
    protected Void doInBackground(Void... voids) {
        publishProgress(10); // 10% progress
        publishProgress(100, 500);  // 100/500 = 20% progress
        publishProgress(20, 100, 40);  // 20% primary and 40% secondary progress
        // ...
    }
    // if required, the full spectra of `mDialog.updateXXX(...)` methods can be called from a custom `onProgressUpdate` implementation
}

and link it to the dialog:

MyTask task = new MyTask();
task.execute();

SimpleProgressDialog.bar()
        .title("...")
        .msg("...")
        .task(task, cancelable, autoDismiss) // optional cancel button and auto-dismiss functionality
        .show(this);

If cancelable is set and the cancel button is pressed, the task is canceled with mayInterruptIfRunning=false, so make sure to frequently check for ìsCancelled() in your tasks doInBackground method.
If autoDismiss is set, the dialog will be dismissed once the task is finished and the onResult method will be called with which=SimpleProgressDialog.COMPLETED.

Receiving results

See SimpleDialog