Browse Source

Merge pull request #11548 from nextmcloud/templateDialogErrorHandling

Handling empty state for rich document dialog.
Tobias Kaminsky 2 năm trước cách đây
mục cha
commit
ba8a8a3a5b

+ 29 - 25
app/src/main/java/com/owncloud/android/ui/dialog/ChooseRichDocumentsTemplateDialogFragment.java

@@ -3,8 +3,10 @@
  * Nextcloud Android client application
  *
  * @author Tobias Kaminsky
+ * @author TSI-mc
  * Copyright (C) 2019 Tobias Kaminsky
  * Copyright (C) 2019 Nextcloud GmbH
+ * Copyright (C) 2023 TSI-mc
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
@@ -86,6 +88,8 @@ public class ChooseRichDocumentsTemplateDialogFragment extends DialogFragment im
     private static final String DOT = ".";
     public static final int SINGLE_TEMPLATE = 1;
 
+    private Set<String> fileNames;
+
     @Inject CurrentAccountProvider currentAccount;
     @Inject ClientFactory clientFactory;
     @Inject ViewThemeUtils viewThemeUtils;
@@ -156,7 +160,7 @@ public class ChooseRichDocumentsTemplateDialogFragment extends DialogFragment im
 
         parentFolder = arguments.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());
@@ -188,31 +192,14 @@ public class ChooseRichDocumentsTemplateDialogFragment extends DialogFragment im
 
             }
 
-            /**
-             * When user enters an already taken file name, a 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.filename.getText() != null) {
-                    newFileName = binding.filename.getText().toString().trim();
-                }
-
-                if (fileNames.contains(newFileName)) {
-                    binding.filenameContainer.setError(getText(R.string.file_already_exists));
-                    positiveButton.setEnabled(false);
-                } else if (binding.filenameContainer.getError() != null) {
-                    binding.filenameContainer.setError(null);
-                    // Called to remove extra padding
-                    binding.filenameContainer.setErrorEnabled(false);
-                    positiveButton.setEnabled(true);
-                }
+                // not needed
             }
 
             @Override
             public void afterTextChanged(Editable s) {
-
+                checkEnablingCreateButton();
             }
         });
 
@@ -297,11 +284,28 @@ public class ChooseRichDocumentsTemplateDialogFragment extends DialogFragment im
     }
 
     private void checkEnablingCreateButton() {
-        Template selectedTemplate = adapter.getSelectedTemplate();
-        String name = Objects.requireNonNull(binding.filename.getText()).toString();
-
-        positiveButton.setEnabled(selectedTemplate != null && !name.isEmpty() &&
-                                      !name.equalsIgnoreCase(DOT + selectedTemplate.getExtension()));
+        if (positiveButton != null) {
+            Template selectedTemplate = adapter.getSelectedTemplate();
+            String name = Objects.requireNonNull(binding.filename.getText()).toString();
+            boolean isNameJustExtension = selectedTemplate != null && name.equalsIgnoreCase(
+                DOT + selectedTemplate.getExtension());
+            boolean isNameEmpty = name.isEmpty() || isNameJustExtension;
+            boolean state = selectedTemplate != null && !isNameEmpty && !fileNames.contains(name);
+
+            positiveButton.setEnabled(selectedTemplate != null && !name.isEmpty() &&
+                                          !name.equalsIgnoreCase(DOT + selectedTemplate.getExtension()));
+            positiveButton.setEnabled(state);
+            positiveButton.setClickable(state);
+            binding.filenameContainer.setErrorEnabled(!state);
+
+            if (!state) {
+                if (isNameEmpty) {
+                    binding.filenameContainer.setError(getText(R.string.filename_empty));
+                } else {
+                    binding.filenameContainer.setError(getText(R.string.file_already_exists));
+                }
+            }
+        }
     }
 
     private static class CreateFileFromTemplateTask extends AsyncTask<Void, Void, String> {

+ 3 - 1
app/src/main/java/com/owncloud/android/ui/dialog/ChooseTemplateDialogFragment.kt

@@ -3,10 +3,12 @@
  *
  * @author Tobias Kaminsky
  * @author Chris Narkiewicz
+ * @author TSI-mc
  *
  * Copyright (C) 2018 Tobias Kaminsky
  * Copyright (C) 2018 Nextcloud GmbH.
  * Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
+ * Copyright (C) 20223TSI-mc
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -327,7 +329,7 @@ class ChooseTemplateDialogFragment : DialogFragment(), View.OnClickListener, Tem
             val fragment = chooseTemplateDialogFragmentWeakReference.get()
             if (fragment != null && fragment.isAdded) {
                 if (url.isEmpty()) {
-                    DisplayUtils.showSnackMessage(fragment.binding.list, "Error creating file from template")
+                    DisplayUtils.showSnackMessage(fragment.binding.list, R.string.error_creating_file_from_template)
                 } else {
                     val editorWebView = Intent(MainApp.getAppContext(), TextEditorWebView::class.java)
                     editorWebView.putExtra(ExternalSiteWebView.EXTRA_TITLE, "Text")