Skip to content

Commit

Permalink
Merge pull request #1067 from orange-elephant/export-dialog-ux
Browse files Browse the repository at this point in the history
Only enable export buttons if options are in a valid state
  • Loading branch information
alexbakker authored Dec 24, 2022
2 parents bf825df + d3e5472 commit 0eccc87
Showing 1 changed file with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,25 +201,45 @@ private void startExport() {
Button btnPos = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
Button btnNeutral = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);

DialogStateValidator stateValidator = () -> {
boolean noGroupsSelected = groupsSelection.getCheckedItems().isEmpty();
boolean validState = (checkBoxEncrypt.isChecked() || checkBoxAccept.isChecked()) &&
(checkBoxExportAllGroups.isChecked() || !noGroupsSelected);

if (noGroupsSelected && groupsSelectionLayout.getError() == null) {
CharSequence errorMsg = getString(R.string.export_no_groups_selected);
groupsSelectionLayout.setError(errorMsg);
} else if (!noGroupsSelected && groupsSelectionLayout.getError() != null) {
groupsSelectionLayout.setError(null);
groupsSelectionLayout.setErrorEnabled(false);
}

btnPos.setEnabled(validState);
btnNeutral.setEnabled(validState);
};

checkBoxEncrypt.setOnCheckedChangeListener((buttonView, isChecked) -> {
warningText.setVisibility(isChecked ? View.GONE : View.VISIBLE);
passwordInfoText.setVisibility(isChecked && isBackupPasswordSet ? View.VISIBLE : View.GONE);
checkBoxAccept.setVisibility(isChecked ? View.GONE : View.VISIBLE);
checkBoxAccept.setChecked(false);
btnPos.setEnabled(isChecked);
btnNeutral.setEnabled(isChecked);

stateValidator.enableIfValid();
});

checkBoxAccept.setOnCheckedChangeListener((buttonView, isChecked) -> {
btnPos.setEnabled(isChecked);
btnNeutral.setEnabled(isChecked);
stateValidator.enableIfValid();
});

checkBoxExportAllGroups.setOnCheckedChangeListener((button, isChecked) -> {
int visibility = isChecked ? View.GONE : View.VISIBLE;
groupsSelectionLayout.setVisibility(visibility);

stateValidator.enableIfValid();
});

groupsSelection.setOnDismissListener(stateValidator::enableIfValid);

btnPos.setOnClickListener(v -> {
dialog.dismiss();

Expand Down Expand Up @@ -257,7 +277,6 @@ private void startExport() {
if (!checkBoxExportAllGroups.isChecked()) {
_exportFilter = getVaultEntryFilter(groupsSelection);
if (_exportFilter == null) {
Toast.makeText(requireContext(), R.string.export_no_groups_selected, Toast.LENGTH_SHORT).show();
return;
}
}
Expand Down Expand Up @@ -521,4 +540,8 @@ private interface FinishExportCallback {
private interface StartExportCallback {
void exportVault(FinishExportCallback exportCb);
}

private interface DialogStateValidator {
void enableIfValid();
}
}

0 comments on commit 0eccc87

Please sign in to comment.