Skip to content

Commit

Permalink
Rework out of time
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Dec 8, 2023
1 parent 15388df commit 52c3165
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,24 @@ class OutOfTimeUseCase(
private val messageHandler: MessageHandler
) {

fun isOutOfTime() =
fun isOutOfTime(): Flow<Boolean?> =
combine(pastAccountExpiry(), isTunnelBlockedBecauseOutOfTime()) {
accountExpiryHasPast,
tunnelOutOfTime ->
accountExpiryHasPast or tunnelOutOfTime
reduce(accountExpiryHasPast, tunnelOutOfTime)
}
.distinctUntilChanged()

private fun reduce(vararg outOfTimeProperty: Boolean?): Boolean? =
when {
// If any advertises as out of time
outOfTimeProperty.any { it == true } -> true
// If all advertise as not out of time
outOfTimeProperty.all { it == false } -> false
// If some are unknown
else -> null
}

private fun isTunnelBlockedBecauseOutOfTime() =
messageHandler
.events<Event.TunnelStateChange>()
Expand All @@ -43,7 +53,7 @@ class OutOfTimeUseCase(
?: false
}

private fun pastAccountExpiry(): Flow<Boolean> =
private fun pastAccountExpiry(): Flow<Boolean?> =
combine(
repository.accountExpiryState.map {
if (it is AccountExpiry.Available) {
Expand All @@ -54,12 +64,12 @@ class OutOfTimeUseCase(
},
timeFlow()
) { expiryDate, time ->
expiryDate?.isBefore(time) ?: false
expiryDate?.isBefore(time)
}

private fun timeFlow() = flow {
while (true) {
emit(DateTime.now().plusMinutes(1))
emit(DateTime.now())
delay(accountRefreshInterval)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
Expand Down Expand Up @@ -138,7 +137,7 @@ class ConnectViewModel(
init {
viewModelScope.launch {
// This once we get isOutOfTime true we will navigate to OutOfTime view.
outOfTimeUseCase.isOutOfTime().filter { it }.first()
outOfTimeUseCase.isOutOfTime().first { it == true }
_uiSideEffect.emit(UiSideEffect.OutOfTime)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class OutOfTimeViewModel(
fun start() {
jobScope = CoroutineScope(Job() + Dispatchers.IO)
jobScope.launch {
outOfTimeUseCase.isOutOfTime().first { !it }
outOfTimeUseCase.isOutOfTime().first { it == false }
paymentUseCase.resetPurchaseResult()
_uiSideEffect.tryEmit(UiSideEffect.OpenConnectScreen)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class WelcomeViewModel(
}
}
launch {
outOfTimeUseCase.isOutOfTime().first { !it }
outOfTimeUseCase.isOutOfTime().first { it == false }
paymentUseCase.resetPurchaseResult()
_uiSideEffect.emit(UiSideEffect.OpenConnectScreen)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import org.junit.Before
import org.junit.Test

class OutOfTimeUseCaseTest {

private val mockAccountRepository: AccountRepository = mockk()
private val mockMessageHandler: MessageHandler = mockk()

Expand All @@ -40,7 +39,7 @@ class OutOfTimeUseCaseTest {
fun `No events should result in no expiry`() = runTest {
// Arrange
// Act, Assert
outOfTimeUseCase.isOutOfTime().test { assertEquals(false, awaitItem()) }
outOfTimeUseCase.isOutOfTime().test { assertEquals(null, awaitItem()) }
}

@Test
Expand All @@ -52,7 +51,7 @@ class OutOfTimeUseCaseTest {
val errorChange = Event.TunnelStateChange(tunnelStateError)

outOfTimeUseCase.isOutOfTime().test {
assertEquals(false, awaitItem())
assertEquals(null, awaitItem())
events.emit(errorChange)
assertEquals(true, awaitItem())
}
Expand All @@ -64,22 +63,22 @@ class OutOfTimeUseCaseTest {
val expiredAccountExpiry = AccountExpiry.Available(DateTime.now().minusDays(1))
// Act, Assert
outOfTimeUseCase.isOutOfTime().test {
assertEquals(false, awaitItem())
assertEquals(null, awaitItem())
expiry.emit(expiredAccountExpiry)
assertEquals(true, awaitItem())
}
}

@Test
fun `Account expiry that has not expired should emit nothing`() = runTest {
fun `Account expiry that has not expired should emit false`() = runTest {
// Arrange
val expiredAccountExpiry = AccountExpiry.Available(DateTime.now().plusDays(1))

// Act, Assert
outOfTimeUseCase.isOutOfTime().test {
assertEquals(false, awaitItem())
assertEquals(null, awaitItem())
expiry.emit(expiredAccountExpiry)
expectNoEvents()
assertEquals(false, awaitItem())
}
}

Expand All @@ -90,10 +89,11 @@ class OutOfTimeUseCaseTest {

// Act, Assert
outOfTimeUseCase.isOutOfTime().test {
// Initial event that doesn't change
assertEquals(false, awaitItem())
expiry.emit(expiredAccountExpiry)
// Initial event
assertEquals(null, awaitItem())

expiry.emit(expiredAccountExpiry)
assertEquals(false, awaitItem())
assertEquals(true, awaitItem())
}
}
Expand Down

0 comments on commit 52c3165

Please sign in to comment.