mirror of
https://github.com/element-hq/element-android
synced 2024-11-28 13:38:49 +03:00
Use handle() pattern
This commit is contained in:
parent
c8ff8d3c9e
commit
0dd3894a49
5 changed files with 55 additions and 13 deletions
|
@ -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()
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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<DeviceListViewState, EmptyAction, DeviceListBottomSheetViewEvents>(initialState) {
|
||||
: VectorViewModel<DeviceListViewState, DeviceListAction, DeviceListBottomSheetViewEvents>(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<DeviceListBottomSheetViewModel, DeviceListViewState> {
|
||||
@JvmStatic
|
||||
override fun create(viewModelContext: ViewModelContext, state: DeviceListViewState): DeviceListBottomSheetViewModel? {
|
||||
|
|
|
@ -62,6 +62,6 @@ class DeviceListFragment @Inject constructor(
|
|||
}
|
||||
|
||||
override fun onDeviceSelected(device: CryptoDeviceInfo) {
|
||||
viewModel.selectDevice(device)
|
||||
viewModel.handle(DeviceListAction.SelectDevice(device))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,6 +62,6 @@ class DeviceTrustInfoActionFragment @Inject constructor(
|
|||
}
|
||||
|
||||
override fun onVerifyManually(device: CryptoDeviceInfo) {
|
||||
viewModel.manuallyVerify(device)
|
||||
viewModel.handle(DeviceListAction.ManuallyVerify(device.deviceId))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue