From bd2a625f7e7b6ca83aa637971214fb5c51aff6dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B6ransson?= Date: Mon, 29 Jul 2024 16:57:35 +0200 Subject: [PATCH] Allow error on getVersionInfo --- .../AppVersionInfoRepository.kt | 28 +++++++++++++++---- .../lib/daemon/grpc/ManagementService.kt | 10 +++++-- .../lib/model/GetVersionInfoError.kt | 5 ++++ 3 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/lib/model/GetVersionInfoError.kt diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/AppVersionInfoRepository.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/AppVersionInfoRepository.kt index 63ce64cd06a8..edf5dba1310b 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/AppVersionInfoRepository.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/AppVersionInfoRepository.kt @@ -1,17 +1,33 @@ package net.mullvad.mullvadvpn.ui.serviceconnection -import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.SharingStarted.Companion.WhileSubscribed +import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.stateIn import net.mullvad.mullvadvpn.lib.daemon.grpc.ManagementService import net.mullvad.mullvadvpn.lib.model.BuildVersion import net.mullvad.mullvadvpn.ui.VersionInfo class AppVersionInfoRepository( private val buildVersion: BuildVersion, - private val managementService: ManagementService + private val managementService: ManagementService, + private val dispatcher: CoroutineDispatcher = Dispatchers.IO ) { - fun versionInfo(): Flow = - managementService.versionInfo.map { appVersionInfo -> - VersionInfo(currentVersion = buildVersion.name, isSupported = appVersionInfo.supported) - } + fun versionInfo(): StateFlow = + managementService.versionInfo + .map { appVersionInfo -> + VersionInfo( + currentVersion = buildVersion.name, + isSupported = appVersionInfo.supported + ) + } + .stateIn( + CoroutineScope(dispatcher), + WhileSubscribed(), + // By default we assume we are supported + VersionInfo(currentVersion = buildVersion.name, isSupported = true) + ) } diff --git a/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/ManagementService.kt b/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/ManagementService.kt index 7c879bc9dfbd..191b79142185 100644 --- a/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/ManagementService.kt +++ b/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/ManagementService.kt @@ -73,6 +73,7 @@ import net.mullvad.mullvadvpn.lib.model.GetAccountDataError import net.mullvad.mullvadvpn.lib.model.GetAccountHistoryError import net.mullvad.mullvadvpn.lib.model.GetDeviceListError import net.mullvad.mullvadvpn.lib.model.GetDeviceStateError +import net.mullvad.mullvadvpn.lib.model.GetVersionInfoError import net.mullvad.mullvadvpn.lib.model.LoginAccountError import net.mullvad.mullvadvpn.lib.model.NewAccessMethodSetting import net.mullvad.mullvadvpn.lib.model.ObfuscationSettings @@ -279,8 +280,11 @@ class ManagementService( private suspend fun getRelayList(): ModelRelayList = grpc.getRelayLocations(Empty.getDefaultInstance()).toDomain() - private suspend fun getVersionInfo(): ModelAppVersionInfo = - grpc.getVersionInfo(Empty.getDefaultInstance()).toDomain() + // On release build this will return error until services have published the new beta, daemon + // will get 404 until the api have been published, thus we need to ignore error downstream. + private suspend fun getVersionInfo(): Either = + Either.catch { grpc.getVersionInfo(Empty.getDefaultInstance()).toDomain() } + .mapLeft { GetVersionInfoError.Unknown(it) } suspend fun logoutAccount() { grpc.logoutAccount(Empty.getDefaultInstance()) @@ -320,7 +324,7 @@ class ManagementService( async { _mutableTunnelState.update { getTunnelState() } }, async { _mutableDeviceState.update { getDeviceState() } }, async { _mutableSettings.update { getSettings() } }, - async { _mutableVersionInfo.update { getVersionInfo() } }, + async { _mutableVersionInfo.update { getVersionInfo().getOrNull() } }, async { _mutableRelayList.update { getRelayList() } }, async { _mutableCurrentAccessMethod.update { getCurrentApiAccessMethod() } } ) diff --git a/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/lib/model/GetVersionInfoError.kt b/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/lib/model/GetVersionInfoError.kt new file mode 100644 index 000000000000..97dc589b7216 --- /dev/null +++ b/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/lib/model/GetVersionInfoError.kt @@ -0,0 +1,5 @@ +package net.mullvad.mullvadvpn.lib.model + +sealed interface GetVersionInfoError { + data class Unknown(val error: Throwable) : GetVersionInfoError +}