Skip to content

Commit d237ad5

Browse files
committed
Simplify the synchronizing of display text and url to avoid unnecessary bugs and improve the UX
1 parent c8f0fcc commit d237ad5

File tree

2 files changed

+23
-50
lines changed

2 files changed

+23
-50
lines changed

app/src/main/java/com/infomaniak/mail/ui/newMessage/InsertLinkDialog.kt

+18-45
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ package com.infomaniak.mail.ui.newMessage
2020
import android.content.Context
2121
import android.content.DialogInterface
2222
import android.util.Patterns
23-
import android.view.View
2423
import androidx.appcompat.app.AlertDialog
24+
import androidx.core.widget.addTextChangedListener
2525
import androidx.core.widget.doOnTextChanged
2626
import com.google.android.material.dialog.MaterialAlertDialogBuilder
2727
import com.infomaniak.lib.core.utils.context
@@ -42,8 +42,6 @@ class InsertLinkDialog @Inject constructor(
4242
val binding: DialogInsertLinkBinding by lazy { DialogInsertLinkBinding.inflate(activity.layoutInflater) }
4343
private var addLink: ((String, String) -> Unit)? = null
4444

45-
private val defaultDisplayNameLabel by lazy { activityContext.getString(R.string.addLinkTextPlaceholder) }
46-
4745
override val alertDialog: AlertDialog = with(binding) {
4846
showDisplayNamePreview()
4947

@@ -63,14 +61,15 @@ class InsertLinkDialog @Inject constructor(
6361

6462
private fun resetDialogState() {
6563
binding.urlLayout.setError(null)
66-
hidePlaceholder()
64+
65+
binding.displayNameLayout.placeholderText = null
6766
}
6867

6968
private fun setConfirmButtonListener(dialog: DialogInterface) = with(binding) {
7069
(dialog as AlertDialog).getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
7170
val url = addMissingHttpsProtocol(urlEditText.trimmedText)
7271
if (validate(url)) {
73-
val displayName = (displayNameEditText.text?.takeIf { it.isNotBlank() } ?: urlEditText.text).toString()
72+
val displayName = (displayNameEditText.text?.takeIf { it.isNotBlank() } ?: urlEditText.text).toString().trim()
7473
addLink?.invoke(displayName, url)
7574
dialog.dismiss()
7675
} else {
@@ -97,52 +96,26 @@ class InsertLinkDialog @Inject constructor(
9796
alertDialog.show()
9897
}
9998

100-
// We need to play with the display text's hint and placeholder to only show the correct one in each situation. Placeholder is
101-
// only visible when the EditText is focused and hint takes the place of the placeholder when the EditText is not focused.
99+
// Pre fills the display name with the url's content if the fields contain the same value.
102100
private fun showDisplayNamePreview() = with(binding) {
103-
urlEditText.doOnTextChanged { _, _, _, _ ->
104-
if (displayNameIsEmpty()) {
105-
displayNameLayout.apply {
106-
val preview = computeDisplayNamePreview()
107-
108-
placeholderText = preview
109-
hint = preview
110-
invalidate() // Required to update the hint, else it never updates visually
111-
}
101+
displayNameEditText.setTextColor(activityContext.getColor(R.color.tertiaryTextColor))
102+
103+
var areInputsSynced = false
104+
urlEditText.addTextChangedListener(
105+
beforeTextChanged = { text, _, _, _ ->
106+
areInputsSynced = text.toString() == displayNameEditText.text.toString()
107+
},
108+
onTextChanged = { text, _, _, _ ->
109+
if (areInputsSynced || displayNameEditText.text.isNullOrBlank()) displayNameEditText.setText(text)
112110
}
113-
}
111+
)
114112

115-
displayNameEditText.doOnTextChanged { _, _, _, _ ->
116-
if (displayNameIsEmpty()) {
117-
if (thereIsNoPreviewToDisplay()) {
118-
hidePlaceholder()
119-
} else {
120-
displayNameLayout.placeholderText = computeDisplayNamePreview()
121-
}
122-
}
123-
}
124-
125-
displayNameEditText.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus ->
126-
displayNameLayout.hint = if (hasFocus) defaultDisplayNameLabel else computeDisplayNamePreview()
127-
if (thereIsNoPreviewToDisplay()) hidePlaceholder()
113+
displayNameEditText.setOnFocusChangeListener { _, hasFocus ->
114+
val textColor = activityContext.getColor(if (hasFocus) R.color.primaryTextColor else R.color.tertiaryTextColor)
115+
displayNameEditText.setTextColor(textColor)
128116
}
129117
}
130118

131-
private fun computeDisplayNamePreview(): String {
132-
if (!displayNameIsEmpty()) return defaultDisplayNameLabel
133-
if (thereIsNoPreviewToDisplay()) return defaultDisplayNameLabel
134-
135-
return binding.urlEditText.text.toString()
136-
}
137-
138-
private fun hidePlaceholder() {
139-
binding.displayNameLayout.placeholderText = null
140-
}
141-
142-
private fun displayNameIsEmpty() = binding.displayNameEditText.text.isNullOrEmpty()
143-
144-
private fun thereIsNoPreviewToDisplay() = binding.urlEditText.text.isNullOrBlank()
145-
146119
private fun addMissingHttpsProtocol(link: String): String {
147120
val protocolEndIndex = link.indexOf("://")
148121
val isProtocolSpecified = protocolEndIndex > 0 // If there is indeed a specified protocol of at least 1 char long

app/src/main/res/layout/dialog_insert_link.xml

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@
3030
android:layout_marginTop="@dimen/marginStandard"
3131
android:text="@string/urlEntryTitle" />
3232

33-
<com.google.android.material.textfield.TextInputLayout
33+
<com.infomaniak.lib.core.views.EndIconTextInputLayout
3434
android:id="@+id/displayNameLayout"
3535
android:layout_width="match_parent"
3636
android:layout_height="wrap_content"
37-
android:layout_marginVertical="@dimen/marginStandardMedium"
38-
app:placeholderTextColor="@color/tertiaryTextColor">
37+
android:layout_marginVertical="@dimen/marginStandardMedium">
3938

4039
<com.google.android.material.textfield.TextInputEditText
4140
android:id="@+id/displayNameEditText"
4241
android:layout_width="match_parent"
4342
android:layout_height="wrap_content"
4443
android:hint="@string/addLinkTextPlaceholder"
45-
android:inputType="text" />
44+
android:inputType="text"
45+
android:selectAllOnFocus="true" />
4646

47-
</com.google.android.material.textfield.TextInputLayout>
47+
</com.infomaniak.lib.core.views.EndIconTextInputLayout>
4848

4949
<com.infomaniak.lib.core.views.EndIconTextInputLayout
5050
android:id="@+id/urlLayout"

0 commit comments

Comments
 (0)