Skip to content

Commit

Permalink
Use Duration instead of Period
Browse files Browse the repository at this point in the history
  • Loading branch information
kl authored and Rawa committed Oct 7, 2024
1 parent 2912d6f commit cd084f8
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import net.mullvad.mullvadvpn.lib.model.TunnelState
import net.mullvad.mullvadvpn.repository.InAppNotification
import net.mullvad.mullvadvpn.ui.VersionInfo
import org.joda.time.DateTime
import org.joda.time.Period
import org.joda.time.Duration
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -572,7 +572,9 @@ class ConnectScreenTest {
deviceName = "",
daysLeftUntilExpiry = null,
inAppNotification =
InAppNotification.AccountExpiry(Period(DateTime.now(), expiryDate)),
InAppNotification.AccountExpiry(
Duration(DateTime.now(), expiryDate)
),
isPlayBuild = false,
)
)
Expand Down Expand Up @@ -633,7 +635,9 @@ class ConnectScreenTest {
deviceName = "",
daysLeftUntilExpiry = null,
inAppNotification =
InAppNotification.AccountExpiry(Period(DateTime.now(), expiryDate)),
InAppNotification.AccountExpiry(
Duration(DateTime.now(), expiryDate)
),
isPlayBuild = false,
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import net.mullvad.mullvadvpn.lib.theme.color.warning
import net.mullvad.mullvadvpn.repository.InAppNotification
import net.mullvad.mullvadvpn.ui.VersionInfo
import net.mullvad.mullvadvpn.ui.notification.StatusLevel
import org.joda.time.Period
import org.joda.time.Duration

@Preview
@Composable
Expand All @@ -48,7 +48,7 @@ private fun PreviewNotificationBanner() {
InAppNotification.UnsupportedVersion(
versionInfo = VersionInfo(currentVersion = "1.0", isSupported = false)
),
InAppNotification.AccountExpiry(expiry = Period.ZERO),
InAppNotification.AccountExpiry(expiry = Duration.ZERO),
InAppNotification.TunnelStateBlocked,
InAppNotification.NewDevice("Courageous Turtle"),
InAppNotification.TunnelStateError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package net.mullvad.mullvadvpn.compose.extensions

import android.content.res.Resources
import net.mullvad.mullvadvpn.R
import org.joda.time.DateTime
import org.joda.time.Duration
import org.joda.time.Period

fun Resources.getExpiryQuantityString(accountExpiry: Period): String {
return if (accountExpiry.isNegative() || accountExpiry == Period.ZERO) {
fun Resources.getExpiryQuantityString(accountExpiry: Duration): String {
val expiryPeriod = Period(DateTime.now(), accountExpiry)
return if (accountExpiry.millis <= 0) {
getString(R.string.out_of_time)
} else if (accountExpiry.years > 0) {
getRemainingText(this, R.plurals.years_left, accountExpiry.years)
} else if (accountExpiry.months >= 3) {
getRemainingText(this, R.plurals.months_left, accountExpiry.months)
} else if (accountExpiry.months > 0 || accountExpiry.days >= 1) {
getRemainingText(this, R.plurals.days_left, accountExpiry.days)
} else if (expiryPeriod.years > 0) {
getRemainingText(this, R.plurals.years_left, expiryPeriod.years)
} else if (expiryPeriod.months >= 3) {
getRemainingText(this, R.plurals.months_left, expiryPeriod.months)
} else if (expiryPeriod.months > 0 || expiryPeriod.days >= 1) {
getRemainingText(this, R.plurals.days_left, expiryPeriod.days)
} else {
getString(R.string.less_than_a_day_left)
}
Expand All @@ -21,5 +24,3 @@ fun Resources.getExpiryQuantityString(accountExpiry: Period): String {
private fun getRemainingText(resources: Resources, pluralId: Int, quantity: Int): String {
return resources.getQuantityString(pluralId, quantity, quantity)
}

fun Period.isNegative() = toStandardDuration().millis < 0
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import net.mullvad.mullvadvpn.usecase.AccountExpiryInAppNotificationUseCase
import net.mullvad.mullvadvpn.usecase.NewDeviceNotificationUseCase
import net.mullvad.mullvadvpn.usecase.TunnelStateNotificationUseCase
import net.mullvad.mullvadvpn.usecase.VersionNotificationUseCase
import org.joda.time.Period
import org.joda.time.Duration

enum class StatusLevel {
Error,
Expand All @@ -38,7 +38,7 @@ sealed class InAppNotification {
override val priority: Long = 999
}

data class AccountExpiry(val expiry: Period) : InAppNotification() {
data class AccountExpiry(val expiry: Duration) : InAppNotification() {
override val statusLevel = StatusLevel.Warning
override val priority: Long = 1001
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import net.mullvad.mullvadvpn.usecase.AccountExpiryInAppNotificationUseCase
import net.mullvad.mullvadvpn.usecase.NewDeviceNotificationUseCase
import net.mullvad.mullvadvpn.usecase.TunnelStateNotificationUseCase
import net.mullvad.mullvadvpn.usecase.VersionNotificationUseCase
import org.joda.time.Period
import org.joda.time.Duration
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -81,7 +81,7 @@ class InAppNotificationControllerTest {
val unsupportedVersion = InAppNotification.UnsupportedVersion(mockk())
versionNotifications.value = listOf(unsupportedVersion)

val accountExpiry = InAppNotification.AccountExpiry(Period.ZERO)
val accountExpiry = InAppNotification.AccountExpiry(Duration.ZERO)
accountExpiryNotifications.value = listOf(accountExpiry)

inAppNotificationController.notifications.test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class AccountExpiryInAppNotificationUseCaseTest {
advanceTimeBy(ACCOUNT_EXPIRY_IN_APP_NOTIFICATION_UPDATE_INTERVAL.millis)
// Advance past the delay after the while loop:
advanceTimeBy(ACCOUNT_EXPIRY_IN_APP_NOTIFICATION_UPDATE_INTERVAL.millis)
assertEquals(Period.ZERO, getExpiryNotificationPeriod(expectMostRecentItem()))
assertEquals(Duration.ZERO, getExpiryNotificationDuration(expectMostRecentItem()))
expectNoEvents()
}
}
Expand All @@ -134,16 +134,15 @@ class AccountExpiryInAppNotificationUseCaseTest {
expiry: DateTime,
notifications: List<InAppNotification>,
) {
val notificationPeriod = getExpiryNotificationPeriod(notifications)
val notificationDuration = getExpiryNotificationDuration(notifications)
val periodNow = Period(DateTime.now(), expiry)
assertTrue(periodNow.toStandardDuration() <= notificationPeriod.toStandardDuration())
assertTrue(periodNow.toStandardDuration() <= notificationDuration)
assertTrue(
periodNow.toStandardDuration().plus(Duration.standardSeconds(5)) >
notificationPeriod.toStandardDuration()
periodNow.toStandardDuration().plus(Duration.standardSeconds(5)) > notificationDuration
)
}

private fun getExpiryNotificationPeriod(notifications: List<InAppNotification>): Period {
private fun getExpiryNotificationDuration(notifications: List<InAppNotification>): Duration {
assertTrue(notifications.size == 1, "Expected a single notification")
val n = notifications[0]
if (n !is InAppNotification.AccountExpiry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import org.joda.time.DateTime
import org.joda.time.Duration
import org.joda.time.Period

fun expiryTickerFlow(
expiry: DateTime,
tickStart: Duration,
updateInterval: (expiry: DateTime) -> Duration,
): Flow<Period> = flow {
): Flow<Duration> = flow {
expiry.millisFromNow().let { expiryMillis ->
if (expiryMillis <= 0) {
// Has expired.
emit(Period.ZERO)
emit(Duration.ZERO)
return@flow
}
if (expiryMillis > tickStart.millis) {
Expand All @@ -27,7 +26,7 @@ fun expiryTickerFlow(
var currentUpdateInterval = updateInterval(expiry).millis

do {
emit(Period(DateTime.now(), expiry))
emit(Duration(DateTime.now(), expiry))
delay(millisUntilNextUpdate(expiry.millisFromNow(), currentUpdateInterval))
currentUpdateInterval = updateInterval(expiry).millis
} while (hasAnotherEmission(expiry.millisFromNow(), currentUpdateInterval))
Expand All @@ -36,7 +35,7 @@ fun expiryTickerFlow(
delay(expiry.millisFromNow())

// We have now expired.
emit(Period.ZERO)
emit(Duration.ZERO)
}

private fun millisUntilNextUpdate(
Expand Down

0 comments on commit cd084f8

Please sign in to comment.