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

232 lines
9.8 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.content.Context
2021-10-28 15:25:02 +13:00
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
2021-11-26 09:45:12 +13:00
import android.util.Log
2021-10-28 15:25:02 +13:00
import android.view.View
2021-11-26 09:45:12 +13:00
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.Button
2021-10-28 15:25:02 +13:00
import android.widget.CheckBox
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope
2021-10-28 15:25:02 +13:00
import com.google.android.material.textfield.TextInputEditText
2021-11-26 09:45:12 +13:00
import com.google.android.material.textfield.TextInputLayout
import io.heckel.ntfy.BuildConfig
2021-10-28 16:04:14 +13:00
import io.heckel.ntfy.R
import io.heckel.ntfy.data.Database
import io.heckel.ntfy.data.Repository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
2021-10-28 15:25:02 +13:00
2021-11-26 09:45:12 +13:00
class AddFragment : DialogFragment() {
private lateinit var repository: Repository
private lateinit var subscribeListener: SubscribeListener
private lateinit var topicNameText: TextInputEditText
2021-11-26 09:45:12 +13:00
private lateinit var baseUrlLayout: TextInputLayout
private lateinit var baseUrlText: AutoCompleteTextView
private lateinit var useAnotherServerCheckbox: CheckBox
2021-11-14 13:26:37 +13:00
private lateinit var useAnotherServerDescription: View
private lateinit var instantDeliveryBox: View
2021-11-14 13:26:37 +13:00
private lateinit var instantDeliveryCheckbox: CheckBox
private lateinit var instantDeliveryDescription: View
private lateinit var subscribeButton: Button
2021-10-28 15:25:02 +13:00
2021-11-26 09:45:12 +13:00
private lateinit var baseUrls: List<String> // List of base URLs already used, excluding app_base_url
interface SubscribeListener {
fun onSubscribe(topic: String, baseUrl: String, instant: Boolean)
}
override fun onAttach(context: Context) {
super.onAttach(context)
subscribeListener = activity as SubscribeListener
}
2021-10-28 15:25:02 +13:00
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
if (activity == null) {
throw IllegalStateException("Activity cannot be null")
}
// Dependencies
2021-11-23 09:45:43 +13:00
val database = Database.getInstance(requireActivity().applicationContext)
val sharedPrefs = requireActivity().getSharedPreferences(Repository.SHARED_PREFS_ID, Context.MODE_PRIVATE)
repository = Repository.getInstance(sharedPrefs, database.subscriptionDao(), database.notificationDao())
// Build root view
2021-11-23 09:45:43 +13:00
val view = requireActivity().layoutInflater.inflate(R.layout.fragment_add_dialog, null)
2021-11-26 09:45:12 +13:00
topicNameText = view.findViewById(R.id.add_dialog_topic_text)
baseUrlLayout = view.findViewById(R.id.add_dialog_base_url_layout)
baseUrlText = view.findViewById(R.id.add_dialog_base_url_text)
instantDeliveryBox = view.findViewById(R.id.add_dialog_instant_delivery_box)
2021-11-26 09:45:12 +13:00
instantDeliveryCheckbox = view.findViewById(R.id.add_dialog_instant_delivery_checkbox)
instantDeliveryDescription = view.findViewById(R.id.add_dialog_instant_delivery_description)
2021-11-26 09:45:12 +13:00
useAnotherServerCheckbox = view.findViewById(R.id.add_dialog_use_another_server_checkbox)
useAnotherServerDescription = view.findViewById(R.id.add_dialog_use_another_server_description)
2021-11-26 09:45:12 +13:00
// Base URL dropdown behavior; Oh my, why is this so complicated?!
val toggleEndIcon = {
if (baseUrlText.text.isNotEmpty()) {
baseUrlLayout.setEndIconDrawable(R.drawable.ic_cancel_gray_24dp)
} else if (baseUrls.isEmpty()) {
baseUrlLayout.setEndIconDrawable(0)
} else {
baseUrlLayout.setEndIconDrawable(R.drawable.ic_drop_down_gray_24dp)
}
}
baseUrlLayout.setEndIconOnClickListener {
if (baseUrlText.text.isNotEmpty()) {
baseUrlText.text.clear()
if (baseUrls.isEmpty()) {
baseUrlLayout.setEndIconDrawable(0)
} else {
baseUrlLayout.setEndIconDrawable(R.drawable.ic_drop_down_gray_24dp)
}
} else if (baseUrlText.text.isEmpty() && baseUrls.isNotEmpty()) {
baseUrlLayout.setEndIconDrawable(R.drawable.ic_drop_up_gray_24dp)
baseUrlText.showDropDown()
}
}
baseUrlText.setOnDismissListener { toggleEndIcon() }
baseUrlText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
toggleEndIcon()
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
// Nothing
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// Nothing
}
})
// Fill autocomplete for base URL
lifecycleScope.launch(Dispatchers.IO) {
val appBaseUrl = getString(R.string.app_base_url)
baseUrls = repository.getSubscriptions()
.groupBy { it.baseUrl }
.map { it.key }
2021-11-26 09:45:12 +13:00
.filterNot { it == appBaseUrl }
.sorted()
2021-11-26 09:45:12 +13:00
val adapter = ArrayAdapter(requireActivity(), R.layout.fragment_add_dialog_dropdown_item, baseUrls)
requireActivity().runOnUiThread {
baseUrlText.threshold = 1
baseUrlText.setAdapter(adapter)
if (baseUrls.count() == 1) {
baseUrlLayout.setEndIconDrawable(R.drawable.ic_cancel_gray_24dp)
2021-11-26 09:45:12 +13:00
baseUrlText.setText(baseUrls.first())
2021-11-28 10:18:09 +13:00
} else if (baseUrls.count() > 1) {
baseUrlLayout.setEndIconDrawable(R.drawable.ic_drop_down_gray_24dp)
2021-11-28 10:18:09 +13:00
} else {
baseUrlLayout.setEndIconDrawable(0)
2021-11-26 09:45:12 +13:00
}
}
}
2021-11-25 10:12:51 +13:00
// Show/hide based on flavor
instantDeliveryBox.visibility = if (BuildConfig.FIREBASE_AVAILABLE) View.VISIBLE else View.GONE
// Build dialog
val alert = AlertDialog.Builder(activity)
.setView(view)
.setPositiveButton(R.string.add_dialog_button_subscribe) { _, _ ->
val topic = topicNameText.text.toString()
val baseUrl = getBaseUrl()
2021-11-25 10:12:51 +13:00
val instant = if (!BuildConfig.FIREBASE_AVAILABLE || useAnotherServerCheckbox.isChecked) {
true
} else {
instantDeliveryCheckbox.isChecked
}
subscribeListener.onSubscribe(topic, baseUrl, instant)
}
.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
subscribeButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
subscribeButton.isEnabled = false
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
2021-10-28 15:25:02 +13:00
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// Nothing
2021-10-28 15:25:02 +13:00
}
}
topicNameText.addTextChangedListener(textWatcher)
baseUrlText.addTextChangedListener(textWatcher)
instantDeliveryCheckbox.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) instantDeliveryDescription.visibility = View.VISIBLE
else instantDeliveryDescription.visibility = View.GONE
}
useAnotherServerCheckbox.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
useAnotherServerDescription.visibility = View.VISIBLE
2021-11-26 09:45:12 +13:00
baseUrlLayout.visibility = View.VISIBLE
instantDeliveryBox.visibility = View.GONE
instantDeliveryDescription.visibility = View.GONE
} else {
useAnotherServerDescription.visibility = View.GONE
2021-11-26 09:45:12 +13:00
baseUrlLayout.visibility = View.GONE
2021-11-25 10:12:51 +13:00
instantDeliveryBox.visibility = if (BuildConfig.FIREBASE_AVAILABLE) View.VISIBLE else View.GONE
if (instantDeliveryCheckbox.isChecked) instantDeliveryDescription.visibility = View.VISIBLE
2021-11-14 13:26:37 +13:00
else instantDeliveryDescription.visibility = View.GONE
}
validateInput()
2021-10-28 15:25:02 +13:00
}
}
2021-10-28 15:25:02 +13:00
return alert
2021-10-28 15:25:02 +13:00
}
private fun validateInput() = lifecycleScope.launch(Dispatchers.IO) {
val baseUrl = getBaseUrl()
val topic = topicNameText.text.toString()
val subscription = repository.getSubscription(baseUrl, topic)
activity?.let {
it.runOnUiThread {
2021-12-13 14:03:53 +13:00
if (subscription != null || DISALLOWED_TOPICS.contains(topic)) {
subscribeButton.isEnabled = false
} else if (useAnotherServerCheckbox.isChecked) {
subscribeButton.isEnabled = topic.isNotBlank()
2021-11-08 15:02:27 +13:00
&& "[-_A-Za-z0-9]{1,64}".toRegex().matches(topic)
&& baseUrl.isNotBlank()
&& "^https?://.+".toRegex().matches(baseUrl)
} else {
subscribeButton.isEnabled = topic.isNotBlank()
2021-11-08 15:02:27 +13:00
&& "[-_A-Za-z0-9]{1,64}".toRegex().matches(topic)
}
}
}
}
private fun getBaseUrl(): String {
return if (useAnotherServerCheckbox.isChecked) {
baseUrlText.text.toString()
} else {
getString(R.string.app_base_url)
}
}
companion object {
2021-11-25 10:12:51 +13:00
const val TAG = "NtfyAddFragment"
2021-12-13 14:03:53 +13:00
private val DISALLOWED_TOPICS = listOf("docs", "static")
}
2021-10-28 15:25:02 +13:00
}