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

View File

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