Эх сурвалжийг харах

Extract methods

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 жил өмнө
parent
commit
48f700e7e0

+ 61 - 50
app/src/main/java/com/owncloud/android/ui/dialog/RenameFileDialogFragment.java

@@ -34,7 +34,6 @@ import android.text.TextUtils;
 import android.text.TextWatcher;
 import android.view.LayoutInflater;
 import android.view.View;
-import android.view.WindowManager;
 
 import com.google.android.material.button.MaterialButton;
 import com.google.android.material.dialog.MaterialAlertDialogBuilder;
@@ -66,7 +65,7 @@ import androidx.fragment.app.DialogFragment;
  * Triggers the rename operation when name is confirmed.
  */
 public class RenameFileDialogFragment
-    extends DialogFragment implements DialogInterface.OnClickListener, Injectable {
+    extends DialogFragment implements DialogInterface.OnClickListener, TextWatcher, Injectable {
 
     private static final String ARG_TARGET_FILE = "TARGET_FILE";
     private static final String ARG_PARENT_FOLDER = "PARENT_FOLDER";
@@ -78,6 +77,7 @@ public class RenameFileDialogFragment
     private EditBoxDialogBinding binding;
     private OCFile mTargetFile;
     private MaterialButton positiveButton;
+    private Set<String> fileNames;
 
     /**
      * Public factory method to create new RenameFileDialogFragment instances.
@@ -97,16 +97,7 @@ public class RenameFileDialogFragment
     @Override
     public void onStart() {
         super.onStart();
-
-        AlertDialog alertDialog = (AlertDialog) getDialog();
-
-        if (alertDialog != null) {
-            positiveButton = (MaterialButton) alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
-            MaterialButton negativeButton = (MaterialButton) alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
-
-            viewThemeUtils.material.colorMaterialButtonPrimaryTonal(positiveButton);
-            viewThemeUtils.material.colorMaterialButtonPrimaryBorderless(negativeButton);
-        }
+        initAlertDialog();
     }
 
     @Override
@@ -135,51 +126,24 @@ public class RenameFileDialogFragment
 
         OCFile parentFolder = requireArguments().getParcelable(ARG_PARENT_FOLDER);
         List<OCFile> folderContent = fileDataStorageManager.getFolderContent(parentFolder, false);
-        Set<String> fileNames = Sets.newHashSetWithExpectedSize(folderContent.size());
+        fileNames = Sets.newHashSetWithExpectedSize(folderContent.size());
 
         for (OCFile file : folderContent) {
             fileNames.add(file.getFileName());
         }
 
         // Add TextChangedListener to handle showing/hiding the input warning message
-        binding.userInput.addTextChangedListener(new TextWatcher() {
-            @Override
-            public void afterTextChanged(Editable s) {
-            }
+        binding.userInput.addTextChangedListener(this);
 
-            @Override
-            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
-            }
+        // Build the dialog
+        MaterialAlertDialogBuilder builder = buildMaterialAlertDialog(view);
 
-            /**
-             * When user enters a hidden file name, the 'hidden file' message is shown.
-             * Otherwise, the message is ensured to be hidden.
-             */
-            @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) && newFileName.charAt(0) == '.') {
-                    binding.userInputContainer.setError(getText(R.string.hidden_file_name_warning));
-                } else if (TextUtils.isEmpty(newFileName)) {
-                    binding.userInputContainer.setError(getString(R.string.filename_empty));
-                    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
-                    binding.userInputContainer.setErrorEnabled(false);
-                    positiveButton.setEnabled(true);
-                }
-            }
-        });
+        viewThemeUtils.dialog.colorMaterialAlertDialogBackground(binding.userInputContainer.getContext(), builder);
 
-        // Build the dialog
+        return builder.create();
+    }
+
+    private MaterialAlertDialogBuilder buildMaterialAlertDialog(View view) {
         MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireActivity());
 
         builder
@@ -188,9 +152,19 @@ public class RenameFileDialogFragment
             .setNegativeButton(R.string.common_cancel, this)
             .setTitle(R.string.rename_dialog_title);
 
-        viewThemeUtils.dialog.colorMaterialAlertDialogBackground(binding.userInputContainer.getContext(), builder);
+        return builder;
+    }
 
-        return builder.create();
+    private void initAlertDialog() {
+        AlertDialog alertDialog = (AlertDialog) getDialog();
+
+        if (alertDialog != null) {
+            positiveButton = (MaterialButton) alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
+            MaterialButton negativeButton = (MaterialButton) alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
+
+            viewThemeUtils.material.colorMaterialButtonPrimaryTonal(positiveButton);
+            viewThemeUtils.material.colorMaterialButtonPrimaryBorderless(negativeButton);
+        }
     }
 
     @Override
@@ -222,4 +196,41 @@ public class RenameFileDialogFragment
         super.onDestroyView();
         binding = null;
     }
+
+    @Override
+    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
+
+    }
+
+    /**
+     * When user enters a hidden file name, the 'hidden file' message is shown.
+     * Otherwise, the message is ensured to be hidden.
+     */
+    @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) && newFileName.charAt(0) == '.') {
+            binding.userInputContainer.setError(getText(R.string.hidden_file_name_warning));
+        } else if (TextUtils.isEmpty(newFileName)) {
+            binding.userInputContainer.setError(getString(R.string.filename_empty));
+            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
+            binding.userInputContainer.setErrorEnabled(false);
+            positiveButton.setEnabled(true);
+        }
+    }
+
+    @Override
+    public void afterTextChanged(Editable s) {
+
+    }
 }