Skip to content

Commit 45c9f28

Browse files
VelikovPetarclaude
andauthored
Use type-specific attachment URL fields and deprecate imagePreviewUrl (#6280)
* Deprecate imagePreviewUrl and use type-specific attachment URL fields Co-Authored-By: Claude <noreply@anthropic.com> * Extract common extensions. --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent a7b8580 commit 45c9f28

25 files changed

Lines changed: 262 additions & 129 deletions

File tree

stream-chat-android-compose-sample/src/main/java/io/getstream/chat/android/compose/sample/ui/channel/attachments/ChannelMediaAttachmentsActivity.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import io.getstream.chat.android.compose.viewmodel.channel.ChannelAttachmentsVie
3434
import io.getstream.chat.android.compose.viewmodel.channel.ChannelAttachmentsViewModelFactory
3535
import io.getstream.chat.android.models.AttachmentType
3636
import io.getstream.chat.android.ui.common.feature.channel.attachments.ChannelAttachmentsViewEvent
37-
import io.getstream.chat.android.ui.common.utils.extensions.imagePreviewUrl
3837
import kotlinx.coroutines.flow.collectLatest
3938

4039
class ChannelMediaAttachmentsActivity : ComponentActivity() {
@@ -50,7 +49,7 @@ class ChannelMediaAttachmentsActivity : ComponentActivity() {
5049
ChannelAttachmentsViewModelFactory(
5150
cid = requireNotNull(intent.getStringExtra(KEY_CID)),
5251
attachmentTypes = listOf(AttachmentType.IMAGE, AttachmentType.VIDEO),
53-
localFilter = { !it.imagePreviewUrl.isNullOrEmpty() && it.titleLink.isNullOrEmpty() },
52+
localFilter = { !(it.imageUrl ?: it.thumbUrl).isNullOrEmpty() && it.titleLink.isNullOrEmpty() },
5453
)
5554
}
5655

stream-chat-android-compose-sample/src/main/java/io/getstream/chat/android/compose/sample/ui/chats/ChatsActivity.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ import io.getstream.chat.android.ui.common.feature.channel.info.ChannelInfoViewE
9797
import io.getstream.chat.android.ui.common.state.channel.info.ChannelInfoViewState
9898
import io.getstream.chat.android.ui.common.state.messages.list.ChannelHeaderViewState
9999
import io.getstream.chat.android.ui.common.state.messages.list.DeletedMessageVisibility
100-
import io.getstream.chat.android.ui.common.utils.extensions.imagePreviewUrl
101100
import kotlinx.coroutines.GlobalScope
102101
import kotlinx.coroutines.delay
103102
import kotlinx.coroutines.flow.collectLatest
@@ -577,7 +576,7 @@ class ChatsActivity : ComponentActivity() {
577576
val viewModelFactory = ChannelAttachmentsViewModelFactory(
578577
cid = cid,
579578
attachmentTypes = listOf(AttachmentType.IMAGE, AttachmentType.VIDEO),
580-
localFilter = { !it.imagePreviewUrl.isNullOrEmpty() && it.titleLink.isNullOrEmpty() },
579+
localFilter = { !(it.imageUrl ?: it.thumbUrl).isNullOrEmpty() && it.titleLink.isNullOrEmpty() },
581580
)
582581
val viewModel = viewModel<ChannelAttachmentsViewModel>(
583582
factory = viewModelFactory,

stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/content/ImageAttachmentPreviewContent.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import io.getstream.chat.android.compose.ui.theme.ChatTheme
4242
import io.getstream.chat.android.compose.ui.util.AsyncImagePreviewHandler
4343
import io.getstream.chat.android.compose.ui.util.StreamAsyncImage
4444
import io.getstream.chat.android.models.Attachment
45-
import io.getstream.chat.android.ui.common.utils.extensions.imagePreviewUrl
4645

4746
/**
4847
* UI for currently selected image attachments, within the [MessageInput].
@@ -76,7 +75,7 @@ private fun ImageAttachmentPreviewContentItem(
7675
attachment: Attachment,
7776
onAttachmentRemoved: (Attachment) -> Unit,
7877
) {
79-
val data = attachment.upload ?: attachment.imagePreviewUrl
78+
val data = attachment.upload ?: attachment.imageUrl
8079

8180
Box(
8281
modifier = Modifier

stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/content/LinkAttachmentContent.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ import io.getstream.chat.android.compose.ui.util.AsyncImagePreviewHandler
6767
import io.getstream.chat.android.compose.ui.util.StreamAsyncImage
6868
import io.getstream.chat.android.models.Attachment
6969
import io.getstream.chat.android.models.Message
70-
import io.getstream.chat.android.ui.common.utils.extensions.imagePreviewUrl
70+
import io.getstream.chat.android.ui.common.utils.extensions.linkPreviewImageUrl
71+
import io.getstream.chat.android.ui.common.utils.extensions.linkUrl
7172
import io.getstream.chat.android.uiutils.extension.addSchemeToUrlIfNeeded
7273
import io.getstream.chat.android.uiutils.extension.hasLink
7374

@@ -137,7 +138,7 @@ public fun LinkAttachmentContent(
137138
"Missing link attachment."
138139
}
139140

140-
val previewUrl = attachment.titleLink ?: attachment.ogUrl
141+
val previewUrl = attachment.linkUrl
141142
val urlWithScheme = previewUrl?.addSchemeToUrlIfNeeded()
142143

143144
checkNotNull(previewUrl) {
@@ -179,8 +180,8 @@ public fun LinkAttachmentContent(
179180
onLongClick = { onLongItemClick(message) },
180181
),
181182
) {
182-
val imagePreviewUrl = attachment.imagePreviewUrl
183-
if (imagePreviewUrl != null) {
183+
val linkPreviewUrl = attachment.linkPreviewImageUrl
184+
if (linkPreviewUrl != null) {
184185
LinkAttachmentImagePreview(attachment, isMine)
185186
}
186187

@@ -198,7 +199,7 @@ public fun LinkAttachmentContent(
198199

199200
@Composable
200201
private fun LinkAttachmentImagePreview(attachment: Attachment, isMine: Boolean) {
201-
val data = attachment.imagePreviewUrl
202+
val data = attachment.linkPreviewImageUrl
202203
var maxWidth by remember { mutableStateOf(0.dp) }
203204

204205
Box(

stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/content/MediaAttachmentPreviewContent.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import androidx.compose.ui.tooling.preview.Preview
3737
import androidx.compose.ui.unit.dp
3838
import coil3.ColorImage
3939
import coil3.compose.LocalAsyncImagePreviewHandler
40+
import io.getstream.chat.android.client.utils.attachment.isImage
4041
import io.getstream.chat.android.compose.ui.attachments.factory.DefaultPreviewItemOverlayContent
4142
import io.getstream.chat.android.compose.ui.components.CancelIcon
4243
import io.getstream.chat.android.compose.ui.components.composer.MessageInput
@@ -45,7 +46,6 @@ import io.getstream.chat.android.compose.ui.util.AsyncImagePreviewHandler
4546
import io.getstream.chat.android.compose.ui.util.StreamAsyncImage
4647
import io.getstream.chat.android.models.Attachment
4748
import io.getstream.chat.android.models.AttachmentType
48-
import io.getstream.chat.android.ui.common.utils.extensions.imagePreviewUrl
4949

5050
/**
5151
* UI for currently selected image and video attachments, within the [MessageInput].
@@ -98,7 +98,8 @@ private fun MediaAttachmentPreviewItem(
9898
onAttachmentRemoved: (Attachment) -> Unit,
9999
overlayContent: @Composable (attachmentType: String?) -> Unit,
100100
) {
101-
val data = mediaAttachment.upload ?: mediaAttachment.imagePreviewUrl
101+
val data = mediaAttachment.upload
102+
?: if (mediaAttachment.isImage()) mediaAttachment.imageUrl else mediaAttachment.thumbUrl
102103

103104
Box(
104105
modifier = Modifier

stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/content/MediaAttachmentQuotedContent.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import io.getstream.chat.android.compose.ui.util.extensions.internal.imagePrevie
4949
import io.getstream.chat.android.models.Attachment
5050
import io.getstream.chat.android.models.AttachmentType
5151
import io.getstream.chat.android.ui.common.images.resizing.applyStreamCdnImageResizingIfEnabled
52-
import io.getstream.chat.android.ui.common.utils.extensions.imagePreviewUrl
52+
import io.getstream.chat.android.ui.common.utils.extensions.giphyFallbackPreviewUrl
5353
import java.io.File
5454

5555
/**
@@ -77,12 +77,9 @@ public fun MediaAttachmentQuotedContent(
7777

7878
val data =
7979
when {
80-
isGiphy ->
81-
attachment.imagePreviewUrl
80+
isGiphy -> attachment.giphyFallbackPreviewUrl
8281
isImageContent ->
83-
attachment.imagePreviewUrl
84-
?.applyStreamCdnImageResizingIfEnabled(ChatTheme.streamCdnImageResizing)
85-
82+
attachment.imageUrl?.applyStreamCdnImageResizingIfEnabled(ChatTheme.streamCdnImageResizing)
8683
else -> attachment.imagePreviewData
8784
}
8885

stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/preview/internal/MediaGalleryPage.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ import io.getstream.chat.android.compose.ui.theme.ChatTheme
6565
import io.getstream.chat.android.compose.ui.util.StreamAsyncImage
6666
import io.getstream.chat.android.compose.ui.util.clickable
6767
import io.getstream.chat.android.models.Attachment
68-
import io.getstream.chat.android.ui.common.utils.extensions.imagePreviewUrl
6968
import kotlinx.coroutines.coroutineScope
7069
import kotlin.math.abs
7170

@@ -103,7 +102,7 @@ internal fun MediaGalleryImagePage(
103102
modifier = Modifier.fillMaxSize(),
104103
contentAlignment = Alignment.Center,
105104
) {
106-
val data = attachment.imagePreviewUrl
105+
val data = attachment.imageUrl
107106
val context = LocalContext.current
108107

109108
// Ensure we have a new imageRequest in case the data changes

stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/channel/attachments/ChannelMediaAttachmentsScreen.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import androidx.compose.ui.tooling.preview.Preview
3939
import androidx.compose.ui.unit.dp
4040
import androidx.lifecycle.compose.collectAsStateWithLifecycle
4141
import androidx.lifecycle.viewmodel.compose.viewModel
42+
import io.getstream.chat.android.client.utils.attachment.isImage
4243
import io.getstream.chat.android.client.utils.attachment.isVideo
4344
import io.getstream.chat.android.compose.R
4445
import io.getstream.chat.android.compose.ui.components.avatar.UserAvatar
@@ -50,7 +51,6 @@ import io.getstream.chat.android.compose.viewmodel.channel.ChannelAttachmentsVie
5051
import io.getstream.chat.android.previewdata.PreviewMessageData
5152
import io.getstream.chat.android.ui.common.feature.channel.attachments.ChannelAttachmentsViewAction
5253
import io.getstream.chat.android.ui.common.state.channel.attachments.ChannelAttachmentsViewState
53-
import io.getstream.chat.android.ui.common.utils.extensions.imagePreviewUrl
5454
import io.getstream.result.Error
5555

5656
/**
@@ -141,7 +141,8 @@ internal fun LazyGridItemScope.ChannelMediaAttachmentsItem(
141141
item: ChannelAttachmentsViewState.Content.Item,
142142
onClick: () -> Unit,
143143
) {
144-
val data = item.attachment.upload ?: item.attachment.imagePreviewUrl
144+
val data = item.attachment.upload
145+
?: if (item.attachment.isImage()) item.attachment.imageUrl else item.attachment.thumbUrl
145146
Box(
146147
modifier = Modifier
147148
.clickable(onClick = onClick),

stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/components/composer/ComposerLinkPreview.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ import io.getstream.chat.android.compose.ui.util.AsyncImagePreviewHandler
6060
import io.getstream.chat.android.compose.ui.util.StreamAsyncImage
6161
import io.getstream.chat.android.models.Attachment
6262
import io.getstream.chat.android.models.LinkPreview
63-
import io.getstream.chat.android.ui.common.utils.extensions.imagePreviewUrl
63+
import io.getstream.chat.android.ui.common.utils.extensions.linkPreviewImageUrl
64+
import io.getstream.chat.android.ui.common.utils.extensions.linkUrl
6465
import io.getstream.chat.android.uiutils.extension.addSchemeToUrlIfNeeded
6566
import io.getstream.log.StreamLog
6667

@@ -91,7 +92,7 @@ public fun ComposerLinkPreview(
9192

9293
val context = LocalContext.current
9394
val attachment = linkPreview.attachment
94-
val previewUrl = attachment.titleLink ?: attachment.ogUrl
95+
val previewUrl = attachment.linkUrl
9596

9697
checkNotNull(previewUrl) {
9798
"Missing preview URL."
@@ -133,14 +134,14 @@ public fun ComposerLinkPreview(
133134

134135
@Composable
135136
private fun ComposerLinkImagePreview(attachment: Attachment) {
136-
val imagePreviewUrl = attachment.imagePreviewUrl ?: return
137+
val linkPreviewUrl = attachment.linkPreviewImageUrl ?: return
137138
val theme = ChatTheme.messageComposerTheme.linkPreview
138139
Box(
139140
modifier = Modifier.padding(theme.imagePadding),
140141
contentAlignment = Alignment.Center,
141142
) {
142143
StreamAsyncImage(
143-
data = imagePreviewUrl,
144+
data = linkPreviewUrl,
144145
modifier = Modifier
145146
.height(theme.imageSize.height)
146147
.width(theme.imageSize.width)
@@ -225,7 +226,7 @@ private fun ComposerLinkCancelIcon(
225226
* @param preview The preview of the link attachment being clicked.
226227
*/
227228
private fun onLinkPreviewClick(context: Context, preview: LinkPreview) {
228-
val previewUrl = preview.attachment.titleLink ?: preview.attachment.ogUrl
229+
val previewUrl = preview.attachment.linkUrl
229230
checkNotNull(previewUrl) {
230231
"Missing preview URL."
231232
}

stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/util/extensions/internal/AttachmentExtensions.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,26 @@ import io.getstream.chat.android.client.utils.attachment.isVideo
2222
import io.getstream.chat.android.compose.ui.theme.ChatTheme
2323
import io.getstream.chat.android.models.Attachment
2424
import io.getstream.chat.android.ui.common.images.resizing.applyStreamCdnImageResizingIfEnabled
25-
import io.getstream.chat.android.ui.common.utils.extensions.imagePreviewUrl
2625

2726
/**
2827
* This property checks if the attachment is an image or a video with enabled thumbnails.
29-
* If so, it returns the image preview URL (applied with Stream CDN image resizing if enabled)
28+
* If so, it returns the appropriate URL (applied with Stream CDN image resizing if enabled)
3029
* or the upload [java.io.File] object.
3130
* Otherwise, it returns null.
31+
*
32+
* For image attachments, [Attachment.imageUrl] is used.
33+
* For video attachments when thumbnails are enabled, [Attachment.thumbUrl] is used.
3234
*/
3335
@get:Composable
3436
internal val Attachment.imagePreviewData: Any?
35-
get() = if (isImage() || (isVideo() && ChatTheme.videoThumbnailsEnabled)) {
36-
imagePreviewUrl
37-
?.applyStreamCdnImageResizingIfEnabled(ChatTheme.streamCdnImageResizing)
38-
?: upload
39-
} else {
40-
null
37+
get() = when {
38+
isImage() ->
39+
imageUrl
40+
?.applyStreamCdnImageResizingIfEnabled(ChatTheme.streamCdnImageResizing)
41+
?: upload
42+
isVideo() && ChatTheme.videoThumbnailsEnabled ->
43+
thumbUrl
44+
?.applyStreamCdnImageResizingIfEnabled(ChatTheme.streamCdnImageResizing)
45+
?: upload
46+
else -> null
4147
}

0 commit comments

Comments
 (0)