Execute use case from a better place.

This commit is contained in:
Onuray Sahin 2022-12-09 15:47:28 +03:00
parent 22cce30e35
commit 7a667b513e
3 changed files with 15 additions and 11 deletions

View File

@ -32,11 +32,13 @@ import im.vector.app.core.platform.EmptyViewEvents
import im.vector.app.core.platform.VectorViewModel
import im.vector.app.core.platform.VectorViewModelAction
import im.vector.app.core.time.Clock
import im.vector.app.features.settings.devices.v2.DeleteUnusedClientInformationUseCase
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.sample
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.NoOpMatrixCallback
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.session.Session
@ -66,6 +68,7 @@ class UnknownDeviceDetectorSharedViewModel @AssistedInject constructor(
private val setUnverifiedSessionsAlertShownUseCase: SetUnverifiedSessionsAlertShownUseCase,
private val isNewLoginAlertShownUseCase: IsNewLoginAlertShownUseCase,
private val setNewLoginAlertShownUseCase: SetNewLoginAlertShownUseCase,
private val deleteUnusedClientInformationUseCase: DeleteUnusedClientInformationUseCase,
) : VectorViewModel<UnknownDevicesState, UnknownDeviceDetectorSharedViewModel.Action, EmptyViewEvents>(initialState) {
sealed class Action : VectorViewModelAction {
@ -102,6 +105,9 @@ class UnknownDeviceDetectorSharedViewModel @AssistedInject constructor(
) { cryptoList, infoList, pInfo ->
// Timber.v("## Detector trigger ${cryptoList.map { "${it.deviceId} ${it.trustLevel}" }}")
// Timber.v("## Detector trigger canCrossSign ${pInfo.get().selfSigned != null}")
deleteUnusedClientInformation(infoList)
infoList
.filter { info ->
// filter verified session, by checking the crypto device info
@ -143,6 +149,12 @@ class UnknownDeviceDetectorSharedViewModel @AssistedInject constructor(
session.cryptoService().fetchDevicesList(NoOpMatrixCallback())
}
private fun deleteUnusedClientInformation(deviceFullInfoList: List<DeviceInfo>) {
viewModelScope.launch {
deleteUnusedClientInformationUseCase.execute(deviceFullInfoList)
}
}
override fun handle(action: Action) {
when (action) {
is Action.IgnoreDevice -> {

View File

@ -18,14 +18,15 @@ package im.vector.app.features.settings.devices.v2
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.session.clientinfo.MATRIX_CLIENT_INFO_KEY_PREFIX
import org.matrix.android.sdk.api.session.crypto.model.DeviceInfo
import javax.inject.Inject
class DeleteUnusedClientInformationUseCase @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder,
) {
suspend fun execute(deviceFullInfoList: List<DeviceFullInfo>) {
val expectedClientInfoKeyList = deviceFullInfoList.map { MATRIX_CLIENT_INFO_KEY_PREFIX + it.deviceInfo.deviceId }
suspend fun execute(deviceInfoList: List<DeviceInfo>) {
val expectedClientInfoKeyList = deviceInfoList.map { MATRIX_CLIENT_INFO_KEY_PREFIX + it.deviceId }
activeSessionHolder
.getSafeActiveSession()
?.accountDataService()

View File

@ -51,7 +51,6 @@ class DevicesViewModel @AssistedInject constructor(
refreshDevicesUseCase: RefreshDevicesUseCase,
private val vectorPreferences: VectorPreferences,
private val toggleIpAddressVisibilityUseCase: ToggleIpAddressVisibilityUseCase,
private val deleteUnusedClientInformationUseCase: DeleteUnusedClientInformationUseCase,
) : VectorSessionsListViewModel<DevicesViewState,
DevicesAction,
DevicesViewEvent>(initialState, activeSessionHolder, refreshDevicesUseCase),
@ -114,8 +113,6 @@ class DevicesViewModel @AssistedInject constructor(
val unverifiedSessionsCount = deviceFullInfoList.count { !it.cryptoDeviceInfo?.trustLevel?.isCrossSigningVerified().orFalse() }
val inactiveSessionsCount = deviceFullInfoList.count { it.isInactive }
deleteUnusedClientInformation(deviceFullInfoList)
copy(
devices = async,
unverifiedSessionsCount = unverifiedSessionsCount,
@ -129,12 +126,6 @@ class DevicesViewModel @AssistedInject constructor(
}
}
private fun deleteUnusedClientInformation(deviceFullInfoList: List<DeviceFullInfo>) {
viewModelScope.launch {
deleteUnusedClientInformationUseCase.execute(deviceFullInfoList)
}
}
private fun refreshDevicesOnCryptoDevicesChange() {
viewModelScope.launch {
refreshDevicesOnCryptoDevicesChangeUseCase.execute()