Skip to content

Commit 8c69fa1

Browse files
Fix message formatting (#1788)
1 parent 2d5d53b commit 8c69fa1

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/MessageFormatter.kt

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.conversation.v2.messages
33
import android.content.Context
44
import android.text.Spannable
55
import android.text.SpannableString
6+
import android.text.SpannableStringBuilder
67
import android.text.style.ForegroundColorSpan
78
import com.squareup.phrase.Phrase
89
import network.loki.messenger.R
@@ -177,29 +178,34 @@ class MessageFormatter @Inject constructor(
177178
context.getString(R.string.communityInvitation)
178179
}
179180

180-
// Show a placeholder text for messages with attachments
181-
lastMessage is MmsMessageRecord -> {
182-
val placeholderBody = lastMessage.slideDeck.body
183-
val messageBody = lastMessage.body
184-
185-
if (placeholderBody.isNotBlank()) {
186-
if (messageBody.isNotBlank()) {
187-
"$placeholderBody: $messageBody"
188-
} else {
189-
placeholderBody
190-
}
191-
} else {
192-
messageBody
193-
}
194-
}
195-
196181
else -> {
197-
val text = formatMessageBody(
182+
val bodyText = formatMessageBody(
198183
context = context,
199184
message = lastMessage,
200185
threadRecipient = thread.recipient
201186
)
202187

188+
// This is used to show a placeholder text for MMS messages in the snippet,
189+
// for example, "<image> Attachment"
190+
val mmsPlaceholderBody = (lastMessage as? MmsMessageRecord)?.slideDeck?.body
191+
192+
val text = when {
193+
// If both body and placeholder are blank, return empty string
194+
bodyText.isBlank() && mmsPlaceholderBody.isNullOrBlank() -> ""
195+
196+
// If both body and placeholder are non-blank, combine them
197+
bodyText.isNotBlank() && !mmsPlaceholderBody.isNullOrBlank() ->
198+
SpannableStringBuilder(mmsPlaceholderBody)
199+
.append(": ")
200+
.append(bodyText)
201+
202+
// If only placeholder is non-blank, use it
203+
!mmsPlaceholderBody.isNullOrBlank() -> mmsPlaceholderBody
204+
205+
// Otherwise, use the body text
206+
else -> bodyText
207+
}
208+
203209
when {
204210
// There are certain messages that we want to keep their formatting
205211
lastMessage.groupUpdateMessage?.isGroupLeavingKind() == true ||

0 commit comments

Comments
 (0)