Skip to content

Commit

Permalink
Issue 307
Browse files Browse the repository at this point in the history
projectbuendia#307

Do not close patient_dialog_fragment on hitting "Ok". Wait until ItemCreateEvent<Patient> or ItemUpdatedEvent<Patient> to dismiss dialog.

Also, disable the Ok button while request is in flight. Re-enable if submission failed.

Would like a second set of eyes to check the event bus wiring. I am concerned about the potential for leaks here.
  • Loading branch information
mjanes committed Jun 28, 2019
1 parent 35caaed commit 157437a
Showing 1 changed file with 61 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
Expand All @@ -33,6 +34,10 @@
import org.projectbuendia.client.App;
import org.projectbuendia.client.R;
import org.projectbuendia.client.events.CrudEventBus;
import org.projectbuendia.client.events.data.ItemCreatedEvent;
import org.projectbuendia.client.events.data.ItemUpdatedEvent;
import org.projectbuendia.client.events.data.PatientAddFailedEvent;
import org.projectbuendia.client.events.data.PatientUpdateFailedEvent;
import org.projectbuendia.client.models.AppModel;
import org.projectbuendia.client.models.Patient;
import org.projectbuendia.client.models.PatientDelta;
Expand Down Expand Up @@ -65,6 +70,8 @@ public class EditPatientDialogFragment extends DialogFragment {

private LayoutInflater mInflater;

PatientSubmissionSubscriber mSubscriber;

/** Creates a new instance and registers the given UI, if specified. */
public static EditPatientDialogFragment newInstance(Patient patient) {
EditPatientDialogFragment fragment = new EditPatientDialogFragment();
Expand Down Expand Up @@ -208,19 +215,67 @@ public void focusFirstEmptyField(Dialog dialog) {
String title = args.getBoolean("new") ? getString(R.string.title_activity_patient_add) : getString(R.string.action_edit_patient);
populateFields(args);

AlertDialog dialog = new AlertDialog.Builder(getActivity())
final AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setCancelable(false) // Disable auto-cancel.
.setTitle(title)
.setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialogInterface, int i) {
onSubmit();
}
})
.setPositiveButton(getResources().getString(R.string.ok), null)
.setNegativeButton(getResources().getString(R.string.cancel), null)
.setView(fragment)
.create();

focusFirstEmptyField(dialog);

dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
final Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mSubscriber = new PatientSubmissionSubscriber(dialog, button);
mCrudEventBus.register(mSubscriber);
button.setEnabled(false);
onSubmit();
}
});
}
});

dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
mCrudEventBus.unregister(mSubscriber);
}
});

return dialog;
}

private static class PatientSubmissionSubscriber {

private final AlertDialog dialog;
private final Button button;

PatientSubmissionSubscriber(final AlertDialog dialog, final Button button) {
this.dialog = dialog;
this.button = button;
}

public void onEventMainThread(ItemCreatedEvent<Patient> event) {
dialog.dismiss();
}

public void onEventMainThread(ItemUpdatedEvent<Patient> event) {
dialog.dismiss();
}

public void onEventMainThread(PatientUpdateFailedEvent event) {
button.setEnabled(true);
}

public void onEventMainThread(PatientAddFailedEvent event) {
button.setEnabled(true);
}

}
}

0 comments on commit 157437a

Please sign in to comment.