Code review AliasAvailabilityResult

This commit is contained in:
Valere 2021-06-15 11:22:56 +02:00
parent 9190e1bfb5
commit 19a2b56011
4 changed files with 43 additions and 10 deletions

View File

@ -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()
}

View File

@ -41,5 +41,5 @@ interface RoomDirectoryService {
*/
suspend fun setRoomDirectoryVisibility(roomId: String, roomDirectoryVisibility: RoomDirectoryVisibility)
suspend fun checkAliasAvailability(aliasLocalPart: String?) : Result<Unit>
suspend fun checkAliasAvailability(aliasLocalPart: String?) : AliasAvailabilityResult
}

View File

@ -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<Unit> {
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)
}
}
}

View File

@ -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)
}
}
}
}