Skip to content

Commit

Permalink
getUser() can return null more often than before, since the SDK will …
Browse files Browse the repository at this point in the history
…retrieve data asynchronously. So ensure that the initial state can always be built.
  • Loading branch information
bmarty committed Oct 14, 2022
1 parent 8fc35ef commit 0a6d620
Showing 1 changed file with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ import im.vector.app.core.di.MavericksAssistedViewModelFactory
import im.vector.app.core.di.SingletonEntryPoint
import im.vector.app.core.di.hiltMavericksViewModelFactory
import im.vector.app.core.platform.VectorViewModel
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.crypto.crosssigning.MXCrossSigningInfo
import org.matrix.android.sdk.api.session.crypto.model.CryptoDeviceInfo
import org.matrix.android.sdk.api.session.crypto.verification.VerificationMethod
import org.matrix.android.sdk.api.session.getUser
import org.matrix.android.sdk.api.session.getUserOrDefault
import org.matrix.android.sdk.api.util.MatrixItem
import org.matrix.android.sdk.api.util.toMatrixItem
import org.matrix.android.sdk.flow.flow
Expand All @@ -60,17 +62,15 @@ class DeviceListBottomSheetViewModel @AssistedInject constructor(

companion object : MavericksViewModelFactory<DeviceListBottomSheetViewModel, DeviceListViewState> by hiltMavericksViewModelFactory() {

override fun initialState(viewModelContext: ViewModelContext): DeviceListViewState? {
override fun initialState(viewModelContext: ViewModelContext): DeviceListViewState {
val args = viewModelContext.args<DeviceListBottomSheet.Args>()
val userId = args.userId
val session = EntryPoints.get(viewModelContext.app(), SingletonEntryPoint::class.java).activeSessionHolder().getActiveSession()
return session.getUser(userId)?.toMatrixItem()?.let {
DeviceListViewState(
userId = userId,
allowDeviceAction = args.allowDeviceAction,
userItem = it,
)
} ?: return super.initialState(viewModelContext)
return DeviceListViewState(
userId = userId,
allowDeviceAction = args.allowDeviceAction,
userItem = session.getUserOrDefault(userId).toMatrixItem(),
)
}
}

Expand All @@ -86,6 +86,16 @@ class DeviceListBottomSheetViewModel @AssistedInject constructor(
.execute {
copy(memberCrossSigningKey = it.invoke()?.getOrNull())
}

updateMatrixItem()
}

private fun updateMatrixItem() {
viewModelScope.launch {
tryOrNull { session.userService().resolveUser(initialState.userId) }
?.toMatrixItem()
?.let { setState { copy(userItem = it) } }
}
}

override fun handle(action: DeviceListAction) {
Expand Down

0 comments on commit 0a6d620

Please sign in to comment.