Skip to content

Commit d43b0bd

Browse files
PhilLabalperozturk96
authored andcommitted
FileNameTextWatcher: More descriptive parameter and variable names
Addressing the review comments on the PR Signed-off-by: Philipp Hasper <vcs@hasper.info>
1 parent c006da2 commit d43b0bd

3 files changed

Lines changed: 48 additions & 38 deletions

File tree

app/src/main/java/com/nextcloud/utils/fileNameValidator/FileNameTextWatcher.kt

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,45 @@ import com.owncloud.android.lib.resources.status.OCCapability
2424
class FileNameTextWatcher(
2525
private val previousFileName: String?,
2626
private val context: Context,
27-
private val getCapabilities: () -> OCCapability,
28-
private val getExistingFileNames: () -> Set<String>?,
29-
private val onError: Consumer<String>,
30-
private val onWarning: Consumer<String>,
31-
private val onOkay: Runnable
27+
private val capabilitiesProvider: () -> OCCapability,
28+
private val existingFileNamesProvider: () -> Set<String>?,
29+
private val onValidationError: Consumer<String>,
30+
private val onValidationWarning: Consumer<String>,
31+
private val onValidationSuccess: Runnable
3232
) : TextWatcher {
3333

34-
private var isOkay: Boolean = true // Used to trigger the onOkay callback only once
34+
// Used to trigger the onValidationSuccess callback only once (on "error/warn -> valid" transition)
35+
private var isNameCurrentlyValid: Boolean = true
3536

3637
override fun afterTextChanged(s: Editable?) {
37-
var newFileName = ""
38-
if (s != null) {
39-
newFileName = s.toString()
40-
}
38+
val currentFileName = s?.toString().orEmpty()
39+
val validationError = checkFileName(
40+
currentFileName,
41+
capabilitiesProvider(),
42+
context,
43+
existingFileNamesProvider()
44+
)
45+
46+
when {
47+
isFileHidden(currentFileName) -> {
48+
isNameCurrentlyValid = false
49+
onValidationWarning.accept(context.getString(R.string.hidden_file_name_warning))
50+
}
51+
52+
validationError != null -> {
53+
isNameCurrentlyValid = false
54+
onValidationError.accept(validationError)
55+
}
56+
57+
isExtensionChanged(previousFileName, currentFileName) -> {
58+
isNameCurrentlyValid = false
59+
onValidationWarning.accept(context.getString(R.string.warn_rename_extension))
60+
}
4161

42-
val errorMessage = checkFileName(newFileName, getCapabilities(), context, getExistingFileNames())
43-
44-
if (isFileHidden(newFileName)) {
45-
isOkay = false
46-
onWarning.accept(context.getString(R.string.hidden_file_name_warning))
47-
} else if (errorMessage != null) {
48-
isOkay = false
49-
onError.accept(errorMessage)
50-
} else if (isExtensionChanged(previousFileName, newFileName)) {
51-
isOkay = false
52-
onWarning.accept(context.getString(R.string.warn_rename_extension))
53-
} else if (!isOkay) {
54-
isOkay = true
55-
onOkay.run()
62+
!isNameCurrentlyValid -> {
63+
isNameCurrentlyValid = true
64+
onValidationSuccess.run()
65+
}
5666
}
5767
}
5868

app/src/main/java/com/owncloud/android/ui/activity/ReceiveExternalFilesActivity.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -867,15 +867,15 @@ private void setupFileNameInputField() {
867867
this,
868868
this::getCapabilities,
869869
() -> receiveExternalFilesAdapter.getFileNames(),
870-
s -> {
871-
binding.userInputContainer.setError(s);
870+
validationError -> {
871+
binding.userInputContainer.setError(validationError);
872872
binding.uploaderChooseFolder.setEnabled(false);
873873
},
874-
s -> {
875-
binding.userInputContainer.setError(s);
874+
validationWarning -> {
875+
binding.userInputContainer.setError(validationWarning);
876876
binding.uploaderChooseFolder.setEnabled(true);
877877
},
878-
() -> {
878+
() -> { // onValidationSuccess
879879
binding.userInputContainer.setError(null);
880880
binding.userInputContainer.setErrorEnabled(false);
881881
binding.uploaderChooseFolder.setEnabled(true);

app/src/main/java/com/owncloud/android/ui/dialog/RenameFileDialogFragment.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,19 @@ class RenameFileDialogFragment :
9797

9898
binding.userInput.addTextChangedListener(
9999
FileNameTextWatcher(
100-
mTargetFile?.fileName,
101-
binding.userInputContainer.context,
102-
{ oCCapability },
103-
{ fileNames },
104-
{ s: String? ->
105-
binding.userInputContainer.error = s
100+
previousFileName = mTargetFile?.fileName,
101+
context = binding.userInputContainer.context,
102+
capabilitiesProvider = { oCCapability },
103+
existingFileNamesProvider = { fileNames },
104+
onValidationError = { validationError: String ->
105+
binding.userInputContainer.error = validationError
106106
positiveButton?.isEnabled = false
107107
},
108-
{ s: String? ->
109-
binding.userInputContainer.error = s
108+
onValidationWarning = { validationWarning: String ->
109+
binding.userInputContainer.error = validationWarning
110110
positiveButton?.isEnabled = true
111111
},
112-
{
112+
onValidationSuccess = {
113113
binding.userInputContainer.error = null
114114
// Called to remove extra padding
115115
binding.userInputContainer.isErrorEnabled = false

0 commit comments

Comments
 (0)