Convert RoomCryptoService to suspend functions

Signed-off-by: Dominic Fischer <dominicfischer7@gmail.com>
This commit is contained in:
Dominic Fischer 2020-11-08 12:24:47 +00:00
parent 812b1f7baa
commit 3ce8deec07
4 changed files with 14 additions and 22 deletions

View file

@ -68,8 +68,8 @@ class CryptoTestHelper(private val mTestHelper: CommonTestHelper) {
if (encryptedRoom) {
val room = aliceSession.getRoom(roomId)!!
mTestHelper.doSync<Unit> {
room.enableEncryption(callback = it)
mTestHelper.runBlockingTest {
room.enableEncryption()
}
}

View file

@ -16,7 +16,6 @@
package org.matrix.android.sdk.api.session.room.crypto
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM
interface RoomCryptoService {
@ -30,6 +29,5 @@ interface RoomCryptoService {
/**
* Enable encryption of the room
*/
fun enableEncryption(algorithm: String = MXCRYPTO_ALGORITHM_MEGOLM,
callback: MatrixCallback<Unit>)
suspend fun enableEncryption(algorithm: String = MXCRYPTO_ALGORITHM_MEGOLM)
}

View file

@ -101,13 +101,13 @@ internal class DefaultRoom @Inject constructor(override val roomId: String,
return cryptoService.shouldEncryptForInvitedMembers(roomId)
}
override fun enableEncryption(algorithm: String, callback: MatrixCallback<Unit>) {
override suspend fun enableEncryption(algorithm: String) {
when {
isEncrypted() -> {
callback.onFailure(IllegalStateException("Encryption is already enabled for this room"))
throw IllegalStateException("Encryption is already enabled for this room")
}
algorithm != MXCRYPTO_ALGORITHM_MEGOLM -> {
callback.onFailure(InvalidParameterException("Only MXCRYPTO_ALGORITHM_MEGOLM algorithm is supported"))
throw InvalidParameterException("Only MXCRYPTO_ALGORITHM_MEGOLM algorithm is supported")
}
else -> {
val params = SendStateTask.Params(
@ -118,11 +118,7 @@ internal class DefaultRoom @Inject constructor(override val roomId: String,
"algorithm" to algorithm
))
sendStateTask
.configureWith(params) {
this.callback = callback
}
.executeBy(taskExecutor)
sendStateTask.execute(params)
}
}
}

View file

@ -17,6 +17,7 @@
package im.vector.app.features.roomprofile.settings
import androidx.core.net.toFile
import androidx.lifecycle.viewModelScope
import com.airbnb.mvrx.FragmentViewModelContext
import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.ViewModelContext
@ -27,7 +28,7 @@ import im.vector.app.core.platform.VectorViewModel
import im.vector.app.features.powerlevel.PowerLevelsObservableFactory
import io.reactivex.Completable
import io.reactivex.Observable
import org.matrix.android.sdk.api.MatrixCallback
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.Session
@ -228,16 +229,13 @@ class RoomSettingsViewModel @AssistedInject constructor(@Assisted initialState:
private fun handleEnableEncryption() {
postLoading(true)
room.enableEncryption(callback = object : MatrixCallback<Unit> {
override fun onFailure(failure: Throwable) {
postLoading(false)
viewModelScope.launch {
val result = runCatching { room.enableEncryption() }
postLoading(false)
result.onFailure { failure ->
_viewEvents.post(RoomSettingsViewEvents.Failure(failure))
}
override fun onSuccess(data: Unit) {
postLoading(false)
}
})
}
}
private fun postLoading(isLoading: Boolean) {