Using unwrap() extension method

This commit is contained in:
Maxime NATUREL 2022-09-08 13:46:26 +02:00
parent 9553fe0648
commit 15cf8b63a0
5 changed files with 20 additions and 16 deletions

View File

@ -27,7 +27,6 @@ import im.vector.app.core.platform.EmptyViewEvents
import im.vector.app.core.platform.VectorViewModel
import im.vector.app.features.settings.devices.v2.overview.GetDeviceFullInfoUseCase
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.onEach
// TODO add unit tests
@ -49,7 +48,6 @@ class SessionDetailsViewModel @AssistedInject constructor(
private fun observeSessionInfo(deviceId: String) {
getDeviceFullInfoUseCase.execute(deviceId)
.mapNotNull { it.getOrNull() }
.onEach { setState { copy(deviceInfo = Success(it.deviceInfo)) } }
.launchIn(viewModelScope)
}

View File

@ -25,8 +25,8 @@ import im.vector.app.features.settings.devices.v2.list.CheckIfSessionIsInactiveU
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.emptyFlow
import org.matrix.android.sdk.api.util.Optional
import org.matrix.android.sdk.api.util.toOptional
import org.matrix.android.sdk.flow.unwrap
import javax.inject.Inject
class GetDeviceFullInfoUseCase @Inject constructor(
@ -36,7 +36,7 @@ class GetDeviceFullInfoUseCase @Inject constructor(
private val checkIfSessionIsInactiveUseCase: CheckIfSessionIsInactiveUseCase,
) {
fun execute(deviceId: String): Flow<Optional<DeviceFullInfo>> {
fun execute(deviceId: String): Flow<DeviceFullInfo> {
return activeSessionHolder.getSafeActiveSession()?.let { session ->
combine(
getCurrentSessionCrossSigningInfoUseCase.execute(),
@ -58,7 +58,7 @@ class GetDeviceFullInfoUseCase @Inject constructor(
null
}
fullInfo.toOptional()
}
}.unwrap()
} ?: emptyFlow()
}
}

View File

@ -54,7 +54,6 @@ class SessionOverviewViewModel @AssistedInject constructor(
private fun observeSessionInfo(deviceId: String) {
getDeviceFullInfoUseCase.execute(deviceId)
.mapNotNull { it.getOrNull() }
.onEach { setState { copy(deviceInfo = Success(it)) } }
.launchIn(viewModelScope)
}

View File

@ -72,6 +72,7 @@ class GetDeviceFullInfoUseCaseTest {
@Test
fun `given current session and info for device when getting device info then the result is correct`() = runTest {
// Given
val currentSessionCrossSigningInfo = givenCurrentSessionCrossSigningInfo()
val deviceInfo = DeviceInfo(
lastSeenTs = A_TIMESTAMP
@ -85,15 +86,15 @@ class GetDeviceFullInfoUseCaseTest {
val isInactive = false
every { checkIfSessionIsInactiveUseCase.execute(any()) } returns isInactive
// When
val deviceFullInfo = getDeviceFullInfoUseCase.execute(A_DEVICE_ID).firstOrNull()
deviceFullInfo shouldBeEqualTo Optional(
DeviceFullInfo(
deviceInfo = deviceInfo,
cryptoDeviceInfo = cryptoDeviceInfo,
roomEncryptionTrustLevel = trustLevel,
isInactive = isInactive,
)
// Then
deviceFullInfo shouldBeEqualTo DeviceFullInfo(
deviceInfo = deviceInfo,
cryptoDeviceInfo = cryptoDeviceInfo,
roomEncryptionTrustLevel = trustLevel,
isInactive = isInactive,
)
verify { fakeActiveSessionHolder.instance.getSafeActiveSession() }
verify { getCurrentSessionCrossSigningInfoUseCase.execute() }
@ -104,16 +105,19 @@ class GetDeviceFullInfoUseCaseTest {
}
@Test
fun `given current session and no info for device when getting device info then the result is null`() = runTest {
fun `given current session and no info for device when getting device info then the result is empty`() = runTest {
// Given
givenCurrentSessionCrossSigningInfo()
fakeActiveSessionHolder.fakeSession.fakeCryptoService.myDevicesInfoWithIdLiveData = MutableLiveData(Optional(null))
fakeActiveSessionHolder.fakeSession.fakeCryptoService.myDevicesInfoWithIdLiveData.givenAsFlow()
fakeActiveSessionHolder.fakeSession.fakeCryptoService.cryptoDeviceInfoWithIdLiveData = MutableLiveData(Optional(null))
fakeActiveSessionHolder.fakeSession.fakeCryptoService.cryptoDeviceInfoWithIdLiveData.givenAsFlow()
// When
val deviceFullInfo = getDeviceFullInfoUseCase.execute(A_DEVICE_ID).firstOrNull()
deviceFullInfo shouldBeEqualTo Optional(null)
// Then
deviceFullInfo shouldBeEqualTo null
verify { fakeActiveSessionHolder.instance.getSafeActiveSession() }
verify { fakeActiveSessionHolder.fakeSession.fakeCryptoService.getMyDevicesInfoLive(A_DEVICE_ID).asFlow() }
verify { fakeActiveSessionHolder.fakeSession.fakeCryptoService.getLiveCryptoDeviceInfoWithId(A_DEVICE_ID).asFlow() }
@ -121,10 +125,13 @@ class GetDeviceFullInfoUseCaseTest {
@Test
fun `given no current session when getting device info then the result is empty`() = runTest {
// Given
fakeActiveSessionHolder.givenGetSafeActiveSessionReturns(null)
// When
val deviceFullInfo = getDeviceFullInfoUseCase.execute(A_DEVICE_ID).firstOrNull()
// Then
deviceFullInfo shouldBeEqualTo null
verify { fakeActiveSessionHolder.instance.getSafeActiveSession() }
}

View File

@ -54,7 +54,7 @@ class SessionOverviewViewModelTest {
fun `given the viewModel has been initialized then viewState is updated with session info`() {
val sessionParams = givenIdForSession(A_SESSION_ID)
val deviceFullInfo = mockk<DeviceFullInfo>()
every { getDeviceFullInfoUseCase.execute(A_SESSION_ID) } returns flowOf(Optional(deviceFullInfo))
every { getDeviceFullInfoUseCase.execute(A_SESSION_ID) } returns flowOf(deviceFullInfo)
val expectedState = SessionOverviewViewState(
deviceId = A_SESSION_ID,
isCurrentSession = true,