From eb5253ab1ad06b607171bcb9c58b1f9bb81ecc38 Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Fri, 16 Sep 2022 14:51:40 +0300 Subject: [PATCH] Refactor duplicated code. --- .../settings/devices/v2/DevicesViewModel.kt | 44 ++----------------- .../devices/v2/VectorSessionsListViewModel.kt | 38 +++++++++++++++- .../othersessions/OtherSessionsViewModel.kt | 40 ++--------------- 3 files changed, 44 insertions(+), 78 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/DevicesViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/DevicesViewModel.kt index 2fd1c2ce94..9c1b70a7e2 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/DevicesViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/DevicesViewModel.kt @@ -29,18 +29,15 @@ import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import org.matrix.android.sdk.api.extensions.orFalse -import org.matrix.android.sdk.api.session.crypto.verification.VerificationService -import org.matrix.android.sdk.api.session.crypto.verification.VerificationTransaction -import org.matrix.android.sdk.api.session.crypto.verification.VerificationTxState class DevicesViewModel @AssistedInject constructor( @Assisted initialState: DevicesViewState, - private val activeSessionHolder: ActiveSessionHolder, + activeSessionHolder: ActiveSessionHolder, private val getCurrentSessionCrossSigningInfoUseCase: GetCurrentSessionCrossSigningInfoUseCase, private val getDeviceFullInfoListUseCase: GetDeviceFullInfoListUseCase, private val refreshDevicesOnCryptoDevicesChangeUseCase: RefreshDevicesOnCryptoDevicesChangeUseCase, refreshDevicesUseCase: RefreshDevicesUseCase, -) : VectorSessionsListViewModel(initialState, refreshDevicesUseCase), VerificationService.Listener { +) : VectorSessionsListViewModel(initialState, activeSessionHolder, refreshDevicesUseCase) { @AssistedFactory interface Factory : MavericksAssistedViewModelFactory { @@ -50,30 +47,10 @@ class DevicesViewModel @AssistedInject constructor( companion object : MavericksViewModelFactory by hiltMavericksViewModelFactory() init { - addVerificationListener() observeCurrentSessionCrossSigningInfo() observeDevices() refreshDevicesOnCryptoDevicesChange() - queryRefreshDevicesList() - } - - override fun onCleared() { - removeVerificationListener() - super.onCleared() - } - - private fun addVerificationListener() { - activeSessionHolder.getSafeActiveSession() - ?.cryptoService() - ?.verificationService() - ?.addListener(this) - } - - private fun removeVerificationListener() { - activeSessionHolder.getSafeActiveSession() - ?.cryptoService() - ?.verificationService() - ?.removeListener(this) + refreshDeviceList() } private fun observeCurrentSessionCrossSigningInfo() { @@ -115,21 +92,6 @@ class DevicesViewModel @AssistedInject constructor( } } - override fun transactionUpdated(tx: VerificationTransaction) { - if (tx.state == VerificationTxState.Verified) { - queryRefreshDevicesList() - } - } - - /** - * Force the refresh of the devices list. - * The devices list is the list of the devices where the user is logged in. - * It can be any mobile devices, and any browsers. - */ - private fun queryRefreshDevicesList() { - refreshDeviceList() - } - override fun handle(action: DevicesAction) { when (action) { is DevicesAction.MarkAsManuallyVerified -> handleMarkAsManuallyVerifiedAction() diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/VectorSessionsListViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/VectorSessionsListViewModel.kt index dfd0c1be6d..8cb69a31ed 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/VectorSessionsListViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/VectorSessionsListViewModel.kt @@ -17,6 +17,7 @@ package im.vector.app.features.settings.devices.v2 import com.airbnb.mvrx.MavericksState +import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.platform.VectorViewEvents import im.vector.app.core.platform.VectorViewModel import im.vector.app.core.platform.VectorViewModelAction @@ -24,20 +25,44 @@ import im.vector.app.core.utils.PublishDataSource import im.vector.lib.core.utils.flow.throttleFirst import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach +import org.matrix.android.sdk.api.session.crypto.verification.VerificationService +import org.matrix.android.sdk.api.session.crypto.verification.VerificationTransaction +import org.matrix.android.sdk.api.session.crypto.verification.VerificationTxState import kotlin.time.Duration.Companion.seconds abstract class VectorSessionsListViewModel( initialState: S, + private val activeSessionHolder: ActiveSessionHolder, private val refreshDevicesUseCase: RefreshDevicesUseCase, -) : VectorViewModel(initialState) { +) : VectorViewModel(initialState), VerificationService.Listener { private val refreshSource = PublishDataSource() private val refreshThrottleDelayMs = 4.seconds.inWholeMilliseconds init { + addVerificationListener() observeRefreshSource() } + override fun onCleared() { + removeVerificationListener() + super.onCleared() + } + + private fun addVerificationListener() { + activeSessionHolder.getSafeActiveSession() + ?.cryptoService() + ?.verificationService() + ?.addListener(this) + } + + private fun removeVerificationListener() { + activeSessionHolder.getSafeActiveSession() + ?.cryptoService() + ?.verificationService() + ?.removeListener(this) + } + private fun observeRefreshSource() { refreshSource.stream() .throttleFirst(refreshThrottleDelayMs) @@ -45,6 +70,17 @@ abstract class VectorSessionsListViewModel(initialState, refreshDevicesUseCase), - VerificationService.Listener { +) : VectorSessionsListViewModel( + initialState, activeSessionHolder, refreshDevicesUseCase +) { @AssistedFactory interface Factory : MavericksAssistedViewModelFactory { @@ -51,12 +49,6 @@ class OtherSessionsViewModel @AssistedInject constructor( init { observeDevices(initialState.currentFilter) - addVerificationListener() - } - - override fun onCleared() { - removeVerificationListener() - super.onCleared() } private fun observeDevices(currentFilter: DeviceManagerFilterType) { @@ -72,30 +64,6 @@ class OtherSessionsViewModel @AssistedInject constructor( } } - private fun addVerificationListener() { - activeSessionHolder.getSafeActiveSession() - ?.cryptoService() - ?.verificationService() - ?.addListener(this) - } - - private fun removeVerificationListener() { - activeSessionHolder.getSafeActiveSession() - ?.cryptoService() - ?.verificationService() - ?.removeListener(this) - } - - override fun transactionUpdated(tx: VerificationTransaction) { - if (tx.state == VerificationTxState.Verified) { - queryRefreshDevicesList() - } - } - - private fun queryRefreshDevicesList() { - refreshDeviceList() - } - override fun handle(action: OtherSessionsAction) { when (action) { is OtherSessionsAction.FilterDevices -> handleFilterDevices(action)