diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-07-23 19:01:39 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2020-07-28 13:52:59 +0000 |
| commit | 297e65df9fa72c84084f06fd2cbbd4946f02a707 (patch) | |
| tree | e05839bca2121ab4a41acc8617d5c53ea3bd7472 /android | |
| parent | aa9ef3810a37439a01e2d3ee2b024c70022fd221 (diff) | |
| download | mullvadvpn-297e65df9fa72c84084f06fd2cbbd4946f02a707.tar.xz mullvadvpn-297e65df9fa72c84084f06fd2cbbd4946f02a707.zip | |
Create an `AccountCell` widget
Diffstat (limited to 'android')
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/AccountCell.kt | 76 | ||||
| -rw-r--r-- | android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/NavigateCell.kt | 2 |
2 files changed, 77 insertions, 1 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/AccountCell.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/AccountCell.kt new file mode 100644 index 0000000000..15c860ef00 --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/AccountCell.kt @@ -0,0 +1,76 @@ +package net.mullvad.mullvadvpn.ui.widget + +import android.content.Context +import android.graphics.Typeface +import android.util.AttributeSet +import android.util.TypedValue +import android.view.Gravity +import android.widget.TextView +import kotlin.properties.Delegates.observable +import net.mullvad.mullvadvpn.R +import net.mullvad.mullvadvpn.util.TimeLeftFormatter +import org.joda.time.DateTime +import org.joda.time.Duration + +class AccountCell : NavigateCell { + private val formatter = TimeLeftFormatter(resources) + + private val expiredColor = context.getColor(R.color.red) + private val normalColor = context.getColor(R.color.white60) + + private val remainingTimeLabel = TextView(context).apply { + layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.0f) + gravity = Gravity.RIGHT + + resources.getDimensionPixelSize(R.dimen.cell_label_horizontal_padding).let { padding -> + setPadding(padding, 0, padding, 0) + } + + setAllCaps(true) + setTextColor(normalColor) + setTextSize(TypedValue.COMPLEX_UNIT_SP, 13.0f) + setTypeface(null, Typeface.BOLD) + + text = "" + } + + var accountExpiry by observable<DateTime?>(null) { _, _, expiry -> + remainingTimeLabel.apply { + if (expiry != null) { + val remainingTime = Duration(DateTime.now(), expiry) + + if (remainingTime.isShorterThan(Duration.ZERO)) { + setText(R.string.out_of_time) + setTextColor(expiredColor) + } else { + setText(formatter.format(expiry, remainingTime)) + setTextColor(normalColor) + } + } else { + text = "" + } + } + } + + constructor(context: Context) : super(context) {} + + constructor(context: Context, attributes: AttributeSet) : super(context, attributes) {} + + constructor(context: Context, attributes: AttributeSet, defaultStyleAttribute: Int) : + super(context, attributes, defaultStyleAttribute) {} + + constructor( + context: Context, + attributes: AttributeSet, + defaultStyleAttribute: Int, + defaultStyleResource: Int + ) : super(context, attributes, defaultStyleAttribute, defaultStyleResource) {} + + init { + cell.addView(remainingTimeLabel, cell.childCount - 1) + } + + private fun getRemainingText(pluralId: Int, quantity: Int): String { + return resources.getQuantityString(pluralId, quantity, quantity) + } +} diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/NavigateCell.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/NavigateCell.kt index c24d58e9a3..07b687e34a 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/NavigateCell.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/NavigateCell.kt @@ -5,7 +5,7 @@ import android.util.AttributeSet import android.widget.ImageView import net.mullvad.mullvadvpn.R -class NavigateCell : Cell { +open class NavigateCell : Cell { private val chevron = ImageView(context).apply { val width = resources.getDimensionPixelSize(R.dimen.chevron_width) val height = resources.getDimensionPixelSize(R.dimen.chevron_height) |
