Reduce usage of rxSingle when it's easy to do it

This commit is contained in:
Benoit Marty 2021-04-20 17:08:17 +02:00
parent 187d6136fd
commit 5862c8cb60
5 changed files with 46 additions and 34 deletions

View file

@ -19,9 +19,7 @@ package org.matrix.android.sdk.rx
import android.net.Uri import android.net.Uri
import io.reactivex.Completable import io.reactivex.Completable
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.Single
import kotlinx.coroutines.rx2.rxCompletable import kotlinx.coroutines.rx2.rxCompletable
import kotlinx.coroutines.rx2.rxSingle
import org.matrix.android.sdk.api.query.QueryStringValue import org.matrix.android.sdk.api.query.QueryStringValue
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.identity.ThreePid import org.matrix.android.sdk.api.session.identity.ThreePid
@ -91,15 +89,6 @@ class RxRoom(private val room: Room) {
return room.getMyReadReceiptLive().asObservable() return room.getMyReadReceiptLive().asObservable()
} }
fun loadRoomMembersIfNeeded(): Single<Unit> = rxSingle {
room.loadRoomMembersIfNeeded()
}
fun joinRoom(reason: String? = null,
viaServers: List<String> = emptyList()): Single<Unit> = rxSingle {
room.join(reason, viaServers)
}
fun liveEventReadReceipts(eventId: String): Observable<List<ReadReceipt>> { fun liveEventReadReceipts(eventId: String): Observable<List<ReadReceipt>> {
return room.getEventReadReceiptsLive(eventId).asObservable() return room.getEventReadReceiptsLive(eventId).asObservable()
} }

View file

@ -38,7 +38,6 @@ import org.matrix.android.sdk.api.session.room.RoomSummaryQueryParams
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.sync.SyncState import org.matrix.android.sdk.api.session.sync.SyncState
import org.matrix.android.sdk.api.session.user.model.User import org.matrix.android.sdk.api.session.user.model.User
import org.matrix.android.sdk.api.session.widgets.model.Widget import org.matrix.android.sdk.api.session.widgets.model.Widget
@ -124,22 +123,12 @@ class RxSession(private val session: Session) {
.startWithCallable { session.getPendingThreePids() } .startWithCallable { session.getPendingThreePids() }
} }
fun createRoom(roomParams: CreateRoomParams): Single<String> = rxSingle {
session.createRoom(roomParams)
}
fun searchUsersDirectory(search: String, fun searchUsersDirectory(search: String,
limit: Int, limit: Int,
excludedUserIds: Set<String>): Single<List<User>> = rxSingle { excludedUserIds: Set<String>): Single<List<User>> = rxSingle {
session.searchUsersDirectory(search, limit, excludedUserIds) session.searchUsersDirectory(search, limit, excludedUserIds)
} }
fun joinRoom(roomIdOrAlias: String,
reason: String? = null,
viaServers: List<String> = emptyList()): Single<Unit> = rxSingle {
session.joinRoom(roomIdOrAlias, reason, viaServers)
}
fun getRoomIdByAlias(roomAlias: String, fun getRoomIdByAlias(roomAlias: String,
searchOnServer: Boolean): Single<Optional<RoomAliasDescription>> = rxSingle { searchOnServer: Boolean): Single<Optional<RoomAliasDescription>> = rxSingle {
session.getRoomIdByAlias(roomAlias, searchOnServer) session.getRoomIdByAlias(roomAlias, searchOnServer)

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2021 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.app.core.mvrx
import com.airbnb.mvrx.Async
import com.airbnb.mvrx.Fail
import com.airbnb.mvrx.Success
fun <A> Result<A>.foldToAsync(): Async<A> = fold(
{ Success(it) },
{ Fail(it) }
)

View file

@ -26,6 +26,7 @@ import dagger.assisted.Assisted
import dagger.assisted.AssistedInject import dagger.assisted.AssistedInject
import dagger.assisted.AssistedFactory import dagger.assisted.AssistedFactory
import im.vector.app.core.extensions.exhaustive import im.vector.app.core.extensions.exhaustive
import im.vector.app.core.mvrx.foldToAsync
import im.vector.app.core.platform.VectorViewModel import im.vector.app.core.platform.VectorViewModel
import im.vector.app.features.raw.wellknown.getElementWellknown import im.vector.app.features.raw.wellknown.getElementWellknown
import im.vector.app.features.raw.wellknown.isE2EByDefault import im.vector.app.features.raw.wellknown.isE2EByDefault
@ -35,7 +36,6 @@ import kotlinx.coroutines.launch
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.rx.rx
class CreateDirectRoomViewModel @AssistedInject constructor(@Assisted class CreateDirectRoomViewModel @AssistedInject constructor(@Assisted
initialState: CreateDirectRoomViewState, initialState: CreateDirectRoomViewState,
@ -99,11 +99,15 @@ class CreateDirectRoomViewModel @AssistedInject constructor(@Assisted
enableEncryptionIfInvitedUsersSupportIt = adminE2EByDefault enableEncryptionIfInvitedUsersSupportIt = adminE2EByDefault
} }
session.rx() val result = runCatching {
.createRoom(roomParams) session.createRoom(roomParams)
.execute { }.foldToAsync()
copy(createAndInviteState = it)
} setState {
copy(
createAndInviteState = result
)
}
} }
} }
} }

View file

@ -35,6 +35,7 @@ import dagger.assisted.AssistedInject
import im.vector.app.BuildConfig import im.vector.app.BuildConfig
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.extensions.exhaustive import im.vector.app.core.extensions.exhaustive
import im.vector.app.core.mvrx.foldToAsync
import im.vector.app.core.platform.VectorViewModel import im.vector.app.core.platform.VectorViewModel
import im.vector.app.core.resources.StringProvider import im.vector.app.core.resources.StringProvider
import im.vector.app.features.call.dialpad.DialPadLookup import im.vector.app.features.call.dialpad.DialPadLookup
@ -649,12 +650,15 @@ class RoomDetailViewModel @AssistedInject constructor(
val viaServers = MatrixPatterns.extractServerNameFromId(action.event.senderId) val viaServers = MatrixPatterns.extractServerNameFromId(action.event.senderId)
?.let { listOf(it) } ?.let { listOf(it) }
.orEmpty() .orEmpty()
session.rx() viewModelScope.launch {
.joinRoom(roomId, viaServers = viaServers) val result = runCatching {
.map { roomId } session.joinRoom(roomId, viaServers = viaServers)
.execute { roomId
copy(tombstoneEventHandling = it) }.foldToAsync()
} setState {
copy(tombstoneEventHandling = result)
}
}
} }
} }