From 8d5d7cccc86a21547783bc43eec839ced8e8d57f Mon Sep 17 00:00:00 2001 From: William Plefka Date: Sun, 7 Nov 2021 20:27:19 -0500 Subject: [PATCH] Added hidden file warning message to the rename file dialog. Resolves #7722 Signed-off-by: William Plefka Signed-off-by: Liam P <89549210+liam-p-23@users.noreply.github.com> --- .../ui/dialog/RenameFileDialogFragment.java | 51 ++++++++++++++++--- src/main/res/layout/edit_box_dialog.xml | 12 ++++- src/main/res/values/strings.xml | 1 + 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/owncloud/android/ui/dialog/RenameFileDialogFragment.java b/src/main/java/com/owncloud/android/ui/dialog/RenameFileDialogFragment.java index ccf9826f6d..cd7d323265 100644 --- a/src/main/java/com/owncloud/android/ui/dialog/RenameFileDialogFragment.java +++ b/src/main/java/com/owncloud/android/ui/dialog/RenameFileDialogFragment.java @@ -29,7 +29,9 @@ package com.owncloud.android.ui.dialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; +import android.text.Editable; import android.text.TextUtils; +import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.Window; @@ -55,7 +57,7 @@ import androidx.fragment.app.DialogFragment; * Triggers the rename operation when name is confirmed. */ public class RenameFileDialogFragment - extends DialogFragment implements DialogInterface.OnClickListener { + extends DialogFragment implements DialogInterface.OnClickListener { private static final String ARG_TARGET_FILE = "TARGET_FILE"; @@ -107,12 +109,40 @@ public class RenameFileDialogFragment ThemeTextInputUtils.colorTextInput(binding.userInputContainer, binding.userInput, ThemeColorUtils.primaryColor(getActivity())); - int selectionStart = 0; - int extensionStart = mTargetFile.isFolder() ? -1 : currentName.lastIndexOf('.'); - int selectionEnd = extensionStart >= 0 ? extensionStart : currentName.length(); - binding.userInput.setSelection(Math.min(selectionStart, selectionEnd), Math.max(selectionStart, selectionEnd)); + + binding.userInput.setSelection(0, getSelectionEndIndex(currentName)); binding.userInput.requestFocus(); + // Add TextChangedListener to the user input EditText + binding.inputWarningMessage.setText(R.string.hidden_file_name_warning); + binding.userInput.addTextChangedListener(new TextWatcher() { + @Override + public void afterTextChanged(Editable s) { + + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + String newFileName = ""; + if (binding.userInput.getText() != null) { + newFileName = binding.userInput.getText().toString().trim(); + } + + if (!TextUtils.isEmpty(newFileName) && + TextUtils.isEmpty(newFileName.substring(0, getSelectionEndIndex(newFileName))) ) { + binding.inputWarningMessage.setVisibility(View.VISIBLE); + } + else if(binding.inputWarningMessage.getVisibility() == View.VISIBLE) { + binding.inputWarningMessage.setVisibility(View.GONE); + } + } + }); + // Build the dialog AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity()); builder.setView(view) @@ -129,6 +159,14 @@ public class RenameFileDialogFragment return d; } + /** + * Calculate the end index of the actual file name (i.e. without the extension) + **/ + private int getSelectionEndIndex(String fileName) { + int extensionStart = mTargetFile.isFolder() ? -1 : fileName.lastIndexOf('.'); + return extensionStart >= 0 ? extensionStart : fileName.length(); + } + @Override public void onClick(DialogInterface dialog, int which) { @@ -146,7 +184,6 @@ public class RenameFileDialogFragment if (!FileUtils.isValidName(newFileName)) { DisplayUtils.showSnackMessage(requireActivity(), R.string.filename_forbidden_charaters_from_server); - return; } @@ -159,4 +196,4 @@ public class RenameFileDialogFragment super.onDestroyView(); binding = null; } -} +} \ No newline at end of file diff --git a/src/main/res/layout/edit_box_dialog.xml b/src/main/res/layout/edit_box_dialog.xml index 8c46a414ce..3d0b95739f 100644 --- a/src/main/res/layout/edit_box_dialog.xml +++ b/src/main/res/layout/edit_box_dialog.xml @@ -21,7 +21,8 @@ + android:padding="@dimen/standard_padding" + android:orientation="vertical"> + + diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 9bdb6cf31f..276073e0d1 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -779,6 +779,7 @@ Rename Add or upload Account creation failed + Name will result in a hidden file Allow %1$s to access your Nextcloud account %2$s? Deny Allow