Skip to content

Commit 7695373

Browse files
committed
Update tutorial to use AttachmentViewFactory for customization
1 parent 394f3df commit 7695373

16 files changed

+167
-229
lines changed

samplejava/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ android {
3535

3636
dependencies {
3737
// Add new dependencies
38-
implementation "io.getstream:stream-chat-android-ui-components:4.6.0"
38+
implementation "io.getstream:stream-chat-android-ui-components:4.7.0"
3939
implementation 'io.coil-kt:coil:1.1.1'
4040

4141
implementation 'androidx.appcompat:appcompat:1.2.0'

samplejava/src/main/java/com/example/chattutorial/ChannelActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5656
MessageListViewModel messageListViewModel = provider.get(MessageListViewModel.class);
5757
MessageInputViewModel messageInputViewModel = provider.get(MessageInputViewModel.class);
5858

59-
// TODO set custom Imgur ViewHolderFactory
59+
// TODO set custom Imgur attachment factory
6060

6161
// Step 2 - Bind the view and ViewModels, they are loosely coupled so it's easy to customize
6262
MessageListHeaderViewModelBinding.bind(messageListHeaderViewModel, binding.messageListHeaderView, this);

samplejava/src/main/java/com/example/chattutorial/ChannelActivity2.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5656
MessageListViewModel messageListViewModel = provider.get(MessageListViewModel.class);
5757
MessageInputViewModel messageInputViewModel = provider.get(MessageInputViewModel.class);
5858

59-
// Set view holder factory for Imgur attachments
60-
binding.messageListView.setMessageViewHolderFactory(new ImgurAttachmentViewHolderFactory());
59+
// Set view factory for Imgur attachments
60+
binding.messageListView.setAttachmentViewFactory(new ImgurAttachmentViewFactory());
6161

6262
// Step 2 - Bind the view and ViewModels, they are loosely coupled so it's easy to customize
6363
MessageListHeaderViewModelBinding.bind(messageListHeaderViewModel, binding.messageListHeaderView, this);

samplejava/src/main/java/com/example/chattutorial/ChannelActivity3.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
6464
MessageListViewModel messageListViewModel = provider.get(MessageListViewModel.class);
6565
MessageInputViewModel messageInputViewModel = provider.get(MessageInputViewModel.class);
6666

67-
// Set custom AttachmentViewHolderFactory
68-
binding.messageListView.setMessageViewHolderFactory(new ImgurAttachmentViewHolderFactory());
67+
// Set view factory for Imgur attachments
68+
binding.messageListView.setAttachmentViewFactory(new ImgurAttachmentViewFactory());
6969

7070
// Step 2 - Bind the view and ViewModels, they are loosely coupled so it's easy to customize
7171
MessageListHeaderViewModelBinding.bind(messageListHeaderViewModel, binding.messageListHeaderView, this);

samplejava/src/main/java/com/example/chattutorial/ChannelActivity4.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
6565
MessageListViewModel messageListViewModel = provider.get(MessageListViewModel.class);
6666
MessageInputViewModel messageInputViewModel = provider.get(MessageInputViewModel.class);
6767

68-
// Set custom AttachmentViewHolderFactory
69-
binding.messageListView.setMessageViewHolderFactory(new ImgurAttachmentViewHolderFactory());
68+
// Set view factory for Imgur attachments
69+
binding.messageListView.setAttachmentViewFactory(new ImgurAttachmentViewFactory());
7070

7171
// Step 2 - Bind the view and ViewModels, they are loosely coupled so it's easy to customize
7272
MessageListHeaderViewModelBinding.bind(messageListHeaderViewModel, binding.messageListHeaderView, this);
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.example.chattutorial;
2+
3+
import android.content.Context;
4+
import android.view.LayoutInflater;
5+
import android.view.View;
6+
import android.view.ViewGroup;
7+
import android.widget.ImageView;
8+
9+
import com.getstream.sdk.chat.adapter.MessageListItem;
10+
import com.example.chattutorial.databinding.ListItemAttachmentImgurBinding;
11+
import com.google.android.material.shape.ShapeAppearanceModel;
12+
13+
import org.jetbrains.annotations.NotNull;
14+
15+
import coil.Coil;
16+
import coil.request.ImageRequest;
17+
import io.getstream.chat.android.client.models.Attachment;
18+
import io.getstream.chat.android.ui.message.list.adapter.MessageListListenerContainer;
19+
import io.getstream.chat.android.ui.message.list.adapter.viewholder.attachment.AttachmentViewFactory;
20+
import io.getstream.chat.android.ui.message.list.internal.MessageListItemStyle;
21+
22+
class ImgurAttachmentViewFactory extends AttachmentViewFactory {
23+
24+
@NotNull
25+
@Override
26+
public View createAttachmentView(@NotNull MessageListItem.MessageItem data,
27+
@NotNull MessageListListenerContainer listeners,
28+
@NotNull MessageListItemStyle style,
29+
@NotNull View parent) {
30+
Attachment imgurAttachment = null;
31+
for (Attachment attachment : data.getMessage().getAttachments()) {
32+
String imageUrl = attachment.getImageUrl();
33+
if (imageUrl != null && imageUrl.contains("imgur")) {
34+
imgurAttachment = attachment;
35+
break;
36+
}
37+
}
38+
39+
if (imgurAttachment != null) {
40+
return createImgurAttachmentView(imgurAttachment, parent);
41+
} else {
42+
return super.createAttachmentView(data, listeners, style, parent);
43+
}
44+
}
45+
46+
private View createImgurAttachmentView(Attachment imgurAttachment, ViewGroup parent) {
47+
ListItemAttachmentImgurBinding binding = ListItemAttachmentImgurBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
48+
49+
float cornerRadius = binding.getRoot()
50+
.getResources()
51+
.getDimension(R.dimen.stream_ui_selected_attachment_corner_radius);
52+
ShapeAppearanceModel model = binding.ivMediaThumb.getShapeAppearanceModel()
53+
.toBuilder()
54+
.setAllCornerSizes(cornerRadius)
55+
.build();
56+
binding.ivMediaThumb.setShapeAppearanceModel(model);
57+
58+
ImageRequest imageRequest = new ImageRequest.Builder(parent.getContext())
59+
.data(imgurAttachment.getImageUrl())
60+
.allowHardware(false)
61+
.crossfade(true)
62+
.placeholder(R.drawable.stream_ui_picture_placeholder)
63+
.target(binding.ivMediaThumb)
64+
.build();
65+
Coil.imageLoader(parent.getContext()).enqueue(imageRequest);
66+
67+
return binding.getRoot();
68+
}
69+
}
70+
71+
72+
class MyAttachmentViewFactory extends AttachmentViewFactory {
73+
74+
private static final String MY_URL_ADDRESS = "https://myurl.com";
75+
76+
@NotNull
77+
@Override
78+
public View createAttachmentView(
79+
@NotNull MessageListItem.MessageItem data,
80+
@NotNull MessageListListenerContainer listeners,
81+
@NotNull MessageListItemStyle style,
82+
@NotNull View parent
83+
) {
84+
boolean containsMyAttachments = false;
85+
for (Attachment attachment: data.getMessage().getAttachments()) {
86+
if (attachment.getImageUrl().contains(MY_URL_ADDRESS)) {
87+
containsMyAttachments = true;
88+
}
89+
}
90+
91+
if (containsMyAttachments) {
92+
// put your custom attachment view creation here
93+
return new View(parent.getContext());
94+
} else {
95+
return super.createAttachmentView(data, listeners, style, parent);
96+
}
97+
}
98+
}

samplejava/src/main/java/com/example/chattutorial/ImgurAttachmentViewHolder.java

Lines changed: 0 additions & 76 deletions
This file was deleted.

samplejava/src/main/java/com/example/chattutorial/ImgurAttachmentViewHolderFactory.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

samplekotlin/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ android {
3939

4040
dependencies {
4141
// Add new dependencies
42-
implementation "io.getstream:stream-chat-android-ui-components:4.6.0"
42+
implementation "io.getstream:stream-chat-android-ui-components:4.7.0"
4343
implementation 'io.coil-kt:coil:1.1.1'
4444
implementation "androidx.activity:activity-ktx:1.2.0"
4545

samplekotlin/src/main/java/com/example/chattutorial/ChannelActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ChannelActivity : AppCompatActivity() {
4141
val messageListViewModel: MessageListViewModel by viewModels { factory }
4242
val messageInputViewModel: MessageInputViewModel by viewModels { factory }
4343

44-
// TODO set custom Imgur ViewHolderFactory
44+
// TODO set custom Imgur attachment factory
4545

4646
// Step 2 - Bind the view and ViewModels, they are loosely coupled so it's easy to customize
4747
messageListHeaderViewModel.bindView(binding.messageListHeaderView, this)

0 commit comments

Comments
 (0)