Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update in app expiry notifications over time #6851

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +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.Duration
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -570,7 +571,10 @@ class ConnectScreenTest {
showLocation = false,
deviceName = "",
daysLeftUntilExpiry = null,
inAppNotification = InAppNotification.AccountExpiry(expiryDate),
inAppNotification =
InAppNotification.AccountExpiry(
Duration(DateTime.now(), expiryDate)
),
isPlayBuild = false,
)
)
Expand Down Expand Up @@ -630,7 +634,10 @@ class ConnectScreenTest {
showLocation = false,
deviceName = "",
daysLeftUntilExpiry = null,
inAppNotification = InAppNotification.AccountExpiry(expiryDate),
inAppNotification =
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.DateTime
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 = DateTime.now()),
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 @@ -4,34 +4,20 @@ 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
import org.joda.time.Period

fun Resources.getExpiryQuantityString(accountExpiry: DateTime): String {
val remainingTime = Duration(DateTime.now(), accountExpiry)

return getExpiryQuantityString(this, accountExpiry, remainingTime)
}

private fun getExpiryQuantityString(
resources: Resources,
accountExpiry: DateTime,
remainingTime: Duration,
): String {
if (remainingTime.isShorterThan(Duration.ZERO)) {
return resources.getString(R.string.out_of_time)
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 (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 {
val remainingTimeInfo =
remainingTime.toPeriodTo(accountExpiry, PeriodType.yearMonthDayTime())

return if (remainingTimeInfo.years > 0) {
getRemainingText(resources, R.plurals.years_left, remainingTimeInfo.years)
} else if (remainingTimeInfo.months >= 3) {
getRemainingText(resources, R.plurals.months_left, remainingTimeInfo.months)
} else if (remainingTimeInfo.months > 0 || remainingTimeInfo.days >= 1) {
getRemainingText(resources, R.plurals.days_left, remainingTime.standardDays.toInt())
} else {
resources.getString(R.string.less_than_a_day_left)
}
getString(R.string.less_than_a_day_left)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import net.mullvad.mullvadvpn.repository.SplitTunnelingRepository
import net.mullvad.mullvadvpn.ui.MainActivity
import net.mullvad.mullvadvpn.ui.serviceconnection.AppVersionInfoRepository
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager
import net.mullvad.mullvadvpn.usecase.AccountExpiryNotificationUseCase
import net.mullvad.mullvadvpn.usecase.AccountExpiryInAppNotificationUseCase
import net.mullvad.mullvadvpn.usecase.AvailableProvidersUseCase
import net.mullvad.mullvadvpn.usecase.EmptyPaymentUseCase
import net.mullvad.mullvadvpn.usecase.FilteredRelayListUseCase
Expand Down Expand Up @@ -141,7 +141,7 @@ val uiModule = module {
)
}

single { AccountExpiryNotificationUseCase(get()) }
single { AccountExpiryInAppNotificationUseCase(get()) }
single { TunnelStateNotificationUseCase(get()) }
single { VersionNotificationUseCase(get(), BuildConfig.ENABLE_IN_APP_VERSION_NOTIFICATIONS) }
single { NewDeviceNotificationUseCase(get(), get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import net.mullvad.mullvadvpn.lib.model.ErrorState
import net.mullvad.mullvadvpn.ui.VersionInfo
import net.mullvad.mullvadvpn.usecase.AccountExpiryNotificationUseCase
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.DateTime
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: DateTime) : InAppNotification() {
data class AccountExpiry(val expiry: Duration) : InAppNotification() {
override val statusLevel = StatusLevel.Warning
override val priority: Long = 1001
}
Expand All @@ -50,7 +50,7 @@ sealed class InAppNotification {
}

class InAppNotificationController(
accountExpiryNotificationUseCase: AccountExpiryNotificationUseCase,
accountExpiryInAppNotificationUseCase: AccountExpiryInAppNotificationUseCase,
newDeviceNotificationUseCase: NewDeviceNotificationUseCase,
versionNotificationUseCase: VersionNotificationUseCase,
tunnelStateNotificationUseCase: TunnelStateNotificationUseCase,
Expand All @@ -61,7 +61,7 @@ class InAppNotificationController(
combine(
tunnelStateNotificationUseCase(),
versionNotificationUseCase(),
accountExpiryNotificationUseCase(),
accountExpiryInAppNotificationUseCase(),
newDeviceNotificationUseCase(),
) { a, b, c, d ->
a + b + c + d
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.mullvad.mullvadvpn.usecase

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import net.mullvad.mullvadvpn.lib.shared.AccountRepository
import net.mullvad.mullvadvpn.repository.InAppNotification
import net.mullvad.mullvadvpn.service.notifications.accountexpiry.ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD
import net.mullvad.mullvadvpn.service.notifications.accountexpiry.ACCOUNT_EXPIRY_IN_APP_NOTIFICATION_UPDATE_INTERVAL
import net.mullvad.mullvadvpn.service.notifications.accountexpiry.expiryTickerFlow

class AccountExpiryInAppNotificationUseCase(private val accountRepository: AccountRepository) {

@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
operator fun invoke(): Flow<List<InAppNotification>> =
accountRepository.accountData
.flatMapLatest { accountData ->
if (accountData != null) {
expiryTickerFlow(
expiry = accountData.expiryDate,
tickStart = ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD,
updateInterval = { ACCOUNT_EXPIRY_IN_APP_NOTIFICATION_UPDATE_INTERVAL },
)
.map { expiresInPeriod -> InAppNotification.AccountExpiry(expiresInPeriod) }
} else {
flowOf<InAppNotification?>(null)
}
}
.map(::listOfNotNull)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
import net.mullvad.mullvadvpn.lib.model.ErrorState
import net.mullvad.mullvadvpn.repository.InAppNotification
import net.mullvad.mullvadvpn.repository.InAppNotificationController
import net.mullvad.mullvadvpn.usecase.AccountExpiryNotificationUseCase
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.DateTime
import org.joda.time.Duration
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand All @@ -42,19 +42,19 @@ class InAppNotificationControllerTest {
fun setup() {
MockKAnnotations.init(this)

val accountExpiryNotificationUseCase: AccountExpiryNotificationUseCase = mockk()
val accountExpiryInAppNotificationUseCase: AccountExpiryInAppNotificationUseCase = mockk()
val newDeviceNotificationUseCase: NewDeviceNotificationUseCase = mockk()
val versionNotificationUseCase: VersionNotificationUseCase = mockk()
val tunnelStateNotificationUseCase: TunnelStateNotificationUseCase = mockk()
every { accountExpiryNotificationUseCase.invoke() } returns accountExpiryNotifications
every { accountExpiryInAppNotificationUseCase.invoke() } returns accountExpiryNotifications
every { newDeviceNotificationUseCase.invoke() } returns newDeviceNotifications
every { versionNotificationUseCase.invoke() } returns versionNotifications
every { tunnelStateNotificationUseCase.invoke() } returns tunnelStateNotifications
job = Job()

inAppNotificationController =
InAppNotificationController(
accountExpiryNotificationUseCase,
accountExpiryInAppNotificationUseCase,
newDeviceNotificationUseCase,
versionNotificationUseCase,
tunnelStateNotificationUseCase,
Expand All @@ -81,7 +81,7 @@ class InAppNotificationControllerTest {
val unsupportedVersion = InAppNotification.UnsupportedVersion(mockk())
versionNotifications.value = listOf(unsupportedVersion)

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

inAppNotificationController.notifications.test {
Expand Down
Loading
Loading