fix: enforce provider availability on restore
This commit is contained in:
@@ -12,6 +12,7 @@ import com.flagship.abox.manager.domain.SessionCoordinator
|
||||
import com.flagship.abox.manager.domain.Username
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
@@ -61,6 +62,15 @@ class LoginViewModel @Inject constructor(
|
||||
},
|
||||
isLoadingPreferences = false,
|
||||
)
|
||||
} catch (error: CancellationException) {
|
||||
throw error
|
||||
} catch (_: Exception) {
|
||||
if (profile?.id == value.id) {
|
||||
_uiState.value = _uiState.value.copy(
|
||||
isLoadingPreferences = false,
|
||||
errorMessage = "Unable to load saved login settings",
|
||||
)
|
||||
}
|
||||
} finally {
|
||||
loadingJob = null
|
||||
}
|
||||
|
||||
+12
-2
@@ -7,6 +7,8 @@ import com.flagship.abox.manager.domain.ProviderProfile
|
||||
import com.flagship.abox.manager.domain.ProviderProfileStore
|
||||
import com.flagship.abox.manager.domain.Session
|
||||
import com.flagship.abox.manager.domain.SessionCoordinator
|
||||
import com.flagship.abox.manager.ui.provider.Availability
|
||||
import com.flagship.abox.manager.ui.provider.ProviderAvailability
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
@@ -19,7 +21,8 @@ import kotlinx.coroutines.launch
|
||||
@HiltViewModel
|
||||
class AppSessionViewModel @Inject constructor(
|
||||
private val coordinator: SessionCoordinator,
|
||||
profileStore: ProviderProfileStore,
|
||||
private val profileStore: ProviderProfileStore,
|
||||
private val availability: ProviderAvailability,
|
||||
) : ViewModel() {
|
||||
private val bootstrapResult = MutableStateFlow<DomainResult<Session?>?>(null)
|
||||
|
||||
@@ -38,7 +41,13 @@ class AppSessionViewModel @Inject constructor(
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
bootstrapResult.value = coordinator.bootstrap()
|
||||
val activeProfile = profileStore.activeProfile()
|
||||
if (activeProfile != null && availability.forType(activeProfile.type) is Availability.Disabled) {
|
||||
coordinator.switchProvider()
|
||||
bootstrapResult.value = DomainResult.Success(null)
|
||||
} else {
|
||||
bootstrapResult.value = coordinator.bootstrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +57,7 @@ class AppSessionViewModel @Inject constructor(
|
||||
bootstrapError: String?,
|
||||
): AppSessionUiState = when {
|
||||
profile == null -> AppSessionUiState.ProviderSelection
|
||||
availability.forType(profile.type) is Availability.Disabled -> AppSessionUiState.ProviderSelection
|
||||
session != null -> AppSessionUiState.Authenticated(profile)
|
||||
else -> AppSessionUiState.Login(profile, bootstrapError)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user