Skip to content

Commit 8c6ecfe

Browse files
committed
Simplify editor focus handling
1 parent db1a067 commit 8c6ecfe

File tree

3 files changed

+20
-35
lines changed

3 files changed

+20
-35
lines changed

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

-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ class NewMessageFragment : Fragment() {
202202
setOnFocusChangedListeners()
203203
observeContacts()
204204
observeCcAndBccVisibility()
205-
observeFocusedElement()
206205
}
207206
}
208207

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

+19-31
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class NewMessageRecipientFieldsManager @Inject constructor(private val snackbarM
3333
private var _externalsManager: NewMessageExternalsManager? = null
3434
private inline val externalsManager: NewMessageExternalsManager get() = _externalsManager!!
3535

36-
private var lastFieldToTakeFocus: FocusableElement? = FocusableElement.TO
36+
private var lastFieldToTakeFocus: FieldType? = TO
3737

3838
fun initValues(
3939
newMessageViewModel: NewMessageViewModel,
@@ -58,7 +58,7 @@ class NewMessageRecipientFieldsManager @Inject constructor(private val snackbarM
5858
onContactAddedCallback = { newMessageViewModel.addRecipientToField(recipient = it, type = TO) },
5959
onContactRemovedCallback = { recipient -> recipient.removeInViewModelAndUpdateBannerVisibility(TO) },
6060
onCopyContactAddressCallback = { fragment.copyRecipientEmailToClipboard(it, snackbarManager) },
61-
gotFocusCallback = { elementGotFocus(FocusableElement.TO) },
61+
gotFocusCallback = { fieldGotFocus(TO) },
6262
onToggleEverythingCallback = ::openAdvancedFields,
6363
)
6464

@@ -68,7 +68,7 @@ class NewMessageRecipientFieldsManager @Inject constructor(private val snackbarM
6868
onContactAddedCallback = { newMessageViewModel.addRecipientToField(recipient = it, type = CC) },
6969
onContactRemovedCallback = { recipient -> recipient.removeInViewModelAndUpdateBannerVisibility(CC) },
7070
onCopyContactAddressCallback = { fragment.copyRecipientEmailToClipboard(it, snackbarManager) },
71-
gotFocusCallback = { elementGotFocus(FocusableElement.CC) },
71+
gotFocusCallback = { fieldGotFocus(CC) },
7272
)
7373

7474
bccField.initRecipientField(
@@ -77,7 +77,7 @@ class NewMessageRecipientFieldsManager @Inject constructor(private val snackbarM
7777
onContactAddedCallback = { newMessageViewModel.addRecipientToField(recipient = it, type = BCC) },
7878
onContactRemovedCallback = { recipient -> recipient.removeInViewModelAndUpdateBannerVisibility(BCC) },
7979
onCopyContactAddressCallback = { fragment.copyRecipientEmailToClipboard(it, snackbarManager) },
80-
gotFocusCallback = { elementGotFocus(FocusableElement.BCC) },
80+
gotFocusCallback = { fieldGotFocus(BCC) },
8181
)
8282
}
8383

@@ -96,24 +96,18 @@ class NewMessageRecipientFieldsManager @Inject constructor(private val snackbarM
9696
externalsManager.updateBannerVisibility()
9797
}
9898

99-
private fun elementGotFocus(element: FocusableElement?) {
100-
newMessageViewModel.focusedElementLiveData.value = element
101-
}
102-
103-
fun observeFocusedElement() = with(binding) {
104-
newMessageViewModel.focusedElementLiveData.observe(viewLifecycleOwner) { field ->
105-
if (lastFieldToTakeFocus == field) return@observe
106-
107-
if (field == null && newMessageViewModel.otherRecipientsFieldsAreEmpty.value == true) {
108-
toField.collapseEverything()
109-
} else {
110-
if (field != FocusableElement.TO) toField.collapse()
111-
if (field != FocusableElement.CC) ccField.collapse()
112-
if (field != FocusableElement.BCC) bccField.collapse()
113-
}
99+
private fun fieldGotFocus(field: FieldType?) = with(binding) {
100+
if (lastFieldToTakeFocus == field) return
114101

115-
lastFieldToTakeFocus = field
102+
if (field == null && newMessageViewModel.otherRecipientsFieldsAreEmpty.value == true) {
103+
toField.collapseEverything()
104+
} else {
105+
if (field != TO) toField.collapse()
106+
if (field != CC) ccField.collapse()
107+
if (field != BCC) bccField.collapse()
116108
}
109+
110+
lastFieldToTakeFocus = field
117111
}
118112

119113
private fun openAdvancedFields(isCollapsed: Boolean) = with(binding) {
@@ -126,9 +120,11 @@ class NewMessageRecipientFieldsManager @Inject constructor(private val snackbarM
126120
initializeFieldsAsOpen.observe(viewLifecycleOwner) { openAdvancedFields(isCollapsed = !it) }
127121
}
128122

129-
fun setOnFocusChangedListeners() = with(binding) {
130-
subjectTextField.setOnFocusChangeListener { _, hasFocus -> if (hasFocus) elementGotFocus(FocusableElement.OTHER) }
131-
editorWebView.setOnFocusChangeListener { _, hasFocus -> if (hasFocus) elementGotFocus(FocusableElement.BODY) }
123+
fun setOnFocusChangedListeners() = with(newMessageViewModel) {
124+
binding.subjectTextField.setOnFocusChangeListener { _, hasFocus -> if (hasFocus) fieldGotFocus(null) }
125+
binding.editorWebView.setOnFocusChangeListener { _, hasFocus -> isEditorWebViewFocusedLiveData.value = hasFocus }
126+
127+
isEditorWebViewFocusedLiveData.observe(viewLifecycleOwner) { hasFocus -> if (hasFocus) fieldGotFocus(null) }
132128
}
133129

134130
fun focusBodyField() {
@@ -159,12 +155,4 @@ class NewMessageRecipientFieldsManager @Inject constructor(private val snackbarM
159155
CC,
160156
BCC,
161157
}
162-
163-
enum class FocusableElement {
164-
TO,
165-
CC,
166-
BCC,
167-
BODY,
168-
OTHER,
169-
}
170158
}

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import com.infomaniak.mail.ui.main.SnackbarManager
5858
import com.infomaniak.mail.ui.newMessage.NewMessageActivity.DraftSaveConfiguration
5959
import com.infomaniak.mail.ui.newMessage.NewMessageEditorManager.EditorAction
6060
import com.infomaniak.mail.ui.newMessage.NewMessageRecipientFieldsManager.FieldType
61-
import com.infomaniak.mail.ui.newMessage.NewMessageRecipientFieldsManager.FocusableElement
6261
import com.infomaniak.mail.ui.newMessage.NewMessageViewModel.SignatureScore.*
6362
import com.infomaniak.mail.utils.*
6463
import com.infomaniak.mail.utils.ContactUtils.arrangeMergedContacts
@@ -146,8 +145,7 @@ class NewMessageViewModel @Inject constructor(
146145
private var snapshot: DraftSnapshot? = null
147146

148147
val otherRecipientsFieldsAreEmpty = MutableLiveData(true)
149-
val focusedElementLiveData = MutableLiveData<FocusableElement>()
150-
val isEditorWebViewFocusedLiveData = focusedElementLiveData.map { it == FocusableElement.BODY }
148+
val isEditorWebViewFocusedLiveData = MutableLiveData<Boolean>()
151149

152150
val initializeFieldsAsOpen = SingleLiveEvent<Boolean>()
153151
val importAttachmentsLiveData = SingleLiveEvent<List<Uri>>()

0 commit comments

Comments
 (0)