diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-11-12 13:38:47 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-12-10 12:55:06 +0000 |
| commit | 221ed6b52a6df797b64deb3ceed4113f43575df0 (patch) | |
| tree | e676176a85c828ab8e7ca17e03c13bfb2ef35c71 /android/src | |
| parent | 18464a3810d96afa72221caaf86d4dd02abfe980 (diff) | |
| download | mullvadvpn-221ed6b52a6df797b64deb3ceed4113f43575df0.tar.xz mullvadvpn-221ed6b52a6df797b64deb3ceed4113f43575df0.zip | |
Make input text red when adding invalid address
Diffstat (limited to 'android/src')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/CustomDnsAdapter.kt | 24 | ||||
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/EditCustomDnsServerHolder.kt | 24 |
2 files changed, 40 insertions, 8 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/CustomDnsAdapter.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/CustomDnsAdapter.kt index 095a7ae7a8..3eff101a3b 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/CustomDnsAdapter.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/CustomDnsAdapter.kt @@ -139,13 +139,19 @@ class CustomDnsAdapter(val customDns: CustomDns) : Adapter<CustomDnsItemHolder>( } } - fun saveDnsServer(address: String) { + fun saveDnsServer(address: String, errorCallback: () -> Unit) { jobTracker.newUiJob("saveDnsServer $address") { editingPosition?.let { position -> + var validAddress: Boolean + if (position >= cachedCustomDnsServers.size) { - addDnsServer(address) + validAddress = addDnsServer(address) } else { - replaceDnsServer(address, position) + validAddress = replaceDnsServer(address, position) + } + + if (!validAddress) { + errorCallback() } } } @@ -190,12 +196,12 @@ class CustomDnsAdapter(val customDns: CustomDns) : Adapter<CustomDnsItemHolder>( } } - private suspend fun addDnsServer(address: String) { + private suspend fun addDnsServer(addressText: String): Boolean { var added = false jobTracker.runOnBackground { - if (inetAddressValidator.isValid(address)) { - val address = InetAddress.getByName(address) + if (inetAddressValidator.isValid(addressText)) { + val address = InetAddress.getByName(addressText) if (customDns.addDnsServer(address)) { cachedCustomDnsServers.add(address) @@ -212,9 +218,11 @@ class CustomDnsAdapter(val customDns: CustomDns) : Adapter<CustomDnsItemHolder>( notifyItemChanged(count - 3) notifyItemInserted(count - 2) } + + return added } - private suspend fun replaceDnsServer(address: String, position: Int) { + private suspend fun replaceDnsServer(address: String, position: Int): Boolean { var replaced = false jobTracker.runOnBackground { @@ -233,6 +241,8 @@ class CustomDnsAdapter(val customDns: CustomDns) : Adapter<CustomDnsItemHolder>( editingPosition = null notifyItemChanged(position) } + + return replaced } private fun editDnsServerAt(position: Int?) { diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/EditCustomDnsServerHolder.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/EditCustomDnsServerHolder.kt index edebc30266..560bfb22d8 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/EditCustomDnsServerHolder.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/EditCustomDnsServerHolder.kt @@ -1,5 +1,7 @@ package net.mullvad.mullvadvpn.ui.customdns +import android.text.Editable +import android.text.TextWatcher import android.view.View import android.view.View.OnFocusChangeListener import android.widget.EditText @@ -9,6 +11,10 @@ import net.mullvad.mullvadvpn.R import net.mullvad.talpid.util.addressString class EditCustomDnsServerHolder(view: View, adapter: CustomDnsAdapter) : CustomDnsItemHolder(view) { + private val context = view.context + private val errorColor = context.getColor(R.color.red) + private val normalColor = context.getColor(R.color.blue) + private val input: EditText = view.findViewById<EditText>(R.id.input).apply { onFocusChangeListener = OnFocusChangeListener { _, hasFocus -> if (!hasFocus) { @@ -19,6 +25,17 @@ class EditCustomDnsServerHolder(view: View, adapter: CustomDnsAdapter) : CustomD } } + private val watcher = object : TextWatcher { + override fun beforeTextChanged(text: CharSequence, start: Int, count: Int, after: Int) {} + + override fun afterTextChanged(text: Editable) { + input.setTextColor(normalColor) + input.removeTextChangedListener(this) + } + + override fun onTextChanged(text: CharSequence, start: Int, before: Int, count: Int) {} + } + var serverAddress by observable<InetAddress?>(null) { _, _, address -> if (address != null) { val addressString = address.addressString() @@ -34,7 +51,12 @@ class EditCustomDnsServerHolder(view: View, adapter: CustomDnsAdapter) : CustomD init { view.findViewById<View>(R.id.save).setOnClickListener { - adapter.saveDnsServer(input.text.toString()) + adapter.saveDnsServer(input.text.toString()) { + input.apply { + setTextColor(errorColor) + addTextChangedListener(watcher) + } + } } } } |
