Skip to content

Commit 5343d83

Browse files
committed
Separate incoming and outgoing message text color
1 parent 2741bee commit 5343d83

File tree

5 files changed

+39
-17
lines changed

5 files changed

+39
-17
lines changed

stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/components/messages/MessageText.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import io.getstream.chat.android.compose.ui.util.showOriginalTextAsState
4949
import io.getstream.chat.android.models.Message
5050
import io.getstream.chat.android.models.User
5151
import io.getstream.chat.android.ui.common.utils.extensions.getUserByNameOrId
52+
import io.getstream.chat.android.ui.common.utils.extensions.isMine
5253

5354
/**
5455
* Default text element for messages, with extra styling and padding for the chat bubble.
@@ -92,7 +93,7 @@ public fun MessageText(
9293
val style = when {
9394
message.isSingleEmoji() -> ChatTheme.typography.singleEmoji
9495
message.isFewEmoji() -> ChatTheme.typography.emojiOnly
95-
else -> MessageStyling.textStyle()
96+
else -> MessageStyling.textStyle(outgoing = message.isMine(currentUser))
9697
}
9798

9899
val annotations = styledText.getStringAnnotations(0, styledText.lastIndex)

stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/components/messages/QuotedMessage.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import io.getstream.chat.android.compose.ui.components.ComposerCancelIcon
5656
import io.getstream.chat.android.compose.ui.components.attachments.files.FileIconData
5757
import io.getstream.chat.android.compose.ui.components.attachments.files.FileTypeIcon
5858
import io.getstream.chat.android.compose.ui.theme.ChatTheme
59+
import io.getstream.chat.android.compose.ui.theme.MessageStyling
5960
import io.getstream.chat.android.compose.ui.theme.StreamPrimitiveColors
6061
import io.getstream.chat.android.compose.ui.theme.StreamTokens
6162
import io.getstream.chat.android.compose.ui.util.StreamAsyncImage
@@ -86,10 +87,12 @@ public fun QuotedMessage(
8687
val colors = ChatTheme.colors
8788
val backgroundColor: Color
8889
val indicatorColor: Color
90+
val isMine: Boolean
8991

9092
if (replyMessage != null) {
9193
// replyMessage is not null: we're rendering an already-sent message
92-
if (replyMessage.isMine(currentUser)) {
94+
isMine = replyMessage.isMine(currentUser)
95+
if (isMine) {
9396
backgroundColor = colors.chatBgAttachmentOutgoing
9497
indicatorColor = colors.chatReplyIndicatorOutgoing
9598
} else {
@@ -98,7 +101,8 @@ public fun QuotedMessage(
98101
}
99102
} else {
100103
// replyMessage is null: we're composing a reply
101-
if (message.isMine(currentUser)) {
104+
isMine = message.isMine(currentUser)
105+
if (isMine) {
102106
backgroundColor = colors.chatBgOutgoing
103107
indicatorColor = colors.chatReplyIndicatorOutgoing
104108
} else {
@@ -139,7 +143,7 @@ public fun QuotedMessage(
139143
verticalArrangement = Arrangement.Center,
140144
) {
141145
QuotedMessageUserName(message, replyMessage, currentUser)
142-
QuotedMessageText(body)
146+
QuotedMessageText(body, outgoing = isMine)
143147
}
144148

145149
QuotedMessageAttachmentPreview(body)
@@ -177,7 +181,8 @@ private fun QuotedMessageUserName(
177181
replyMessage: Message?,
178182
currentUser: User?,
179183
) {
180-
val userName = if (message.isMine(currentUser)) {
184+
val isMine = message.isMine(currentUser)
185+
val userName = if (isMine) {
181186
stringResource(R.string.stream_compose_quoted_message_you)
182187
} else if (replyMessage == null) {
183188
stringResource(R.string.stream_compose_quoted_message_reply_to, message.user.name)
@@ -188,15 +193,15 @@ private fun QuotedMessageUserName(
188193
Text(
189194
text = userName,
190195
fontWeight = FontWeight.SemiBold,
191-
color = ChatTheme.colors.chatTextMessage,
196+
color = MessageStyling.textColor(outgoing = isMine),
192197
maxLines = 1,
193198
overflow = TextOverflow.Ellipsis,
194199
)
195200
}
196201

197202
@Composable
198-
private fun QuotedMessageText(body: QuotedMessageBody) {
199-
val color = ChatTheme.colors.chatTextMessage
203+
private fun QuotedMessageText(body: QuotedMessageBody, outgoing: Boolean) {
204+
val color = MessageStyling.textColor(outgoing)
200205

201206
Row(
202207
horizontalArrangement = Arrangement.spacedBy(StreamTokens.spacing2xs),

stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/theme/MessageStyling.kt

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package io.getstream.chat.android.compose.ui.theme
1919
import androidx.compose.foundation.shape.RoundedCornerShape
2020
import androidx.compose.foundation.shape.ZeroCornerSize
2121
import androidx.compose.runtime.Composable
22+
import androidx.compose.ui.graphics.Color
2223
import androidx.compose.ui.graphics.Shape
2324
import androidx.compose.ui.text.TextStyle
2425
import io.getstream.chat.android.ui.common.state.messages.list.MessagePosition
@@ -31,11 +32,25 @@ internal object MessageStyling {
3132
}
3233

3334
@Composable
34-
fun textStyle(): TextStyle =
35-
textStyle(ChatTheme.typography, ChatTheme.colors)
35+
fun textColor(outgoing: Boolean): Color {
36+
return textColor(outgoing, ChatTheme.colors)
37+
}
38+
39+
fun textColor(outgoing: Boolean, colors: StreamColors): Color {
40+
return if (outgoing) {
41+
colors.chatTextOutgoing
42+
} else {
43+
colors.chatTextIncoming
44+
}
45+
}
3646

37-
fun textStyle(typography: StreamTypography, colors: StreamColors): TextStyle =
38-
typography.bodyDefault.copy(color = colors.chatTextMessage)
47+
@Composable
48+
fun textStyle(outgoing: Boolean): TextStyle =
49+
textStyle(outgoing, ChatTheme.typography, ChatTheme.colors)
50+
51+
fun textStyle(outgoing: Boolean, typography: StreamTypography, colors: StreamColors): TextStyle {
52+
return typography.bodyDefault.copy(color = textColor(outgoing, colors))
53+
}
3954

4055
fun linkStyle(typography: StreamTypography, colors: StreamColors): TextStyle =
4156
typography.bodyDefault.copy(color = colors.chatTextLink)
@@ -58,11 +73,11 @@ internal object MessageStyling {
5873
return when (position) {
5974
MessagePosition.TOP,
6075
MessagePosition.MIDDLE,
61-
-> roundBubble
76+
-> roundBubble
6277

6378
MessagePosition.BOTTOM,
6479
MessagePosition.NONE,
65-
-> when {
80+
-> when {
6681
outgoing -> outgoingBubble
6782
else -> incomingBubble
6883
}

stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/theme/StreamColors.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ public data class StreamColors(
188188
public val chatBgAttachmentOutgoing: Color,
189189
public val chatReplyIndicatorIncoming: Color,
190190
public val chatReplyIndicatorOutgoing: Color,
191-
public val chatTextMessage: Color = textPrimary,
191+
public val chatTextIncoming: Color = textPrimary,
192+
public val chatTextOutgoing: Color = textPrimary,
192193
public val chatTextLink: Color = primaryAccent,
193194
public val chatTextMention: Color = chatTextLink,
194195
public val chatTextTimestamp: Color = textTertiary,

stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/util/MessageTextFormatter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public fun interface MessageTextFormatter {
7171
else -> StreamColors.defaultColors()
7272
},
7373
textStyle: (isMine: Boolean, message: Message) -> TextStyle =
74-
{ _, _ -> MessageStyling.textStyle(typography, colors) },
74+
{ isMine, _ -> MessageStyling.textStyle(outgoing = isMine, typography, colors) },
7575
linkStyle: (isMine: Boolean) -> TextStyle = { MessageStyling.linkStyle(typography, colors) },
7676
mentionColor: (isMine: Boolean) -> Color = { colors.chatTextMention },
7777
builder: AnnotatedMessageTextBuilder? = null,
@@ -114,7 +114,7 @@ public fun interface MessageTextFormatter {
114114
isInDarkMode = isInDarkMode,
115115
typography = typography,
116116
colors = colors,
117-
textStyle = { _, _ -> MessageStyling.textStyle(typography, colors) },
117+
textStyle = { isMine, _ -> MessageStyling.textStyle(outgoing = isMine, typography, colors) },
118118
linkStyle = { MessageStyling.linkStyle(typography, colors) },
119119
mentionColor = { colors.chatTextMention },
120120
builder = builder,

0 commit comments

Comments
 (0)