summaryrefslogtreecommitdiffhomepage
path: root/android/src
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-11-10 21:40:54 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-12-10 12:55:06 +0000
commitc3d3843e6aab553b170ff714a57b74437dc5f93d (patch)
tree57414fecbccb7046dd8a90e9f5553fcede150974 /android/src
parent0c69a68d75de08031368e904bb9c8a8434221431 (diff)
downloadmullvadvpn-c3d3843e6aab553b170ff714a57b74437dc5f93d.tar.xz
mullvadvpn-c3d3843e6aab553b170ff714a57b74437dc5f93d.zip
Refactor adapter to allow replacing DNS servers
Diffstat (limited to 'android/src')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/CustomDnsAdapter.kt69
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/ui/customdns/EditCustomDnsServerHolder.kt2
2 files changed, 51 insertions, 20 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 a8dd24528a..d7d9a5813e 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
@@ -124,35 +124,66 @@ class CustomDnsAdapter(val customDns: CustomDns) : Adapter<CustomDnsItemHolder>(
}
}
- fun addDnsServer(address: String) {
- jobTracker.newUiJob("addDnsServer $address") {
- var added = false
+ fun saveDnsServer(address: String) {
+ jobTracker.newUiJob("saveDnsServer $address") {
+ editingPosition?.let { position ->
+ if (position >= cachedCustomDnsServers.size) {
+ addDnsServer(address)
+ } else {
+ replaceDnsServer(address, position)
+ }
+ }
+ }
+ }
+
+ fun removeDnsServer(address: InetAddress) {
+ jobTracker.newBackgroundJob("removeDnsServer $address") {
+ customDns.removeDnsServer(address)
+ }
+ }
+
+ private suspend fun addDnsServer(address: String) {
+ var added = false
- jobTracker.runOnBackground {
- if (inetAddressValidator.isValid(address)) {
- val address = InetAddress.getByName(address)
+ jobTracker.runOnBackground {
+ if (inetAddressValidator.isValid(address)) {
+ val address = InetAddress.getByName(address)
- if (customDns.addDnsServer(address)) {
- cachedCustomDnsServers.add(address)
- added = true
- }
+ if (customDns.addDnsServer(address)) {
+ cachedCustomDnsServers.add(address)
+ added = true
}
}
+ }
- if (added) {
- editingPosition = null
+ if (added) {
+ editingPosition = null
- val count = getItemCount()
+ val count = getItemCount()
- notifyItemChanged(count - 3)
- notifyItemInserted(count - 2)
- }
+ notifyItemChanged(count - 3)
+ notifyItemInserted(count - 2)
}
}
- fun removeDnsServer(address: InetAddress) {
- jobTracker.newBackgroundJob("removeDnsServer $address") {
- customDns.removeDnsServer(address)
+ private suspend fun replaceDnsServer(address: String, position: Int) {
+ var replaced = false
+
+ jobTracker.runOnBackground {
+ if (inetAddressValidator.isValid(address)) {
+ val newAddress = InetAddress.getByName(address)
+ val oldAddress = cachedCustomDnsServers[position]
+
+ if (customDns.replaceDnsServer(oldAddress, newAddress)) {
+ cachedCustomDnsServers[position] = newAddress
+ replaced = true
+ }
+ }
+ }
+
+ if (replaced) {
+ editingPosition = null
+ notifyItemChanged(position)
}
}
}
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 c0ca781628..d2decf9241 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
@@ -9,7 +9,7 @@ class EditCustomDnsServerHolder(view: View, adapter: CustomDnsAdapter) : CustomD
init {
view.findViewById<View>(R.id.save).setOnClickListener {
- adapter.addDnsServer(input.text.toString())
+ adapter.saveDnsServer(input.text.toString())
}
}
}