diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/AliasAvailabilityResult.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/AliasAvailabilityResult.kt new file mode 100644 index 0000000000..d5c0d06215 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/AliasAvailabilityResult.kt @@ -0,0 +1,24 @@ +/* + * 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 org.matrix.android.sdk.api.session.room + +import org.matrix.android.sdk.api.session.room.alias.RoomAliasError + +sealed class AliasAvailabilityResult { + object Available: AliasAvailabilityResult() + data class NotAvailable(val roomAliasError: RoomAliasError) : AliasAvailabilityResult() +} 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 4b19e7ba96..f3e3913bc1 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 @@ -41,5 +41,5 @@ interface RoomDirectoryService { */ suspend fun setRoomDirectoryVisibility(roomId: String, roomDirectoryVisibility: RoomDirectoryVisibility) - suspend fun checkAliasAvailability(aliasLocalPart: String?) : Result + suspend fun checkAliasAvailability(aliasLocalPart: String?) : AliasAvailabilityResult } 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 7da1da27ed..7330c91c20 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,6 +16,7 @@ package org.matrix.android.sdk.internal.session.room +import org.matrix.android.sdk.api.session.room.AliasAvailabilityResult import org.matrix.android.sdk.api.session.room.RoomDirectoryService import org.matrix.android.sdk.api.session.room.alias.RoomAliasError import org.matrix.android.sdk.api.session.room.model.RoomDirectoryVisibility @@ -47,12 +48,12 @@ internal class DefaultRoomDirectoryService @Inject constructor( setRoomDirectoryVisibilityTask.execute(SetRoomDirectoryVisibilityTask.Params(roomId, roomDirectoryVisibility)) } - override suspend fun checkAliasAvailability(aliasLocalPart: String?): Result { + override suspend fun checkAliasAvailability(aliasLocalPart: String?): AliasAvailabilityResult { return try { roomAliasAvailabilityChecker.check(aliasLocalPart) - Result.success(Unit) + AliasAvailabilityResult.Available } catch (failure: RoomAliasError) { - Result.failure(failure) + AliasAvailabilityResult.NotAvailable(failure) } } } diff --git a/vector/src/main/java/im/vector/app/features/spaces/create/CreateSpaceViewModel.kt b/vector/src/main/java/im/vector/app/features/spaces/create/CreateSpaceViewModel.kt index 7b430dd411..960d428581 100644 --- a/vector/src/main/java/im/vector/app/features/spaces/create/CreateSpaceViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/spaces/create/CreateSpaceViewModel.kt @@ -36,6 +36,7 @@ import im.vector.app.core.resources.StringProvider import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import org.matrix.android.sdk.api.session.Session +import org.matrix.android.sdk.api.session.room.AliasAvailabilityResult import org.matrix.android.sdk.api.session.room.failure.CreateRoomFailure class CreateSpaceViewModel @AssistedInject constructor( @@ -242,8 +243,9 @@ class CreateSpaceViewModel @AssistedInject constructor( copy(aliasVerificationTask = Loading()) } viewModelScope.launch { - session.checkAliasAvailability(aliasLocalPart).fold( - { + try { + when (val result = session.checkAliasAvailability(aliasLocalPart)) { + AliasAvailabilityResult.Available -> { setState { copy( step = CreateSpaceState.Step.AddRooms @@ -251,14 +253,20 @@ class CreateSpaceViewModel @AssistedInject constructor( } _viewEvents.post(CreateSpaceEvents.HideModalLoading) _viewEvents.post(CreateSpaceEvents.NavigateToAddRooms) - }, - { + } + is AliasAvailabilityResult.NotAvailable -> { setState { - copy(aliasVerificationTask = Fail(it)) + copy(aliasVerificationTask = Fail(result.roomAliasError)) } _viewEvents.post(CreateSpaceEvents.HideModalLoading) } - ) + } + } catch (failure: Throwable) { + setState { + copy(aliasVerificationTask = Fail(failure)) + } + _viewEvents.post(CreateSpaceEvents.HideModalLoading) + } } } }