Fix crash when starting app with no available network.

This commit is contained in:
Benoit Marty 2023-05-04 12:02:09 +02:00
parent 3727b653ba
commit 613dc3d7fa
6 changed files with 19 additions and 14 deletions

View file

@ -196,9 +196,7 @@ internal class RustCryptoService @Inject constructor(
override suspend fun fetchDevicesList(): List<DeviceInfo> {
val devicesList: List<DeviceInfo>
withContext(coroutineDispatchers.io) {
devicesList = tryOrNull {
getDevicesTask.execute(Unit).devices
}.orEmpty()
devicesList = getDevicesTask.execute(Unit).devices.orEmpty()
cryptoStore.saveMyDevicesInfo(devicesList)
}
return devicesList
@ -247,7 +245,7 @@ internal class RustCryptoService @Inject constructor(
cryptoCoroutineScope.launch(coroutineDispatchers.io) {
cryptoStore.open()
// Just update
fetchDevicesList()
tryOrNull { fetchDevicesList() }
cryptoStore.tidyUpDataBase()
}
}

View file

@ -40,6 +40,7 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.sample
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.crypto.model.DeviceInfo
import org.matrix.android.sdk.api.session.getUserOrDefault
@ -101,7 +102,7 @@ class UnknownDeviceDetectorSharedViewModel @AssistedInject constructor(
session.flow().liveUserCryptoDevices(session.myUserId),
session.flow().liveMyDevicesInfo(),
session.flow().liveCrossSigningPrivateKeys(),
) { cryptoList, infoList, pInfo ->
) { cryptoList, infoList, pInfo ->
Timber.v("## Detector trigger ${cryptoList.map { "${it.deviceId} ${it.trustLevel}" }}")
Timber.v("## Detector trigger canCrossSign ${pInfo.get().selfSigned != null}")
@ -146,13 +147,13 @@ class UnknownDeviceDetectorSharedViewModel @AssistedInject constructor(
.sample(5_000)
.onEach {
// If we have a new crypto device change, we might want to trigger refresh of device info
session.cryptoService().fetchDevicesList()
tryOrNull { session.cryptoService().fetchDevicesList() }
}
.launchIn(viewModelScope)
// trigger a refresh of lastSeen / last Ip
viewModelScope.launch {
session.cryptoService().fetchDevicesList()
tryOrNull { session.cryptoService().fetchDevicesList() }
}
}

View file

@ -76,6 +76,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.gujun.android.span.span
import org.matrix.android.sdk.api.extensions.getFingerprintHumanReadable
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.raw.RawService
import org.matrix.android.sdk.api.session.crypto.crosssigning.isVerified
import org.matrix.android.sdk.api.session.crypto.model.DeviceInfo
@ -646,9 +647,11 @@ class VectorSettingsSecurityPrivacyFragment :
}
}
// TODO Move to a ViewModel...
val devicesList = session.cryptoService().fetchDevicesList()
withContext(Dispatchers.Main) {
refreshCryptographyPreference(devicesList)
val devicesList = tryOrNull { session.cryptoService().fetchDevicesList() }
devicesList?.let {
withContext(Dispatchers.Main) {
refreshCryptographyPreference(it)
}
}
}
}

View file

@ -52,6 +52,7 @@ import org.matrix.android.sdk.api.auth.data.LoginFlowTypes
import org.matrix.android.sdk.api.auth.registration.RegistrationFlowResponse
import org.matrix.android.sdk.api.auth.registration.nextUncompletedStage
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.failure.Failure
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.crypto.model.CryptoDeviceInfo
@ -190,7 +191,7 @@ class DevicesViewModel @AssistedInject constructor(
.sample(5_000)
.onEach {
// If we have a new crypto device change, we might want to trigger refresh of device info
session.cryptoService().fetchDevicesList()
tryOrNull { session.cryptoService().fetchDevicesList() }
}
.launchIn(viewModelScope)
@ -203,7 +204,7 @@ class DevicesViewModel @AssistedInject constructor(
refreshSource.stream().throttleFirst(4_000)
.onEach {
session.cryptoService().fetchDevicesList()
tryOrNull { session.cryptoService().fetchDevicesList() }
session.cryptoService().downloadKeysIfNeeded(listOf(session.myUserId), true)
}
.launchIn(viewModelScope)

View file

@ -22,6 +22,7 @@ import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.sample
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.flow.flow
import javax.inject.Inject
import kotlin.time.Duration.Companion.seconds
@ -40,7 +41,7 @@ class RefreshDevicesOnCryptoDevicesChangeUseCase @Inject constructor(
.sample(samplingPeriodMs)
.onEach {
// If we have a new crypto device change, we might want to trigger refresh of device info
session.cryptoService().fetchDevicesList()
tryOrNull { session.cryptoService().fetchDevicesList() }
}
.collect()
}

View file

@ -17,6 +17,7 @@
package im.vector.app.features.settings.devices.v2
import im.vector.app.core.di.ActiveSessionHolder
import org.matrix.android.sdk.api.extensions.tryOrNull
import javax.inject.Inject
class RefreshDevicesUseCase @Inject constructor(
@ -24,7 +25,7 @@ class RefreshDevicesUseCase @Inject constructor(
) {
suspend fun execute() {
activeSessionHolder.getSafeActiveSession()?.let { session ->
session.cryptoService().fetchDevicesList()
tryOrNull { session.cryptoService().fetchDevicesList() }
session.cryptoService().downloadKeysIfNeeded(listOf(session.myUserId), true)
}
}