mirror of
https://github.com/vector-im/element-android.git
synced 2024-11-16 02:05:06 +08:00
Refactor duplicated code.
This commit is contained in:
parent
e87d4db72c
commit
eb5253ab1a
@ -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<DevicesViewState, DevicesAction, DevicesViewEvent>(initialState, refreshDevicesUseCase), VerificationService.Listener {
|
||||
) : VectorSessionsListViewModel<DevicesViewState, DevicesAction, DevicesViewEvent>(initialState, activeSessionHolder, refreshDevicesUseCase) {
|
||||
|
||||
@AssistedFactory
|
||||
interface Factory : MavericksAssistedViewModelFactory<DevicesViewModel, DevicesViewState> {
|
||||
@ -50,30 +47,10 @@ class DevicesViewModel @AssistedInject constructor(
|
||||
companion object : MavericksViewModelFactory<DevicesViewModel, DevicesViewState> 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()
|
||||
|
@ -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<S : MavericksState, VA : VectorViewModelAction, VE : VectorViewEvents>(
|
||||
initialState: S,
|
||||
private val activeSessionHolder: ActiveSessionHolder,
|
||||
private val refreshDevicesUseCase: RefreshDevicesUseCase,
|
||||
) : VectorViewModel<S, VA, VE>(initialState) {
|
||||
) : VectorViewModel<S, VA, VE>(initialState), VerificationService.Listener {
|
||||
|
||||
private val refreshSource = PublishDataSource<Unit>()
|
||||
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<S : MavericksState, VA : VectorViewMo
|
||||
.launchIn(viewModelScope)
|
||||
}
|
||||
|
||||
override fun transactionUpdated(tx: VerificationTransaction) {
|
||||
if (tx.state == VerificationTxState.Verified) {
|
||||
refreshDeviceList()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
fun refreshDeviceList() {
|
||||
refreshSource.post(Unit)
|
||||
}
|
||||
|
@ -28,17 +28,15 @@ import im.vector.app.features.settings.devices.v2.RefreshDevicesUseCase
|
||||
import im.vector.app.features.settings.devices.v2.VectorSessionsListViewModel
|
||||
import im.vector.app.features.settings.devices.v2.filter.DeviceManagerFilterType
|
||||
import kotlinx.coroutines.Job
|
||||
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 OtherSessionsViewModel @AssistedInject constructor(
|
||||
@Assisted initialState: OtherSessionsViewState,
|
||||
private val activeSessionHolder: ActiveSessionHolder,
|
||||
activeSessionHolder: ActiveSessionHolder,
|
||||
private val getDeviceFullInfoListUseCase: GetDeviceFullInfoListUseCase,
|
||||
refreshDevicesUseCase: RefreshDevicesUseCase
|
||||
) : VectorSessionsListViewModel<OtherSessionsViewState, OtherSessionsAction, OtherSessionsViewEvents>(initialState, refreshDevicesUseCase),
|
||||
VerificationService.Listener {
|
||||
) : VectorSessionsListViewModel<OtherSessionsViewState, OtherSessionsAction, OtherSessionsViewEvents>(
|
||||
initialState, activeSessionHolder, refreshDevicesUseCase
|
||||
) {
|
||||
|
||||
@AssistedFactory
|
||||
interface Factory : MavericksAssistedViewModelFactory<OtherSessionsViewModel, OtherSessionsViewState> {
|
||||
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user