summaryrefslogtreecommitdiffhomepage
path: root/android/src/main
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-06-05 14:06:22 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-06-08 13:46:41 +0000
commit70a52232c22b6f4933ea1e7de91edcaf5a068ba0 (patch)
tree0499a9b256d72927eb26fbf74901d7dfab517f56 /android/src/main
parent05be799e63292f87d96f5a85f3cc97f94b578e08 (diff)
downloadmullvadvpn-70a52232c22b6f4933ea1e7de91edcaf5a068ba0.tar.xz
mullvadvpn-70a52232c22b6f4933ea1e7de91edcaf5a068ba0.zip
Create `TimeLeftFormatter` helper class
Diffstat (limited to 'android/src/main')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/util/TimeLeftFormatter.kt38
1 files changed, 38 insertions, 0 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/util/TimeLeftFormatter.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/util/TimeLeftFormatter.kt
new file mode 100644
index 0000000000..c3a6aaa1cb
--- /dev/null
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/util/TimeLeftFormatter.kt
@@ -0,0 +1,38 @@
+package net.mullvad.mullvadvpn.util
+
+import android.content.res.Resources
+import net.mullvad.mullvadvpn.R
+import org.joda.time.DateTime
+import org.joda.time.Duration
+import org.joda.time.PeriodType
+
+class TimeLeftFormatter(val resources: Resources) {
+ fun format(accountExpiry: DateTime): String {
+ val remainingTime = Duration(DateTime.now(), accountExpiry)
+
+ return format(accountExpiry, remainingTime)
+ }
+
+ fun format(accountExpiry: DateTime, remainingTime: Duration): String {
+ if (remainingTime.isShorterThan(Duration.ZERO)) {
+ return resources.getString(R.string.out_of_time)
+ } else {
+ val remainingTimeInfo =
+ remainingTime.toPeriodTo(accountExpiry, PeriodType.yearMonthDayTime())
+
+ if (remainingTimeInfo.years > 0) {
+ return getRemainingText(R.plurals.years_left, remainingTimeInfo.years)
+ } else if (remainingTimeInfo.months >= 3) {
+ return getRemainingText(R.plurals.months_left, remainingTimeInfo.months)
+ } else if (remainingTimeInfo.months > 0 || remainingTimeInfo.days >= 1) {
+ return getRemainingText(R.plurals.days_left, remainingTime.standardDays.toInt())
+ } else {
+ return resources.getString(R.string.less_than_a_day_left)
+ }
+ }
+ }
+
+ private fun getRemainingText(pluralId: Int, quantity: Int): String {
+ return resources.getQuantityString(pluralId, quantity, quantity)
+ }
+}