diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-04-01 14:05:45 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-04-27 16:21:51 +0000 |
| commit | 3e96aaf1dc900beef7139912673729fcf37c6626 (patch) | |
| tree | 5ec144948e33869fea4b4f8691d650a9a2d9635b /android/src | |
| parent | a9eb61635a788bff39117a7c969903b6c3c37468 (diff) | |
| download | mullvadvpn-3e96aaf1dc900beef7139912673729fcf37c6626.tar.xz mullvadvpn-3e96aaf1dc900beef7139912673729fcf37c6626.zip | |
Add `whenMissing` attribute to the new widget
Diffstat (limited to 'android/src')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/ui/InformationView.kt | 45 | ||||
| -rw-r--r-- | android/src/main/res/values/attrs.xml | 4 |
2 files changed, 46 insertions, 3 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/InformationView.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/InformationView.kt index 45d5c78ba1..2dfd2f4128 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/InformationView.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/InformationView.kt @@ -9,6 +9,21 @@ import android.widget.TextView import net.mullvad.mullvadvpn.R open class InformationView : LinearLayout { + enum class WhenMissing { + Nothing, + Hide; + + companion object { + internal fun fromCode(code: Int): WhenMissing { + when (code) { + 0 -> return Nothing + 1 -> return Hide + else -> throw Exception("Invalid whenMissing attribute value") + } + } + } + } + private val container = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE).let { service -> val inflater = service as LayoutInflater @@ -22,11 +37,17 @@ open class InformationView : LinearLayout { private val description: TextView = findViewById(R.id.description) private val informationDisplay: TextView = findViewById(R.id.information_display) - var information: String - get() = informationDisplay.text?.toString() ?: "" + var information: String? = null set(value) { + field = value informationDisplay.text = value - setEnabled(value != null) + updateStatus() + } + + var whenMissing = WhenMissing.Nothing + set(value) { + field = value + updateStatus() } var onClick: (() -> Unit)? = null @@ -70,9 +91,27 @@ open class InformationView : LinearLayout { context.theme.obtainStyledAttributes(attributes, styleableId, 0, 0).apply { try { description.text = getString(R.styleable.InformationView_description) ?: "" + whenMissing = WhenMissing.fromCode( + getInteger(R.styleable.InformationView_whenMissing, 0) + ) } finally { recycle() } } } + + private fun updateStatus() { + when (whenMissing) { + WhenMissing.Nothing -> visibility = VISIBLE + WhenMissing.Hide -> { + if (information == null) { + visibility = INVISIBLE + } else { + visibility = VISIBLE + } + } + } + + setEnabled(information != null) + } } diff --git a/android/src/main/res/values/attrs.xml b/android/src/main/res/values/attrs.xml index d8406f4c61..9a51551410 100644 --- a/android/src/main/res/values/attrs.xml +++ b/android/src/main/res/values/attrs.xml @@ -5,5 +5,9 @@ </declare-styleable> <declare-styleable name="InformationView"> <attr name="description" format="reference|string"/> + <attr name="whenMissing" format="enum"> + <enum name="nothing" value="0"/> + <enum name="hide" value="1"/> + </attr> </declare-styleable> </resources> |
