diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-03-13 19:56:19 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-03-15 19:18:52 +0000 |
| commit | 614ff99d121f3e09d3b5bfeed3757e2be0c1c20f (patch) | |
| tree | 39d5295ef119b979bbeba5c0bd81fcde6654010e /android | |
| parent | 14e3729cfbcd1cbda0b33f03efbf65325bd3fb97 (diff) | |
| download | mullvadvpn-614ff99d121f3e09d3b5bfeed3757e2be0c1c20f.tar.xz mullvadvpn-614ff99d121f3e09d3b5bfeed3757e2be0c1c20f.zip | |
Refactor to creater helper `ConnectActionButton`
Diffstat (limited to 'android')
3 files changed, 52 insertions, 6 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectActionButton.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectActionButton.kt new file mode 100644 index 0000000000..44358fa6fb --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectActionButton.kt @@ -0,0 +1,42 @@ +package net.mullvad.mullvadvpn + +import android.view.View +import android.widget.Button + +class ConnectActionButton(val parentView: View) { + private val button: Button = parentView.findViewById(R.id.action_button) + + var state = ConnectionState.Disconnected + set(value) { + when (value) { + ConnectionState.Disconnected -> disconnected() + ConnectionState.Connecting -> connecting() + } + + field = value + } + + var onConnect: (() -> Unit)? = null + var onCancel: (() -> Unit)? = null + + init { + button.setOnClickListener { action() } + } + + private fun action() { + when (state) { + ConnectionState.Disconnected -> onConnect?.invoke() + ConnectionState.Connecting -> onCancel?.invoke() + } + } + + private fun disconnected() { + button.setBackgroundResource(R.drawable.green_button_background) + button.setText(R.string.connect) + } + + private fun connecting() { + button.setBackgroundResource(R.drawable.transparent_red_button_background) + button.setText(R.string.cancel) + } +} diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt index eaf086d604..c516c99981 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt @@ -5,11 +5,10 @@ import android.support.v4.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.Button import android.widget.TextView class ConnectFragment : Fragment() { - private lateinit var actionButton: Button + private lateinit var actionButton: ConnectActionButton private lateinit var connectingSpinner: View private lateinit var headerBar: View private lateinit var notificationBanner: View @@ -22,20 +21,19 @@ class ConnectFragment : Fragment() { ): View { val view = inflater.inflate(R.layout.connect, container, false) - actionButton = view.findViewById(R.id.action_button) connectingSpinner = view.findViewById(R.id.connecting_spinner) headerBar = view.findViewById(R.id.header_bar) notificationBanner = view.findViewById(R.id.notification_banner) status = view.findViewById(R.id.connection_status) - actionButton.setOnClickListener { connect() } + actionButton = ConnectActionButton(view) + actionButton.onConnect = { connect() } return view } private fun connect() { - actionButton.setBackgroundResource(R.drawable.transparent_red_button_background) - actionButton.setText(R.string.cancel) + actionButton.state = ConnectionState.Connecting connectingSpinner.visibility = View.VISIBLE notificationBanner.visibility = View.VISIBLE diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectionState.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectionState.kt new file mode 100644 index 0000000000..6badff3362 --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectionState.kt @@ -0,0 +1,6 @@ +package net.mullvad.mullvadvpn + +enum class ConnectionState { + Disconnected, + Connecting, +} |
