diff --git a/stream-chat-android-compose/api/stream-chat-android-compose.api b/stream-chat-android-compose/api/stream-chat-android-compose.api index 680e226b11f..b94a9d4fb95 100644 --- a/stream-chat-android-compose/api/stream-chat-android-compose.api +++ b/stream-chat-android-compose/api/stream-chat-android-compose.api @@ -580,18 +580,18 @@ public final class io/getstream/chat/android/compose/ui/attachments/content/Medi public static final field $stable I public final fun component1 ()Landroidx/activity/compose/ManagedActivityResultLauncher; public final fun component2 ()Lio/getstream/chat/android/models/Message; - public final fun component3 ()I + public final fun component3 ()Ljava/lang/String; public final fun component4 ()Z public final fun component5 ()Lio/getstream/chat/android/ui/common/helper/DownloadAttachmentUriGenerator; public final fun component6 ()Lio/getstream/chat/android/ui/common/helper/DownloadRequestInterceptor; public final fun component7 ()Lio/getstream/chat/android/ui/common/images/resizing/StreamCdnImageResizing; public final fun component8 ()Z public fun equals (Ljava/lang/Object;)Z - public final fun getAttachmentPosition ()I public final fun getDownloadAttachmentUriGenerator ()Lio/getstream/chat/android/ui/common/helper/DownloadAttachmentUriGenerator; public final fun getDownloadRequestInterceptor ()Lio/getstream/chat/android/ui/common/helper/DownloadRequestInterceptor; public final fun getMediaGalleryPreviewLauncher ()Landroidx/activity/compose/ManagedActivityResultLauncher; public final fun getMessage ()Lio/getstream/chat/android/models/Message; + public final fun getSelectedAttachmentUrl ()Ljava/lang/String; public final fun getSkipEnrichUrl ()Z public final fun getStreamCdnImageResizing ()Lio/getstream/chat/android/ui/common/images/resizing/StreamCdnImageResizing; public final fun getVideoThumbnailsEnabled ()Z @@ -682,8 +682,8 @@ public final class io/getstream/chat/android/compose/ui/attachments/preview/Medi } public final class io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewActivity$Companion { - public final fun getIntent (Landroid/content/Context;Lio/getstream/chat/android/models/Message;IZLio/getstream/chat/android/ui/common/helper/DownloadAttachmentUriGenerator;Lio/getstream/chat/android/ui/common/helper/DownloadRequestInterceptor;Lio/getstream/chat/android/ui/common/images/resizing/StreamCdnImageResizing;ZLio/getstream/chat/android/compose/ui/theme/MediaGalleryConfig;)Landroid/content/Intent; - public static synthetic fun getIntent$default (Lio/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewActivity$Companion;Landroid/content/Context;Lio/getstream/chat/android/models/Message;IZLio/getstream/chat/android/ui/common/helper/DownloadAttachmentUriGenerator;Lio/getstream/chat/android/ui/common/helper/DownloadRequestInterceptor;Lio/getstream/chat/android/ui/common/images/resizing/StreamCdnImageResizing;ZLio/getstream/chat/android/compose/ui/theme/MediaGalleryConfig;ILjava/lang/Object;)Landroid/content/Intent; + public final fun getIntent (Landroid/content/Context;Lio/getstream/chat/android/models/Message;Ljava/lang/String;ZLio/getstream/chat/android/ui/common/helper/DownloadAttachmentUriGenerator;Lio/getstream/chat/android/ui/common/helper/DownloadRequestInterceptor;Lio/getstream/chat/android/ui/common/images/resizing/StreamCdnImageResizing;ZLio/getstream/chat/android/compose/ui/theme/MediaGalleryConfig;)Landroid/content/Intent; + public static synthetic fun getIntent$default (Lio/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewActivity$Companion;Landroid/content/Context;Lio/getstream/chat/android/models/Message;Ljava/lang/String;ZLio/getstream/chat/android/ui/common/helper/DownloadAttachmentUriGenerator;Lio/getstream/chat/android/ui/common/helper/DownloadRequestInterceptor;Lio/getstream/chat/android/ui/common/images/resizing/StreamCdnImageResizing;ZLio/getstream/chat/android/compose/ui/theme/MediaGalleryConfig;ILjava/lang/Object;)Landroid/content/Intent; } public final class io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewContract : androidx/activity/result/contract/ActivityResultContract { @@ -699,20 +699,20 @@ public final class io/getstream/chat/android/compose/ui/attachments/preview/Medi public final class io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewContract$Input { public static final field $stable I - public fun (Lio/getstream/chat/android/models/Message;IZLio/getstream/chat/android/ui/common/helper/DownloadAttachmentUriGenerator;Lio/getstream/chat/android/ui/common/helper/DownloadRequestInterceptor;Lio/getstream/chat/android/ui/common/images/resizing/StreamCdnImageResizing;Z)V - public synthetic fun (Lio/getstream/chat/android/models/Message;IZLio/getstream/chat/android/ui/common/helper/DownloadAttachmentUriGenerator;Lio/getstream/chat/android/ui/common/helper/DownloadRequestInterceptor;Lio/getstream/chat/android/ui/common/images/resizing/StreamCdnImageResizing;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/getstream/chat/android/models/Message;Ljava/lang/String;ZLio/getstream/chat/android/ui/common/helper/DownloadAttachmentUriGenerator;Lio/getstream/chat/android/ui/common/helper/DownloadRequestInterceptor;Lio/getstream/chat/android/ui/common/images/resizing/StreamCdnImageResizing;Z)V + public synthetic fun (Lio/getstream/chat/android/models/Message;Ljava/lang/String;ZLio/getstream/chat/android/ui/common/helper/DownloadAttachmentUriGenerator;Lio/getstream/chat/android/ui/common/helper/DownloadRequestInterceptor;Lio/getstream/chat/android/ui/common/images/resizing/StreamCdnImageResizing;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun getDownloadAttachmentUriGenerator ()Lio/getstream/chat/android/ui/common/helper/DownloadAttachmentUriGenerator; public final fun getDownloadRequestInterceptor ()Lio/getstream/chat/android/ui/common/helper/DownloadRequestInterceptor; - public final fun getInitialPosition ()I public final fun getMessage ()Lio/getstream/chat/android/models/Message; + public final fun getSelectedAttachmentUrl ()Ljava/lang/String; public final fun getSkipEnrichUrl ()Z public final fun getStreamCdnImageResizing ()Lio/getstream/chat/android/ui/common/images/resizing/StreamCdnImageResizing; public final fun getVideoThumbnailsEnabled ()Z } public final class io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewScreenKt { - public static final fun MediaGalleryPreviewScreen (Lio/getstream/chat/android/compose/viewmodel/mediapreview/MediaGalleryPreviewViewModel;ILkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;Lio/getstream/chat/android/compose/ui/theme/MediaGalleryConfig;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function7;Lkotlin/jvm/functions/Function5;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;III)V - public static final fun MediaGalleryPreviewScreen (Lio/getstream/chat/android/models/Message;Lio/getstream/chat/android/models/ConnectionState;Lio/getstream/chat/android/models/User;ILio/getstream/chat/android/models/Attachment;ZZZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;Lio/getstream/chat/android/compose/ui/theme/MediaGalleryConfig;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function7;Lkotlin/jvm/functions/Function5;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;IIII)V + public static final fun MediaGalleryPreviewScreen (Lio/getstream/chat/android/compose/viewmodel/mediapreview/MediaGalleryPreviewViewModel;Ljava/lang/String;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;Lio/getstream/chat/android/compose/ui/theme/MediaGalleryConfig;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function7;Lkotlin/jvm/functions/Function5;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;III)V + public static final fun MediaGalleryPreviewScreen (Lio/getstream/chat/android/models/Message;Lio/getstream/chat/android/models/ConnectionState;Lio/getstream/chat/android/models/User;Ljava/lang/String;Lio/getstream/chat/android/models/Attachment;ZZZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;Lio/getstream/chat/android/compose/ui/theme/MediaGalleryConfig;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function7;Lkotlin/jvm/functions/Function5;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;IIII)V } public final class io/getstream/chat/android/compose/ui/attachments/preview/MediaPreviewActivity : androidx/appcompat/app/AppCompatActivity { diff --git a/stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/content/MediaAttachmentContent.kt b/stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/content/MediaAttachmentContent.kt index 2f7fda7d1b2..9fc2e272a7e 100644 --- a/stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/content/MediaAttachmentContent.kt +++ b/stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/content/MediaAttachmentContent.kt @@ -100,6 +100,7 @@ import io.getstream.chat.android.ui.common.helper.DownloadAttachmentUriGenerator import io.getstream.chat.android.ui.common.helper.DownloadRequestInterceptor import io.getstream.chat.android.ui.common.images.resizing.StreamCdnImageResizing import io.getstream.chat.android.ui.common.utils.extensions.hasLink +import io.getstream.chat.android.ui.common.utils.extensions.imagePreviewUrl /** * Displays a preview of single or multiple video or attachments. @@ -139,7 +140,7 @@ public fun MediaAttachmentContent( onItemClick: ( mediaGalleryPreviewLauncher: ManagedActivityResultLauncher, message: Message, - attachmentPosition: Int, + selectedAttachmentUrl: String?, videoThumbnailsEnabled: Boolean, downloadAttachmentUriGenerator: DownloadAttachmentUriGenerator, downloadRequestInterceptor: DownloadRequestInterceptor, @@ -161,7 +162,7 @@ public fun MediaAttachmentContent( onItemClick( it.mediaGalleryPreviewLauncher, it.message, - it.attachmentPosition, + it.selectedAttachmentUrl, it.videoThumbnailsEnabled, it.downloadAttachmentUriGenerator, it.downloadRequestInterceptor, @@ -198,7 +199,7 @@ public fun MediaAttachmentContent( onMediaAttachmentContentItemClick( it.mediaGalleryPreviewLauncher, it.message, - it.attachmentPosition, + it.selectedAttachmentUrl, it.videoThumbnailsEnabled, it.downloadAttachmentUriGenerator, it.downloadRequestInterceptor, @@ -314,7 +315,6 @@ internal fun SingleMediaAttachment( ), shape = if (shouldBeFullSize) null else RoundedCornerShape(StreamTokens.radiusLg), message = message, - attachmentPosition = 0, onMediaGalleryPreviewResult = onMediaGalleryPreviewResult, onLongItemClick = onLongItemClick, skipEnrichUrl = skipEnrichUrl, @@ -380,7 +380,6 @@ internal fun RowScope.MultipleMediaAttachmentsColumns( shape = shape, message = message, skipEnrichUrl = skipEnrichUrl, - attachmentPosition = positionInColumn, onMediaGalleryPreviewResult = onMediaGalleryPreviewResult, onLongItemClick = onLongItemClick, onItemClick = onContentItemClick, @@ -418,7 +417,6 @@ internal fun RowScope.MultipleMediaAttachmentsColumns( shape = shape, message = message, skipEnrichUrl = skipEnrichUrl, - attachmentPosition = attachmentIndex, forceShimmer = anyOverflowUploading, onMediaGalleryPreviewResult = onMediaGalleryPreviewResult, onLongItemClick = onLongItemClick, @@ -440,7 +438,6 @@ internal fun RowScope.MultipleMediaAttachmentsColumns( shape = shape, message = message, skipEnrichUrl = skipEnrichUrl, - attachmentPosition = attachmentIndex, onMediaGalleryPreviewResult = onMediaGalleryPreviewResult, onLongItemClick = onLongItemClick, onItemClick = onContentItemClick, @@ -474,9 +471,6 @@ private fun attachmentShape( * Displays previews of image and video attachments. * * @param message The original message containing the attachments. - * @param attachmentPosition The position of the attachment in the list - * of attachments. Used to remember the item position when viewing it in a separate - * activity. * @param attachment The attachment that is previewed. * @param skipEnrichUrl Used by the media gallery. If set to true will skip enriching URLs when you update the message * by deleting an attachment contained within it. Set to false by default. @@ -494,7 +488,6 @@ private fun attachmentShape( @Composable internal fun MediaAttachmentContentItem( message: Message, - attachmentPosition: Int, attachment: Attachment, skipEnrichUrl: Boolean, shape: Shape?, @@ -558,14 +551,14 @@ internal fun MediaAttachmentContentItem( if (message.syncStatus == SyncStatus.COMPLETED) { onItemClick( MediaAttachmentClickData( - mixedMediaPreviewLauncher, - message, - attachmentPosition, - videoThumbnailsEnabled, - downloadAttachmentUriGenerator, - downloadRequestInterceptor, - streamCdnImageResizing, - skipEnrichUrl, + mediaGalleryPreviewLauncher = mixedMediaPreviewLauncher, + message = message, + selectedAttachmentUrl = attachment.imagePreviewUrl, + videoThumbnailsEnabled = videoThumbnailsEnabled, + downloadAttachmentUriGenerator = downloadAttachmentUriGenerator, + downloadRequestInterceptor = downloadRequestInterceptor, + streamCdnImageResizing = streamCdnImageResizing, + skipEnrichUrl = skipEnrichUrl, ), ) } else { @@ -696,7 +689,7 @@ internal fun MediaAttachmentShowMoreOverlay( * * @param mediaGalleryPreviewLauncher The [ManagedActivityResultLauncher] to launch the media gallery. * @param message The message that holds the clicked attachment. - * @param attachmentPosition The position of the clicked attachment in the message's attachments list. + * @param selectedAttachmentUrl The preview URL of the clicked attachment. * @param videoThumbnailsEnabled Whether video thumbnails are enabled. * @param downloadAttachmentUriGenerator The [DownloadAttachmentUriGenerator] used to generate URIs for downloading * attachments. @@ -709,7 +702,7 @@ internal fun MediaAttachmentShowMoreOverlay( public data class MediaAttachmentClickData internal constructor( val mediaGalleryPreviewLauncher: ManagedActivityResultLauncher, val message: Message, - val attachmentPosition: Int, + val selectedAttachmentUrl: String?, val videoThumbnailsEnabled: Boolean, val downloadAttachmentUriGenerator: DownloadAttachmentUriGenerator, val downloadRequestInterceptor: DownloadRequestInterceptor, @@ -746,7 +739,7 @@ private const val EqualDimensionsRatio = 1f * @param mediaGalleryPreviewLauncher The launcher used for launching the media gallery after * clicking on an attachment. * @param message The message which contains the attachment. - * @param attachmentPosition The position (inside the message) of the attachment being clicked on. + * @param selectedAttachmentUrl The preview URL of the attachment being clicked on. * @param skipEnrichUrl Whether the URL should skip being enriched, i.e. rendered as * a link attachment. Used when updating the message from the gallery by doing actions * such as deleting an attachment. @@ -755,7 +748,7 @@ private const val EqualDimensionsRatio = 1f internal fun onMediaAttachmentContentItemClick( mediaGalleryPreviewLauncher: ManagedActivityResultLauncher, message: Message, - attachmentPosition: Int, + selectedAttachmentUrl: String?, videoThumbnailsEnabled: Boolean, downloadAttachmentUriGenerator: DownloadAttachmentUriGenerator, downloadRequestInterceptor: DownloadRequestInterceptor, @@ -765,7 +758,7 @@ internal fun onMediaAttachmentContentItemClick( mediaGalleryPreviewLauncher.launch( Input( message = message, - initialPosition = attachmentPosition, + selectedAttachmentUrl = selectedAttachmentUrl, videoThumbnailsEnabled = videoThumbnailsEnabled, downloadAttachmentUriGenerator = downloadAttachmentUriGenerator, downloadRequestInterceptor = downloadRequestInterceptor, diff --git a/stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewActivity.kt b/stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewActivity.kt index b0b40fdbbc5..8e193efe045 100644 --- a/stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewActivity.kt +++ b/stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewActivity.kt @@ -152,7 +152,7 @@ public class MediaGalleryPreviewActivity : AppCompatActivity() { } } - val attachmentPosition = intent?.getIntExtra(KeyAttachmentPosition, 0) ?: 0 + val selectedAttachmentUrl = intent?.getStringExtra(KeySelectedAttachmentUrl) setContent { ChatTheme( @@ -180,7 +180,7 @@ public class MediaGalleryPreviewActivity : AppCompatActivity() { MediaGalleryPreviewScreen( modifier = Modifier.windowInsetsPadding(WindowInsets.systemBars), viewModel = mediaGalleryPreviewViewModel, - initialPage = attachmentPosition, + selectedAttachmentUrl = selectedAttachmentUrl, onHeaderLeadingContentClick = ::finish, onOptionClick = { attachment, option -> handleMediaAction( @@ -475,9 +475,9 @@ public class MediaGalleryPreviewActivity : AppCompatActivity() { private const val KeyStreamCdnResizeImageCropMode: String = "streamCdnResizeImageCropMode" /** - * Represents the key for the starting attachment position based on the clicked attachment. + * Represents the key for the preview URL of the selected attachment. */ - private const val KeyAttachmentPosition: String = "attachmentPosition" + private const val KeySelectedAttachmentUrl: String = "selectedAttachmentUrl" /** * Represents the key for the result of the preview, like scrolling to the message. @@ -511,7 +511,7 @@ public class MediaGalleryPreviewActivity : AppCompatActivity() { * * @param context The context to start the activity with. * @param message The [Message] containing the attachments. - * @param attachmentPosition The initial position of the clicked media attachment. + * @param selectedAttachmentUrl The preview URL of the selected attachment to display first. * @param videoThumbnailsEnabled Whether video thumbnails will be displayed in previews or not. * @param downloadAttachmentUriGenerator Used to generate download URIs for attachments. * @param downloadRequestInterceptor Used to intercept download requests. @@ -526,7 +526,7 @@ public class MediaGalleryPreviewActivity : AppCompatActivity() { public fun getIntent( context: Context, message: Message, - attachmentPosition: Int, + selectedAttachmentUrl: String?, videoThumbnailsEnabled: Boolean, downloadAttachmentUriGenerator: DownloadAttachmentUriGenerator, downloadRequestInterceptor: DownloadRequestInterceptor, @@ -540,7 +540,7 @@ public class MediaGalleryPreviewActivity : AppCompatActivity() { val mediaGalleryPreviewActivityState = message.toMediaGalleryPreviewActivityState() putExtra(KeyMediaGalleryPreviewActivityState, mediaGalleryPreviewActivityState) - putExtra(KeyAttachmentPosition, attachmentPosition) + putExtra(KeySelectedAttachmentUrl, selectedAttachmentUrl) putExtra(KeyVideoThumbnailsEnabled, videoThumbnailsEnabled) // Image resizing options diff --git a/stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewContract.kt b/stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewContract.kt index 228583b9592..d96f6985839 100644 --- a/stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewContract.kt +++ b/stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewContract.kt @@ -37,7 +37,6 @@ public class MediaGalleryPreviewContract(private val config: MediaGalleryConfig /** * Creates the intent to start the [MediaGalleryPreviewActivity]. - * It receives a data pair of a [String] and an [Int] that represent the messageId and the attachmentPosition. * * @return The [Intent] to start the [MediaGalleryPreviewActivity]. */ @@ -45,7 +44,7 @@ public class MediaGalleryPreviewContract(private val config: MediaGalleryConfig return MediaGalleryPreviewActivity.getIntent( context, message = input.message, - attachmentPosition = input.initialPosition, + selectedAttachmentUrl = input.selectedAttachmentUrl, downloadAttachmentUriGenerator = input.downloadAttachmentUriGenerator, downloadRequestInterceptor = input.downloadRequestInterceptor, videoThumbnailsEnabled = input.videoThumbnailsEnabled, @@ -68,7 +67,7 @@ public class MediaGalleryPreviewContract(private val config: MediaGalleryConfig * Defines the input for the [MediaGalleryPreviewContract]. * * @param message The message containing the attachments. - * @param initialPosition The initial position of the media gallery, based on the clicked item. + * @param selectedAttachmentUrl The preview URL of the selected attachment to display first. * @param downloadAttachmentUriGenerator The URI generator for downloading attachments. * @param downloadRequestInterceptor The request interceptor for downloading attachments. * @param videoThumbnailsEnabled Whether video thumbnails will be displayed in previews or not. @@ -78,7 +77,7 @@ public class MediaGalleryPreviewContract(private val config: MediaGalleryConfig */ public class Input( public val message: Message, - public val initialPosition: Int = 0, + public val selectedAttachmentUrl: String? = null, public val videoThumbnailsEnabled: Boolean, public val downloadAttachmentUriGenerator: DownloadAttachmentUriGenerator, public val downloadRequestInterceptor: DownloadRequestInterceptor, diff --git a/stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewScreen.kt b/stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewScreen.kt index 3dff6d135c8..e3e88a0ffbb 100644 --- a/stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewScreen.kt +++ b/stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewScreen.kt @@ -91,6 +91,7 @@ import io.getstream.chat.android.models.Constants import io.getstream.chat.android.models.Message import io.getstream.chat.android.models.User import io.getstream.chat.android.ui.common.utils.extensions.hasLink +import io.getstream.chat.android.ui.common.utils.extensions.imagePreviewUrl import kotlinx.coroutines.launch import java.util.Date @@ -108,7 +109,8 @@ import java.util.Date * - Trailing content (photos/gallery icon) * * @param viewModel The [MediaGalleryPreviewViewModel] instance to use for managing the state of the screen. - * @param initialPage The initial page to display in the pager. + * @param selectedAttachmentUrl The preview URL of the initially selected attachment. Used to resolve the starting + * page in the pager by matching against the filtered attachments list. * @param onHeaderLeadingContentClick Callback to be invoked when the leading content in the header is clicked. Usually * closes the screen. * @param onOptionClick Callback to be invoked when an option in the options menu is clicked. @@ -135,7 +137,7 @@ import java.util.Date @Composable public fun MediaGalleryPreviewScreen( viewModel: MediaGalleryPreviewViewModel, - initialPage: Int, + selectedAttachmentUrl: String?, onHeaderLeadingContentClick: () -> Unit, onOptionClick: (Attachment, MediaGalleryPreviewOption) -> Unit, onRequestShareAttachment: (Attachment) -> Unit, @@ -209,7 +211,7 @@ public fun MediaGalleryPreviewScreen( message = viewModel.message, connectionState = viewModel.connectionState, currentUser = user, - initialPage = initialPage, + selectedAttachmentUrl = selectedAttachmentUrl, promptedAttachment = viewModel.promptedAttachment, isSharingInProgress = viewModel.isSharingInProgress, isShowingOptions = viewModel.isShowingOptions, @@ -249,7 +251,8 @@ public fun MediaGalleryPreviewScreen( * @param message The message containing the attachments to be previewed. * @param connectionState TThe network connection state. * @param currentUser The currently logged user. - * @param initialPage The initial page to display in the pager. + * @param selectedAttachmentUrl The preview URL of the initially selected attachment. Used to resolve the starting + * page in the pager by matching against the filtered attachments list. * @param onHeaderLeadingContentClick Callback to be invoked when the leading content in the header is clicked. Usually * closes the screen. * @param onOptionClick Callback to be invoked when an option in the options menu is clicked. @@ -278,7 +281,7 @@ public fun MediaGalleryPreviewScreen( message: Message, connectionState: ConnectionState, currentUser: User?, - initialPage: Int, + selectedAttachmentUrl: String?, promptedAttachment: Attachment?, isSharingInProgress: Boolean, isShowingOptions: Boolean, @@ -353,12 +356,19 @@ public fun MediaGalleryPreviewScreen( ) { // Filters out non-media and link attachments. Pass this value along to all children // Composable-s that read message attachments to prevent inconsistent state. - val filteredAttachments = remember(message) { + val filteredAttachments = remember(message.attachments) { message.attachments.filter { attachment -> !attachment.hasLink() && (attachment.isImage() || attachment.isVideo()) } } - val startingPosition = if (initialPage !in filteredAttachments.indices) 0 else initialPage + val startingPosition = if (selectedAttachmentUrl == null) { + 0 + } else { + filteredAttachments + .indexOfFirst { it.imagePreviewUrl == selectedAttachmentUrl } + .coerceAtLeast(0) + } + val pagerState = rememberPagerState( initialPage = startingPosition, pageCount = { filteredAttachments.size }, @@ -1163,7 +1173,7 @@ private fun MediaGalleryPreviewScreenPreview() { message = message, connectionState = ConnectionState.Connected, currentUser = user, - initialPage = 0, + selectedAttachmentUrl = null, promptedAttachment = null, isShowingOptions = true, isShowingGallery = false, diff --git a/stream-chat-android-compose/src/test/kotlin/io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewScreenTest.kt b/stream-chat-android-compose/src/test/kotlin/io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewScreenTest.kt index 88b189584fa..a0af970f1e5 100644 --- a/stream-chat-android-compose/src/test/kotlin/io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewScreenTest.kt +++ b/stream-chat-android-compose/src/test/kotlin/io/getstream/chat/android/compose/ui/attachments/preview/MediaGalleryPreviewScreenTest.kt @@ -115,7 +115,7 @@ internal class MediaGalleryPreviewScreenTest : PaparazziComposeTest { message = message, connectionState = ConnectionState.Offline, currentUser = message.user, - initialPage = 0, + selectedAttachmentUrl = null, promptedAttachment = null, isSharingInProgress = false, isShowingOptions = false, @@ -132,7 +132,7 @@ internal class MediaGalleryPreviewScreenTest : PaparazziComposeTest { message = message, connectionState = ConnectionState.Connected, currentUser = message.user, - initialPage = 0, + selectedAttachmentUrl = null, promptedAttachment = null, isSharingInProgress = false, isShowingOptions = false, @@ -149,7 +149,7 @@ internal class MediaGalleryPreviewScreenTest : PaparazziComposeTest { message = message, connectionState = ConnectionState.Connected, currentUser = message.user, - initialPage = 0, + selectedAttachmentUrl = null, promptedAttachment = null, isSharingInProgress = false, isShowingOptions = true, @@ -166,7 +166,7 @@ internal class MediaGalleryPreviewScreenTest : PaparazziComposeTest { message = message, connectionState = ConnectionState.Connected, currentUser = message.user, - initialPage = 0, + selectedAttachmentUrl = null, promptedAttachment = message.attachments[0], isSharingInProgress = false, isShowingOptions = false, @@ -183,7 +183,7 @@ internal class MediaGalleryPreviewScreenTest : PaparazziComposeTest { message = message, connectionState = ConnectionState.Connected, currentUser = message.user, - initialPage = 0, + selectedAttachmentUrl = null, promptedAttachment = null, isSharingInProgress = false, isShowingOptions = false,