From ee3e10a4b019defced04ef9f9c42094852c7f313 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 31 Aug 2020 14:46:05 +0200 Subject: [PATCH] Add threePid: improve UI for phone numbers --- .../threepids/ThreePidsSettingsController.kt | 5 +++++ .../threepids/ThreePidsSettingsFragment.kt | 17 ++++++++++++++--- .../threepids/ThreePidsSettingsViewModel.kt | 16 +++++++++++++++- vector/src/main/res/values/strings.xml | 1 + 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/threepids/ThreePidsSettingsController.kt b/vector/src/main/java/im/vector/app/features/settings/threepids/ThreePidsSettingsController.kt index cf3e66bc2a..e8321ad456 100644 --- a/vector/src/main/java/im/vector/app/features/settings/threepids/ThreePidsSettingsController.kt +++ b/vector/src/main/java/im/vector/app/features/settings/threepids/ThreePidsSettingsController.kt @@ -31,6 +31,7 @@ import im.vector.app.core.resources.StringProvider import im.vector.app.core.ui.list.genericButtonItem import im.vector.app.core.ui.list.genericFooterItem import im.vector.app.features.discovery.settingsContinueCancelItem +import im.vector.app.features.discovery.settingsInfoItem import im.vector.app.features.discovery.settingsInformationItem import im.vector.app.features.discovery.settingsSectionTitleItem import im.vector.app.features.form.formEditTextItem @@ -149,6 +150,10 @@ class ThreePidsSettingsController @Inject constructor( } is ThreePidsSettingsState.AddingEmail -> Unit is ThreePidsSettingsState.AddingPhoneNumber -> { + settingsInfoItem { + id("addingMsisdnInfo") + helperText(stringProvider.getString(R.string.login_msisdn_notice)) + } formEditTextItem { id("addingMsisdn") inputType(InputType.TYPE_CLASS_PHONE) diff --git a/vector/src/main/java/im/vector/app/features/settings/threepids/ThreePidsSettingsFragment.kt b/vector/src/main/java/im/vector/app/features/settings/threepids/ThreePidsSettingsFragment.kt index 06ebf13b3b..27844f0c22 100644 --- a/vector/src/main/java/im/vector/app/features/settings/threepids/ThreePidsSettingsFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/threepids/ThreePidsSettingsFragment.kt @@ -94,15 +94,17 @@ class ThreePidsSettingsFragment @Inject constructor( } override fun doAddEmail(email: String) { + // Sanity + val safeEmail = email.trim().replace(" ", "") viewModel.handle(ThreePidsSettingsAction.ChangeState(ThreePidsSettingsState.AddingEmail(null))) // Check that email is valid - if (!email.isEmail()) { + if (!safeEmail.isEmail()) { viewModel.handle(ThreePidsSettingsAction.ChangeState(ThreePidsSettingsState.AddingEmail(getString(R.string.auth_invalid_email)))) return } - viewModel.handle(ThreePidsSettingsAction.AddThreePid(ThreePid.Email(email))) + viewModel.handle(ThreePidsSettingsAction.AddThreePid(ThreePid.Email(safeEmail))) } override fun addMsisdn() { @@ -110,9 +112,18 @@ class ThreePidsSettingsFragment @Inject constructor( } override fun doAddMsisdn(msisdn: String) { + // Sanity + val safeMsisdn = msisdn.trim().replace(" ", "") + viewModel.handle(ThreePidsSettingsAction.ChangeState(ThreePidsSettingsState.AddingPhoneNumber(null))) - TODO("Not yet implemented") + // Check that phone number is valid + if (!msisdn.startsWith("+")) { + viewModel.handle(ThreePidsSettingsAction.ChangeState(ThreePidsSettingsState.AddingPhoneNumber(getString(R.string.login_msisdn_error_not_international)))) + return + } + + viewModel.handle(ThreePidsSettingsAction.AddThreePid(ThreePid.Msisdn(safeMsisdn))) } override fun cancelAdding() { diff --git a/vector/src/main/java/im/vector/app/features/settings/threepids/ThreePidsSettingsViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/threepids/ThreePidsSettingsViewModel.kt index ece17547e6..f0bb0e9e41 100644 --- a/vector/src/main/java/im/vector/app/features/settings/threepids/ThreePidsSettingsViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/threepids/ThreePidsSettingsViewModel.kt @@ -155,7 +155,21 @@ class ThreePidsSettingsViewModel @AssistedInject constructor( _viewEvents.post(ThreePidsSettingsViewEvents.Failure(IllegalArgumentException(stringProvider.getString(R.string.auth_email_already_defined)))) } else { viewModelScope.launch { - session.addThreePid(action.threePid, loadingCallback) + session.addThreePid(action.threePid, object : MatrixCallback { + override fun onSuccess(data: Unit) { + // Also reset the state + setState { + copy( + state = ThreePidsSettingsState.Idle + ) + } + loadingCallback.onSuccess(data) + } + + override fun onFailure(failure: Throwable) { + loadingCallback.onFailure(failure) + } + }) } } } diff --git a/vector/src/main/res/values/strings.xml b/vector/src/main/res/values/strings.xml index 43a2f73947..975525c750 100644 --- a/vector/src/main/res/values/strings.xml +++ b/vector/src/main/res/values/strings.xml @@ -1951,6 +1951,7 @@ Send again Next + "Please use the international format (phone number must start with '+')" "International phone numbers must start with '+'" "Phone number seems invalid. Please check it"