From 0dd3894a4984d3c57fefc29a3073eaaf11e7b4e4 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 7 Feb 2020 19:20:02 +0100 Subject: [PATCH] Use handle() pattern --- .../devices/DeviceListAction.kt | 30 +++++++++++++++++ .../devices/DeviceListBottomSheet.kt | 2 +- .../devices/DeviceListBottomSheetViewModel.kt | 32 +++++++++++++------ .../devices/DeviceListFragment.kt | 2 +- .../devices/DeviceTrustInfoActionFragment.kt | 2 +- 5 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceListAction.kt diff --git a/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceListAction.kt b/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceListAction.kt new file mode 100644 index 0000000000..f298985120 --- /dev/null +++ b/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceListAction.kt @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2020 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.riotx.features.roommemberprofile.devices + +import im.vector.matrix.android.internal.crypto.model.CryptoDeviceInfo +import im.vector.riotx.core.platform.VectorViewModelAction + +sealed class DeviceListAction : VectorViewModelAction { + // TODO Valere, this is not used? + object Refresh : DeviceListAction() + + data class SelectDevice(val device: CryptoDeviceInfo) : DeviceListAction() + object DeselectDevice : DeviceListAction() + + data class ManuallyVerify(val deviceId: String) : DeviceListAction() +} diff --git a/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceListBottomSheet.kt b/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceListBottomSheet.kt index 3f21aa3168..591b014c72 100644 --- a/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceListBottomSheet.kt +++ b/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceListBottomSheet.kt @@ -63,7 +63,7 @@ class DeviceListBottomSheet : VectorBaseBottomSheetDialogFragment() { withState(viewModel) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (it.selectedDevice != null) { - viewModel.selectDevice(null) + viewModel.handle(DeviceListAction.DeselectDevice) return@withState true } else { return@withState false diff --git a/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceListBottomSheetViewModel.kt b/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceListBottomSheetViewModel.kt index 6d37cbbd43..6c3b52b00b 100644 --- a/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceListBottomSheetViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceListBottomSheetViewModel.kt @@ -32,7 +32,7 @@ import im.vector.matrix.android.api.util.toMatrixItem import im.vector.matrix.android.internal.crypto.model.CryptoDeviceInfo import im.vector.matrix.rx.rx import im.vector.riotx.core.di.HasScreenInjector -import im.vector.riotx.core.platform.EmptyAction +import im.vector.riotx.core.extensions.exhaustive import im.vector.riotx.core.platform.VectorViewModel data class DeviceListViewState( @@ -46,7 +46,7 @@ data class DeviceListViewState( class DeviceListBottomSheetViewModel @AssistedInject constructor(@Assisted private val initialState: DeviceListViewState, @Assisted private val userId: String, private val session: Session) - : VectorViewModel(initialState) { + : VectorViewModel(initialState) { @AssistedInject.Factory interface Factory { @@ -67,6 +67,16 @@ class DeviceListBottomSheetViewModel @AssistedInject constructor(@Assisted priva } } + override fun handle(action: DeviceListAction) { + when (action) { + is DeviceListAction.Refresh -> refreshSelectedId() + is DeviceListAction.SelectDevice -> selectDevice(action) + is DeviceListAction.DeselectDevice -> deselectDevice() + is DeviceListAction.ManuallyVerify -> manuallyVerify(action) + }.exhaustive + } + + // TODO Valere: not used? private fun refreshSelectedId() = withState { state -> if (state.selectedDevice != null) { state.cryptoDevices.invoke()?.firstOrNull { state.selectedDevice.deviceId == it.deviceId }?.let { @@ -79,22 +89,24 @@ class DeviceListBottomSheetViewModel @AssistedInject constructor(@Assisted priva } } - // TODO Use handle() - fun selectDevice(device: CryptoDeviceInfo?) { + private fun selectDevice(action: DeviceListAction.SelectDevice) { setState { - copy(selectedDevice = device) + copy(selectedDevice = action.device) } } - // TODO Use handle() - fun manuallyVerify(device: CryptoDeviceInfo) { - session.getVerificationService().beginKeyVerification(VerificationMethod.SAS, userId, device.deviceId, null)?.let { txID -> + private fun deselectDevice() { + setState { + copy(selectedDevice = null) + } + } + + private fun manuallyVerify(action: DeviceListAction.ManuallyVerify) { + session.getVerificationService().beginKeyVerification(VerificationMethod.SAS, userId, action.deviceId, null)?.let { txID -> _viewEvents.post(DeviceListBottomSheetViewEvents.Verify(userId, txID)) } } - override fun handle(action: EmptyAction) {} - companion object : MvRxViewModelFactory { @JvmStatic override fun create(viewModelContext: ViewModelContext, state: DeviceListViewState): DeviceListBottomSheetViewModel? { diff --git a/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceListFragment.kt b/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceListFragment.kt index c598d051f7..93c51b2008 100644 --- a/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceListFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceListFragment.kt @@ -62,6 +62,6 @@ class DeviceListFragment @Inject constructor( } override fun onDeviceSelected(device: CryptoDeviceInfo) { - viewModel.selectDevice(device) + viewModel.handle(DeviceListAction.SelectDevice(device)) } } diff --git a/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceTrustInfoActionFragment.kt b/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceTrustInfoActionFragment.kt index 6c9de742bc..d955e4c9dc 100644 --- a/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceTrustInfoActionFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/roommemberprofile/devices/DeviceTrustInfoActionFragment.kt @@ -62,6 +62,6 @@ class DeviceTrustInfoActionFragment @Inject constructor( } override fun onVerifyManually(device: CryptoDeviceInfo) { - viewModel.manuallyVerify(device) + viewModel.handle(DeviceListAction.ManuallyVerify(device.deviceId)) } }