Convert to ViewEvents -> DeviceListBottomSheetViewModel

This commit is contained in:
Benoit Marty 2020-02-07 18:59:31 +01:00
parent 70973c3302
commit c8ff8d3c9e
3 changed files with 43 additions and 32 deletions

View File

@ -21,15 +21,13 @@ import android.os.Bundle
import android.view.KeyEvent
import androidx.fragment.app.Fragment
import com.airbnb.mvrx.MvRx
import com.airbnb.mvrx.Success
import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.withState
import im.vector.riotx.R
import im.vector.riotx.core.di.ScreenComponent
import im.vector.riotx.core.extensions.commitTransaction
import im.vector.riotx.core.extensions.observeEvent
import im.vector.riotx.core.extensions.exhaustive
import im.vector.riotx.core.platform.VectorBaseBottomSheetDialogFragment
import im.vector.riotx.features.crypto.verification.VerificationAction
import im.vector.riotx.features.crypto.verification.VerificationBottomSheet
import javax.inject.Inject
import kotlin.reflect.KClass
@ -48,20 +46,16 @@ class DeviceListBottomSheet : VectorBaseBottomSheetDialogFragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel.requestLiveData.observeEvent(this) { async ->
when (async) {
is Success -> {
when (val action = async.invoke()) {
is VerificationAction.StartSASVerification -> {
VerificationBottomSheet.withArgs(
roomId = null,
otherUserId = action.otherUserId,
transactionId = action.pendingRequestTransactionId
).show(requireActivity().supportFragmentManager, "REQPOP")
}
}
viewModel.observeViewEvents {
when (it) {
is DeviceListBottomSheetViewEvents.Verify -> {
VerificationBottomSheet.withArgs(
roomId = null,
otherUserId = it.userId,
transactionId = it.txID
).show(requireActivity().supportFragmentManager, "REQPOP")
}
}
}.exhaustive
}
}

View File

@ -0,0 +1,29 @@
/*
* 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.riotx.core.platform.VectorViewEvents
/**
* Transient events for device list screen
*/
sealed class DeviceListBottomSheetViewEvents : VectorViewEvents {
// data class Loading(val message: CharSequence? = null) : RoomDirectoryViewEvents()
// data class Failure(val throwable: Throwable) : DeviceListBottomSheetViewEvents()
data class Verify(val userId: String, val txID: String) : DeviceListBottomSheetViewEvents()
}

View File

@ -16,14 +16,11 @@
*/
package im.vector.riotx.features.roommemberprofile.devices
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.airbnb.mvrx.Async
import com.airbnb.mvrx.FragmentViewModelContext
import com.airbnb.mvrx.Loading
import com.airbnb.mvrx.MvRxState
import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.Success
import com.airbnb.mvrx.ViewModelContext
import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject
@ -36,11 +33,7 @@ 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.platform.EmptyViewEvents
import im.vector.riotx.core.platform.VectorViewModel
import im.vector.riotx.core.resources.StringProvider
import im.vector.riotx.core.utils.LiveEvent
import im.vector.riotx.features.crypto.verification.VerificationAction
data class DeviceListViewState(
val userItem: MatrixItem? = null,
@ -52,14 +45,8 @@ data class DeviceListViewState(
class DeviceListBottomSheetViewModel @AssistedInject constructor(@Assisted private val initialState: DeviceListViewState,
@Assisted private val userId: String,
private val stringProvider: StringProvider,
private val session: Session)
: VectorViewModel<DeviceListViewState, EmptyAction, EmptyViewEvents>(initialState) {
// Can be used for several actions, for a one shot result
private val _requestLiveData = MutableLiveData<LiveEvent<Async<VerificationAction>>>()
val requestLiveData: LiveData<LiveEvent<Async<VerificationAction>>>
get() = _requestLiveData
: VectorViewModel<DeviceListViewState, EmptyAction, DeviceListBottomSheetViewEvents>(initialState) {
@AssistedInject.Factory
interface Factory {
@ -67,7 +54,6 @@ class DeviceListBottomSheetViewModel @AssistedInject constructor(@Assisted priva
}
init {
session.rx().liveUserCryptoDevices(userId)
.execute {
copy(cryptoDevices = it).also {
@ -93,15 +79,17 @@ class DeviceListBottomSheetViewModel @AssistedInject constructor(@Assisted priva
}
}
// TODO Use handle()
fun selectDevice(device: CryptoDeviceInfo?) {
setState {
copy(selectedDevice = device)
}
}
// TODO Use handle()
fun manuallyVerify(device: CryptoDeviceInfo) {
session.getVerificationService().beginKeyVerification(VerificationMethod.SAS, userId, device.deviceId, null)?.let { txID ->
_requestLiveData.postValue(LiveEvent(Success(VerificationAction.StartSASVerification(userId, txID))))
_viewEvents.post(DeviceListBottomSheetViewEvents.Verify(userId, txID))
}
}