Skip to content

Commit

Permalink
Implement MessageContentFactory (#5498)
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves authored Dec 3, 2024
1 parent 09b3b6e commit 8889171
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 19 deletions.
4 changes: 4 additions & 0 deletions .idea/inspectionProfiles/ktlint.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
### ✅ Added
- Add `ChatTheme.downloadAttachmentUriGenerator` property to generate download URIs for attachments. [#5490](https://github.com/GetStream/stream-chat-android/pull/5490)
- Add `ChatTheme.downloadRequestInterceptor` property to intercept download requests. [#5490](https://github.com/GetStream/stream-chat-android/pull/5490)
- Add `MessageContentFactory` class for customizing message contents. [#5498](https://github.com/GetStream/stream-chat-android/pull/5498)

### ⚠️ Changed

Expand Down
14 changes: 12 additions & 2 deletions stream-chat-android-compose/api/stream-chat-android-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@ public final class io/getstream/chat/android/compose/ui/components/messages/Mess
}

public final class io/getstream/chat/android/compose/ui/components/messages/MessageContentKt {
public static final fun MessageContent (Lio/getstream/chat/android/models/Message;Lio/getstream/chat/android/models/User;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;III)V
public static final fun MessageContent (Lio/getstream/chat/android/models/Message;Lio/getstream/chat/android/models/User;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lio/getstream/chat/android/compose/ui/components/messages/factory/MessageContentFactory;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;III)V
}

public final class io/getstream/chat/android/compose/ui/components/messages/MessageFooterKt {
Expand Down Expand Up @@ -1202,6 +1202,15 @@ public final class io/getstream/chat/android/compose/ui/components/messages/Uplo
public static final fun UploadingFooter (Lio/getstream/chat/android/models/Message;Landroidx/compose/ui/Modifier;Landroidx/compose/runtime/Composer;II)V
}

public class io/getstream/chat/android/compose/ui/components/messages/factory/MessageContentFactory {
public static final field $stable I
public fun <init> ()V
public fun MessageDeletedContent (Landroidx/compose/ui/Modifier;Landroidx/compose/runtime/Composer;I)V
public fun MessageGiphyContent (Lio/getstream/chat/android/models/Message;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;I)V
public fun MessageTextContent (Lio/getstream/chat/android/models/Message;Lio/getstream/chat/android/models/User;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;I)V
public fun QuotedMessageContent (Lio/getstream/chat/android/models/Message;Lio/getstream/chat/android/models/User;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;I)V
}

public final class io/getstream/chat/android/compose/ui/components/moderatedmessage/ComposableSingletons$ModeratedMessageDialogKt {
public static final field INSTANCE Lio/getstream/chat/android/compose/ui/components/moderatedmessage/ComposableSingletons$ModeratedMessageDialogKt;
public static field lambda-1 Lkotlin/jvm/functions/Function2;
Expand Down Expand Up @@ -2018,6 +2027,7 @@ public final class io/getstream/chat/android/compose/ui/theme/ChatTheme {
public final fun getDimens (Landroidx/compose/runtime/Composer;I)Lio/getstream/chat/android/compose/ui/theme/StreamDimens;
public final fun getMessageAlignmentProvider (Landroidx/compose/runtime/Composer;I)Lio/getstream/chat/android/compose/ui/util/MessageAlignmentProvider;
public final fun getMessageComposerTheme (Landroidx/compose/runtime/Composer;I)Lio/getstream/chat/android/compose/ui/theme/MessageComposerTheme;
public final fun getMessageContentFactory (Landroidx/compose/runtime/Composer;I)Lio/getstream/chat/android/compose/ui/components/messages/factory/MessageContentFactory;
public final fun getMessageDateSeparatorTheme (Landroidx/compose/runtime/Composer;I)Lio/getstream/chat/android/compose/ui/theme/MessageDateSeparatorTheme;
public final fun getMessageOptionsTheme (Landroidx/compose/runtime/Composer;I)Lio/getstream/chat/android/compose/ui/theme/MessageOptionsTheme;
public final fun getMessageOptionsUserReactionAlignment (Landroidx/compose/runtime/Composer;I)Lio/getstream/chat/android/ui/common/state/messages/list/MessageOptionsUserReactionAlignment;
Expand Down Expand Up @@ -2049,7 +2059,7 @@ public final class io/getstream/chat/android/compose/ui/theme/ChatTheme {
}

public final class io/getstream/chat/android/compose/ui/theme/ChatThemeKt {
public static final fun ChatTheme (ZZZZLio/getstream/chat/android/compose/ui/theme/StreamColors;Lio/getstream/chat/android/compose/ui/theme/StreamDimens;Lio/getstream/chat/android/compose/ui/theme/StreamTypography;Lio/getstream/chat/android/compose/ui/theme/StreamShapes;Lio/getstream/chat/android/compose/ui/theme/StreamRippleConfiguration;Ljava/util/List;Ljava/util/List;Ljava/util/List;Lio/getstream/chat/android/compose/ui/util/ReactionIconFactory;Lio/getstream/chat/android/compose/ui/theme/ReactionOptionsTheme;Lio/getstream/chat/android/compose/ui/util/MessagePreviewIconFactory;Lio/getstream/chat/android/compose/ui/util/PollSwitchItemFactory;ZLio/getstream/chat/android/ui/common/helper/DateFormatter;Lio/getstream/chat/android/ui/common/helper/TimeProvider;Lio/getstream/chat/android/ui/common/utils/ChannelNameFormatter;Lio/getstream/chat/android/compose/ui/util/MessagePreviewFormatter;Lio/getstream/chat/android/compose/ui/util/SearchResultNameFormatter;Lio/getstream/chat/android/compose/ui/util/StreamCoilImageLoaderFactory;Lio/getstream/chat/android/ui/common/helper/ImageHeadersProvider;Lio/getstream/chat/android/ui/common/helper/DownloadAttachmentUriGenerator;Lio/getstream/chat/android/ui/common/helper/DownloadRequestInterceptor;Lio/getstream/chat/android/ui/common/helper/ImageAssetTransformer;Lio/getstream/chat/android/compose/ui/util/MessageAlignmentProvider;Lio/getstream/chat/android/compose/ui/theme/MessageOptionsTheme;Lio/getstream/chat/android/ui/common/state/messages/list/MessageOptionsUserReactionAlignment;Ljava/util/List;ZLio/getstream/chat/android/ui/common/images/resizing/StreamCdnImageResizing;ZLio/getstream/chat/android/compose/ui/theme/MessageTheme;Lio/getstream/chat/android/compose/ui/theme/MessageTheme;Lio/getstream/chat/android/compose/ui/theme/MessageDateSeparatorTheme;Lio/getstream/chat/android/compose/ui/theme/MessageUnreadSeparatorTheme;Lio/getstream/chat/android/compose/ui/theme/MessageComposerTheme;Lio/getstream/chat/android/compose/ui/theme/AttachmentPickerTheme;Lio/getstream/chat/android/compose/ui/util/MessageTextFormatter;Lio/getstream/chat/android/compose/ui/util/QuotedMessageTextFormatter;Lio/getstream/sdk/chat/audio/recording/StreamMediaRecorder;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;IIIIIII)V
public static final fun ChatTheme (ZZZZLio/getstream/chat/android/compose/ui/theme/StreamColors;Lio/getstream/chat/android/compose/ui/theme/StreamDimens;Lio/getstream/chat/android/compose/ui/theme/StreamTypography;Lio/getstream/chat/android/compose/ui/theme/StreamShapes;Lio/getstream/chat/android/compose/ui/theme/StreamRippleConfiguration;Ljava/util/List;Lio/getstream/chat/android/compose/ui/components/messages/factory/MessageContentFactory;Ljava/util/List;Ljava/util/List;Lio/getstream/chat/android/compose/ui/util/ReactionIconFactory;Lio/getstream/chat/android/compose/ui/theme/ReactionOptionsTheme;Lio/getstream/chat/android/compose/ui/util/MessagePreviewIconFactory;Lio/getstream/chat/android/compose/ui/util/PollSwitchItemFactory;ZLio/getstream/chat/android/ui/common/helper/DateFormatter;Lio/getstream/chat/android/ui/common/helper/TimeProvider;Lio/getstream/chat/android/ui/common/utils/ChannelNameFormatter;Lio/getstream/chat/android/compose/ui/util/MessagePreviewFormatter;Lio/getstream/chat/android/compose/ui/util/SearchResultNameFormatter;Lio/getstream/chat/android/compose/ui/util/StreamCoilImageLoaderFactory;Lio/getstream/chat/android/ui/common/helper/ImageHeadersProvider;Lio/getstream/chat/android/ui/common/helper/DownloadAttachmentUriGenerator;Lio/getstream/chat/android/ui/common/helper/DownloadRequestInterceptor;Lio/getstream/chat/android/ui/common/helper/ImageAssetTransformer;Lio/getstream/chat/android/compose/ui/util/MessageAlignmentProvider;Lio/getstream/chat/android/compose/ui/theme/MessageOptionsTheme;Lio/getstream/chat/android/ui/common/state/messages/list/MessageOptionsUserReactionAlignment;Ljava/util/List;ZLio/getstream/chat/android/ui/common/images/resizing/StreamCdnImageResizing;ZLio/getstream/chat/android/compose/ui/theme/MessageTheme;Lio/getstream/chat/android/compose/ui/theme/MessageTheme;Lio/getstream/chat/android/compose/ui/theme/MessageDateSeparatorTheme;Lio/getstream/chat/android/compose/ui/theme/MessageUnreadSeparatorTheme;Lio/getstream/chat/android/compose/ui/theme/MessageComposerTheme;Lio/getstream/chat/android/compose/ui/theme/AttachmentPickerTheme;Lio/getstream/chat/android/compose/ui/util/MessageTextFormatter;Lio/getstream/chat/android/compose/ui/util/QuotedMessageTextFormatter;Lio/getstream/sdk/chat/audio/recording/StreamMediaRecorder;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;IIIIIII)V
}

public final class io/getstream/chat/android/compose/ui/theme/ComponentOffset {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import io.getstream.chat.android.client.utils.message.isGiphyEphemeral
import io.getstream.chat.android.compose.R
import io.getstream.chat.android.compose.state.mediagallerypreview.MediaGalleryPreviewResult
import io.getstream.chat.android.compose.ui.attachments.content.MessageAttachmentsContent
import io.getstream.chat.android.compose.ui.components.messages.factory.MessageContentFactory
import io.getstream.chat.android.compose.ui.messages.list.DefaultMessageTextContent
import io.getstream.chat.android.compose.ui.theme.ChatTheme
import io.getstream.chat.android.models.Message
Expand All @@ -44,6 +45,7 @@ import io.getstream.chat.android.ui.common.state.messages.list.GiphyAction
* @param onGiphyActionClick Handler for Giphy actions.
* @param onQuotedMessageClick Handler for quoted message click action.
* @param onLinkClick Handler for clicking on a link in the message.
* @param messageContentFactory Factory for creating message content.
* @param onMediaGalleryPreviewResult Handler when the user selects an option in the Media Gallery Preview screen.
* @param giphyEphemeralContent Composable that represents the default Giphy message content.
* @param deletedMessageContent Composable that represents the default content of a deleted message.
Expand All @@ -60,21 +62,23 @@ public fun MessageContent(
onQuotedMessageClick: (Message) -> Unit = {},
onUserMentionClick: (User) -> Unit = {},
onLinkClick: ((Message, String) -> Unit)? = null,
messageContentFactory: MessageContentFactory = ChatTheme.messageContentFactory,
onMediaGalleryPreviewResult: (MediaGalleryPreviewResult?) -> Unit = {},
giphyEphemeralContent: @Composable () -> Unit = {
DefaultMessageGiphyContent(
messageContentFactory.MessageGiphyContent(
message = message,
onGiphyActionClick = onGiphyActionClick,
)
},
deletedMessageContent: @Composable () -> Unit = {
DefaultMessageDeletedContent(modifier = modifier)
messageContentFactory.MessageDeletedContent(modifier = modifier)
},
regularMessageContent: @Composable () -> Unit = {
DefaultMessageContent(
message = message,
currentUser = currentUser,
onLongItemClick = onLongItemClick,
messageContentFactory = messageContentFactory,
onMediaGalleryPreviewResult = onMediaGalleryPreviewResult,
onQuotedMessageClick = onQuotedMessageClick,
onLinkClick = onLinkClick,
Expand Down Expand Up @@ -135,6 +139,7 @@ internal fun DefaultMessageDeletedContent(
* @param message The message to show.
* @param onLongItemClick Handler when the item is long clicked.
* @param onMediaGalleryPreviewResult Handler when the user selects an option in the Media Gallery Preview screen.
* @param messageContentFactory Factory for creating message content.
* @param onQuotedMessageClick Handler for quoted message click action.
* @param onLinkClick Handler for clicking on a link in the message.
*/
Expand All @@ -143,6 +148,7 @@ internal fun DefaultMessageContent(
message: Message,
currentUser: User?,
onLongItemClick: (Message) -> Unit,
messageContentFactory: MessageContentFactory = ChatTheme.messageContentFactory,
onMediaGalleryPreviewResult: (MediaGalleryPreviewResult?) -> Unit = {},
onQuotedMessageClick: (Message) -> Unit,
onUserMentionClick: (User) -> Unit = {},
Expand All @@ -159,6 +165,7 @@ internal fun DefaultMessageContent(
DefaultMessageTextContent(
message = message,
currentUser = currentUser,
messageContentFactory = messageContentFactory,
onLongItemClick = onLongItemClick,
onQuotedMessageClick = onQuotedMessageClick,
onLinkClick = onLinkClick,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) 2014-2024 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.components.messages.factory

import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import io.getstream.chat.android.compose.ui.components.messages.DefaultMessageDeletedContent
import io.getstream.chat.android.compose.ui.components.messages.DefaultMessageGiphyContent
import io.getstream.chat.android.compose.ui.components.messages.MessageText
import io.getstream.chat.android.compose.ui.components.messages.QuotedMessage
import io.getstream.chat.android.models.Message
import io.getstream.chat.android.models.User
import io.getstream.chat.android.ui.common.state.messages.list.GiphyAction

/**
* Factory for creating message contents that are used to represent the chat message items.
*/
@Stable
public open class MessageContentFactory {

/**
* Represents the default Giphy message content.
*/
@Composable
public open fun MessageGiphyContent(
message: Message,
onGiphyActionClick: (GiphyAction) -> Unit,
) {
DefaultMessageGiphyContent(
message = message,
onGiphyActionClick = onGiphyActionClick,
)
}

/**
* Represents the default content of a deleted message.
*/
@Composable
public open fun MessageDeletedContent(
modifier: Modifier,
) {
DefaultMessageDeletedContent(modifier = modifier)
}

/**
* Represents the default regular message content.
*/
@Composable
public open fun MessageTextContent(
message: Message,
currentUser: User?,
onLongItemClick: (Message) -> Unit,
onLinkClick: ((Message, String) -> Unit)?,
onUserMentionClick: (User) -> Unit,
) {
MessageText(
message = message,
currentUser = currentUser,
onLongItemClick = onLongItemClick,
onLinkClick = onLinkClick,
)
}

/**
* Represents the default quoted message content.
*/
@Composable
public open fun QuotedMessageContent(
message: Message,
currentUser: User?,
onLongItemClick: (Message) -> Unit,
onQuotedMessageClick: (Message) -> Unit,
) {
val quotedMessage = message.replyTo
if (quotedMessage != null) {
QuotedMessage(
modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp),
message = quotedMessage,
currentUser = currentUser,
replyMessage = message,
onLongItemClick = { onLongItemClick(message) },
onQuotedMessageClick = onQuotedMessageClick,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,10 @@ import io.getstream.chat.android.compose.ui.components.messages.MessageContent
import io.getstream.chat.android.compose.ui.components.messages.MessageFooter
import io.getstream.chat.android.compose.ui.components.messages.MessageHeaderLabel
import io.getstream.chat.android.compose.ui.components.messages.MessageReactions
import io.getstream.chat.android.compose.ui.components.messages.MessageText
import io.getstream.chat.android.compose.ui.components.messages.OwnedMessageVisibilityContent
import io.getstream.chat.android.compose.ui.components.messages.PollMessageContent
import io.getstream.chat.android.compose.ui.components.messages.QuotedMessage
import io.getstream.chat.android.compose.ui.components.messages.UploadingFooter
import io.getstream.chat.android.compose.ui.components.messages.factory.MessageContentFactory
import io.getstream.chat.android.compose.ui.theme.ChatTheme
import io.getstream.chat.android.compose.ui.util.isEmojiOnlyWithoutBubble
import io.getstream.chat.android.compose.ui.util.isErrorOrFailed
Expand Down Expand Up @@ -686,25 +685,21 @@ private fun getMessageBubbleColor(message: Message, ownsMessage: Boolean): Color
internal fun DefaultMessageTextContent(
message: Message,
currentUser: User?,
messageContentFactory: MessageContentFactory = ChatTheme.messageContentFactory,
onLongItemClick: (Message) -> Unit,
onQuotedMessageClick: (Message) -> Unit,
onUserMentionClick: (User) -> Unit = {},
onLinkClick: ((Message, String) -> Unit)? = null,
) {
val quotedMessage = message.replyTo

Column {
if (quotedMessage != null) {
QuotedMessage(
modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp),
message = quotedMessage,
currentUser = currentUser,
replyMessage = message,
onLongItemClick = { onLongItemClick(message) },
onQuotedMessageClick = onQuotedMessageClick,
)
}
MessageText(
messageContentFactory.QuotedMessageContent(
message = message,
currentUser = currentUser,
onLongItemClick = onLongItemClick,
onQuotedMessageClick = onQuotedMessageClick,
)

messageContentFactory.MessageTextContent(
message = message,
currentUser = currentUser,
onLongItemClick = onLongItemClick,
Expand Down
Loading

0 comments on commit 8889171

Please sign in to comment.