diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-08-06 12:02:24 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2019-08-06 12:02:24 -0300 |
| commit | b34d0ead64b0101bb4656a0e24865fb4b22af3ed (patch) | |
| tree | 4b223343a88550ccfb8d1b19a565bf5e2348c60c | |
| parent | 00e6702f059d06c60f015993e44a0eca304b248e (diff) | |
| parent | dbd2071458ca24ecbe401cb19e8362f1edb4a948 (diff) | |
| download | mullvadvpn-b34d0ead64b0101bb4656a0e24865fb4b22af3ed.tar.xz mullvadvpn-b34d0ead64b0101bb4656a0e24865fb4b22af3ed.zip | |
Merge branch 'multi-line-switch-location-button'
5 files changed, 46 insertions, 3 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt index a0c94873f4..09aadb0a7a 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt @@ -86,7 +86,7 @@ class ConnectFragment : Fragment() { onDisconnect = { connectionProxy.disconnect() } } - switchLocationButton = SwitchLocationButton(view) + switchLocationButton = SwitchLocationButton(view, resources) switchLocationButton.onClick = { openSwitchLocationScreen() } updateKeyStatusJob = updateKeyStatus(keyStatusListener.keyStatus) diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt index 4d3dae8159..f86fac6768 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt @@ -5,18 +5,26 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Job +import android.content.res.Resources import android.graphics.drawable.Drawable +import android.text.TextUtils.TruncateAt import android.view.View +import android.view.ViewGroup.MarginLayoutParams import android.widget.Button import net.mullvad.mullvadvpn.model.ActionAfterDisconnect import net.mullvad.mullvadvpn.model.TunnelState import net.mullvad.mullvadvpn.relaylist.RelayItem -class SwitchLocationButton(val parentView: View) { +class SwitchLocationButton(val parentView: View, val resources: Resources) { private val button: Button = parentView.findViewById(R.id.switch_location) private val chevron: Drawable = button.compoundDrawables[2] + private val normalButtonHeight = resources.getDimensionPixelSize(R.dimen.normal_button_height) + private val tallButtonHeight = resources.getDimensionPixelSize(R.dimen.tall_button_height) + private val topMargin = tallButtonHeight - normalButtonHeight + + private var tall = false private var updateJob: Job? = null var location: RelayItem? = null @@ -35,6 +43,7 @@ class SwitchLocationButton(val parentView: View) { init { button.setOnClickListener { onClick?.invoke() } + button.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> resizeIfNecessary() } } fun onDestroy() { @@ -65,6 +74,7 @@ class SwitchLocationButton(val parentView: View) { private fun showLabel() { button.setText(R.string.switch_location) button.setCompoundDrawables(null, null, null, null) + resizeIfNecessary() } private fun showLocation() { @@ -75,6 +85,35 @@ class SwitchLocationButton(val parentView: View) { } else { button.setText(locationName) button.setCompoundDrawables(null, null, chevron, null) + resizeIfNecessary() + } + } + + private fun resizeIfNecessary() { + val layoutParams = button.layoutParams + + if (button.lineCount > 1 && !tall) { + tall = true + + if (layoutParams is MarginLayoutParams) { + layoutParams.height = tallButtonHeight + layoutParams.topMargin = 0 + } + + button.maxLines = 2 + button.ellipsize = TruncateAt.END + button.requestLayout() + } else if (button.lineCount <= 1 && tall) { + tall = false + + if (layoutParams is MarginLayoutParams) { + layoutParams.height = normalButtonHeight + layoutParams.topMargin = topMargin + } + + button.maxLines = -1 + button.ellipsize = null + button.requestLayout() } } } diff --git a/android/src/main/res/layout/connect.xml b/android/src/main/res/layout/connect.xml index 579e3c6a4e..5c7849c23e 100644 --- a/android/src/main/res/layout/connect.xml +++ b/android/src/main/res/layout/connect.xml @@ -234,7 +234,9 @@ android:padding="24dp" > <Button android:id="@+id/switch_location" + android:layout_marginTop="20dp" android:layout_marginBottom="16dp" + android:paddingHorizontal="8dp" android:text="@string/switch_location" android:drawableRight="@drawable/icon_chevron" android:paddingRight="8dp" diff --git a/android/src/main/res/values/dimensions.xml b/android/src/main/res/values/dimensions.xml index 41d910fb6c..800e1ed0ac 100644 --- a/android/src/main/res/values/dimensions.xml +++ b/android/src/main/res/values/dimensions.xml @@ -4,4 +4,6 @@ <dimen name="relay_row_padding">60dp</dimen> <dimen name="relay_list_divider">1dp</dimen> <dimen name="account_input_corner_radius">4dp</dimen> + <dimen name="normal_button_height">44dp</dimen> + <dimen name="tall_button_height">64dp</dimen> </resources> diff --git a/android/src/main/res/values/styles.xml b/android/src/main/res/values/styles.xml index 2ace57e88b..2e5b22613b 100644 --- a/android/src/main/res/values/styles.xml +++ b/android/src/main/res/values/styles.xml @@ -14,7 +14,7 @@ </style> <style name="Button" parent="Widget.AppCompat.Button.Borderless"> - <item name="android:layout_height">44dp</item> + <item name="android:layout_height">@dimen/normal_button_height</item> <item name="android:layout_width">match_parent</item> <item name="android:textAllCaps">false</item> <item name="android:textColor">@color/white</item> |
