diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-06-21 22:00:32 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-06-24 17:31:04 +0000 |
| commit | 405b19d5c54cf0e4163d7941c4862964e609160a (patch) | |
| tree | 61cfc49da906ef9ab4cb5124831b74c26aa38f71 | |
| parent | 5a8b3fd40724f23304f41a73f7a8a91538722c2d (diff) | |
| download | mullvadvpn-405b19d5c54cf0e4163d7941c4862964e609160a.tar.xz mullvadvpn-405b19d5c54cf0e4163d7941c4862964e609160a.zip | |
Update button when state changes
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt | 3 | ||||
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt | 40 |
2 files changed, 43 insertions, 0 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt index 39c535c1d7..60cb955496 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt @@ -87,6 +87,7 @@ class ConnectFragment : Fragment() { override fun onDestroyView() { locationInfo.onDestroy() + switchLocationButton.onDestroy() waitForDaemonJob?.cancel() attachListenerJob?.cancel() @@ -186,6 +187,8 @@ class ConnectFragment : Fragment() { private fun updateView(state: TunnelStateTransition) = GlobalScope.launch(Dispatchers.Main) { actionButton.state = state + switchLocationButton.state = state + headerBar.setState(state) notificationBanner.setState(state) status.setState(state) diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt index 72a33dc295..f3d2ac2b67 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt @@ -1,14 +1,54 @@ package net.mullvad.mullvadvpn +import kotlinx.coroutines.launch +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.Job + import android.view.View import android.widget.Button +import net.mullvad.mullvadvpn.model.TunnelStateTransition + class SwitchLocationButton(val parentView: View) { private val button: Button = parentView.findViewById(R.id.switch_location) + private var updateJob: Job? = null + + var state: TunnelStateTransition = TunnelStateTransition.Disconnected() + set(value) { + field = value + update() + } + var onClick: (() -> Unit)? = null init { button.setOnClickListener { onClick?.invoke() } } + + fun onDestroy() { + updateJob?.cancel() + } + + private fun update() { + updateJob?.cancel() + updateJob = GlobalScope.launch(Dispatchers.Main) { + when (state) { + is TunnelStateTransition.Disconnected -> showLocation() + is TunnelStateTransition.Disconnecting -> showLocation() + is TunnelStateTransition.Connecting -> showLabel() + is TunnelStateTransition.Connected -> showLabel() + is TunnelStateTransition.Blocked -> showLocation() + } + } + } + + private fun showLabel() { + button.setText(R.string.switch_location) + } + + private fun showLocation() { + showLabel() + } } |
