Skip to content

Commit

Permalink
Add timeout to login screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun authored and albin-mullvad committed Nov 17, 2023
1 parent 1cd0cbc commit 1a4e589
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package net.mullvad.mullvadvpn.constant

const val LOGIN_TIMEOUT_MILLIS = 10000L // 10 seconds
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package net.mullvad.mullvadvpn.util

import android.view.animation.Animation
import kotlin.coroutines.EmptyCoroutineContext
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.withTimeoutOrNull
import net.mullvad.mullvadvpn.lib.common.util.safeOffer
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionState
import net.mullvad.talpid.util.EventNotifier
Expand Down Expand Up @@ -124,3 +126,6 @@ inline fun <T1, T2, T3, T4, T5, T6, T7, T8, R> combine(
)
}
}

suspend inline fun <T> Deferred<T>.awaitWithTimeoutOrNull(timeout: Long) =
withTimeoutOrNull(timeout) { await() }
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import net.mullvad.mullvadvpn.compose.state.LoginError
import net.mullvad.mullvadvpn.compose.state.LoginState
import net.mullvad.mullvadvpn.compose.state.LoginState.*
import net.mullvad.mullvadvpn.compose.state.LoginUiState
import net.mullvad.mullvadvpn.constant.LOGIN_TIMEOUT_MILLIS
import net.mullvad.mullvadvpn.model.AccountCreationResult
import net.mullvad.mullvadvpn.model.AccountToken
import net.mullvad.mullvadvpn.model.LoginResult
import net.mullvad.mullvadvpn.repository.AccountRepository
import net.mullvad.mullvadvpn.repository.DeviceRepository
import net.mullvad.mullvadvpn.usecase.NewDeviceNotificationUseCase
import net.mullvad.mullvadvpn.util.awaitWithTimeoutOrNull

private const val MINIMUM_LOADING_SPINNER_TIME_MILLIS = 500L

Expand Down Expand Up @@ -81,7 +83,8 @@ class LoginViewModel(
delay(MINIMUM_LOADING_SPINNER_TIME_MILLIS)

val uiState =
when (val result = loginDeferred.await()) {
// If timed out will go to the else branch
when (val result = loginDeferred.awaitWithTimeoutOrNull(LOGIN_TIMEOUT_MILLIS)) {
LoginResult.Ok -> {
launch {
delay(1000)
Expand Down

0 comments on commit 1a4e589

Please sign in to comment.