diff --git a/vector/src/main/java/im/vector/riotx/core/error/ErrorFormatter.kt b/vector/src/main/java/im/vector/riotx/core/error/ErrorFormatter.kt index 65f48f5e7b..29506cf880 100644 --- a/vector/src/main/java/im/vector/riotx/core/error/ErrorFormatter.kt +++ b/vector/src/main/java/im/vector/riotx/core/error/ErrorFormatter.kt @@ -54,6 +54,9 @@ class ErrorFormatter @Inject constructor(private val stringProvider: StringProvi && throwable.error.message == "Invalid password" -> { stringProvider.getString(R.string.auth_invalid_login_param) } + throwable.error.code == MatrixError.USER_IN_USE -> { + stringProvider.getString(R.string.login_signup_error_user_in_use) + } else -> { throwable.error.message.takeIf { it.isNotEmpty() } ?: throwable.error.code.takeIf { it.isNotEmpty() } diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginFragment.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginFragment.kt index f21ce74d1a..88c1917616 100644 --- a/vector/src/main/java/im/vector/riotx/features/login/LoginFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginFragment.kt @@ -177,5 +177,18 @@ class LoginFragment @Inject constructor( // Success is handled by the LoginActivity is Success -> Unit } + + when (state.asyncRegistration) { + is Loading -> { + // Ensure password is hidden + passwordShown = false + renderPasswordField() + } + is Fail -> { + loginFieldTil.error = errorFormatter.toHumanReadable(state.asyncRegistration.error) + } + // Success is handled by the LoginActivity + is Success -> Unit + } } } diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginViewModel.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginViewModel.kt index bfb6be6733..f0d872ce48 100644 --- a/vector/src/main/java/im/vector/riotx/features/login/LoginViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginViewModel.kt @@ -74,6 +74,24 @@ class LoginViewModel @AssistedInject constructor(@Assisted initialState: LoginVi private var homeServerConnectionConfig: HomeServerConnectionConfig? = null private var currentTask: Cancelable? = null + private val registrationCallback = object : MatrixCallback { + override fun onSuccess(data: RegistrationResult) { + when (data) { + is RegistrationResult.Success -> onSessionCreated(data.session) + is RegistrationResult.FlowResponse -> onFlowResponse(data.flowResult) + } + } + + override fun onFailure(failure: Throwable) { + // TODO Handled JobCancellationException + setState { + copy( + asyncRegistration = Fail(failure) + ) + } + } + } + override fun handle(action: LoginAction) { when (action) { is LoginAction.UpdateServerType -> handleUpdateServerType(action) @@ -83,10 +101,27 @@ class LoginViewModel @AssistedInject constructor(@Assisted initialState: LoginVi is LoginAction.Login -> handleLogin(action) is LoginAction.WebLoginSuccess -> handleWebLoginSuccess(action) is LoginAction.ResetPassword -> handleResetPassword(action) + is LoginAction.RegisterAction -> handleRegisterAction(action) is LoginAction.ResetAction -> handleResetAction(action) } } + private fun handleRegisterAction(action: LoginAction.RegisterAction) { + when (action) { + is LoginAction.RegisterWith -> handleRegisterWith(action) + } + } + + private fun handleRegisterWith(action: LoginAction.RegisterWith) { + setState { + copy( + asyncRegistration = Loading() + ) + } + + currentTask = registrationWizard?.createAccount(action.username, action.password, null /* TODO InitialDisplayName */, registrationCallback) + } + private fun handleResetAction(action: LoginAction.ResetAction) { // Cancel any request currentTask?.cancel() diff --git a/vector/src/main/res/values/strings_riotX.xml b/vector/src/main/res/values/strings_riotX.xml index 42667ce5fa..abd44dc54c 100644 --- a/vector/src/main/res/values/strings_riotX.xml +++ b/vector/src/main/res/values/strings_riotX.xml @@ -93,5 +93,6 @@ Username Password Next + That username is taken