Skip to content

Commit 1bf3b0b

Browse files
authored
Merge pull request #48 from GetStream/feature/new-attachment-factory
Update tutorial to use AttachmentViewFactory for customization
2 parents 394f3df + d694495 commit 1bf3b0b

27 files changed

+156
-270
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Android Chat Tutorial Sample
22

3-
This repository allows you to check the result after completing each step described in the [Android Chat Tutorial](https://getstream.io/tutorials/android-chat/#kotlin). It contains samples written in both **Kotlin** (_samplekotlin_ module) and **Java** (_samplejava_ module). For more Android Chat examples, see the [Github repo for UX/Views and its Sample app](https://github.com/GetStream/stream-chat-android).
3+
This repository allows you to check the result after completing each step described in the [Android Chat Tutorial](https://getstream.io/tutorials/android-chat/#kotlin). It contains samples written in both **Kotlin** (_samplekotlin_ module) and **Java** (_samplejava_ module). For more Android Chat examples, see the [Github repo for the SDK](https://github.com/GetStream/stream-chat-android) and the [UI Components sample app](https://github.com/GetStream/stream-chat-android/tree/main/stream-chat-android-ui-components-sample) in it.
44

55
The project is pre-configured with a shared [Stream](https://getstream.io) account for testing purposes. You can learn more about Stream Chat [here](https://getstream.io/chat/), and then sign up for an account and obtain your own keys [here](https://getstream.io/chat/trial).
66

@@ -28,8 +28,7 @@ channelListView.setOnChannelClickListener { channel ->
2828
```
2929

3030
Currently, you can choose from four different `ChannelActivity` implementations:
31-
<!-- TODO: Add links when the new version of the Android Tutorial is published -->
31+
3232
* `ChannelActivity` - a basic _Message List_ implementation
3333
* `ChannelActivity2` - includes a new _MessageListView_ style and custom attachment type
34-
* `ChannelActivity3` - includes a custom _Typing Header_ component created with the [LiveData&Offline](https://github.com/GetStream/stream-chat-android-livedata) library
35-
* `ChannelActivity4` - includes a custom _Typing Header_ component created with the [Low-Level Client](https://github.com/GetStream/stream-chat-android-client) library
34+
* `ChannelActivity4` - includes a custom _Typing Header_ component created with the [Low-Level Client](https://github.com/GetStream/stream-chat-android/tree/main/stream-chat-android-client) library

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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.example.chattutorial;
2+
3+
import android.view.LayoutInflater;
4+
import android.view.View;
5+
import android.view.ViewGroup;
6+
7+
import com.example.chattutorial.databinding.AttachmentImgurBinding;
8+
import com.getstream.sdk.chat.adapter.MessageListItem;
9+
import com.google.android.material.shape.ShapeAppearanceModel;
10+
11+
import org.jetbrains.annotations.NotNull;
12+
13+
import coil.Coil;
14+
import coil.request.ImageRequest;
15+
import io.getstream.chat.android.client.models.Attachment;
16+
import io.getstream.chat.android.ui.message.list.adapter.MessageListListenerContainer;
17+
import io.getstream.chat.android.ui.message.list.adapter.viewholder.attachment.AttachmentViewFactory;
18+
import io.getstream.chat.android.ui.message.list.internal.MessageListItemStyle;
19+
20+
public class ImgurAttachmentViewFactory extends AttachmentViewFactory {
21+
22+
@NotNull
23+
@Override
24+
public View createAttachmentView(@NotNull MessageListItem.MessageItem data,
25+
@NotNull MessageListListenerContainer listeners,
26+
@NotNull MessageListItemStyle style,
27+
@NotNull ViewGroup parent) {
28+
Attachment imgurAttachment = null;
29+
for (Attachment attachment : data.getMessage().getAttachments()) {
30+
String imageUrl = attachment.getImageUrl();
31+
if (imageUrl != null && imageUrl.contains("imgur")) {
32+
imgurAttachment = attachment;
33+
break;
34+
}
35+
}
36+
37+
if (imgurAttachment != null) {
38+
return createImgurAttachmentView(imgurAttachment, parent);
39+
} else {
40+
return super.createAttachmentView(data, listeners, style, parent);
41+
}
42+
}
43+
44+
private View createImgurAttachmentView(Attachment imgurAttachment, ViewGroup parent) {
45+
AttachmentImgurBinding binding = AttachmentImgurBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
46+
47+
float cornerRadius = binding.getRoot()
48+
.getResources()
49+
.getDimension(R.dimen.stream_ui_selected_attachment_corner_radius);
50+
ShapeAppearanceModel model = binding.ivMediaThumb.getShapeAppearanceModel()
51+
.toBuilder()
52+
.setAllCornerSizes(cornerRadius)
53+
.build();
54+
binding.ivMediaThumb.setShapeAppearanceModel(model);
55+
56+
ImageRequest imageRequest = new ImageRequest.Builder(parent.getContext())
57+
.data(imgurAttachment.getImageUrl())
58+
.allowHardware(false)
59+
.crossfade(true)
60+
.placeholder(R.drawable.stream_ui_picture_placeholder)
61+
.target(binding.ivMediaThumb)
62+
.build();
63+
Coil.imageLoader(parent.getContext()).enqueue(imageRequest);
64+
65+
return binding.getRoot();
66+
}
67+
}

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.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import org.jetbrains.annotations.Nullable;
1212

1313
import io.getstream.chat.android.client.ChatClient;
14+
import io.getstream.chat.android.client.api.models.FilterObject;
1415
import io.getstream.chat.android.client.models.Filters;
1516
import io.getstream.chat.android.client.models.User;
16-
import io.getstream.chat.android.client.utils.FilterObject;
1717
import io.getstream.chat.android.livedata.ChatDomain;
1818
import io.getstream.chat.android.ui.channel.list.viewmodel.ChannelListViewModel;
1919
import io.getstream.chat.android.ui.channel.list.viewmodel.ChannelListViewModelBinding;

0 commit comments

Comments
 (0)