|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2025 Stream.io Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Stream License; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://github.com/GetStream/stream-chat-android/blob/main/LICENSE |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.getstream.chat.android.compose.sample.ui.component |
| 18 | + |
| 19 | +import androidx.compose.material.icons.Icons |
| 20 | +import androidx.compose.material.icons.outlined.Info |
| 21 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 22 | +import androidx.compose.material3.ModalBottomSheet |
| 23 | +import androidx.compose.runtime.Composable |
| 24 | +import androidx.compose.runtime.getValue |
| 25 | +import androidx.compose.runtime.mutableStateOf |
| 26 | +import androidx.compose.runtime.remember |
| 27 | +import androidx.compose.runtime.setValue |
| 28 | +import androidx.compose.ui.Modifier |
| 29 | +import androidx.compose.ui.graphics.vector.rememberVectorPainter |
| 30 | +import io.getstream.chat.android.compose.sample.R |
| 31 | +import io.getstream.chat.android.compose.state.messageoptions.MessageOptionItemState |
| 32 | +import io.getstream.chat.android.compose.ui.theme.ChatComponentFactory |
| 33 | +import io.getstream.chat.android.compose.ui.theme.ChatTheme |
| 34 | +import io.getstream.chat.android.models.Message |
| 35 | +import io.getstream.chat.android.ui.common.state.messages.CustomAction |
| 36 | +import io.getstream.chat.android.ui.common.state.messages.MessageAction |
| 37 | + |
| 38 | +/** |
| 39 | + * Factory for creating components related to message info. |
| 40 | + */ |
| 41 | +class MessageInfoComponentFactory : ChatComponentFactory { |
| 42 | + |
| 43 | + /** |
| 44 | + * Creates a message menu with option for message info. |
| 45 | + */ |
| 46 | + @OptIn(ExperimentalMaterial3Api::class) |
| 47 | + @Suppress("LongMethod") |
| 48 | + @Composable |
| 49 | + override fun MessageMenu( |
| 50 | + modifier: Modifier, |
| 51 | + message: Message, |
| 52 | + messageOptions: List<MessageOptionItemState>, |
| 53 | + ownCapabilities: Set<String>, |
| 54 | + onMessageAction: (MessageAction) -> Unit, |
| 55 | + onShowMore: () -> Unit, |
| 56 | + onDismiss: () -> Unit, |
| 57 | + ) { |
| 58 | + var showMessageInfoDialog by remember { mutableStateOf(false) } |
| 59 | + |
| 60 | + val allOptions = listOf( |
| 61 | + MessageOptionItemState( |
| 62 | + title = R.string.message_option_message_info, |
| 63 | + titleColor = ChatTheme.colors.textHighEmphasis, |
| 64 | + iconPainter = rememberVectorPainter(Icons.Outlined.Info), |
| 65 | + iconColor = ChatTheme.colors.textLowEmphasis, |
| 66 | + action = CustomAction(message, mapOf("message_info" to true)), |
| 67 | + ) |
| 68 | + ) + messageOptions |
| 69 | + |
| 70 | + val extendedOnMessageAction: (MessageAction) -> Unit = { action -> |
| 71 | + when { |
| 72 | + action is CustomAction && action.extraProperties.contains("message_info") -> |
| 73 | + showMessageInfoDialog = true |
| 74 | + |
| 75 | + else -> onMessageAction(action) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + var dismissed by remember { mutableStateOf(false) } |
| 80 | + |
| 81 | + if (showMessageInfoDialog) { |
| 82 | + ModalBottomSheet( |
| 83 | + onDismissRequest = { |
| 84 | + showMessageInfoDialog = false |
| 85 | + onDismiss() |
| 86 | + dismissed = true // Mark as dismissed to avoid animating the menu again |
| 87 | + }, |
| 88 | + containerColor = ChatTheme.colors.appBackground, |
| 89 | + ) { |
| 90 | + // TODO Replace with a proper Message Info Screen |
| 91 | + } |
| 92 | + } else if (!dismissed) { |
| 93 | + super.MessageMenu( |
| 94 | + modifier = modifier, |
| 95 | + message = message, |
| 96 | + messageOptions = allOptions, |
| 97 | + ownCapabilities = ownCapabilities, |
| 98 | + onMessageAction = extendedOnMessageAction, |
| 99 | + onShowMore = onShowMore, |
| 100 | + onDismiss = onDismiss, |
| 101 | + ) |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments