From 1303979cdfdb96b047be1bef74cc3da153a08cd3 Mon Sep 17 00:00:00 2001 From: andrew Date: Mon, 3 Jul 2023 10:35:50 +0930 Subject: [PATCH] Add image attachments --- .../conversation/v2/MessageDetailActivity.kt | 72 ++++++++++++------- 1 file changed, 47 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailActivity.kt index bf32c2c61f..0fd5e6eb90 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/MessageDetailActivity.kt @@ -10,11 +10,13 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.ExperimentalLayoutApi import androidx.compose.foundation.layout.FlowRow import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.aspectRatio import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.width import androidx.compose.foundation.pager.HorizontalPager +import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll import androidx.compose.material.Divider @@ -230,31 +232,7 @@ class MessageDetailActivity: PassphraseRequiredActionBarActivity() { modifier = Modifier.verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(16.dp) ) { - HorizontalPager(pageCount = attachments.size) {i -> - val attachment = attachments[i] - attachment.apply { - Column(verticalArrangement = Arrangement.spacedBy(16.dp)) { - Cell { - if (slide.hasImage()) GlideImage( - contentScale = ContentScale.FillHeight, - modifier = Modifier.fillMaxWidth(), - model = attachment.slide.uri, - contentDescription = attachment.slide.fileName.orNull() ?: "image" - ) - } - fileDetails.takeIf { it.isNotEmpty() }?.let { - CellWithPadding { - FlowRow( - verticalArrangement = Arrangement.spacedBy(16.dp), - maxItemsInEachRow = 2 - ) { - it.forEach { titledText(it, Modifier.weight(1f)) } - } - } - } - } - } - } + Attachments(attachments) if (sent != null || received != null || senderInfo != null) CellWithPadding { Column(verticalArrangement = Arrangement.spacedBy(16.dp)) { sent?.let { titledText(it) } @@ -299,6 +277,50 @@ class MessageDetailActivity: PassphraseRequiredActionBarActivity() { } } + @Composable + fun Attachments(attachments: List) { + val slide = attachments.firstOrNull()?.slide ?: return + when { + slide.hasImage() -> ImageAttachments(attachments) + } + } + + @OptIn( + ExperimentalFoundationApi::class, + ExperimentalGlideComposeApi::class, + ExperimentalLayoutApi::class, + ) + @Composable + fun ImageAttachments(attachments: List) { + val pagerState = rememberPagerState() + + Column(verticalArrangement = Arrangement.spacedBy(16.dp)) { + Cell { + val imageAttachments = attachments.filter { it.slide.hasImage() } + HorizontalPager(state = pagerState, pageCount = imageAttachments.size) { i -> + imageAttachments[i].slide.apply { + GlideImage( + contentScale = ContentScale.Crop, + modifier = Modifier.aspectRatio(1f), + model = uri, + contentDescription = fileName.orNull() ?: "image" + ) + } + } + } + attachments[pagerState.currentPage].fileDetails.takeIf { it.isNotEmpty() }?.let { + CellWithPadding { + FlowRow( + verticalArrangement = Arrangement.spacedBy(16.dp), + maxItemsInEachRow = 2 + ) { + it.forEach { titledText(it, Modifier.weight(1f)) } + } + } + } + } + } + @Composable fun Divider() { Divider(modifier = Modifier.padding(horizontal = 16.dp), thickness = 1.dp, color = LocalExtraColors.current.divider)