Convert RoomService to suspend functions

Signed-off-by: Dominic Fischer <dominicfischer7@gmail.com>
This commit is contained in:
Dominic Fischer 2021-04-02 11:02:32 +01:00
parent 2df8eb199b
commit 55e8f519df
12 changed files with 129 additions and 165 deletions

View file

@ -124,8 +124,8 @@ class RxSession(private val session: Session) {
.startWithCallable { session.getPendingThreePids() } .startWithCallable { session.getPendingThreePids() }
} }
fun createRoom(roomParams: CreateRoomParams): Single<String> = singleBuilder { fun createRoom(roomParams: CreateRoomParams): Single<String> = rxSingle {
session.createRoom(roomParams, it) session.createRoom(roomParams)
} }
fun searchUsersDirectory(search: String, fun searchUsersDirectory(search: String,
@ -136,8 +136,8 @@ class RxSession(private val session: Session) {
fun joinRoom(roomIdOrAlias: String, fun joinRoom(roomIdOrAlias: String,
reason: String? = null, reason: String? = null,
viaServers: List<String> = emptyList()): Single<Unit> = singleBuilder { viaServers: List<String> = emptyList()): Single<Unit> = rxSingle {
session.joinRoom(roomIdOrAlias, reason, viaServers, it) session.joinRoom(roomIdOrAlias, reason, viaServers)
} }
fun getRoomIdByAlias(roomAlias: String, fun getRoomIdByAlias(roomAlias: String,

View file

@ -66,8 +66,8 @@ class CryptoTestHelper(private val mTestHelper: CommonTestHelper) {
fun doE2ETestWithAliceInARoom(encryptedRoom: Boolean = true): CryptoTestData { fun doE2ETestWithAliceInARoom(encryptedRoom: Boolean = true): CryptoTestData {
val aliceSession = mTestHelper.createAccount(TestConstants.USER_ALICE, defaultSessionParams) val aliceSession = mTestHelper.createAccount(TestConstants.USER_ALICE, defaultSessionParams)
val roomId = mTestHelper.doSync<String> { val roomId = mTestHelper.runBlockingTest {
aliceSession.createRoom(CreateRoomParams().apply { name = "MyRoom" }, it) aliceSession.createRoom(CreateRoomParams().apply { name = "MyRoom" })
} }
if (encryptedRoom) { if (encryptedRoom) {
@ -135,7 +135,7 @@ class CryptoTestHelper(private val mTestHelper: CommonTestHelper) {
bobRoomSummariesLive.observeForever(roomJoinedObserver) bobRoomSummariesLive.observeForever(roomJoinedObserver)
} }
mTestHelper.doSync<Unit> { bobSession.joinRoom(aliceRoomId, callback = it) } mTestHelper.runBlockingTest { bobSession.joinRoom(aliceRoomId) }
mTestHelper.await(lock) mTestHelper.await(lock)
@ -176,8 +176,8 @@ class CryptoTestHelper(private val mTestHelper: CommonTestHelper) {
room.invite(samSession.myUserId, null) room.invite(samSession.myUserId, null)
} }
mTestHelper.doSync<Unit> { mTestHelper.runBlockingTest {
samSession.joinRoom(room.roomId, null, emptyList(), it) samSession.joinRoom(room.roomId, null, emptyList())
} }
return samSession return samSession
@ -256,8 +256,8 @@ class CryptoTestHelper(private val mTestHelper: CommonTestHelper) {
} }
fun createDM(alice: Session, bob: Session): String { fun createDM(alice: Session, bob: Session): String {
val roomId = mTestHelper.doSync<String> { val roomId = mTestHelper.runBlockingTest {
alice.createDirectRoom(bob.myUserId, it) alice.createDirectRoom(bob.myUserId)
} }
mTestHelper.waitWithLatch { latch -> mTestHelper.waitWithLatch { latch ->
@ -300,7 +300,7 @@ class CryptoTestHelper(private val mTestHelper: CommonTestHelper) {
bobRoomSummariesLive.observeForever(newRoomObserver) bobRoomSummariesLive.observeForever(newRoomObserver)
} }
mTestHelper.doSync<Unit> { bob.joinRoom(roomId, callback = it) } mTestHelper.runBlockingTest { bob.joinRoom(roomId) }
} }
return roomId return roomId
@ -398,8 +398,8 @@ class CryptoTestHelper(private val mTestHelper: CommonTestHelper) {
val aliceSession = mTestHelper.createAccount(TestConstants.USER_ALICE, defaultSessionParams) val aliceSession = mTestHelper.createAccount(TestConstants.USER_ALICE, defaultSessionParams)
aliceSession.cryptoService().setWarnOnUnknownDevices(false) aliceSession.cryptoService().setWarnOnUnknownDevices(false)
val roomId = mTestHelper.doSync<String> { val roomId = mTestHelper.runBlockingTest {
aliceSession.createRoom(CreateRoomParams().apply { name = "MyRoom" }, it) aliceSession.createRoom(CreateRoomParams().apply { name = "MyRoom" })
} }
val room = aliceSession.getRoom(roomId)!! val room = aliceSession.getRoom(roomId)!!
@ -412,7 +412,7 @@ class CryptoTestHelper(private val mTestHelper: CommonTestHelper) {
val session = mTestHelper.createAccount("User_$index", defaultSessionParams) val session = mTestHelper.createAccount("User_$index", defaultSessionParams)
mTestHelper.runBlockingTest(timeout = 600_000) { room.invite(session.myUserId, null) } mTestHelper.runBlockingTest(timeout = 600_000) { room.invite(session.myUserId, null) }
println("TEST -> " + session.myUserId + " invited") println("TEST -> " + session.myUserId + " invited")
mTestHelper.doSync<Unit> { session.joinRoom(room.roomId, null, emptyList(), it) } mTestHelper.runBlockingTest { session.joinRoom(room.roomId, null, emptyList()) }
println("TEST -> " + session.myUserId + " joined") println("TEST -> " + session.myUserId + " joined")
sessions.add(session) sessions.add(session)
} }

View file

@ -71,13 +71,12 @@ class KeyShareTests : InstrumentedTest {
val aliceSession = mTestHelper.createAccount(TestConstants.USER_ALICE, SessionTestParams(true)) val aliceSession = mTestHelper.createAccount(TestConstants.USER_ALICE, SessionTestParams(true))
// Create an encrypted room and add a message // Create an encrypted room and add a message
val roomId = mTestHelper.doSync<String> { val roomId = mTestHelper.runBlockingTest {
aliceSession.createRoom( aliceSession.createRoom(
CreateRoomParams().apply { CreateRoomParams().apply {
visibility = RoomDirectoryVisibility.PRIVATE visibility = RoomDirectoryVisibility.PRIVATE
enableEncryption() enableEncryption()
}, }
it
) )
} }
val room = aliceSession.getRoom(roomId) val room = aliceSession.getRoom(roomId)
@ -332,13 +331,12 @@ class KeyShareTests : InstrumentedTest {
} }
// Create an encrypted room and send a couple of messages // Create an encrypted room and send a couple of messages
val roomId = mTestHelper.doSync<String> { val roomId = mTestHelper.runBlockingTest {
aliceSession.createRoom( aliceSession.createRoom(
CreateRoomParams().apply { CreateRoomParams().apply {
visibility = RoomDirectoryVisibility.PRIVATE visibility = RoomDirectoryVisibility.PRIVATE
enableEncryption() enableEncryption()
}, }
it
) )
} }
val roomAlicePov = aliceSession.getRoom(roomId) val roomAlicePov = aliceSession.getRoom(roomId)
@ -371,8 +369,8 @@ class KeyShareTests : InstrumentedTest {
roomAlicePov.invite(bobSession.myUserId, null) roomAlicePov.invite(bobSession.myUserId, null)
} }
mTestHelper.doSync<Unit> { mTestHelper.runBlockingTest {
bobSession.joinRoom(roomAlicePov.roomId, null, emptyList(), it) bobSession.joinRoom(roomAlicePov.roomId, null, emptyList())
} }
// we want to discard alice outbound session // we want to discard alice outbound session

View file

@ -17,14 +17,12 @@
package org.matrix.android.sdk.api.session.room package org.matrix.android.sdk.api.session.room
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.session.events.model.Event import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.room.members.ChangeMembershipState import org.matrix.android.sdk.api.session.room.members.ChangeMembershipState
import org.matrix.android.sdk.api.session.room.model.RoomMemberSummary import org.matrix.android.sdk.api.session.room.model.RoomMemberSummary
import org.matrix.android.sdk.api.session.room.model.RoomSummary import org.matrix.android.sdk.api.session.room.model.RoomSummary
import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams
import org.matrix.android.sdk.api.session.room.peeking.PeekResult import org.matrix.android.sdk.api.session.room.peeking.PeekResult
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.api.util.Optional import org.matrix.android.sdk.api.util.Optional
import org.matrix.android.sdk.internal.session.room.alias.RoomAliasDescription import org.matrix.android.sdk.internal.session.room.alias.RoomAliasDescription
@ -36,22 +34,19 @@ interface RoomService {
/** /**
* Create a room asynchronously * Create a room asynchronously
*/ */
fun createRoom(createRoomParams: CreateRoomParams, suspend fun createRoom(createRoomParams: CreateRoomParams): String
callback: MatrixCallback<String>): Cancelable
/** /**
* Create a direct room asynchronously. This is a facility method to create a direct room with the necessary parameters * Create a direct room asynchronously. This is a facility method to create a direct room with the necessary parameters
*/ */
fun createDirectRoom(otherUserId: String, suspend fun createDirectRoom(otherUserId: String): String {
callback: MatrixCallback<String>): Cancelable {
return createRoom( return createRoom(
CreateRoomParams() CreateRoomParams()
.apply { .apply {
invitedUserIds.add(otherUserId) invitedUserIds.add(otherUserId)
setDirectMessage() setDirectMessage()
enableEncryptionIfInvitedUsersSupportIt = true enableEncryptionIfInvitedUsersSupportIt = true
}, }
callback
) )
} }
@ -61,10 +56,9 @@ interface RoomService {
* @param reason optional reason for joining the room * @param reason optional reason for joining the room
* @param viaServers the servers to attempt to join the room through. One of the servers must be participating in the room. * @param viaServers the servers to attempt to join the room through. One of the servers must be participating in the room.
*/ */
fun joinRoom(roomIdOrAlias: String, suspend fun joinRoom(roomIdOrAlias: String,
reason: String? = null, reason: String? = null,
viaServers: List<String> = emptyList(), viaServers: List<String> = emptyList())
callback: MatrixCallback<Unit>): Cancelable
/** /**
* Get a room from a roomId * Get a room from a roomId
@ -110,20 +104,18 @@ interface RoomService {
* Inform the Matrix SDK that a room is displayed. * Inform the Matrix SDK that a room is displayed.
* The SDK will update the breadcrumbs in the user account data * The SDK will update the breadcrumbs in the user account data
*/ */
fun onRoomDisplayed(roomId: String): Cancelable suspend fun onRoomDisplayed(roomId: String)
/** /**
* Mark all rooms as read * Mark all rooms as read
*/ */
fun markAllAsRead(roomIds: List<String>, suspend fun markAllAsRead(roomIds: List<String>)
callback: MatrixCallback<Unit>): Cancelable
/** /**
* Resolve a room alias to a room ID. * Resolve a room alias to a room ID.
*/ */
fun getRoomIdByAlias(roomAlias: String, suspend fun getRoomIdByAlias(roomAlias: String,
searchOnServer: Boolean, searchOnServer: Boolean): Optional<RoomAliasDescription>
callback: MatrixCallback<Optional<RoomAliasDescription>>): Cancelable
/** /**
* Delete a room alias * Delete a room alias
@ -170,12 +162,12 @@ interface RoomService {
/** /**
* Get some state events about a room * Get some state events about a room
*/ */
fun getRoomState(roomId: String, callback: MatrixCallback<List<Event>>) suspend fun getRoomState(roomId: String): List<Event>
/** /**
* Use this if you want to get information from a room that you are not yet in (or invited) * Use this if you want to get information from a room that you are not yet in (or invited)
* It might be possible to get some information on this room if it is public or if guest access is allowed * It might be possible to get some information on this room if it is public or if guest access is allowed
* This call will try to gather some information on this room, but it could fail and get nothing more * This call will try to gather some information on this room, but it could fail and get nothing more
*/ */
fun peekRoom(roomIdOrAlias: String, callback: MatrixCallback<PeekResult>) suspend fun peekRoom(roomIdOrAlias: String): PeekResult
} }

View file

@ -19,7 +19,6 @@ package org.matrix.android.sdk.internal.session.room
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations import androidx.lifecycle.Transformations
import com.zhuinden.monarchy.Monarchy import com.zhuinden.monarchy.Monarchy
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.session.events.model.Event import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.room.Room import org.matrix.android.sdk.api.session.room.Room
import org.matrix.android.sdk.api.session.room.RoomService import org.matrix.android.sdk.api.session.room.RoomService
@ -29,7 +28,6 @@ import org.matrix.android.sdk.api.session.room.model.RoomMemberSummary
import org.matrix.android.sdk.api.session.room.model.RoomSummary import org.matrix.android.sdk.api.session.room.model.RoomSummary
import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams
import org.matrix.android.sdk.api.session.room.peeking.PeekResult import org.matrix.android.sdk.api.session.room.peeking.PeekResult
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.api.util.Optional import org.matrix.android.sdk.api.util.Optional
import org.matrix.android.sdk.api.util.toOptional import org.matrix.android.sdk.api.util.toOptional
import org.matrix.android.sdk.internal.database.mapper.asDomain import org.matrix.android.sdk.internal.database.mapper.asDomain
@ -47,8 +45,6 @@ import org.matrix.android.sdk.internal.session.room.peeking.ResolveRoomStateTask
import org.matrix.android.sdk.internal.session.room.read.MarkAllRoomsReadTask import org.matrix.android.sdk.internal.session.room.read.MarkAllRoomsReadTask
import org.matrix.android.sdk.internal.session.room.summary.RoomSummaryDataSource import org.matrix.android.sdk.internal.session.room.summary.RoomSummaryDataSource
import org.matrix.android.sdk.internal.session.user.accountdata.UpdateBreadcrumbsTask import org.matrix.android.sdk.internal.session.user.accountdata.UpdateBreadcrumbsTask
import org.matrix.android.sdk.internal.task.TaskExecutor
import org.matrix.android.sdk.internal.task.configureWith
import org.matrix.android.sdk.internal.util.fetchCopied import org.matrix.android.sdk.internal.util.fetchCopied
import javax.inject.Inject import javax.inject.Inject
@ -64,16 +60,11 @@ internal class DefaultRoomService @Inject constructor(
private val peekRoomTask: PeekRoomTask, private val peekRoomTask: PeekRoomTask,
private val roomGetter: RoomGetter, private val roomGetter: RoomGetter,
private val roomSummaryDataSource: RoomSummaryDataSource, private val roomSummaryDataSource: RoomSummaryDataSource,
private val roomChangeMembershipStateDataSource: RoomChangeMembershipStateDataSource, private val roomChangeMembershipStateDataSource: RoomChangeMembershipStateDataSource
private val taskExecutor: TaskExecutor
) : RoomService { ) : RoomService {
override fun createRoom(createRoomParams: CreateRoomParams, callback: MatrixCallback<String>): Cancelable { override suspend fun createRoom(createRoomParams: CreateRoomParams): String {
return createRoomTask return createRoomTask.execute(createRoomParams)
.configureWith(createRoomParams) {
this.callback = callback
}
.executeBy(taskExecutor)
} }
override fun getRoom(roomId: String): Room? { override fun getRoom(roomId: String): Room? {
@ -104,34 +95,20 @@ internal class DefaultRoomService @Inject constructor(
return roomSummaryDataSource.getBreadcrumbsLive(queryParams) return roomSummaryDataSource.getBreadcrumbsLive(queryParams)
} }
override fun onRoomDisplayed(roomId: String): Cancelable { override suspend fun onRoomDisplayed(roomId: String) {
return updateBreadcrumbsTask updateBreadcrumbsTask.execute(UpdateBreadcrumbsTask.Params(roomId))
.configureWith(UpdateBreadcrumbsTask.Params(roomId))
.executeBy(taskExecutor)
} }
override fun joinRoom(roomIdOrAlias: String, reason: String?, viaServers: List<String>, callback: MatrixCallback<Unit>): Cancelable { override suspend fun joinRoom(roomIdOrAlias: String, reason: String?, viaServers: List<String>) {
return joinRoomTask joinRoomTask.execute(JoinRoomTask.Params(roomIdOrAlias, reason, viaServers))
.configureWith(JoinRoomTask.Params(roomIdOrAlias, reason, viaServers)) {
this.callback = callback
}
.executeBy(taskExecutor)
} }
override fun markAllAsRead(roomIds: List<String>, callback: MatrixCallback<Unit>): Cancelable { override suspend fun markAllAsRead(roomIds: List<String>) {
return markAllRoomsReadTask markAllRoomsReadTask.execute(MarkAllRoomsReadTask.Params(roomIds))
.configureWith(MarkAllRoomsReadTask.Params(roomIds)) {
this.callback = callback
}
.executeBy(taskExecutor)
} }
override fun getRoomIdByAlias(roomAlias: String, searchOnServer: Boolean, callback: MatrixCallback<Optional<RoomAliasDescription>>): Cancelable { override suspend fun getRoomIdByAlias(roomAlias: String, searchOnServer: Boolean): Optional<RoomAliasDescription> {
return roomIdByAliasTask return roomIdByAliasTask.execute(GetRoomIdByAliasTask.Params(roomAlias, searchOnServer))
.configureWith(GetRoomIdByAliasTask.Params(roomAlias, searchOnServer)) {
this.callback = callback
}
.executeBy(taskExecutor)
} }
override suspend fun deleteRoomAlias(roomAlias: String) { override suspend fun deleteRoomAlias(roomAlias: String) {
@ -162,19 +139,11 @@ internal class DefaultRoomService @Inject constructor(
} }
} }
override fun getRoomState(roomId: String, callback: MatrixCallback<List<Event>>) { override suspend fun getRoomState(roomId: String): List<Event> {
resolveRoomStateTask return resolveRoomStateTask.execute(ResolveRoomStateTask.Params(roomId))
.configureWith(ResolveRoomStateTask.Params(roomId)) {
this.callback = callback
}
.executeBy(taskExecutor)
} }
override fun peekRoom(roomIdOrAlias: String, callback: MatrixCallback<PeekResult>) { override suspend fun peekRoom(roomIdOrAlias: String): PeekResult {
peekRoomTask return peekRoomTask.execute(PeekRoomTask.Params(roomIdOrAlias))
.configureWith(PeekRoomTask.Params(roomIdOrAlias)) {
this.callback = callback
}
.executeBy(taskExecutor)
} }
} }

View file

@ -22,7 +22,6 @@ import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.raw.RawService import org.matrix.android.sdk.api.raw.RawService
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams
import org.matrix.android.sdk.internal.util.awaitCallback
import javax.inject.Inject import javax.inject.Inject
class DirectRoomHelper @Inject constructor( class DirectRoomHelper @Inject constructor(
@ -45,9 +44,7 @@ class DirectRoomHelper @Inject constructor(
setDirectMessage() setDirectMessage()
enableEncryptionIfInvitedUsersSupportIt = adminE2EByDefault enableEncryptionIfInvitedUsersSupportIt = adminE2EByDefault
} }
roomId = awaitCallback { roomId = session.createRoom(roomParams)
session.createRoom(roomParams, it)
}
} }
return roomId return roomId
} }

View file

@ -249,32 +249,34 @@ class VerificationBottomSheetViewModel @AssistedInject constructor(
pendingRequest = Loading() pendingRequest = Loading()
) )
} }
session.createDirectRoom(otherUserId, object : MatrixCallback<String> { viewModelScope.launch {
override fun onSuccess(data: String) { val result = runCatching { session.createDirectRoom(otherUserId) }
setState { result.fold(
copy( { data ->
roomId = data, setState {
pendingRequest = Success( copy(
session roomId = data,
.cryptoService() pendingRequest = Success(
.verificationService() session
.requestKeyVerificationInDMs( .cryptoService()
supportedVerificationMethodsProvider.provide(), .verificationService()
otherUserId, .requestKeyVerificationInDMs(
data, supportedVerificationMethodsProvider.provide(),
pendingLocalId otherUserId,
) data,
pendingLocalId
)
)
) )
) }
} },
} { failure ->
setState {
override fun onFailure(failure: Throwable) { copy(pendingRequest = Fail(failure))
setState { }
copy(pendingRequest = Fail(failure)) }
} )
} }
})
} else { } else {
setState { setState {
copy( copy(

View file

@ -184,7 +184,12 @@ class RoomDetailViewModel @AssistedInject constructor(
} }
} }
// Inform the SDK that the room is displayed // Inform the SDK that the room is displayed
session.onRoomDisplayed(initialState.roomId) viewModelScope.launch {
try {
session.onRoomDisplayed(initialState.roomId)
} catch (_: Exception) {
}
}
callManager.addPstnSupportListener(this) callManager.addPstnSupportListener(this)
callManager.checkForPSTNSupportIfNeeded() callManager.checkForPSTNSupportIfNeeded()
chatEffectManager.delegate = this chatEffectManager.delegate = this
@ -902,19 +907,19 @@ class RoomDetailViewModel @AssistedInject constructor(
} }
private fun handleJoinToAnotherRoomSlashCommand(command: ParsedCommand.JoinRoom) { private fun handleJoinToAnotherRoomSlashCommand(command: ParsedCommand.JoinRoom) {
session.joinRoom(command.roomAlias, command.reason, emptyList(), object : MatrixCallback<Unit> { viewModelScope.launch {
override fun onSuccess(data: Unit) { try {
session.getRoomSummary(command.roomAlias) session.joinRoom(command.roomAlias, command.reason, emptyList())
?.roomId } catch (failure: Throwable) {
?.let {
_viewEvents.post(RoomDetailViewEvents.JoinRoomCommandSuccess(it))
}
}
override fun onFailure(failure: Throwable) {
_viewEvents.post(RoomDetailViewEvents.SlashCommandResultError(failure)) _viewEvents.post(RoomDetailViewEvents.SlashCommandResultError(failure))
return@launch
} }
}) session.getRoomSummary(command.roomAlias)
?.roomId
?.let {
_viewEvents.post(RoomDetailViewEvents.JoinRoomCommandSuccess(it))
}
}
} }
private fun legacyRiotQuoteText(quotedText: String?, myText: String): String { private fun legacyRiotQuoteText(quotedText: String?, myText: String): String {

View file

@ -26,7 +26,6 @@ import im.vector.app.core.utils.DataSource
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.NoOpMatrixCallback
import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.model.Membership
@ -169,7 +168,14 @@ class RoomListViewModel @Inject constructor(initialState: RoomListViewState,
?.filter { it.membership == Membership.JOIN } ?.filter { it.membership == Membership.JOIN }
?.map { it.roomId } ?.map { it.roomId }
?.toList() ?.toList()
?.let { session.markAllAsRead(it, NoOpMatrixCallback()) } ?.let {
viewModelScope.launch {
try {
session.markAllAsRead(it)
} catch (_: Exception) {
}
}
}
} }
private fun handleChangeNotificationMode(action: RoomListAction.ChangeRoomNotificationState) { private fun handleChangeNotificationMode(action: RoomListAction.ChangeRoomNotificationState) {

View file

@ -31,7 +31,6 @@ import im.vector.app.core.platform.VectorViewModel
import im.vector.app.features.settings.VectorPreferences import im.vector.app.features.settings.VectorPreferences
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.failure.Failure
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
@ -232,17 +231,16 @@ class RoomDirectoryViewModel @AssistedInject constructor(
val viaServers = state.roomDirectoryData.homeServer val viaServers = state.roomDirectoryData.homeServer
?.let { listOf(it) } ?.let { listOf(it) }
.orEmpty() .orEmpty()
session.joinRoom(action.roomId, viaServers = viaServers, callback = object : MatrixCallback<Unit> { viewModelScope.launch {
override fun onSuccess(data: Unit) { try {
session.joinRoom(action.roomId, viaServers = viaServers)
// We do not update the joiningRoomsIds here, because, the room is not joined yet regarding the sync data. // We do not update the joiningRoomsIds here, because, the room is not joined yet regarding the sync data.
// Instead, we wait for the room to be joined // Instead, we wait for the room to be joined
} } catch (failure: Throwable) {
override fun onFailure(failure: Throwable) {
// Notify the user // Notify the user
_viewEvents.post(RoomDirectoryViewEvents.Failure(failure)) _viewEvents.post(RoomDirectoryViewEvents.Failure(failure))
} }
}) }
} }
override fun onCleared() { override fun onCleared() {

View file

@ -34,7 +34,6 @@ import im.vector.app.features.raw.wellknown.getElementWellknown
import im.vector.app.features.raw.wellknown.isE2EByDefault import im.vector.app.features.raw.wellknown.isE2EByDefault
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.raw.RawService import org.matrix.android.sdk.api.raw.RawService
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
@ -216,19 +215,22 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: Cr
} }
} }
session.createRoom(createRoomParams, object : MatrixCallback<String> { // TODO: Should this be non-cancellable?
override fun onSuccess(data: String) { viewModelScope.launch {
setState { val result = runCatching { session.createRoom(createRoomParams) }
copy(asyncCreateRoomRequest = Success(data)) result.fold(
} { roomId ->
} setState {
copy(asyncCreateRoomRequest = Success(roomId))
override fun onFailure(failure: Throwable) { }
setState { },
copy(asyncCreateRoomRequest = Fail(failure)) { failure ->
} setState {
_viewEvents.post(CreateRoomViewEvents.Failure(failure)) copy(asyncCreateRoomRequest = Fail(failure))
} }
}) _viewEvents.post(CreateRoomViewEvents.Failure(failure))
}
)
}
} }
} }

