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 99afc33a8a..2fd1c2ce94 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 @@ -24,10 +24,7 @@ import dagger.assisted.AssistedInject import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.MavericksAssistedViewModelFactory import im.vector.app.core.di.hiltMavericksViewModelFactory -import im.vector.app.core.platform.VectorViewModel -import im.vector.app.core.utils.PublishDataSource import im.vector.app.features.settings.devices.v2.filter.DeviceManagerFilterType -import im.vector.lib.core.utils.flow.throttleFirst import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch @@ -35,16 +32,15 @@ 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 -import kotlin.time.Duration.Companion.seconds class DevicesViewModel @AssistedInject constructor( @Assisted initialState: DevicesViewState, private val activeSessionHolder: ActiveSessionHolder, private val getCurrentSessionCrossSigningInfoUseCase: GetCurrentSessionCrossSigningInfoUseCase, private val getDeviceFullInfoListUseCase: GetDeviceFullInfoListUseCase, - private val refreshDevicesUseCase: RefreshDevicesUseCase, private val refreshDevicesOnCryptoDevicesChangeUseCase: RefreshDevicesOnCryptoDevicesChangeUseCase, -) : VectorViewModel(initialState), VerificationService.Listener { + refreshDevicesUseCase: RefreshDevicesUseCase, +) : VectorSessionsListViewModel(initialState, refreshDevicesUseCase), VerificationService.Listener { @AssistedFactory interface Factory : MavericksAssistedViewModelFactory { @@ -53,14 +49,10 @@ class DevicesViewModel @AssistedInject constructor( companion object : MavericksViewModelFactory by hiltMavericksViewModelFactory() - private val refreshSource = PublishDataSource() - private val refreshThrottleDelayMs = 4.seconds.inWholeMilliseconds - init { addVerificationListener() observeCurrentSessionCrossSigningInfo() observeDevices() - observeRefreshSource() refreshDevicesOnCryptoDevicesChange() queryRefreshDevicesList() } @@ -123,13 +115,6 @@ class DevicesViewModel @AssistedInject constructor( } } - private fun observeRefreshSource() { - refreshSource.stream() - .throttleFirst(refreshThrottleDelayMs) - .onEach { refreshDevicesUseCase.execute() } - .launchIn(viewModelScope) - } - override fun transactionUpdated(tx: VerificationTransaction) { if (tx.state == VerificationTxState.Verified) { queryRefreshDevicesList() @@ -142,7 +127,7 @@ class DevicesViewModel @AssistedInject constructor( * It can be any mobile devices, and any browsers. */ private fun queryRefreshDevicesList() { - refreshSource.post(Unit) + refreshDeviceList() } override fun handle(action: DevicesAction) { 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 new file mode 100644 index 0000000000..dfd0c1be6d --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/VectorSessionsListViewModel.kt @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.settings.devices.v2 + +import com.airbnb.mvrx.MavericksState +import im.vector.app.core.platform.VectorViewEvents +import im.vector.app.core.platform.VectorViewModel +import im.vector.app.core.platform.VectorViewModelAction +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 kotlin.time.Duration.Companion.seconds + +abstract class VectorSessionsListViewModel( + initialState: S, + private val refreshDevicesUseCase: RefreshDevicesUseCase, +) : VectorViewModel(initialState) { + + private val refreshSource = PublishDataSource() + private val refreshThrottleDelayMs = 4.seconds.inWholeMilliseconds + + init { + observeRefreshSource() + } + + private fun observeRefreshSource() { + refreshSource.stream() + .throttleFirst(refreshThrottleDelayMs) + .onEach { refreshDevicesUseCase.execute() } + .launchIn(viewModelScope) + } + + fun refreshDeviceList() { + refreshSource.post(Unit) + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/othersessions/OtherSessionsViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/othersessions/OtherSessionsViewModel.kt index 4a7f911ff4..e4d758eb18 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/othersessions/OtherSessionsViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/othersessions/OtherSessionsViewModel.kt @@ -17,33 +17,28 @@ package im.vector.app.features.settings.devices.v2.othersessions import com.airbnb.mvrx.MavericksViewModelFactory -import com.airbnb.mvrx.Success import dagger.assisted.Assisted import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.MavericksAssistedViewModelFactory import im.vector.app.core.di.hiltMavericksViewModelFactory -import im.vector.app.core.platform.VectorViewModel -import im.vector.app.core.utils.PublishDataSource import im.vector.app.features.settings.devices.v2.GetDeviceFullInfoListUseCase 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 im.vector.lib.core.utils.flow.throttleFirst import kotlinx.coroutines.Job -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 class OtherSessionsViewModel @AssistedInject constructor( @Assisted initialState: OtherSessionsViewState, private val activeSessionHolder: ActiveSessionHolder, private val getDeviceFullInfoListUseCase: GetDeviceFullInfoListUseCase, - private val refreshDevicesUseCase: RefreshDevicesUseCase, -) : VectorViewModel(initialState), VerificationService.Listener { + refreshDevicesUseCase: RefreshDevicesUseCase +) : VectorSessionsListViewModel(initialState, refreshDevicesUseCase), + VerificationService.Listener { @AssistedFactory interface Factory : MavericksAssistedViewModelFactory { @@ -54,13 +49,9 @@ class OtherSessionsViewModel @AssistedInject constructor( private var observeDevicesJob: Job? = null - private val refreshSource = PublishDataSource() - private val refreshThrottleDelayMs = 4.seconds.inWholeMilliseconds - init { observeDevices(initialState.currentFilter) addVerificationListener() - observeRefreshSource() } override fun onCleared() { @@ -75,15 +66,9 @@ class OtherSessionsViewModel @AssistedInject constructor( excludeCurrentDevice = true ) .execute { async -> - if (async is Success) { - copy( - devices = async, - ) - } else { - copy( - devices = async - ) - } + copy( + devices = async, + ) } } @@ -101,13 +86,6 @@ class OtherSessionsViewModel @AssistedInject constructor( ?.removeListener(this) } - private fun observeRefreshSource() { - refreshSource.stream() - .throttleFirst(refreshThrottleDelayMs) - .onEach { refreshDevicesUseCase.execute() } - .launchIn(viewModelScope) - } - override fun transactionUpdated(tx: VerificationTransaction) { if (tx.state == VerificationTxState.Verified) { queryRefreshDevicesList() @@ -115,7 +93,7 @@ class OtherSessionsViewModel @AssistedInject constructor( } private fun queryRefreshDevicesList() { - refreshSource.post(Unit) + refreshDeviceList() } override fun handle(action: OtherSessionsAction) {