diff --git a/app/src/main/java/com/beemdevelopment/aegis/ui/fragments/preferences/ImportExportPreferencesFragment.java b/app/src/main/java/com/beemdevelopment/aegis/ui/fragments/preferences/ImportExportPreferencesFragment.java index f175e3aac5..7e15f04fc4 100644 --- a/app/src/main/java/com/beemdevelopment/aegis/ui/fragments/preferences/ImportExportPreferencesFragment.java +++ b/app/src/main/java/com/beemdevelopment/aegis/ui/fragments/preferences/ImportExportPreferencesFragment.java @@ -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(); @@ -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; } } @@ -521,4 +540,8 @@ private interface FinishExportCallback { private interface StartExportCallback { void exportVault(FinishExportCallback exportCb); } + + private interface DialogStateValidator { + void enableIfValid(); + } }