View file

@ -31,7 +31,6 @@ import im.vector.app.core.platform.VectorViewModel
import im.vector.app.features.roomdirectory.JoinState import im.vector.app.features.roomdirectory.JoinState
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.query.QueryStringValue import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
@ -39,7 +38,6 @@ import org.matrix.android.sdk.api.session.room.members.ChangeMembershipState
import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.api.session.room.peeking.PeekResult import org.matrix.android.sdk.api.session.room.peeking.PeekResult
import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams
import org.matrix.android.sdk.internal.util.awaitCallback
import org.matrix.android.sdk.rx.rx import org.matrix.android.sdk.rx.rx
import timber.log.Timber import timber.log.Timber
@ -77,9 +75,7 @@ class RoomPreviewViewModel @AssistedInject constructor(@Assisted private val ini
} }
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
val peekResult = tryOrNull { val peekResult = tryOrNull {
awaitCallback<PeekResult> { session.peekRoom(initialState.roomAlias ?: initialState.roomId)
session.peekRoom(initialState.roomAlias ?: initialState.roomId, it)
}
} }
when (peekResult) { when (peekResult) {
@ -177,15 +173,14 @@ class RoomPreviewViewModel @AssistedInject constructor(@Assisted private val ini
Timber.w("Try to join an already joining room. Should not happen") Timber.w("Try to join an already joining room. Should not happen")
return@withState return@withState
} }
session.joinRoom(state.roomId, viaServers = state.homeServers, callback = object : MatrixCallback<Unit> { viewModelScope.launch {
override fun onSuccess(data: Unit) { try {
session.joinRoom(state.roomId, viaServers = state.homeServers)
// We do not update the joiningRoomsIds here, because, the room is not joined yet regarding the sync data. // We do not update the joiningRoomsIds here, because, the room is not joined yet regarding the sync data.
// Instead, we wait for the room to be joined // Instead, we wait for the room to be joined
} } catch (failure: Throwable) {
override fun onFailure(failure: Throwable) {
setState { copy(lastError = failure) } setState { copy(lastError = failure) }
} }
}) }
} }
} }