From b5e7946569154ffafb1b5b3368b7b1fbd3d7bc56 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Tue, 2 Jul 2024 15:03:19 +0200 Subject: [PATCH] Remove duplicated logics Signed-off-by: alperozturk --- .../fileNameValidator/FileNameValidator.kt | 10 +++++++- .../ui/dialog/ChooseTemplateDialogFragment.kt | 23 ++++++++++++------- .../ui/dialog/CreateFolderDialogFragment.kt | 8 ++++--- .../ui/dialog/RenameFileDialogFragment.java | 9 +++----- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/nextcloud/utils/fileNameValidator/FileNameValidator.kt b/app/src/main/java/com/nextcloud/utils/fileNameValidator/FileNameValidator.kt index fac6415e6b..c4cd6389d3 100644 --- a/app/src/main/java/com/nextcloud/utils/fileNameValidator/FileNameValidator.kt +++ b/app/src/main/java/com/nextcloud/utils/fileNameValidator/FileNameValidator.kt @@ -22,7 +22,7 @@ object FileNameValidator { "LPT¹", "LPT²", "LPT³" ) - fun isValid(name: String, context: Context): String? { + fun isValid(name: String, context: Context, fileNames: MutableSet? = 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): Boolean = fileNames.contains(name) } diff --git a/app/src/main/java/com/owncloud/android/ui/dialog/ChooseTemplateDialogFragment.kt b/app/src/main/java/com/owncloud/android/ui/dialog/ChooseTemplateDialogFragment.kt index a405d112f0..1e5835b297 100644 --- a/app/src/main/java/com/owncloud/android/ui/dialog/ChooseTemplateDialogFragment.kt +++ b/app/src/main/java/com/owncloud/android/ui/dialog/ChooseTemplateDialogFragment.kt @@ -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 } } diff --git a/app/src/main/java/com/owncloud/android/ui/dialog/CreateFolderDialogFragment.kt b/app/src/main/java/com/owncloud/android/ui/dialog/CreateFolderDialogFragment.kt index 2aa4d244bd..f38797ad50 100644 --- a/app/src/main/java/com/owncloud/android/ui/dialog/CreateFolderDialogFragment.kt +++ b/app/src/main/java/com/owncloud/android/ui/dialog/CreateFolderDialogFragment.kt @@ -117,13 +117,11 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList private fun checkFileNameAfterEachType(fileNames: MutableSet) { 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 diff --git a/app/src/main/java/com/owncloud/android/ui/dialog/RenameFileDialogFragment.java b/app/src/main/java/com/owncloud/android/ui/dialog/RenameFileDialogFragment.java index bbde6a5290..dce02fe14c 100644 --- a/app/src/main/java/com/owncloud/android/ui/dialog/RenameFileDialogFragment.java +++ b/app/src/main/java/com/owncloud/android/ui/dialog/RenameFileDialogFragment.java @@ -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