diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-04-03 20:33:34 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-04-27 16:21:52 +0000 |
| commit | ed6145f9affc8d8d443ea5d553824f44e6a408c6 (patch) | |
| tree | 2ed591a726dba9451109f607e55b475fbe73b71f /android/src/main/kotlin | |
| parent | e70831b7fa9e020977f9c882c1391c1e508b57fe (diff) | |
| download | mullvadvpn-ed6145f9affc8d8d443ea5d553824f44e6a408c6.tar.xz mullvadvpn-ed6145f9affc8d8d443ea5d553824f44e6a408c6.zip | |
Change `Button` to support showing a spinner
Diffstat (limited to 'android/src/main/kotlin')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/Button.kt | 84 |
1 files changed, 79 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() + } + } + } } |
