Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e28a80d6 authored by Philipp Heckel's avatar Philipp Heckel
Browse files

Validate input when checkbox is pressed

parent 170bdc24
Loading
Loading
Loading
Loading
+17 −13
Original line number Diff line number Diff line
@@ -22,10 +22,6 @@ class AddFragment(private val listener: Listener) : DialogFragment() {
            val topicNameText = view.findViewById(R.id.add_dialog_topic_text) as TextInputEditText
            val baseUrlText = view.findViewById(R.id.add_dialog_base_url_text) as TextInputEditText
            val useAnotherServerCheckbox = view.findViewById(R.id.add_dialog_use_another_server_checkbox) as CheckBox
            useAnotherServerCheckbox.setOnCheckedChangeListener { buttonView, isChecked ->
                if (isChecked) baseUrlText.visibility = View.VISIBLE
                else baseUrlText.visibility = View.GONE
            }

            // Build dialog
            val alert = AlertDialog.Builder(it)
@@ -51,8 +47,7 @@ class AddFragment(private val listener: Listener) : DialogFragment() {
                val subscribeButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
                subscribeButton.isEnabled = false

                val textWatcher = object : TextWatcher {
                    override fun afterTextChanged(s: Editable?) {
                val validateInput: () -> Unit = {
                    if (useAnotherServerCheckbox.isChecked) {
                        subscribeButton.isEnabled = topicNameText.text.toString().isNotBlank()
                                && "[-_A-Za-z0-9]+".toRegex().matches(topicNameText.text.toString())
@@ -63,6 +58,10 @@ class AddFragment(private val listener: Listener) : DialogFragment() {
                                && "[-_A-Za-z0-9]+".toRegex().matches(topicNameText.text.toString())
                    }
                }
                val textWatcher = object : TextWatcher {
                    override fun afterTextChanged(s: Editable?) {
                        validateInput()
                    }
                    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                        // Nothing
                    }
@@ -72,6 +71,11 @@ class AddFragment(private val listener: Listener) : DialogFragment() {
                }
                topicNameText.addTextChangedListener(textWatcher)
                baseUrlText.addTextChangedListener(textWatcher)
                useAnotherServerCheckbox.setOnCheckedChangeListener { buttonView, isChecked ->
                    if (isChecked) baseUrlText.visibility = View.VISIBLE
                    else baseUrlText.visibility = View.GONE
                    validateInput()
                }
            }

            alert