diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomDirectoryService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomDirectoryService.kt index 55f3b76760..176de8e408 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomDirectoryService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomDirectoryService.kt @@ -16,11 +16,9 @@ package org.matrix.android.sdk.api.session.room -import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.session.room.model.RoomDirectoryVisibility import org.matrix.android.sdk.api.session.room.model.roomdirectory.PublicRoomsParams import org.matrix.android.sdk.api.session.room.model.roomdirectory.PublicRoomsResponse -import org.matrix.android.sdk.api.util.Cancelable /** * This interface defines methods to get and join public rooms. It's implemented at the session level. @@ -30,9 +28,8 @@ interface RoomDirectoryService { /** * Get rooms from directory */ - fun getPublicRooms(server: String?, - publicRoomsParams: PublicRoomsParams, - callback: MatrixCallback): Cancelable + suspend fun getPublicRooms(server: String?, + publicRoomsParams: PublicRoomsParams): PublicRoomsResponse /** * Get the visibility of a room in the directory diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoomDirectoryService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoomDirectoryService.kt index f9047fdf3c..218d846afb 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoomDirectoryService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoomDirectoryService.kt @@ -16,33 +16,24 @@ package org.matrix.android.sdk.internal.session.room -import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.session.room.RoomDirectoryService import org.matrix.android.sdk.api.session.room.model.RoomDirectoryVisibility import org.matrix.android.sdk.api.session.room.model.roomdirectory.PublicRoomsParams import org.matrix.android.sdk.api.session.room.model.roomdirectory.PublicRoomsResponse -import org.matrix.android.sdk.api.util.Cancelable import org.matrix.android.sdk.internal.session.room.directory.GetPublicRoomTask import org.matrix.android.sdk.internal.session.room.directory.GetRoomDirectoryVisibilityTask import org.matrix.android.sdk.internal.session.room.directory.SetRoomDirectoryVisibilityTask -import org.matrix.android.sdk.internal.task.TaskExecutor -import org.matrix.android.sdk.internal.task.configureWith import javax.inject.Inject internal class DefaultRoomDirectoryService @Inject constructor( private val getPublicRoomTask: GetPublicRoomTask, private val getRoomDirectoryVisibilityTask: GetRoomDirectoryVisibilityTask, - private val setRoomDirectoryVisibilityTask: SetRoomDirectoryVisibilityTask, - private val taskExecutor: TaskExecutor) : RoomDirectoryService { + private val setRoomDirectoryVisibilityTask: SetRoomDirectoryVisibilityTask +) : RoomDirectoryService { - override fun getPublicRooms(server: String?, - publicRoomsParams: PublicRoomsParams, - callback: MatrixCallback): Cancelable { - return getPublicRoomTask - .configureWith(GetPublicRoomTask.Params(server, publicRoomsParams)) { - this.callback = callback - } - .executeBy(taskExecutor) + override suspend fun getPublicRooms(server: String?, + publicRoomsParams: PublicRoomsParams): PublicRoomsResponse { + return getPublicRoomTask.execute(GetPublicRoomTask.Params(server, publicRoomsParams)) } override suspend fun getRoomDirectoryVisibility(roomId: String): RoomDirectoryVisibility { diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/RoomDirectoryViewModel.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/RoomDirectoryViewModel.kt index 7a57be773f..ea1a5d3ada 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/RoomDirectoryViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/RoomDirectoryViewModel.kt @@ -16,6 +16,7 @@ package im.vector.app.features.roomdirectory +import androidx.lifecycle.viewModelScope import com.airbnb.mvrx.ActivityViewModelContext import com.airbnb.mvrx.Fail import com.airbnb.mvrx.Loading @@ -24,9 +25,11 @@ import com.airbnb.mvrx.Success import com.airbnb.mvrx.ViewModelContext import com.airbnb.mvrx.appendAt import dagger.assisted.Assisted -import dagger.assisted.AssistedInject import dagger.assisted.AssistedFactory +import dagger.assisted.AssistedInject import im.vector.app.core.platform.VectorViewModel +import kotlinx.coroutines.Job +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.failure.Failure @@ -34,10 +37,8 @@ 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.roomdirectory.PublicRoomsFilter import org.matrix.android.sdk.api.session.room.model.roomdirectory.PublicRoomsParams -import org.matrix.android.sdk.api.session.room.model.roomdirectory.PublicRoomsResponse import org.matrix.android.sdk.api.session.room.model.thirdparty.RoomDirectoryData import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams -import org.matrix.android.sdk.api.util.Cancelable import org.matrix.android.sdk.rx.rx import timber.log.Timber @@ -63,7 +64,7 @@ class RoomDirectoryViewModel @AssistedInject constructor(@Assisted initialState: private var since: String? = null - private var currentTask: Cancelable? = null + private var currentJob: Job? = null init { // Observe joined room (from the sync) @@ -122,7 +123,7 @@ class RoomDirectoryViewModel @AssistedInject constructor(@Assisted initialState: private fun filterWith(action: RoomDirectoryAction.FilterWith) = withState { state -> if (state.currentFilter != action.filter) { - currentTask?.cancel() + currentJob?.cancel() reset(action.filter) load(action.filter, state.roomDirectoryData) @@ -144,7 +145,7 @@ class RoomDirectoryViewModel @AssistedInject constructor(@Assisted initialState: } private fun loadMore() = withState { state -> - if (currentTask == null) { + if (currentJob == null) { setState { copy( asyncPublicRoomsRequest = Loading() @@ -155,47 +156,50 @@ class RoomDirectoryViewModel @AssistedInject constructor(@Assisted initialState: } private fun load(filter: String, roomDirectoryData: RoomDirectoryData) { - currentTask = session.getPublicRooms(roomDirectoryData.homeServer, - PublicRoomsParams( - limit = PUBLIC_ROOMS_LIMIT, - filter = PublicRoomsFilter(searchTerm = filter), - includeAllNetworks = roomDirectoryData.includeAllNetworks, - since = since, - thirdPartyInstanceId = roomDirectoryData.thirdPartyInstanceId - ), - object : MatrixCallback { - override fun onSuccess(data: PublicRoomsResponse) { - currentTask = null + currentJob = viewModelScope.launch { + val data = try { + session.getPublicRooms(roomDirectoryData.homeServer, + PublicRoomsParams( + limit = PUBLIC_ROOMS_LIMIT, + filter = PublicRoomsFilter(searchTerm = filter), + includeAllNetworks = roomDirectoryData.includeAllNetworks, + since = since, + thirdPartyInstanceId = roomDirectoryData.thirdPartyInstanceId + ) + ) + } catch (failure: Throwable) { + if (failure is Failure.Cancelled) { + // Ignore, another request should be already started + return@launch + } - since = data.nextBatch + currentJob = null - setState { - copy( - asyncPublicRoomsRequest = Success(Unit), - // It's ok to append at the end of the list, so I use publicRooms.size() - publicRooms = publicRooms.appendAt(data.chunk!!, publicRooms.size) - // Rageshake #8206 tells that we can have several times the same room - .distinctBy { it.roomId }, - hasMore = since != null - ) - } - } + setState { + copy( + asyncPublicRoomsRequest = Fail(failure) + ) + } + null + } - override fun onFailure(failure: Throwable) { - if (failure is Failure.Cancelled) { - // Ignore, another request should be already started - return - } + data ?: return@launch - currentTask = null + currentJob = null - setState { - copy( - asyncPublicRoomsRequest = Fail(failure) - ) - } - } - }) + since = data.nextBatch + + setState { + copy( + asyncPublicRoomsRequest = Success(Unit), + // It's ok to append at the end of the list, so I use publicRooms.size() + publicRooms = publicRooms.appendAt(data.chunk!!, publicRooms.size) + // Rageshake #8206 tells that we can have several times the same room + .distinctBy { it.roomId }, + hasMore = since != null + ) + } + } } private fun joinRoom(action: RoomDirectoryAction.JoinRoom) = withState { state -> @@ -222,7 +226,7 @@ class RoomDirectoryViewModel @AssistedInject constructor(@Assisted initialState: } override fun onCleared() { - currentTask?.cancel() + currentJob?.cancel() super.onCleared() } }