@@ -20,8 +20,8 @@ package com.infomaniak.mail.ui.newMessage
20
20
import android.content.Context
21
21
import android.content.DialogInterface
22
22
import android.util.Patterns
23
- import android.view.View
24
23
import androidx.appcompat.app.AlertDialog
24
+ import androidx.core.widget.addTextChangedListener
25
25
import androidx.core.widget.doOnTextChanged
26
26
import com.google.android.material.dialog.MaterialAlertDialogBuilder
27
27
import com.infomaniak.lib.core.utils.context
@@ -42,8 +42,6 @@ class InsertLinkDialog @Inject constructor(
42
42
val binding: DialogInsertLinkBinding by lazy { DialogInsertLinkBinding .inflate(activity.layoutInflater) }
43
43
private var addLink: ((String , String ) -> Unit )? = null
44
44
45
- private val defaultDisplayNameLabel by lazy { activityContext.getString(R .string.addLinkTextPlaceholder) }
46
-
47
45
override val alertDialog: AlertDialog = with (binding) {
48
46
showDisplayNamePreview()
49
47
@@ -63,14 +61,15 @@ class InsertLinkDialog @Inject constructor(
63
61
64
62
private fun resetDialogState () {
65
63
binding.urlLayout.setError(null )
66
- hidePlaceholder()
64
+
65
+ binding.displayNameLayout.placeholderText = null
67
66
}
68
67
69
68
private fun setConfirmButtonListener (dialog : DialogInterface ) = with (binding) {
70
69
(dialog as AlertDialog ).getButton(AlertDialog .BUTTON_POSITIVE ).setOnClickListener {
71
70
val url = addMissingHttpsProtocol(urlEditText.trimmedText)
72
71
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()
74
73
addLink?.invoke(displayName, url)
75
74
dialog.dismiss()
76
75
} else {
@@ -97,52 +96,26 @@ class InsertLinkDialog @Inject constructor(
97
96
alertDialog.show()
98
97
}
99
98
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.
102
100
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)
112
110
}
113
- }
111
+ )
114
112
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)
128
116
}
129
117
}
130
118
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
-
146
119
private fun addMissingHttpsProtocol (link : String ): String {
147
120
val protocolEndIndex = link.indexOf(" ://" )
148
121
val isProtocolSpecified = protocolEndIndex > 0 // If there is indeed a specified protocol of at least 1 char long
0 commit comments