Skip to content

Commit

Permalink
Fix remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Oct 17, 2023
1 parent 55c3143 commit 9f68ccb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import androidx.constraintlayout.compose.Dimension
import net.mullvad.mullvadvpn.compose.component.MullvadTopBar
import net.mullvad.mullvadvpn.compose.test.NOTIFICATION_BANNER
import net.mullvad.mullvadvpn.compose.util.rememberPrevious
import net.mullvad.mullvadvpn.lib.theme.AlphaDescription
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens
import net.mullvad.mullvadvpn.lib.theme.color.AlphaDescription
import net.mullvad.mullvadvpn.repository.InAppNotification
import net.mullvad.mullvadvpn.ui.VersionInfo
import net.mullvad.mullvadvpn.ui.notification.StatusLevel
Expand Down Expand Up @@ -86,8 +86,8 @@ fun NotificationBanner(
onClickShowAccount: () -> Unit,
onClickDismissNewDevice: () -> Unit
) {
// Fix for animating to invisible state
val previous = rememberPrevious(current = notification, shouldUpdate = { _, _ -> true })
// Fix for animating t hide
AnimatedVisibility(
visible = notification != null,
enter = slideInVertically(initialOffsetY = { -it }),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package net.mullvad.mullvadvpn.constant

const val ACCOUNT_EXPIRY_POLL_INTERVAL: Long = 15 /* s */ * 1000 /* ms */
const val ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD_DAYS = 3
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@ package net.mullvad.mullvadvpn.usecase

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import net.mullvad.mullvadvpn.constant.ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD_DAYS
import net.mullvad.mullvadvpn.model.AccountExpiry
import net.mullvad.mullvadvpn.repository.AccountRepository
import net.mullvad.mullvadvpn.repository.InAppNotification
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager
import net.mullvad.mullvadvpn.util.flatMapReadyConnectionOrDefault
import org.joda.time.DateTime

class AccountExpiryNotificationUseCase(
private val serviceConnectionManager: ServiceConnectionManager,
private val accountRepository: AccountRepository,
) {
fun notifications(): Flow<List<InAppNotification>> =
serviceConnectionManager.connectionState
.flatMapReadyConnectionOrDefault(flowOf(emptyList())) {
it.container.accountDataSource.accountExpiry
.map { accountExpiry -> accountExpiryNotification(accountExpiry) }
.map(::listOfNotNull)
}
accountRepository.accountExpiryState
.map(::accountExpiryNotification)
.map(::listOfNotNull)
.distinctUntilChanged()

private fun accountExpiryNotification(accountExpiry: AccountExpiry) =
Expand All @@ -28,7 +24,8 @@ class AccountExpiryNotificationUseCase(
} else null

private fun AccountExpiry.isCloseToExpiring(): Boolean {
val threeDaysFromNow = DateTime.now().plusDays(3)
val threeDaysFromNow =
DateTime.now().plusDays(ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD_DAYS)
return this.date()?.isBefore(threeDaysFromNow) == true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.runTest
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
import net.mullvad.mullvadvpn.model.AccountExpiry
import net.mullvad.mullvadvpn.repository.AccountRepository
import net.mullvad.mullvadvpn.repository.InAppNotification
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionAccountDataSource
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionContainer
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionState
import org.joda.time.DateTime
import org.junit.After
import org.junit.Before
Expand All @@ -25,24 +22,17 @@ import org.junit.Test
class AccountExpiryNotificationUseCaseTest {
@get:Rule val testCoroutineRule = TestCoroutineRule()

private val mockServiceConnectionManager: ServiceConnectionManager = mockk()
private val mockServiceConnectionContainer: ServiceConnectionContainer = mockk()
private val serviceConnectionState =
MutableStateFlow<ServiceConnectionState>(ServiceConnectionState.Disconnected)
private val accountExpiry = MutableStateFlow<AccountExpiry>(AccountExpiry.Missing)
private lateinit var accountExpiryNotificationUseCase: AccountExpiryNotificationUseCase

@Before
fun setup() {
MockKAnnotations.init(this)

val accountDataSource = mockk<ServiceConnectionAccountDataSource>()
every { mockServiceConnectionManager.connectionState } returns serviceConnectionState
every { mockServiceConnectionContainer.accountDataSource } returns accountDataSource
every { accountDataSource.accountExpiry } returns accountExpiry
val accountRepository = mockk<AccountRepository>()
every { accountRepository.accountExpiryState } returns accountExpiry

accountExpiryNotificationUseCase =
AccountExpiryNotificationUseCase(mockServiceConnectionManager)
accountExpiryNotificationUseCase = AccountExpiryNotificationUseCase(accountRepository)
}

@After
Expand All @@ -63,11 +53,13 @@ class AccountExpiryNotificationUseCaseTest {
// Arrange, Act, Assert
accountExpiryNotificationUseCase.notifications().test {
assertTrue { awaitItem().isEmpty() }
val expiryDate = DateTime.now().plusDays(2)
accountExpiry.value = AccountExpiry.Available(expiryDate)
serviceConnectionState.value =
ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer)
assertEquals(listOf(InAppNotification.AccountExpiry(expiryDate)), awaitItem())
val closeToExpiry = AccountExpiry.Available(DateTime.now().plusDays(2))
accountExpiry.value = closeToExpiry

assertEquals(
listOf(InAppNotification.AccountExpiry(closeToExpiry.expiryDateTime)),
awaitItem()
)
}
}

Expand All @@ -77,8 +69,6 @@ class AccountExpiryNotificationUseCaseTest {
accountExpiryNotificationUseCase.notifications().test {
assertTrue { awaitItem().isEmpty() }
accountExpiry.value = AccountExpiry.Available(DateTime.now().plusDays(4))
serviceConnectionState.value =
ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer)
expectNoEvents()
}
}
Expand Down

0 comments on commit 9f68ccb

Please sign in to comment.