Skip to content

Commit ffcdd18

Browse files
committed
Better write some code according to code review
1 parent 0e10d3b commit ffcdd18

File tree

5 files changed

+29
-18
lines changed

5 files changed

+29
-18
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ package com.infomaniak.mail.ui.newMessage
2222
* @param type The type of representation of [content]. Each type will lead to different processing of the content.
2323
*/
2424
data class BodyContentPayload(val content: String, val type: BodyContentType) {
25+
2526
companion object {
26-
fun emptyBody(): BodyContentPayload = BodyContentPayload("", BodyContentType.TEXT_PLAIN_WITHOUT_HTML)
27+
fun emptyBody() = BodyContentPayload("", BodyContentType.TEXT_PLAIN_WITHOUT_HTML)
2728
}
2829
}
2930

30-
enum class BodyContentType { HTML_SANITIZED, HTML_UNSANITIZED, TEXT_PLAIN_WITH_HTML, TEXT_PLAIN_WITHOUT_HTML }
31+
enum class BodyContentType {
32+
HTML_SANITIZED,
33+
HTML_UNSANITIZED,
34+
TEXT_PLAIN_WITH_HTML,
35+
TEXT_PLAIN_WITHOUT_HTML,
36+
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,15 @@ class EditorContentManager @Inject constructor() {
5656
private fun String.sanitize(): String = HtmlSanitizer.getInstance()
5757
.sanitize(Jsoup.parse(this))
5858
.apply { outputSettings().prettyPrint(false) }
59-
.extractHtmlWithoutDocumentWrapping()
59+
.getHtmlWithoutDocumentWrapping()
6060

61-
private fun Document.extractHtmlWithoutDocumentWrapping(): String {
61+
private fun Document.getHtmlWithoutDocumentWrapping(): String {
6262
val html = root().firstElementChild() ?: return html()
6363
val nodeSize = html.childNodeSize()
6464
val elements = html.children()
65-
val elementSize = elements.count()
6665

6766
val canRemoveDocumentWrapping = nodeSize == 2
68-
&& elementSize == 2
67+
&& elements.count() == 2
6968
&& elements[0].tagName().uppercase() == "HEAD"
7069
&& elements[0].childNodeSize() == 0
7170
&& elements[1].tagName().uppercase() == "BODY"

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ class NewMessageAiManager @Inject constructor(
8585
fragment = fragment,
8686
freeReferences = {
8787
_aiViewModel = null
88-
_editorContentManager = null
8988
valueAnimator?.cancel()
9089
valueAnimator = null
9190
},

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,18 @@ class NewMessageFragment : Fragment() {
327327
}
328328
})
329329

330+
initEditorUi()
331+
332+
setupSendButton()
333+
externalsManager.setupExternalBanner()
334+
335+
scrim.setOnClickListener {
336+
scrim.isClickable = false
337+
aiManager.closeAiPrompt()
338+
}
339+
}
340+
341+
private fun initEditorUi() = with(binding) {
330342
editor.apply {
331343
enableAlgorithmicDarkening(true)
332344
if (context.isNightModeEnabled()) editor.addCss(context.readRawResource(R.raw.custom_dark_mode))
@@ -343,14 +355,6 @@ class NewMessageFragment : Fragment() {
343355
.onEach { isVisible -> newMessagePlaceholder.isVisible = isVisible }
344356
.launchIn(lifecycleScope)
345357
}
346-
347-
setupSendButton()
348-
externalsManager.setupExternalBanner()
349-
350-
scrim.setOnClickListener {
351-
scrim.isClickable = false
352-
aiManager.closeAiPrompt()
353-
}
354358
}
355359

356360
private fun initializeDraft() = with(newMessageViewModel) {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ class NewMessageViewModel @Inject constructor(
123123
// 3. When saving or sending the draft now, the channel still holds the previous body as it wasn't consumed.
124124
// channelExpirationIdTarget lets us identify and discard outdated messages, ensuring the correct message is processed.
125125
private var channelExpirationIdTarget = 0
126-
private val _subjectAndBodyChannel: Channel<Triple<String, String, Int>> = Channel(capacity = CONFLATED)
127-
private val subjectAndBodyChannel: ReceiveChannel<Triple<String, String, Int>> = _subjectAndBodyChannel
126+
private val _subjectAndBodyChannel: Channel<SubjectAndBodyData> = Channel(capacity = CONFLATED)
127+
private val subjectAndBodyChannel: ReceiveChannel<SubjectAndBodyData> = _subjectAndBodyChannel
128128
private var subjectAndBodyJob: Job? = null
129129

130130
var isAutoCompletionOpened = false
@@ -776,7 +776,7 @@ class NewMessageViewModel @Inject constructor(
776776

777777
fun saveBodyAndSubject(subject: String, html: String) {
778778
globalCoroutineScope.launch(ioDispatcher) {
779-
_subjectAndBodyChannel.send(Triple(subject, html, channelExpirationIdTarget))
779+
_subjectAndBodyChannel.send(SubjectAndBodyData(subject, html, channelExpirationIdTarget))
780780
}
781781
}
782782

@@ -790,6 +790,7 @@ class NewMessageViewModel @Inject constructor(
790790
val subject: String
791791
val body: String
792792
while (true) {
793+
// receive() is a blocking call as long as it has no data
793794
val (receivedSubject, receivedBody, expirationId) = subjectAndBodyChannel.receive()
794795
if (expirationId == channelExpirationIdTarget) {
795796
subject = receivedSubject
@@ -1021,6 +1022,8 @@ class NewMessageViewModel @Inject constructor(
10211022
val attachmentsLocalUuids: Set<String>,
10221023
)
10231024

1025+
private data class SubjectAndBodyData(val subject: String, val body: String, val expirationId: Int)
1026+
10241027
companion object {
10251028
private val TAG = NewMessageViewModel::class.java.simpleName
10261029
private const val ATTACHMENTS_MAX_SIZE = 25L * 1_024L * 1_024L // 25 MB

0 commit comments

Comments
 (0)