summaryrefslogtreecommitdiffhomepage
path: root/android/src/main
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-09-03 08:13:45 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-09-03 10:13:36 +0000
commit1cbab38ade96b3df42b6165fbfb0658c68bb9ace (patch)
tree7912d4cbaec5a2da24c5d58ebe9e5829c3adfdbd /android/src/main
parent569f33079abe275f7ba01c6dc1f9bf57b049ebe8 (diff)
downloadmullvadvpn-1cbab38ade96b3df42b6165fbfb0658c68bb9ace.tar.xz
mullvadvpn-1cbab38ade96b3df42b6165fbfb0658c68bb9ace.zip
Create `SwitchLocationButton` widget
Diffstat (limited to 'android/src/main')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/SwitchLocationButton.kt89
-rw-r--r--android/src/main/res/layout/switch_location_button.xml14
2 files changed, 103 insertions, 0 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/SwitchLocationButton.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/SwitchLocationButton.kt
new file mode 100644
index 0000000000..9ca390b4e1
--- /dev/null
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/SwitchLocationButton.kt
@@ -0,0 +1,89 @@
+package net.mullvad.mullvadvpn.ui.widget
+
+import android.content.Context
+import android.util.AttributeSet
+import android.view.LayoutInflater
+import android.view.View
+import android.widget.FrameLayout
+import android.widget.TextView
+import kotlin.properties.Delegates.observable
+import net.mullvad.mullvadvpn.R
+import net.mullvad.mullvadvpn.model.TunnelState
+import net.mullvad.mullvadvpn.relaylist.RelayItem
+import net.mullvad.talpid.tunnel.ActionAfterDisconnect
+
+class SwitchLocationButton : FrameLayout {
+ private val container =
+ context.getSystemService(Context.LAYOUT_INFLATER_SERVICE).let { service ->
+ val inflater = service as LayoutInflater
+
+ inflater.inflate(R.layout.switch_location_button, this)
+ }
+
+ private val buttonWithLabel = container.findViewById<View>(R.id.button_with_label).apply {
+ setOnClickListener { onClick?.invoke() }
+ }
+
+ private val buttonWithLocation =
+ container.findViewById<TextView>(R.id.button_with_location).apply {
+ setOnClickListener { onClick?.invoke() }
+ }
+
+ var onClick: (() -> Unit)? = null
+
+ var location by observable<RelayItem?>(null) { _, _, location ->
+ buttonWithLocation.text = location?.locationName ?: ""
+ }
+
+ var tunnelState by observable<TunnelState>(TunnelState.Disconnected()) { _, _, state ->
+ when (state) {
+ is TunnelState.Disconnected -> showLocation()
+ is TunnelState.Disconnecting -> {
+ when (state.actionAfterDisconnect) {
+ ActionAfterDisconnect.Nothing -> showLocation()
+ ActionAfterDisconnect.Block -> showLocation()
+ ActionAfterDisconnect.Reconnect -> showLabel()
+ }
+ }
+ is TunnelState.Connecting -> showLabel()
+ is TunnelState.Connected -> showLabel()
+ is TunnelState.Error -> showLocation()
+ }
+ }
+
+ constructor(context: Context) : super(context) {}
+
+ constructor(context: Context, attributes: AttributeSet) : super(context, attributes) {}
+
+ constructor(context: Context, attributes: AttributeSet, defaultStyleAttribute: Int) :
+ super(context, attributes, defaultStyleAttribute) {}
+
+ constructor(
+ context: Context,
+ attributes: AttributeSet,
+ defaultStyleAttribute: Int,
+ defaultStyleResource: Int
+ ) : super(context, attributes, defaultStyleAttribute, defaultStyleResource) {}
+
+ private fun showLabel() {
+ updateButton(buttonWithLabel, true)
+ updateButton(buttonWithLocation, false)
+ }
+
+ private fun showLocation() {
+ updateButton(buttonWithLabel, false)
+ updateButton(buttonWithLocation, true)
+ }
+
+ private fun updateButton(button: View, show: Boolean) {
+ button.apply {
+ setEnabled(show)
+
+ visibility = if (show) {
+ View.VISIBLE
+ } else {
+ View.INVISIBLE
+ }
+ }
+ }
+}
diff --git a/android/src/main/res/layout/switch_location_button.xml b/android/src/main/res/layout/switch_location_button.xml
new file mode 100644
index 0000000000..d9ed79956f
--- /dev/null
+++ b/android/src/main/res/layout/switch_location_button.xml
@@ -0,0 +1,14 @@
+<merge xmlns:android="http://schemas.android.com/apk/res/android">
+ <Button android:id="@+id/button_with_label"
+ android:layout_gravity="bottom"
+ android:paddingHorizontal="9dp"
+ android:text="@string/switch_location"
+ style="@style/White20Button" />
+ <Button android:id="@+id/button_with_location"
+ android:layout_gravity="bottom"
+ android:paddingHorizontal="9dp"
+ android:text="@string/switch_location"
+ android:drawableRight="@drawable/icon_chevron"
+ android:visibility="invisible"
+ style="@style/White20Button" />
+</merge>