ntfy-android/app/src/main/java/io/heckel/ntfy/ui/AddFragment.kt

89 lines
3.9 KiB
Kotlin
Raw Normal View History

2021-10-28 16:04:14 +13:00
package io.heckel.ntfy.ui
2021-10-28 15:25:02 +13:00
import android.app.AlertDialog
import android.app.Dialog
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.CheckBox
import androidx.fragment.app.DialogFragment
import com.google.android.material.textfield.TextInputEditText
2021-10-28 16:04:14 +13:00
import io.heckel.ntfy.R
2021-10-28 15:25:02 +13:00
2021-10-29 04:45:34 +13:00
class AddFragment(private val listener: AddSubscriptionListener) : DialogFragment() {
interface AddSubscriptionListener {
2021-10-30 14:13:58 +13:00
fun onSubscribe(topic: String, baseUrl: String)
2021-10-28 15:25:02 +13:00
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
// Build root view
2021-10-29 01:28:22 +13:00
val view = requireActivity().layoutInflater.inflate(R.layout.add_dialog_fragment, null)
2021-10-28 15:25:02 +13:00
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
2021-10-30 14:13:58 +13:00
// FIXME For now, other servers are disabled
useAnotherServerCheckbox.visibility = View.GONE
2021-10-28 15:25:02 +13:00
// Build dialog
val alert = AlertDialog.Builder(it)
.setView(view)
.setPositiveButton(R.string.add_dialog_button_subscribe) { _, _ ->
val topic = topicNameText.text.toString()
val baseUrl = if (useAnotherServerCheckbox.isChecked) {
baseUrlText.text.toString()
} else {
2021-10-30 14:13:58 +13:00
getString(R.string.app_base_url)
2021-10-28 15:25:02 +13:00
}
2021-10-30 14:13:58 +13:00
listener.onSubscribe(topic, baseUrl)
2021-10-28 15:25:02 +13:00
}
.setNegativeButton(R.string.add_dialog_button_cancel) { _, _ ->
dialog?.cancel()
}
.create()
// Add logic to disable "Subscribe" button on invalid input
alert.setOnShowListener {
val dialog = it as AlertDialog
val subscribeButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
subscribeButton.isEnabled = false
val validateInput: () -> Unit = {
if (useAnotherServerCheckbox.isChecked) {
subscribeButton.isEnabled = topicNameText.text.toString().isNotBlank()
&& "[-_A-Za-z0-9]+".toRegex().matches(topicNameText.text.toString())
&& baseUrlText.text.toString().isNotBlank()
&& "^https?://.+".toRegex().matches(baseUrlText.text.toString())
} else {
subscribeButton.isEnabled = topicNameText.text.toString().isNotBlank()
&& "[-_A-Za-z0-9]+".toRegex().matches(topicNameText.text.toString())
}
}
2021-10-28 15:25:02 +13:00
val textWatcher = object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
validateInput()
2021-10-28 15:25:02 +13:00
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
// Nothing
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// Nothing
}
}
topicNameText.addTextChangedListener(textWatcher)
baseUrlText.addTextChangedListener(textWatcher)
2021-10-29 01:28:22 +13:00
useAnotherServerCheckbox.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) baseUrlText.visibility = View.VISIBLE
else baseUrlText.visibility = View.GONE
validateInput()
}
2021-10-28 15:25:02 +13:00
}
alert
} ?: throw IllegalStateException("Activity cannot be null")
}
}