diff options
| author | Albin <albin@mullvad.net> | 2022-07-21 11:28:53 +0200 |
|---|---|---|
| committer | Albin <albin@mullvad.net> | 2022-07-21 11:28:53 +0200 |
| commit | a8d3d1535cd100d62dcae43fbdb69bd726666976 (patch) | |
| tree | 3f74b50829d9d72c48271f53339f804aee06aa9e /android/app/src | |
| parent | c8552e08a4d3bf3e43a8375b3b4917d7487dac2b (diff) | |
| parent | 34dcb5122e2161fc821d6dd47676b272efa3b224 (diff) | |
| download | mullvadvpn-a8d3d1535cd100d62dcae43fbdb69bd726666976.tar.xz mullvadvpn-a8d3d1535cd100d62dcae43fbdb69bd726666976.zip | |
Merge branch 'fix-expiry-fetch-and-notification'
Diffstat (limited to 'android/app/src')
2 files changed, 16 insertions, 11 deletions
diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt index 3c1b2f0bed..8d1e7dd7e7 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt @@ -7,7 +7,6 @@ import kotlinx.coroutines.channels.ClosedReceiveChannelException import kotlinx.coroutines.channels.actor import kotlinx.coroutines.channels.sendBlocking import kotlinx.coroutines.flow.collect -import kotlinx.coroutines.flow.lastOrNull import net.mullvad.mullvadvpn.ipc.Event import net.mullvad.mullvadvpn.ipc.Request import net.mullvad.mullvadvpn.model.AccountCreationResult @@ -43,10 +42,17 @@ class AccountCache(private val endpoint: ServiceEndpoint) { private var accountExpiry by onAccountExpiryChange.notifiable() private var accountHistory by onAccountHistoryChange.notifiable() + private var cachedAccountToken: String? = null + private var cachedCreatedAccountToken: String? = null + + val isNewAccount: Boolean + get() = cachedAccountToken == cachedCreatedAccountToken + init { jobTracker.newBackgroundJob("autoFetchAccountExpiry") { daemon.await().deviceStateUpdates.collect { deviceState -> accountExpiry = deviceState.token() + .also { cachedAccountToken = it } ?.let { fetchAccountExpiry(it) } ?: AccountExpiry.Missing } } @@ -76,8 +82,8 @@ class AccountCache(private val endpoint: ServiceEndpoint) { registerHandler(Request.FetchAccountExpiry::class) { _ -> jobTracker.newBackgroundJob("fetchAccountExpiry") { - accountExpiry = - accountToken()?.let { fetchAccountExpiry(it) } ?: AccountExpiry.Missing + accountExpiry = cachedAccountToken + ?.let { fetchAccountExpiry(it) } ?: AccountExpiry.Missing } } @@ -104,10 +110,6 @@ class AccountCache(private val endpoint: ServiceEndpoint) { commandChannel.close() } - private suspend fun accountToken(): String? { - return daemon.await().deviceStateUpdates.lastOrNull()?.token() - } - private fun spawnActor() = GlobalScope.actor<Command>(Dispatchers.Default, Channel.UNLIMITED) { try { for (command in channel) { @@ -129,9 +131,12 @@ class AccountCache(private val endpoint: ServiceEndpoint) { private suspend fun doCreateAccount() { daemon.await().createNewAccount() - .let { newAccountNumber -> - if (newAccountNumber != null) { - AccountCreationResult.Success(newAccountNumber) + .also { newAccountToken -> + cachedCreatedAccountToken = newAccountToken + } + .let { newAccountToken -> + if (newAccountToken != null) { + AccountCreationResult.Success(newAccountToken) } else { AccountCreationResult.Failure } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/AccountExpiryNotification.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/AccountExpiryNotification.kt index 8c27f985a2..3181f2ee95 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/AccountExpiryNotification.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/AccountExpiryNotification.kt @@ -67,7 +67,7 @@ class AccountExpiryNotification( val expiryDate = expiry.date() val durationUntilExpiry = expiryDate?.remainingTime() - if (durationUntilExpiry?.isCloseToExpiry() == true) { + if (accountCache.isNewAccount.not() && durationUntilExpiry?.isCloseToExpiry() == true) { val notification = build(expiryDate, durationUntilExpiry) channel.notificationManager.notify(NOTIFICATION_ID, notification) jobTracker.newUiJob("scheduleUpdate") { scheduleUpdate() } |
