-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update in app expiry notifications over time
- Loading branch information
Showing
8 changed files
with
179 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 53 additions & 13 deletions
66
...id/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryNotificationUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,69 @@ | ||
package net.mullvad.mullvadvpn.usecase | ||
|
||
import kotlin.math.roundToInt | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.flatMapLatest | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.flow.map | ||
import net.mullvad.mullvadvpn.lib.model.AccountData | ||
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_DAYS | ||
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 org.joda.time.DateTime | ||
import org.joda.time.Duration | ||
import org.joda.time.Period | ||
|
||
class AccountExpiryNotificationUseCase(private val accountRepository: AccountRepository) { | ||
|
||
operator fun invoke(): Flow<List<InAppNotification>> = | ||
accountRepository.accountData | ||
.map(::accountExpiryNotification) | ||
.flatMapLatest { accountData -> | ||
if (accountData != null) { | ||
flow { | ||
val expiry = accountData.expiryDate | ||
|
||
expiresInMillis(expiry).let { millis -> | ||
if (millis <= 0) { | ||
// has expired | ||
emit(InAppNotification.AccountExpiry(Period.ZERO)) | ||
return@flow | ||
} | ||
delayUntilNotificationThreshold(millis) | ||
} | ||
|
||
emit(InAppNotification.AccountExpiry(Period(DateTime.now(), expiry))) | ||
|
||
val updateMillis = ACCOUNT_EXPIRY_IN_APP_NOTIFICATION_UPDATE_INTERVAL.millis | ||
|
||
// Delay until the next update interval | ||
delay(expiresInMillis(expiry) % updateMillis) | ||
|
||
// Calculate how many times we need to delay and show the notification | ||
// until the expiry time is reached. | ||
val delayCount = | ||
(expiresInMillis(expiry).toDouble() / updateMillis).roundToInt() | ||
|
||
repeat(delayCount) { | ||
emit(InAppNotification.AccountExpiry(Period(DateTime.now(), expiry))) | ||
delay(updateMillis) | ||
} | ||
|
||
emit(InAppNotification.AccountExpiry(Period.ZERO)) | ||
} | ||
} else { | ||
flowOf<InAppNotification?>(null) | ||
} | ||
} | ||
.map(::listOfNotNull) | ||
.distinctUntilChanged() | ||
|
||
private fun accountExpiryNotification(accountData: AccountData?) = | ||
if (accountData != null && accountData.expiryDate.isCloseToExpiring()) { | ||
InAppNotification.AccountExpiry(accountData.expiryDate) | ||
} else null | ||
private fun expiresInMillis(expiry: DateTime): Long = Duration(DateTime.now(), expiry).millis | ||
|
||
private fun DateTime.isCloseToExpiring(): Boolean { | ||
val threeDaysFromNow = | ||
DateTime.now().plusDays(ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD_DAYS) | ||
return isBefore(threeDaysFromNow) | ||
private suspend fun delayUntilNotificationThreshold(expiresInMillis: Long) { | ||
val thresholdMillis = ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD.millis | ||
if (expiresInMillis > thresholdMillis) { | ||
delay(expiresInMillis - thresholdMillis) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 7 additions & 2 deletions
9
...otlin/net/mullvad/mullvadvpn/service/notifications/accountexpiry/AccountExpiryConstant.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
package net.mullvad.mullvadvpn.service.notifications.accountexpiry | ||
|
||
const val ACCOUNT_EXPIRY_POLL_INTERVAL: Long = 15 /* s */ * 1000 /* ms */ | ||
const val ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD_DAYS = 3 | ||
import kotlin.time.Duration.Companion.seconds | ||
import org.joda.time.Duration | ||
|
||
val ACCOUNT_EXPIRY_POLL_INTERVAL = 15.seconds | ||
val ACCOUNT_EXPIRY_IN_APP_NOTIFICATION_UPDATE_INTERVAL: Duration = Duration.standardDays(1) | ||
@Suppress("MagicNumber") | ||
val ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD: Duration = Duration.standardDays(3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters