Skip to content

Commit 4256478

Browse files
committed
feat: set restrictions for space name
1 parent e9e049b commit 4256478

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎owncloudApp/src/main/java/com/owncloud/android/presentation/spaces/createspace/CreateSpaceDialogFragment.kt‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,59 @@ import android.os.Bundle
2424
import android.view.LayoutInflater
2525
import android.view.View
2626
import android.view.ViewGroup
27+
import androidx.core.widget.doOnTextChanged
2728
import androidx.fragment.app.DialogFragment
29+
import com.owncloud.android.R
2830
import com.owncloud.android.databinding.CreateSpaceDialogBinding
2931

3032
class CreateSpaceDialogFragment : DialogFragment() {
3133
private var _binding: CreateSpaceDialogBinding? = null
3234
private val binding get() = _binding!!
3335

36+
private val forbiddenRegex = Regex(FORBIDDEN_CHARACTERS)
37+
3438
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
3539
_binding = CreateSpaceDialogBinding.inflate(inflater, container, false)
3640
return binding.root
3741
}
3842

3943
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
4044
super.onViewCreated(view, savedInstanceState)
41-
binding.cancelButton.setOnClickListener { dialog?.dismiss() }
45+
binding.apply {
46+
cancelButton.setOnClickListener { dialog?.dismiss() }
47+
createSpaceDialogNameValue.doOnTextChanged { name, _, _, _ ->
48+
val errorMessage = validateName(name.toString())
49+
updateUI(errorMessage)
50+
}
51+
}
52+
}
53+
54+
private fun validateName(spaceName: String): String? =
55+
if (spaceName.trim().isEmpty()) {
56+
getString(R.string.create_space_dialog_empty_error)
57+
} else if (spaceName.length > 255) {
58+
getString(R.string.create_space_dialog_length_error)
59+
} else if (spaceName.contains(forbiddenRegex)) {
60+
getString(R.string.create_space_dialog_characters_error)
61+
} else {
62+
null
63+
}
64+
65+
private fun updateUI(errorMessage: String?) {
66+
val colorButton = if (errorMessage == null) {
67+
resources.getColor(R.color.primary_button_background_color, null)
68+
} else {
69+
resources.getColor(R.color.grey, null)
70+
}
71+
72+
binding.createSpaceDialogNameValue.error = errorMessage
73+
binding.createButton.apply {
74+
setTextColor(colorButton)
75+
isEnabled = errorMessage == null
76+
}
77+
}
78+
79+
companion object {
80+
private const val FORBIDDEN_CHARACTERS = """[/\\.:?*"'><|]"""
4281
}
4382
}

‎owncloudApp/src/main/res/values/strings.xml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,9 @@
841841
<item>100 GB</item>
842842
<item>No restriction</item>
843843
</string-array>
844+
<string name="create_space_dialog_empty_error">Space name must not be empty</string>
845+
<string name="create_space_dialog_length_error">Space name must not be longer than 255 characters</string>
846+
<string name="create_space_dialog_characters_error">Forbidden characters: / \\ . : ? * &quot; ' &gt; &lt; |</string>
844847

845848
<string name="feedback_dialog_get_in_contact_description"><![CDATA[ Ask for help in our <a href=\"%1$s\"><b>forum</b></a> or contribute in our <a href=\"%2$s\"><b>GitHub repo</b></a>]]></string>
846849

0 commit comments

Comments
 (0)