Skip to content

Commit cf877ba

Browse files
committed
Avoid unnecessary uses of the body field in Draft objects
1 parent 6c0f52c commit cf877ba

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

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

+14-12
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class NewMessageViewModel @Inject constructor(
230230
if (draft.identityId.isNullOrBlank()) {
231231
draft.identityId = SignatureController.getDefaultSignatureWithFallback(realm, draftMode)?.id?.toString()
232232
}
233-
if (draft.body.isNotEmpty()) splitSignatureAndQuoteFromBody(draft)
233+
splitSignatureAndQuoteFromBody(draft)
234234
}
235235
}
236236

@@ -274,8 +274,6 @@ class NewMessageViewModel @Inject constructor(
274274
)
275275

276276
populateWithExternalMailDataIfNeeded(draft = this, intent)
277-
278-
body = getWholeBody("", uiSignature, uiQuote)
279277
}
280278

281279
private fun saveNavArgsToSavedState(localUuid: String) {
@@ -287,9 +285,12 @@ class NewMessageViewModel @Inject constructor(
287285
}
288286

289287
private fun splitSignatureAndQuoteFromBody(draft: Draft) {
288+
val remoteBody = draft.body
289+
if (remoteBody.isEmpty()) return
290+
290291
val (body, signature, quote) = when (draft.mimeType) {
291-
Utils.TEXT_PLAIN -> Triple(BodyContentPayload(draft.body, BodyContentType.TEXT_PLAIN_WITHOUT_HTML), null, null)
292-
Utils.TEXT_HTML -> splitSignatureAndQuoteFromHtml(draft)
292+
Utils.TEXT_PLAIN -> Triple(BodyContentPayload(remoteBody, BodyContentType.TEXT_PLAIN_WITHOUT_HTML), null, null)
293+
Utils.TEXT_HTML -> splitSignatureAndQuoteFromHtml(remoteBody)
293294
else -> error("Cannot load an email which is not of type text/plain or text/html")
294295
}
295296

@@ -300,7 +301,7 @@ class NewMessageViewModel @Inject constructor(
300301
}
301302
}
302303

303-
private fun splitSignatureAndQuoteFromHtml(draft: Draft): Triple<BodyContentPayload, String?, String?> {
304+
private fun splitSignatureAndQuoteFromHtml(draftBody: String): Triple<BodyContentPayload, String?, String?> {
304305

305306
fun Document.split(divClassName: String, defaultValue: String): Pair<String, String?> {
306307
return getElementsByClass(divClassName).firstOrNull()?.let {
@@ -316,12 +317,12 @@ class NewMessageViewModel @Inject constructor(
316317
return if (index == -1) Int.MAX_VALUE else index
317318
}
318319

319-
val doc = Jsoup.parse(draft.body).also { it.outputSettings().prettyPrint(false) }
320+
val doc = Jsoup.parse(draftBody).also { it.outputSettings().prettyPrint(false) }
320321

321-
val (bodyWithQuote, signature) = doc.split(MessageBodyUtils.INFOMANIAK_SIGNATURE_HTML_CLASS_NAME, draft.body)
322+
val (bodyWithQuote, signature) = doc.split(MessageBodyUtils.INFOMANIAK_SIGNATURE_HTML_CLASS_NAME, draftBody)
322323

323-
val replyPosition = draft.body.lastIndexOfOrMax(MessageBodyUtils.INFOMANIAK_REPLY_QUOTE_HTML_CLASS_NAME)
324-
val forwardPosition = draft.body.lastIndexOfOrMax(MessageBodyUtils.INFOMANIAK_FORWARD_QUOTE_HTML_CLASS_NAME)
324+
val replyPosition = draftBody.lastIndexOfOrMax(MessageBodyUtils.INFOMANIAK_REPLY_QUOTE_HTML_CLASS_NAME)
325+
val forwardPosition = draftBody.lastIndexOfOrMax(MessageBodyUtils.INFOMANIAK_FORWARD_QUOTE_HTML_CLASS_NAME)
325326
val (body, quote) = if (replyPosition < forwardPosition) {
326327
doc.split(MessageBodyUtils.INFOMANIAK_REPLY_QUOTE_HTML_CLASS_NAME, bodyWithQuote)
327328
} else {
@@ -842,7 +843,7 @@ class NewMessageViewModel @Inject constructor(
842843

843844
val signature = uiSignatureLiveData.value
844845
val quote = uiQuoteLiveData.value
845-
body = getWholeBody(uiBodyValue, signature, quote)
846+
body = assembleWholeBody(uiBodyValue, signature, quote)
846847

847848
/**
848849
* If we are opening for the 1st time an existing Draft created somewhere else
@@ -904,7 +905,8 @@ class NewMessageViewModel @Inject constructor(
904905
}
905906
}
906907

907-
private fun getWholeBody(body: String, signature: String?, quote: String?): String = body + (signature ?: "") + (quote ?: "")
908+
private fun assembleWholeBody(body: String, signature: String?, quote: String?): String =
909+
body + (signature ?: "") + (quote ?: "")
908910

909911
private fun isSnapshotTheSame(subjectValue: String?, uiBodyValue: String): Boolean {
910912
return snapshot?.let { draftSnapshot ->

0 commit comments

Comments
 (0)