Remove duplicated logics

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-07-02 15:03:19 +02:00 committed by Alper Öztürk
parent 5ae25ec56d
commit b5e7946569
4 changed files with 32 additions and 18 deletions

View file

@ -22,7 +22,7 @@ object FileNameValidator {
"LPT¹", "LPT²", "LPT³"
)
fun isValid(name: String, context: Context): String? {
fun isValid(name: String, context: Context, fileNames: MutableSet<String>? = null): String? {
val invalidCharacter = name.find { it.toString().matches(reservedWindowsChars) || it.toString().matches(reservedUnixChars) }
if (invalidCharacter != null) {
return context.getString(R.string.file_name_validator_error_invalid_character, invalidCharacter)
@ -40,6 +40,14 @@ object FileNameValidator {
return context.getString(R.string.filename_empty)
}
if (isFileNameAlreadyExist(name, fileNames ?: mutableSetOf())) {
return context.getString(R.string.file_already_exists)
}
return null
}
fun isFileHidden(name: String): Boolean = !TextUtils.isEmpty(name) && name[0] == '.'
private fun isFileNameAlreadyExist(name: String, fileNames: MutableSet<String>): Boolean = fileNames.contains(name)
}

View file

@ -248,18 +248,25 @@ class ChooseTemplateDialogFragment : DialogFragment(), View.OnClickListener, Tem
val errorMessage = FileNameValidator.isValid(name, requireContext())
val error = when {
name.isEmpty() || isNameJustExtension -> null
name[0] == '.' -> getText(R.string.hidden_file_name_warning)
isNameJustExtension -> null
errorMessage != null -> errorMessage
fileNames.contains(name) -> getText(R.string.file_already_exists)
else -> null
}
positiveButton?.isEnabled = (error == null)
positiveButton?.isClickable = (error == null)
binding.filenameContainer.isErrorEnabled = (error != null)
if (error != null) {
binding.filenameContainer.error = error
if (error != null || name.equals(DOT + selectedTemplate?.extension, ignoreCase = true)) {
binding.filenameContainer.error = error ?: getString(R.string.enter_filename)
positiveButton?.isEnabled = false
positiveButton?.isClickable = false
binding.filenameContainer.isErrorEnabled = true
} else if (FileNameValidator.isFileHidden(name)) {
positiveButton?.isEnabled = true
positiveButton?.isClickable = true
binding.filenameContainer.isErrorEnabled = true
binding.filenameContainer.error = getText(R.string.hidden_file_name_warning)
} else {
positiveButton?.isEnabled = true
positiveButton?.isClickable = true
binding.filenameContainer.isErrorEnabled = false
}
}

View file

@ -117,13 +117,11 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
private fun checkFileNameAfterEachType(fileNames: MutableSet<String>) {
val newFileName = binding.userInput.text?.toString()?.trim() ?: ""
val errorMessage: String? = FileNameValidator.isValid(newFileName, requireContext())
val errorMessage: String? = FileNameValidator.isValid(newFileName, requireContext(), fileNames)
val error = when {
newFileName.isEmpty() -> null
newFileName[0] == '.' -> getString(R.string.hidden_file_name_warning)
errorMessage != null -> errorMessage
fileNames.contains(newFileName) -> getString(R.string.file_already_exists)
else -> null
}
@ -133,6 +131,10 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
if (positiveButton == null) {
bindButton()
}
} else if (FileNameValidator.isFileHidden(newFileName)) {
binding.userInputContainer.error = requireContext().getString(R.string.hidden_file_name_warning)
binding.userInputContainer.isErrorEnabled = true
positiveButton?.isEnabled = true
} else {
binding.userInputContainer.error = null
binding.userInputContainer.isErrorEnabled = false

View file

@ -159,7 +159,7 @@ public class RenameFileDialogFragment
newFileName = binding.userInput.getText().toString().trim();
}
String errorMessage = FileNameValidator.INSTANCE.isValid(newFileName, requireContext());
String errorMessage = FileNameValidator.INSTANCE.isValid(newFileName, requireContext(), null);
if (errorMessage != null) {
DisplayUtils.showSnackMessage(requireActivity(), errorMessage);
return;
@ -193,16 +193,13 @@ public class RenameFileDialogFragment
newFileName = binding.userInput.getText().toString().trim();
}
String errorMessage = FileNameValidator.INSTANCE.isValid(newFileName, requireContext());
String errorMessage = FileNameValidator.INSTANCE.isValid(newFileName, requireContext(), fileNames);
if (!TextUtils.isEmpty(newFileName) && newFileName.charAt(0) == '.') {
if (FileNameValidator.INSTANCE.isFileHidden(newFileName)) {
binding.userInputContainer.setError(getText(R.string.hidden_file_name_warning));
} else if (errorMessage != null) {
binding.userInputContainer.setError(errorMessage);
positiveButton.setEnabled(false);
} else if (fileNames.contains(newFileName)) {
binding.userInputContainer.setError(getText(R.string.file_already_exists));
positiveButton.setEnabled(false);
} else if (binding.userInputContainer.getError() != null) {
binding.userInputContainer.setError(null);
// Called to remove extra padding