17
17
*/
18
18
package com.infomaniak.mail.ui.newMessage
19
19
20
- import android.view.View
21
20
import androidx.core.view.isGone
22
21
import androidx.core.view.isVisible
23
22
import com.infomaniak.mail.data.models.correspondent.Recipient
@@ -34,7 +33,7 @@ class NewMessageRecipientFieldsManager @Inject constructor(private val snackbarM
34
33
private var _externalsManager : NewMessageExternalsManager ? = null
35
34
private inline val externalsManager: NewMessageExternalsManager get() = _externalsManager !!
36
35
37
- private var lastFieldToTakeFocus: FieldType ? = TO
36
+ private var lastFieldToTakeFocus: FocusableElement ? = FocusableElement . TO
38
37
39
38
fun initValues (
40
39
newMessageViewModel : NewMessageViewModel ,
@@ -59,7 +58,7 @@ class NewMessageRecipientFieldsManager @Inject constructor(private val snackbarM
59
58
onContactAddedCallback = { newMessageViewModel.addRecipientToField(recipient = it, type = TO ) },
60
59
onContactRemovedCallback = { recipient -> recipient.removeInViewModelAndUpdateBannerVisibility(TO ) },
61
60
onCopyContactAddressCallback = { fragment.copyRecipientEmailToClipboard(it, snackbarManager) },
62
- gotFocusCallback = { fieldGotFocus( TO ) },
61
+ gotFocusCallback = { elementGotFocus( FocusableElement . TO ) },
63
62
onToggleEverythingCallback = ::openAdvancedFields,
64
63
)
65
64
@@ -69,7 +68,7 @@ class NewMessageRecipientFieldsManager @Inject constructor(private val snackbarM
69
68
onContactAddedCallback = { newMessageViewModel.addRecipientToField(recipient = it, type = CC ) },
70
69
onContactRemovedCallback = { recipient -> recipient.removeInViewModelAndUpdateBannerVisibility(CC ) },
71
70
onCopyContactAddressCallback = { fragment.copyRecipientEmailToClipboard(it, snackbarManager) },
72
- gotFocusCallback = { fieldGotFocus( CC ) },
71
+ gotFocusCallback = { elementGotFocus( FocusableElement . CC ) },
73
72
)
74
73
75
74
bccField.initRecipientField(
@@ -78,7 +77,7 @@ class NewMessageRecipientFieldsManager @Inject constructor(private val snackbarM
78
77
onContactAddedCallback = { newMessageViewModel.addRecipientToField(recipient = it, type = BCC ) },
79
78
onContactRemovedCallback = { recipient -> recipient.removeInViewModelAndUpdateBannerVisibility(BCC ) },
80
79
onCopyContactAddressCallback = { fragment.copyRecipientEmailToClipboard(it, snackbarManager) },
81
- gotFocusCallback = { fieldGotFocus( BCC ) },
80
+ gotFocusCallback = { elementGotFocus( FocusableElement . BCC ) },
82
81
)
83
82
}
84
83
@@ -97,18 +96,24 @@ class NewMessageRecipientFieldsManager @Inject constructor(private val snackbarM
97
96
externalsManager.updateBannerVisibility()
98
97
}
99
98
100
- private fun fieldGotFocus (field : FieldType ? ) = with (binding) {
101
- if (lastFieldToTakeFocus == field) return
99
+ private fun elementGotFocus (element : FocusableElement ? ) {
100
+ newMessageViewModel.focusedElementLiveData.value = element
101
+ }
102
102
103
- if (field == null && newMessageViewModel.otherRecipientsFieldsAreEmpty.value == true ) {
104
- toField.collapseEverything()
105
- } else {
106
- if (field != TO ) toField.collapse()
107
- if (field != CC ) ccField.collapse()
108
- if (field != BCC ) bccField.collapse()
109
- }
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
+ }
110
114
111
- lastFieldToTakeFocus = field
115
+ lastFieldToTakeFocus = field
116
+ }
112
117
}
113
118
114
119
private fun openAdvancedFields (isCollapsed : Boolean ) = with (binding) {
@@ -122,9 +127,8 @@ class NewMessageRecipientFieldsManager @Inject constructor(private val snackbarM
122
127
}
123
128
124
129
fun setOnFocusChangedListeners () = with (binding) {
125
- val listener = View .OnFocusChangeListener { _, hasFocus -> if (hasFocus) fieldGotFocus(null ) }
126
- subjectTextField.onFocusChangeListener = listener
127
- editorWebView.onFocusChangeListener = listener
130
+ subjectTextField.setOnFocusChangeListener { _, hasFocus -> if (hasFocus) elementGotFocus(FocusableElement .OTHER ) }
131
+ editorWebView.setOnFocusChangeListener { _, hasFocus -> if (hasFocus) elementGotFocus(FocusableElement .BODY ) }
128
132
}
129
133
130
134
fun focusBodyField () {
@@ -155,4 +159,12 @@ class NewMessageRecipientFieldsManager @Inject constructor(private val snackbarM
155
159
CC ,
156
160
BCC ,
157
161
}
162
+
163
+ enum class FocusableElement {
164
+ TO ,
165
+ CC ,
166
+ BCC ,
167
+ BODY ,
168
+ OTHER ,
169
+ }
158
170
}
0 commit comments