From 58938a239e9fc26f00413f19a81b045546b9f314 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 28 Aug 2020 19:07:41 +0200 Subject: [PATCH] Format phone number --- .../im/vector/app/core/extensions/ThreePid.kt | 36 +++++++++++++++++++ .../discovery/DiscoverySettingsController.kt | 14 ++------ .../threepids/ThreePidsSettingsController.kt | 20 ++++++++--- vector/src/main/res/values/strings.xml | 1 + 4 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/core/extensions/ThreePid.kt diff --git a/vector/src/main/java/im/vector/app/core/extensions/ThreePid.kt b/vector/src/main/java/im/vector/app/core/extensions/ThreePid.kt new file mode 100644 index 0000000000..8afbfd0f63 --- /dev/null +++ b/vector/src/main/java/im/vector/app/core/extensions/ThreePid.kt @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2020 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 im.vector.app.core.extensions + +import com.google.i18n.phonenumbers.PhoneNumberUtil +import org.matrix.android.sdk.api.extensions.tryThis +import org.matrix.android.sdk.api.session.identity.ThreePid + +fun ThreePid.getFormattedValue(): String { + return when (this) { + is ThreePid.Email -> email + is ThreePid.Msisdn -> { + tryThis(message = "Unable to parse the phone number") { + PhoneNumberUtil.getInstance().parse("+$msisdn", null) + } + ?.let { + PhoneNumberUtil.getInstance().format(it, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL) + } + ?: msisdn + } + } +} diff --git a/vector/src/main/java/im/vector/app/features/discovery/DiscoverySettingsController.kt b/vector/src/main/java/im/vector/app/features/discovery/DiscoverySettingsController.kt index b63f82be81..e68537a108 100644 --- a/vector/src/main/java/im/vector/app/features/discovery/DiscoverySettingsController.kt +++ b/vector/src/main/java/im/vector/app/features/discovery/DiscoverySettingsController.kt @@ -23,19 +23,18 @@ import com.airbnb.mvrx.Incomplete import com.airbnb.mvrx.Loading import com.airbnb.mvrx.Success import com.airbnb.mvrx.Uninitialized -import com.google.i18n.phonenumbers.PhoneNumberUtil import im.vector.app.R import im.vector.app.core.epoxy.attributes.ButtonStyle import im.vector.app.core.epoxy.attributes.ButtonType import im.vector.app.core.epoxy.attributes.IconMode import im.vector.app.core.epoxy.loadingItem import im.vector.app.core.error.ErrorFormatter +import im.vector.app.core.extensions.getFormattedValue import im.vector.app.core.resources.ColorProvider import im.vector.app.core.resources.StringProvider import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.session.identity.SharedState import org.matrix.android.sdk.api.session.identity.ThreePid -import timber.log.Timber import javax.inject.Inject import javax.net.ssl.HttpsURLConnection @@ -235,16 +234,7 @@ class DiscoverySettingsController @Inject constructor( } private fun buildMsisdn(pidInfo: PidInfo) { - val phoneNumber = try { - PhoneNumberUtil.getInstance().parse("+${pidInfo.threePid.value}", null) - } catch (t: Throwable) { - Timber.e(t, "Unable to parse the phone number") - null - } - ?.let { - PhoneNumberUtil.getInstance().format(it, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL) - } - ?: pidInfo.threePid.value + val phoneNumber = pidInfo.threePid.getFormattedValue() buildThreePid(pidInfo, phoneNumber) 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 3899ac9166..9e77adafc9 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 @@ -24,6 +24,8 @@ import com.airbnb.mvrx.Loading import com.airbnb.mvrx.Success import im.vector.app.R import im.vector.app.core.epoxy.loadingItem +import im.vector.app.core.epoxy.noResultItem +import im.vector.app.core.extensions.getFormattedValue import im.vector.app.core.resources.ColorProvider import im.vector.app.core.resources.StringProvider import im.vector.app.core.ui.list.genericButtonItem @@ -107,20 +109,30 @@ class ThreePidsSettingsController @Inject constructor( ?.filterIsInstance(ThreePid.Msisdn::class.java) ?.forEach { buildPendingThreePid("p_msisdn ", it) } + /* + // TODO Support adding MSISDN genericButtonItem { id("addMsisdn") text(stringProvider.getString(R.string.settings_add_phone_number)) textColor(colorProvider.getColor(R.color.riotx_accent)) buttonClickAction(View.OnClickListener { interactionListener?.addMsisdn() }) } + */ + // Avoid empty area + if (msisdn.isEmpty()) { + noResultItem { + id("no_msisdn") + text(stringProvider.getString(R.string.settings_phone_numbers_empty)) + } + } } private fun buildThreePid(idPrefix: String, threePid: ThreePid) { threePidItem { id(idPrefix + threePid.value) // TODO Add an icon for emails - iconResId(if (threePid is ThreePid.Msisdn) R.drawable.ic_phone else null) - title(threePid.value) + // iconResId(if (threePid is ThreePid.Msisdn) R.drawable.ic_phone else null) + title(threePid.getFormattedValue()) deleteClickListener { interactionListener?.deleteThreePid(threePid) } } } @@ -129,8 +141,8 @@ class ThreePidsSettingsController @Inject constructor( threePidItem { id(idPrefix + threePid.value) // TODO Add an icon for emails - iconResId(if (threePid is ThreePid.Msisdn) R.drawable.ic_phone else null) - title(threePid.value) + // iconResId(if (threePid is ThreePid.Msisdn) R.drawable.ic_phone else null) + title(threePid.getFormattedValue()) } if (threePid is ThreePid.Email) { diff --git a/vector/src/main/res/values/strings.xml b/vector/src/main/res/values/strings.xml index 43a2f73947..553dc1674e 100644 --- a/vector/src/main/res/values/strings.xml +++ b/vector/src/main/res/values/strings.xml @@ -684,6 +684,7 @@ Email addresses Phone numbers + No phone number has been added to your account Remove %s? Ensure that you have clicked on the link in the email we have sent to you.