Skip to content

Commit b8eed57

Browse files
committed
Better write some code according to code review
1 parent ac64e50 commit b8eed57

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

+8-2
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

+3-4
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

-1
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

+12-8
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

+6-3
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
@@ -769,7 +769,7 @@ class NewMessageViewModel @Inject constructor(
769769

770770
fun saveBodyAndSubject(subject: String, html: String) {
771771
globalCoroutineScope.launch(ioDispatcher) {
772-
_subjectAndBodyChannel.send(Triple(subject, html, channelExpirationIdTarget))
772+
_subjectAndBodyChannel.send(SubjectAndBodyData(subject, html, channelExpirationIdTarget))
773773
}
774774
}
775775

@@ -783,6 +783,7 @@ class NewMessageViewModel @Inject constructor(
783783
val subject: String
784784
val body: String
785785
while (true) {
786+
// receive() is a blocking call as long as it has no data
786787
val (receivedSubject, receivedBody, expirationId) = subjectAndBodyChannel.receive()
787788
if (expirationId == channelExpirationIdTarget) {
788789
subject = receivedSubject
@@ -1009,6 +1010,8 @@ class NewMessageViewModel @Inject constructor(
10091010
val attachmentsLocalUuids: Set<String>,
10101011
)
10111012

1013+
private data class SubjectAndBodyData(val subject: String, val body: String, val expirationId: Int)
1014+
10121015
companion object {
10131016
private val TAG = NewMessageViewModel::class.java.simpleName
10141017
private const val ATTACHMENTS_MAX_SIZE = 25L * 1_024L * 1_024L // 25 MB

0 commit comments

Comments
 (0)