Skip to content

Commit

Permalink
Only enable export buttons if options are in a valid state
Browse files Browse the repository at this point in the history
  • Loading branch information
orange-elephant committed Dec 20, 2022
1 parent bf825df commit d3e5472
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 d3e5472

Please sign in to comment.