11package com.example.chattutorial
22
33import android.view.LayoutInflater
4- import android.view.View
54import android.view.ViewGroup
65import coil.load
76import com.example.chattutorial.databinding.AttachmentImgurBinding
8- import com.getstream.sdk.chat.adapter.MessageListItem
97import io.getstream.chat.android.client.models.Attachment
8+ import io.getstream.chat.android.client.models.Message
109import io.getstream.chat.android.ui.message.list.adapter.MessageListListenerContainer
11- import io.getstream.chat.android.ui.message.list.adapter.viewholder.attachment.AttachmentViewFactory
12- import io.getstream.chat.android.ui.message.list.MessageListItemStyle
10+ import io.getstream.chat.android.ui.message.list.adapter.viewholder.attachment.AttachmentFactory
11+ import io.getstream.chat.android.ui.message.list.adapter.viewholder.attachment.InnerAttachmentViewHolder
1312
14- class ImgurAttachmentViewFactory : AttachmentViewFactory () {
13+ /* * A custom attachment factory to show an imgur logo if the attachment URL is an imgur image. */
14+ class ImgurAttachmentFactory : AttachmentFactory {
1515
16- override fun createAttachmentView (
17- data : MessageListItem .MessageItem ,
18- listeners : MessageListListenerContainer ? ,
19- style : MessageListItemStyle ,
20- parent : ViewGroup ,
21- ): View {
22- val imgurAttachment = data.message.attachments.firstOrNull { it.isImgurAttachment() }
23- return when {
24- imgurAttachment != null -> createImgurAttachmentView(imgurAttachment, parent)
25- else -> super .createAttachmentView(data, listeners, style, parent)
26- }
16+ override fun canHandle (message : Message ): Boolean {
17+ val imgurAttachment = message.attachments.firstOrNull { it.isImgurAttachment() }
18+ return imgurAttachment != null
2719 }
2820
29- private fun Attachment.isImgurAttachment (): Boolean = imageUrl?.contains(" imgur" ) == true
30-
31- private fun createImgurAttachmentView (imgurAttachment : Attachment , parent : ViewGroup ): View {
21+ override fun createViewHolder (
22+ message : Message ,
23+ listeners : MessageListListenerContainer ? ,
24+ parent : ViewGroup
25+ ): InnerAttachmentViewHolder {
26+ val imgurAttachment = message.attachments.firstOrNull { it.isImgurAttachment() }
27+ ? : return createViewHolder(message, listeners, parent)
3228 val binding = AttachmentImgurBinding
3329 .inflate(LayoutInflater .from(parent.context), null , false )
30+ return ImgurAttachmentViewHolder (
31+ imgurAttachment = imgurAttachment,
32+ binding = binding
33+ )
34+ }
35+
36+ private fun Attachment.isImgurAttachment (): Boolean = imageUrl?.contains(" imgur" ) == true
3437
35- binding.ivMediaThumb.apply {
36- shapeAppearanceModel = shapeAppearanceModel
37- .toBuilder()
38- .setAllCornerSizes(resources.getDimension(R .dimen.stream_ui_selected_attachment_corner_radius))
39- .build()
40- load(imgurAttachment.imageUrl) {
41- allowHardware(false )
42- crossfade(true )
43- placeholder(R .drawable.stream_ui_picture_placeholder)
38+ private class ImgurAttachmentViewHolder (
39+ binding : AttachmentImgurBinding ,
40+ imgurAttachment : Attachment
41+ ) :
42+ InnerAttachmentViewHolder (binding.root) {
43+
44+ init {
45+ binding.ivMediaThumb.apply {
46+ shapeAppearanceModel = shapeAppearanceModel
47+ .toBuilder()
48+ .setAllCornerSizes(resources.getDimension(R .dimen.stream_ui_selected_attachment_corner_radius))
49+ .build()
50+ load(imgurAttachment.imageUrl) {
51+ allowHardware(false )
52+ crossfade(true )
53+ placeholder(R .drawable.stream_ui_picture_placeholder)
54+ }
4455 }
4556 }
46-
47- return binding.root
4857 }
49-
50- }
58+ }
0 commit comments