/
MessagePreviewFormatter.kt
212 lines (197 loc) · 7.75 KB
/
MessagePreviewFormatter.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (c) 2014-2022 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.getstream.chat.android.compose.ui.util
import android.content.Context
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import io.getstream.chat.android.client.models.Attachment
import io.getstream.chat.android.client.models.Message
import io.getstream.chat.android.client.models.User
import io.getstream.chat.android.client.models.getTranslation
import io.getstream.chat.android.compose.ui.attachments.AttachmentFactory
import io.getstream.chat.android.compose.ui.theme.StreamTypography
/**
* An interface that allows to generate a preview text for the given message.
*/
public fun interface MessagePreviewFormatter {
/**
* Generates a preview text for the given message.
*
* @param message The message whose data is used to generate the preview text.
* @param currentUser The currently logged in user.
* @return The formatted text representation for the given message.
*/
public fun formatMessagePreview(message: Message, currentUser: User?): AnnotatedString
public companion object {
/**
* Builds the default message preview text formatter.
*
* @param context The context to load string resources.
* @param autoTranslationEnabled Whether the auto-translation is enabled.
* @param typography The typography to use for styling.
* @param attachmentFactories The list of [AttachmentFactory] to use for formatting attachments.
* @return The default implementation of [MessagePreviewFormatter].
*
* @see [DefaultMessagePreviewFormatter]
*/
public fun defaultFormatter(
context: Context,
autoTranslationEnabled: Boolean,
typography: StreamTypography,
attachmentFactories: List<AttachmentFactory>,
): MessagePreviewFormatter {
return DefaultMessagePreviewFormatter(
context = context,
autoTranslationEnabled = autoTranslationEnabled,
messageTextStyle = typography.bodyBold,
senderNameTextStyle = typography.bodyBold,
attachmentTextFontStyle = typography.bodyItalic,
attachmentFactories = attachmentFactories
)
}
}
}
/**
* The default implementation of [MessagePreviewFormatter] that allows to generate a preview text for
* a message with the following spans: sender name, message text, attachments preview text.
*
* @param context The context to load string resources.
*/
private class DefaultMessagePreviewFormatter(
private val context: Context,
private val autoTranslationEnabled: Boolean,
private val messageTextStyle: TextStyle,
private val senderNameTextStyle: TextStyle,
private val attachmentTextFontStyle: TextStyle,
private val attachmentFactories: List<AttachmentFactory>,
) : MessagePreviewFormatter {
/**
* Generates a preview text for the given message.
*
* @param message The message whose data is used to generate the preview text.
* @param currentUser The currently logged in user.
* @return The formatted text representation for the given message.
*/
override fun formatMessagePreview(
message: Message,
currentUser: User?,
): AnnotatedString {
return buildAnnotatedString {
message.let { message ->
val userLanguage = currentUser?.language.orEmpty()
val displayedText = when (autoTranslationEnabled) {
true -> message.getTranslation(userLanguage).ifEmpty { message.text }
else -> message.text
}.trim()
if (message.isSystem()) {
append(displayedText)
} else {
appendSenderName(
message = message,
currentUser = currentUser,
senderNameTextStyle = senderNameTextStyle
)
appendMessageText(
messageText = displayedText,
messageTextStyle = messageTextStyle
)
appendAttachmentText(
attachments = message.attachments,
attachmentFactories = attachmentFactories,
attachmentTextStyle = attachmentTextFontStyle
)
}
}
}
}
/**
* Appends the sender name to the [AnnotatedString].
*/
private fun AnnotatedString.Builder.appendSenderName(
message: Message,
currentUser: User?,
senderNameTextStyle: TextStyle,
) {
val sender = message.getSenderDisplayName(context, currentUser)
if (sender != null) {
append("$sender: ")
addStyle(
SpanStyle(
fontStyle = senderNameTextStyle.fontStyle,
fontWeight = senderNameTextStyle.fontWeight,
fontFamily = senderNameTextStyle.fontFamily
),
start = 0,
end = sender.length
)
}
}
/**
* Appends the message text to the [AnnotatedString].
*/
private fun AnnotatedString.Builder.appendMessageText(
messageText: String,
messageTextStyle: TextStyle,
) {
if (messageText.isNotEmpty()) {
val startIndex = this.length
append("$messageText ")
addStyle(
SpanStyle(
fontStyle = messageTextStyle.fontStyle,
fontFamily = messageTextStyle.fontFamily
),
start = startIndex,
end = startIndex + messageText.length
)
}
}
/**
* Appends a string representations of [attachments] to the [AnnotatedString].
*/
private fun AnnotatedString.Builder.appendAttachmentText(
attachments: List<Attachment>,
attachmentFactories: List<AttachmentFactory>,
attachmentTextStyle: TextStyle,
) {
if (attachments.isNotEmpty()) {
attachmentFactories
.firstOrNull { it.canHandle(attachments) }
?.textFormatter
?.let { textFormatter ->
attachments.mapNotNull { attachment ->
textFormatter.invoke(attachment)
.let { previewText ->
previewText.ifEmpty { null }
}
}.joinToString()
}?.let { attachmentText ->
val startIndex = this.length
append(attachmentText)
addStyle(
SpanStyle(
fontStyle = attachmentTextStyle.fontStyle,
fontFamily = attachmentTextStyle.fontFamily
),
start = startIndex,
end = startIndex + attachmentText.length
)
}
}
}
}