Optimisation and cleanup

This commit is contained in:
Benoit Marty 2020-08-31 22:20:59 +02:00
parent ddb858380e
commit 5594489b69
5 changed files with 47 additions and 19 deletions

View file

@ -17,6 +17,7 @@
package im.vector.app.core.extensions
import com.google.i18n.phonenumbers.PhoneNumberUtil
import org.matrix.android.sdk.api.extensions.ensurePrefix
import org.matrix.android.sdk.api.extensions.tryThis
import org.matrix.android.sdk.api.session.identity.ThreePid
@ -25,7 +26,7 @@ fun ThreePid.getFormattedValue(): String {
is ThreePid.Email -> email
is ThreePid.Msisdn -> {
tryThis(message = "Unable to parse the phone number") {
PhoneNumberUtil.getInstance().parse("+$msisdn", null)
PhoneNumberUtil.getInstance().parse(msisdn.ensurePrefix("+"), null)
}
?.let {
PhoneNumberUtil.getInstance().format(it, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL)

View file

@ -267,8 +267,8 @@ class DiscoverySettingsController @Inject constructor(
}
}
override fun onCodeChange(code: String) {
codes[pidInfo.threePid] = code
override fun onTextChange(text: String) {
codes[pidInfo.threePid] = text
}
})
}

View file

@ -31,15 +31,17 @@ import im.vector.app.core.extensions.setTextOrHide
@EpoxyModelClass(layout = R.layout.item_settings_edit_text)
abstract class SettingsEditTextItem : EpoxyModelWithHolder<SettingsEditTextItem.Holder>() {
@EpoxyAttribute var hint: String? = null
@EpoxyAttribute var descriptionText: String? = null
@EpoxyAttribute var errorText: String? = null
@EpoxyAttribute var inProgress: Boolean = false
@EpoxyAttribute var inputType: Int? = null
@EpoxyAttribute
var interactionListener: Listener? = null
private val textChangeListener: (text: CharSequence?, start: Int, count: Int, after: Int) -> Unit = { code, _, _, _ ->
code?.let { interactionListener?.onCodeChange(it.toString()) }
private val textChangeListener: (text: CharSequence?, start: Int, count: Int, after: Int) -> Unit = { text, _, _, _ ->
text?.let { interactionListener?.onTextChange(it.toString()) }
}
private val editorActionListener = object : TextView.OnEditorActionListener {
@ -63,6 +65,8 @@ abstract class SettingsEditTextItem : EpoxyModelWithHolder<SettingsEditTextItem.
} else {
holder.textInputLayout.error = errorText
}
holder.textInputLayout.hint = hint
inputType?.let { holder.editText.inputType = it }
holder.editText.doOnTextChanged(textChangeListener)
holder.editText.setOnEditorActionListener(editorActionListener)
@ -76,6 +80,6 @@ abstract class SettingsEditTextItem : EpoxyModelWithHolder<SettingsEditTextItem.
interface Listener {
fun onValidate()
fun onCodeChange(code: String)
fun onTextChange(text: String)
}
}

View file

@ -31,11 +31,12 @@ import im.vector.app.core.resources.ColorProvider
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.SettingsEditTextItem
import im.vector.app.features.discovery.settingsContinueCancelItem
import im.vector.app.features.discovery.settingsEditTextItem
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
import org.matrix.android.sdk.api.session.identity.ThreePid
import javax.inject.Inject
@ -127,13 +128,20 @@ class ThreePidsSettingsController @Inject constructor(
buttonClickAction(View.OnClickListener { interactionListener?.addEmail() })
}
is ThreePidsSettingsState.AddingEmail -> {
formEditTextItem {
settingsEditTextItem {
id("addingEmail")
inputType(InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS)
hint(stringProvider.getString(R.string.medium_email))
errorMessage(data.state.error)
onTextChange { currentInputValue = it }
showBottomSeparator(false)
errorText(data.state.error)
interactionListener(object : SettingsEditTextItem.Listener {
override fun onValidate() {
interactionListener?.doAddEmail(currentInputValue)
}
override fun onTextChange(text: String) {
currentInputValue = text
}
})
}
settingsContinueCancelItem {
id("contAddingEmail")
@ -180,13 +188,20 @@ class ThreePidsSettingsController @Inject constructor(
id("addingMsisdnInfo")
helperText(stringProvider.getString(R.string.login_msisdn_notice))
}
formEditTextItem {
settingsEditTextItem {
id("addingMsisdn")
inputType(InputType.TYPE_CLASS_PHONE)
hint(stringProvider.getString(R.string.medium_phone_number))
errorMessage(data.state.error)
onTextChange { currentInputValue = it }
showBottomSeparator(false)
errorText(data.state.error)
interactionListener(object : SettingsEditTextItem.Listener {
override fun onValidate() {
interactionListener?.doAddMsisdn(currentInputValue)
}
override fun onTextChange(text: String) {
currentInputValue = text
}
})
}
settingsContinueCancelItem {
id("contAddingMsisdn")
@ -234,12 +249,19 @@ class ThreePidsSettingsController @Inject constructor(
message(stringProvider.getString(R.string.settings_text_message_sent, threePid.getFormattedValue()))
colorProvider(colorProvider)
}
formEditTextItem {
settingsEditTextItem {
id("msisdnVerification${threePid.value}")
inputType(InputType.TYPE_CLASS_NUMBER)
hint(stringProvider.getString(R.string.settings_text_message_sent_hint))
showBottomSeparator(false)
onTextChange { currentCodes[threePid] = it }
interactionListener(object : SettingsEditTextItem.Listener {
override fun onValidate() {
interactionListener?.submitCode(threePid, currentCodes[threePid] ?: "")
}
override fun onTextChange(text: String) {
currentCodes[threePid] = text
}
})
}
settingsContinueCancelItem {
id("cont" + idPrefix + threePid.value)

View file

@ -28,6 +28,7 @@ import im.vector.app.core.dialogs.withColoredButton
import im.vector.app.core.extensions.cleanup
import im.vector.app.core.extensions.configureWith
import im.vector.app.core.extensions.exhaustive
import im.vector.app.core.extensions.getFormattedValue
import im.vector.app.core.extensions.hideKeyboard
import im.vector.app.core.extensions.isEmail
import im.vector.app.core.extensions.isMsisdn
@ -158,7 +159,7 @@ class ThreePidsSettingsFragment @Inject constructor(
override fun deleteThreePid(threePid: ThreePid) {
AlertDialog.Builder(requireActivity())
.setMessage(getString(R.string.settings_remove_three_pid_confirmation_content, threePid.value))
.setMessage(getString(R.string.settings_remove_three_pid_confirmation_content, threePid.getFormattedValue()))
.setPositiveButton(R.string.remove) { _, _ ->
viewModel.handle(ThreePidsSettingsAction.DeleteThreePid(threePid))
}