diff options
Diffstat (limited to 'android/src/main')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/Button.kt | 84 | ||||
| -rw-r--r-- | android/src/main/res/layout/button.xml | 16 | ||||
| -rw-r--r-- | android/src/main/res/values/attrs.xml | 8 | ||||
| -rw-r--r-- | android/src/main/res/values/styles.xml | 1 |
4 files changed, 104 insertions, 5 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/Button.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/Button.kt index d2d8a987be..2738e2aba6 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/Button.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/Button.kt @@ -2,20 +2,67 @@ 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 net.mullvad.mullvadvpn.R import net.mullvad.mullvadvpn.util.JobTracker -open class Button : android.widget.Button { +open class Button : FrameLayout { + enum class ButtonColor { + Blue, + Green; + + companion object { + internal fun fromCode(code: Int): ButtonColor { + when (code) { + 0 -> return Blue + 1 -> return Green + else -> throw Exception("Invalid buttonColor attribute value") + } + } + } + } + + private val container = + context.getSystemService(Context.LAYOUT_INFLATER_SERVICE).let { service -> + val inflater = service as LayoutInflater + + inflater.inflate(R.layout.button, this) + } + + private val button = container.findViewById<android.widget.Button>(R.id.button) + private val spinner: View = container.findViewById(R.id.spinner) + private var clickJobName: String? = null private var onClickAction: (suspend () -> Unit)? = null protected var jobTracker: JobTracker? = null + var buttonColor: ButtonColor = ButtonColor.Blue + set(value) { + field = value + + val backgroundResource = when (value) { + ButtonColor.Blue -> R.drawable.blue_button_background + ButtonColor.Green -> R.drawable.green_button_background + } + + button.setBackgroundResource(backgroundResource) + } + + var showSpinner = false + constructor(context: Context) : super(context) {} - constructor(context: Context, attributes: AttributeSet) : super(context, attributes) {} + constructor(context: Context, attributes: AttributeSet) : super(context, attributes) { + loadAttributes(attributes) + } constructor(context: Context, attributes: AttributeSet, defaultStyleAttribute: Int) : - super(context, attributes, defaultStyleAttribute) {} + super(context, attributes, defaultStyleAttribute) { + loadAttributes(attributes) + } constructor( context: Context, @@ -23,6 +70,7 @@ open class Button : android.widget.Button { defaultStyleAttribute: Int, defaultStyleResource: Int ) : super(context, attributes, defaultStyleAttribute, defaultStyleResource) { + loadAttributes(attributes) } override fun setEnabled(enabled: Boolean) { @@ -36,8 +84,16 @@ open class Button : android.widget.Button { } init { - setOnClickListener { - jobTracker?.newUiJob(clickJobName!!, onClickAction!!) + button.setOnClickListener { + jobTracker?.newUiJob(clickJobName!!) { + if (showSpinner) { + spinner.visibility = VISIBLE + } + + onClickAction!!.invoke() + + spinner.visibility = GONE + } } } @@ -46,4 +102,22 @@ open class Button : android.widget.Button { jobTracker = tracker onClickAction = action } + + fun setText(textResource: Int) { + button.setText(textResource) + } + + private fun loadAttributes(attributes: AttributeSet) { + var styleableId = R.styleable.Button + + context.theme.obtainStyledAttributes(attributes, styleableId, 0, 0).apply { + try { + button.text = getString(R.styleable.Button_text) ?: "" + buttonColor = ButtonColor.fromCode(getInteger(R.styleable.Button_buttonColor, 0)) + showSpinner = getBoolean(R.styleable.Button_showSpinner, false) + } finally { + recycle() + } + } + } } diff --git a/android/src/main/res/layout/button.xml b/android/src/main/res/layout/button.xml new file mode 100644 index 0000000000..1f7cb39130 --- /dev/null +++ b/android/src/main/res/layout/button.xml @@ -0,0 +1,16 @@ +<merge xmlns:android="http://schemas.android.com/apk/res/android"> + <Button android:id="@+id/button" + android:gravity="center" + android:text="" + style="@style/Button" /> + <ProgressBar android:id="@+id/spinner" + android:layout_width="20dp" + android:layout_height="20dp" + android:layout_marginHorizontal="9dp" + android:layout_gravity="right|center_vertical" + android:indeterminate="true" + android:indeterminateOnly="true" + android:indeterminateDuration="600" + android:indeterminateDrawable="@drawable/icon_spinner" + android:visibility="gone" /> +</merge> diff --git a/android/src/main/res/values/attrs.xml b/android/src/main/res/values/attrs.xml index e97d6cd591..8ffa0ef8df 100644 --- a/android/src/main/res/values/attrs.xml +++ b/android/src/main/res/values/attrs.xml @@ -1,4 +1,12 @@ <resources> + <declare-styleable name="Button"> + <attr name="buttonColor" format="enum"> + <enum name="blue" value="0"/> + <enum name="green" value="1"/> + </attr> + <attr name="showSpinner" format="boolean"/> + <attr name="text" format="reference|string"/> + </declare-styleable> <declare-styleable name="CopyableInformationView"> <attr name="clipboardLabel" format="reference|string"/> <attr name="copiedToast" format="reference|string"/> diff --git a/android/src/main/res/values/styles.xml b/android/src/main/res/values/styles.xml index d20f758ad9..c380a2f51c 100644 --- a/android/src/main/res/values/styles.xml +++ b/android/src/main/res/values/styles.xml @@ -20,6 +20,7 @@ <item name="android:layout_height"> @dimen/normal_button_height</item> <item name="android:layout_width">match_parent</item> + <item name="android:padding">0dp</item> <item name="android:textAllCaps">false</item> <item name="android:textColor">@color/white</item> <item name="android:textSize">20sp</item> |
