|
| 1 | +/* |
| 2 | + * Wire |
| 3 | + * Copyright (C) 2025 Wire Swiss GmbH |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see http://www.gnu.org/licenses/. |
| 17 | + */ |
| 18 | +package com.wire.android.ui.home.conversations.messages |
| 19 | + |
| 20 | +import androidx.compose.foundation.Image |
| 21 | +import androidx.compose.foundation.border |
| 22 | +import androidx.compose.foundation.layout.Arrangement |
| 23 | +import androidx.compose.foundation.layout.Box |
| 24 | +import androidx.compose.foundation.layout.Column |
| 25 | +import androidx.compose.foundation.layout.Row |
| 26 | +import androidx.compose.foundation.layout.fillMaxSize |
| 27 | +import androidx.compose.foundation.layout.height |
| 28 | +import androidx.compose.foundation.layout.size |
| 29 | +import androidx.compose.foundation.layout.width |
| 30 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 31 | +import androidx.compose.material.Text |
| 32 | +import androidx.compose.material3.Icon |
| 33 | +import androidx.compose.material3.MaterialTheme |
| 34 | +import androidx.compose.runtime.Composable |
| 35 | +import androidx.compose.ui.Alignment |
| 36 | +import androidx.compose.ui.Modifier |
| 37 | +import androidx.compose.ui.draw.clip |
| 38 | +import androidx.compose.ui.layout.ContentScale |
| 39 | +import androidx.compose.ui.platform.LocalContext |
| 40 | +import androidx.compose.ui.res.painterResource |
| 41 | +import androidx.compose.ui.res.pluralStringResource |
| 42 | +import androidx.compose.ui.res.stringResource |
| 43 | +import coil.compose.AsyncImage |
| 44 | +import coil.decode.VideoFrameDecoder |
| 45 | +import coil.request.ImageRequest |
| 46 | +import com.wire.android.R |
| 47 | +import com.wire.android.feature.cells.domain.model.AttachmentFileType |
| 48 | +import com.wire.android.feature.cells.domain.model.AttachmentFileType.IMAGE |
| 49 | +import com.wire.android.feature.cells.domain.model.AttachmentFileType.VIDEO |
| 50 | +import com.wire.android.feature.cells.domain.model.icon |
| 51 | +import com.wire.android.model.Clickable |
| 52 | +import com.wire.android.ui.common.colorsScheme |
| 53 | +import com.wire.android.ui.common.dimensions |
| 54 | +import com.wire.android.ui.home.conversations.model.UIMultipartQuotedContent |
| 55 | +import com.wire.android.ui.theme.Accent |
| 56 | +import com.wire.android.ui.theme.wireColorScheme |
| 57 | +import com.wire.android.util.ui.UIText |
| 58 | +import com.wire.kalium.logic.util.fileExtension |
| 59 | + |
| 60 | +@Composable |
| 61 | +fun QuotedMultipartMessage( |
| 62 | + senderName: UIText, |
| 63 | + originalDateTimeText: UIText, |
| 64 | + text: String?, |
| 65 | + attachments: List<UIMultipartQuotedContent>, |
| 66 | + style: QuotedMessageStyle, |
| 67 | + accent: Accent, |
| 68 | + clickable: Clickable?, |
| 69 | + modifier: Modifier = Modifier, |
| 70 | + startContent: @Composable () -> Unit = {} |
| 71 | +) { |
| 72 | + QuotedMessageContent( |
| 73 | + senderName = senderName.asString(), |
| 74 | + style = style, |
| 75 | + accent = accent, |
| 76 | + modifier = modifier, |
| 77 | + centerContent = { |
| 78 | + Column( |
| 79 | + modifier = Modifier.fillMaxSize(), |
| 80 | + verticalArrangement = Arrangement.spacedBy( |
| 81 | + dimensions().spacing4x, |
| 82 | + Alignment.CenterVertically |
| 83 | + ), |
| 84 | + ) { |
| 85 | + |
| 86 | + // Message text or file name |
| 87 | + when { |
| 88 | + text?.isNotEmpty() == true -> MainMarkdownText(text, messageStyle = style.messageStyle) |
| 89 | + attachments.isSingleMediaAttachment() -> |
| 90 | + if (attachments.first().assetAvailable) { |
| 91 | + MainContentText(attachments.first().name) |
| 92 | + } else { |
| 93 | + MainContentText(stringResource(R.string.asset_message_failed_download_text)) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + when { |
| 98 | + attachments.size > 1 -> MultipleAttachmentsLabel(attachments.size) |
| 99 | + attachments.isSingleFileAttachment() -> FileIconAndNameRow(attachments.first()) |
| 100 | + } |
| 101 | + } |
| 102 | + }, |
| 103 | + startContent = { |
| 104 | + startContent() |
| 105 | + }, |
| 106 | + endContent = { |
| 107 | + if (attachments.isSingleMediaAttachment()) { |
| 108 | + MediaAttachmentThumbnail(attachments.first()) |
| 109 | + } |
| 110 | + }, |
| 111 | + footerContent = { QuotedMessageOriginalDate(originalDateTimeText) }, |
| 112 | + clickable = clickable |
| 113 | + ) |
| 114 | +} |
| 115 | + |
| 116 | +@Composable |
| 117 | +private fun MediaAttachmentThumbnail(attachment: UIMultipartQuotedContent) { |
| 118 | + if (attachment.assetAvailable) { |
| 119 | + Box( |
| 120 | + modifier = Modifier |
| 121 | + .width(dimensions().spacing40x) |
| 122 | + .height(dimensions().spacing40x) |
| 123 | + .border( |
| 124 | + width = dimensions().spacing1x, |
| 125 | + color = MaterialTheme.wireColorScheme.outline, |
| 126 | + shape = RoundedCornerShape(dimensions().spacing8x) |
| 127 | + ) |
| 128 | + .clip(RoundedCornerShape(dimensions().spacing8x)), |
| 129 | + ) { |
| 130 | + AsyncImage( |
| 131 | + model = attachment.imageModel(), |
| 132 | + alignment = Alignment.Center, |
| 133 | + contentScale = ContentScale.Crop, |
| 134 | + contentDescription = null, |
| 135 | + ) |
| 136 | + |
| 137 | + if (AttachmentFileType.fromMimeType(attachment.mimeType) == VIDEO) { |
| 138 | + Image( |
| 139 | + modifier = Modifier |
| 140 | + .size(dimensions().spacing24x) |
| 141 | + .align(Alignment.Center), |
| 142 | + painter = painterResource(id = R.drawable.ic_play_circle_filled), |
| 143 | + contentDescription = null, |
| 144 | + ) |
| 145 | + } |
| 146 | + } |
| 147 | + } else { |
| 148 | + Image( |
| 149 | + modifier = Modifier.size(dimensions().spacing24x), |
| 150 | + painter = painterResource(id = R.drawable.ic_file_not_available), |
| 151 | + contentDescription = null, |
| 152 | + ) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +@Composable |
| 157 | +private fun MultipleAttachmentsLabel(count: Int) { |
| 158 | + Row( |
| 159 | + horizontalArrangement = Arrangement.spacedBy(dimensions().spacing4x) |
| 160 | + ) { |
| 161 | + Icon( |
| 162 | + painter = painterResource(R.drawable.ic_multiple_files), |
| 163 | + contentDescription = null, |
| 164 | + tint = colorsScheme().secondaryText |
| 165 | + ) |
| 166 | + Text( |
| 167 | + text = pluralStringResource(R.plurals.reply_multiple_files, count, count), |
| 168 | + color = colorsScheme().secondaryText |
| 169 | + ) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +@Composable |
| 174 | +private fun FileIconAndNameRow(file: UIMultipartQuotedContent) { |
| 175 | + |
| 176 | + val name = file.name |
| 177 | + val attachmentFileType = name.fileExtension()?.let { AttachmentFileType.fromExtension(it) } ?: AttachmentFileType.OTHER |
| 178 | + |
| 179 | + Row( |
| 180 | + horizontalArrangement = Arrangement.spacedBy(dimensions().spacing4x) |
| 181 | + ) { |
| 182 | + Image( |
| 183 | + modifier = Modifier.size(dimensions().spacing16x), |
| 184 | + painter = if (file.assetAvailable) { |
| 185 | + painterResource(id = attachmentFileType.icon()) |
| 186 | + } else { |
| 187 | + painterResource(id = R.drawable.ic_file_not_available) |
| 188 | + }, |
| 189 | + contentDescription = null, |
| 190 | + ) |
| 191 | + Text( |
| 192 | + text = if (file.assetAvailable) name else stringResource(R.string.asset_message_failed_download_text), |
| 193 | + color = colorsScheme().secondaryText |
| 194 | + ) |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +@Composable |
| 199 | +private fun UIMultipartQuotedContent.imageModel(): ImageRequest { |
| 200 | + |
| 201 | + val builder = ImageRequest.Builder(LocalContext.current) |
| 202 | + .diskCacheKey(previewUrl) |
| 203 | + .memoryCacheKey(previewUrl) |
| 204 | + .data(localPath ?: previewUrl) |
| 205 | + .crossfade(true) |
| 206 | + |
| 207 | + if (localPath != null && AttachmentFileType.fromMimeType(mimeType) == VIDEO) { |
| 208 | + builder.decoderFactory { result, options, _ -> |
| 209 | + VideoFrameDecoder(result.source, options) |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + return builder.build() |
| 214 | +} |
| 215 | + |
| 216 | +private fun UIMultipartQuotedContent.isMediaAttachment() = |
| 217 | + when (AttachmentFileType.fromMimeType(mimeType)) { |
| 218 | + IMAGE, VIDEO -> true |
| 219 | + else -> false |
| 220 | + } |
| 221 | + |
| 222 | +private fun List<UIMultipartQuotedContent>.isSingleMediaAttachment() = |
| 223 | + size == 1 && first().isMediaAttachment() |
| 224 | + |
| 225 | +private fun List<UIMultipartQuotedContent>.isSingleFileAttachment() = |
| 226 | + size == 1 && !first().isMediaAttachment() |
0 commit comments