Skip to content

Commit 058a8c8

Browse files
author
Carter Hudson
committed
Implemented imgur view holder
1 parent b7e9bb7 commit 058a8c8

File tree

10 files changed

+131
-102
lines changed

10 files changed

+131
-102
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99
import androidx.appcompat.app.AppCompatActivity;
1010
import androidx.lifecycle.ViewModelProvider;
1111

12-
import com.getstream.sdk.chat.view.ChannelHeaderView;
13-
import com.getstream.sdk.chat.view.MessageListView;
14-
import com.getstream.sdk.chat.view.messageinput.MessageInputView;
1512
import com.getstream.sdk.chat.viewmodel.ChannelHeaderViewModel;
16-
import com.getstream.sdk.chat.viewmodel.ChannelHeaderViewModelBinding;
1713
import com.getstream.sdk.chat.viewmodel.MessageInputViewModel;
18-
import com.getstream.sdk.chat.viewmodel.MessageInputViewModelBinding;
1914
import com.getstream.sdk.chat.viewmodel.factory.ChannelViewModelFactory;
2015
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel;
2116
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel.Mode.Normal;
2217
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel.Mode.Thread;
2318
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel.State.NavigateUp;
24-
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModelBinding;
2519

2620
import io.getstream.chat.android.client.models.Channel;
21+
import io.getstream.chat.android.ui.messages.header.ChannelHeaderViewModelBinding;
22+
import io.getstream.chat.android.ui.messages.header.MessagesHeaderView;
23+
import io.getstream.chat.android.ui.messages.view.MessageListView;
24+
import io.getstream.chat.android.ui.messages.view.MessageListViewModelBinding;
25+
import io.getstream.chat.android.ui.textinput.MessageInputView;
26+
import io.getstream.chat.android.ui.textinput.MessageInputViewModelBinding;
2727
import kotlin.Unit;
2828

2929
public class ChannelActivity extends AppCompatActivity {
@@ -47,7 +47,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4747

4848
// Step 0 - Get View references
4949
MessageListView messageListView = findViewById(R.id.messageListView);
50-
ChannelHeaderView channelHeaderView = findViewById(R.id.channelHeaderView);
50+
MessagesHeaderView channelHeaderView = findViewById(R.id.channelHeaderView);
5151
MessageInputView messageInputView = findViewById(R.id.messageInputView);
5252

5353
// Step 1 - Create 3 separate ViewModels for the views so it's easy to customize one of the components
@@ -86,14 +86,14 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
8686
});
8787

8888
// Step 6 - Handle back button behaviour correctly when you're in a thread
89-
channelHeaderView.setOnBackClick(() -> {
89+
MessagesHeaderView.OnClickListener backHandler = () -> {
9090
messageListViewModel.onEvent(MessageListViewModel.Event.BackButtonPressed.INSTANCE);
91-
return Unit.INSTANCE;
92-
});
91+
};
92+
channelHeaderView.setBackButtonClickListener(backHandler);
9393
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
9494
@Override
9595
public void handleOnBackPressed() {
96-
channelHeaderView.getOnBackClick().invoke();
96+
backHandler.onClick();
9797
}
9898
});
9999
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99
import androidx.appcompat.app.AppCompatActivity;
1010
import androidx.lifecycle.ViewModelProvider;
1111

12-
import com.getstream.sdk.chat.view.ChannelHeaderView;
13-
import com.getstream.sdk.chat.view.MessageListView;
14-
import com.getstream.sdk.chat.view.messageinput.MessageInputView;
1512
import com.getstream.sdk.chat.viewmodel.ChannelHeaderViewModel;
16-
import com.getstream.sdk.chat.viewmodel.ChannelHeaderViewModelBinding;
1713
import com.getstream.sdk.chat.viewmodel.MessageInputViewModel;
18-
import com.getstream.sdk.chat.viewmodel.MessageInputViewModelBinding;
1914
import com.getstream.sdk.chat.viewmodel.factory.ChannelViewModelFactory;
2015
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel;
2116
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel.Mode.Normal;
2217
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel.Mode.Thread;
2318
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel.State.NavigateUp;
24-
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModelBinding;
2519

2620
import io.getstream.chat.android.client.models.Channel;
21+
import io.getstream.chat.android.ui.messages.header.ChannelHeaderViewModelBinding;
22+
import io.getstream.chat.android.ui.messages.header.MessagesHeaderView;
23+
import io.getstream.chat.android.ui.messages.view.MessageListView;
24+
import io.getstream.chat.android.ui.messages.view.MessageListViewModelBinding;
25+
import io.getstream.chat.android.ui.textinput.MessageInputView;
26+
import io.getstream.chat.android.ui.textinput.MessageInputViewModelBinding;
2727
import kotlin.Unit;
2828

2929
public class ChannelActivity2 extends AppCompatActivity {
@@ -47,7 +47,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4747

4848
// Step 0 - Get View references
4949
MessageListView messageListView = findViewById(R.id.messageListView);
50-
ChannelHeaderView channelHeaderView = findViewById(R.id.channelHeaderView);
50+
MessagesHeaderView channelHeaderView = findViewById(R.id.channelHeaderView);
5151
MessageInputView messageInputView = findViewById(R.id.messageInputView);
5252

5353
// Step 1 - Create 3 separate ViewModels for the views so it's easy to customize one of the components
@@ -58,7 +58,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5858
MessageInputViewModel messageInputViewModel = provider.get(MessageInputViewModel.class);
5959

6060
// Set custom AttachmentViewHolderFactory
61-
messageListView.setAttachmentViewHolderFactory(new MyAttachmentViewHolderFactory());
61+
// messageListView.setAttachmentViewHolderFactory(new MyAttachmentViewHolderFactory());
6262

6363
// Step 2 - Bind the view and ViewModels, they are loosely coupled so it's easy to customize
6464
ChannelHeaderViewModelBinding.bind(channelHeaderViewModel, channelHeaderView, this);
@@ -88,14 +88,14 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
8888
});
8989

9090
// Step 6 - Handle back button behaviour correctly when you're in a thread
91-
channelHeaderView.setOnBackClick(() -> {
91+
MessagesHeaderView.OnClickListener backHandler = () -> {
9292
messageListViewModel.onEvent(MessageListViewModel.Event.BackButtonPressed.INSTANCE);
93-
return Unit.INSTANCE;
94-
});
93+
};
94+
channelHeaderView.setBackButtonClickListener(backHandler);
9595
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
9696
@Override
9797
public void handleOnBackPressed() {
98-
channelHeaderView.getOnBackClick().invoke();
98+
backHandler.onClick();
9999
}
100100
});
101101

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,13 @@
1111
import androidx.appcompat.app.AppCompatActivity;
1212
import androidx.lifecycle.ViewModelProvider;
1313

14-
import com.getstream.sdk.chat.view.ChannelHeaderView;
15-
import com.getstream.sdk.chat.view.MessageListView;
16-
import com.getstream.sdk.chat.view.messageinput.MessageInputView;
1714
import com.getstream.sdk.chat.viewmodel.ChannelHeaderViewModel;
18-
import com.getstream.sdk.chat.viewmodel.ChannelHeaderViewModelBinding;
1915
import com.getstream.sdk.chat.viewmodel.MessageInputViewModel;
20-
import com.getstream.sdk.chat.viewmodel.MessageInputViewModelBinding;
2116
import com.getstream.sdk.chat.viewmodel.factory.ChannelViewModelFactory;
2217
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel;
2318
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel.Mode.Normal;
2419
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel.Mode.Thread;
2520
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel.State.NavigateUp;
26-
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModelBinding;
2721

2822
import java.util.LinkedList;
2923
import java.util.List;
@@ -32,6 +26,12 @@
3226
import io.getstream.chat.android.client.models.User;
3327
import io.getstream.chat.android.livedata.ChatDomain;
3428
import io.getstream.chat.android.livedata.controller.ChannelController;
29+
import io.getstream.chat.android.ui.messages.header.ChannelHeaderViewModelBinding;
30+
import io.getstream.chat.android.ui.messages.header.MessagesHeaderView;
31+
import io.getstream.chat.android.ui.messages.view.MessageListView;
32+
import io.getstream.chat.android.ui.messages.view.MessageListViewModelBinding;
33+
import io.getstream.chat.android.ui.textinput.MessageInputView;
34+
import io.getstream.chat.android.ui.textinput.MessageInputViewModelBinding;
3535
import kotlin.Unit;
3636

3737
public class ChannelActivity3 extends AppCompatActivity {
@@ -55,7 +55,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5555

5656
// Step 0 - Get View references
5757
MessageListView messageListView = findViewById(R.id.messageListView);
58-
ChannelHeaderView channelHeaderView = findViewById(R.id.channelHeaderView);
58+
MessagesHeaderView channelHeaderView = findViewById(R.id.channelHeaderView);
5959
MessageInputView messageInputView = findViewById(R.id.messageInputView);
6060

6161
// Step 1 - Create 3 separate ViewModels for the views so it's easy to customize one of the components
@@ -66,7 +66,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
6666
MessageInputViewModel messageInputViewModel = provider.get(MessageInputViewModel.class);
6767

6868
// Set custom AttachmentViewHolderFactory
69-
messageListView.setAttachmentViewHolderFactory(new MyAttachmentViewHolderFactory());
69+
// messageListView.setAttachmentViewHolderFactory(new MyAttachmentViewHolderFactory());
7070

7171
// Step 2 - Bind the view and ViewModels, they are loosely coupled so it's easy to customize
7272
ChannelHeaderViewModelBinding.bind(channelHeaderViewModel, channelHeaderView, this);
@@ -96,14 +96,14 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
9696
});
9797

9898
// Step 6 - Handle back button behaviour correctly when you're in a thread
99-
channelHeaderView.setOnBackClick(() -> {
99+
MessagesHeaderView.OnClickListener backHandler = () -> {
100100
messageListViewModel.onEvent(MessageListViewModel.Event.BackButtonPressed.INSTANCE);
101-
return Unit.INSTANCE;
102-
});
101+
};
102+
channelHeaderView.setBackButtonClickListener(backHandler);
103103
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
104104
@Override
105105
public void handleOnBackPressed() {
106-
channelHeaderView.getOnBackClick().invoke();
106+
backHandler.onClick();
107107
}
108108
});
109109

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
import androidx.lifecycle.ViewModelProvider;
77

88
import com.getstream.sdk.chat.ChatUI;
9-
import com.getstream.sdk.chat.view.channels.ChannelsView;
109
import com.getstream.sdk.chat.viewmodel.channels.ChannelsViewModel;
11-
import com.getstream.sdk.chat.viewmodel.channels.ChannelsViewModelBinding;
1210
import com.getstream.sdk.chat.viewmodel.factory.ChannelsViewModelFactory;
1311

1412
import org.jetbrains.annotations.Nullable;
@@ -18,7 +16,8 @@
1816
import io.getstream.chat.android.client.models.User;
1917
import io.getstream.chat.android.client.utils.FilterObject;
2018
import io.getstream.chat.android.livedata.ChatDomain;
21-
import kotlin.Unit;
19+
import io.getstream.chat.android.ui.channel.list.ChannelsView;
20+
import io.getstream.chat.android.ui.channel.list.viewmodel.ChannelsViewModelBinding;
2221

2322
import static java.util.Collections.singletonList;
2423

@@ -61,10 +60,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
6160
// Step 4 - Connect the ChannelsViewModel to the ChannelsView, loose coupling makes it easy to customize
6261
ChannelsView channelsView = findViewById(R.id.channelsView);
6362
ChannelsViewModelBinding.bind(channelsViewModel, channelsView, this);
64-
channelsView.setOnChannelClickListener((channel -> {
65-
startActivity(ChannelActivity4.newIntent(this, channel));
66-
return Unit.INSTANCE;
67-
}));
63+
channelsView.setChannelItemClickListener((channel -> startActivity(ChannelActivity4.newIntent(this, channel))));
6864
}
6965
}
7066

samplejava/src/main/java/com/example/chattutorialjava/MyAttachmentViewHolderFactory.java

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@
22

33
import android.view.ViewGroup;
44

5-
import com.getstream.sdk.chat.adapter.AttachmentListItem;
6-
import com.getstream.sdk.chat.adapter.AttachmentViewHolderFactory;
7-
import com.getstream.sdk.chat.adapter.MessageListItem;
8-
import com.getstream.sdk.chat.adapter.viewholder.attachment.BaseAttachmentViewHolder;
9-
import com.getstream.sdk.chat.view.MessageListViewStyle;
10-
11-
import org.jetbrains.annotations.NotNull;
12-
13-
public class MyAttachmentViewHolderFactory extends AttachmentViewHolderFactory {
14-
private static final int IMGUR_TYPE = 0;
15-
16-
@Override
17-
public int getAttachmentViewType(@NotNull AttachmentListItem attachmentListItem) {
18-
String imageUrl = attachmentListItem.getAttachment().getImageUrl();
19-
if (imageUrl != null && imageUrl.contains("imgur")) {
20-
return IMGUR_TYPE;
21-
}
22-
return super.getAttachmentViewType(attachmentListItem);
23-
}
24-
25-
@NotNull
26-
@Override
27-
public BaseAttachmentViewHolder createAttachmentViewHolder(
28-
@NotNull ViewGroup parent,
29-
int viewType,
30-
@NotNull MessageListViewStyle style,
31-
@NotNull MessageListItem.MessageItem messageItem) {
32-
33-
if (viewType == IMGUR_TYPE) {
34-
return new AttachmentViewHolderImgur(parent, bubbleHelper, messageItem);
35-
}
36-
return super.createAttachmentViewHolder(parent, viewType, style, messageItem);
37-
}
38-
}
5+
//import com.getstream.sdk.chat.adapter.AttachmentListItem;
6+
//import com.getstream.sdk.chat.adapter.AttachmentViewHolderFactory;
7+
//import com.getstream.sdk.chat.adapter.MessageListItem;
8+
//import com.getstream.sdk.chat.adapter.viewholder.attachment.BaseAttachmentViewHolder;
9+
//import com.getstream.sdk.chat.view.MessageListViewStyle;
10+
//
11+
//import org.jetbrains.annotations.NotNull;
12+
//
13+
//public class MyAttachmentViewHolderFactory extends AttachmentViewHolderFactory {
14+
// private static final int IMGUR_TYPE = 0;
15+
//
16+
// @Override
17+
// public int getAttachmentViewType(@NotNull AttachmentListItem attachmentListItem) {
18+
// String imageUrl = attachmentListItem.getAttachment().getImageUrl();
19+
// if (imageUrl != null && imageUrl.contains("imgur")) {
20+
// return IMGUR_TYPE;
21+
// }
22+
// return super.getAttachmentViewType(attachmentListItem);
23+
// }
24+
//
25+
// @NotNull
26+
// @Override
27+
// public BaseAttachmentViewHolder createAttachmentViewHolder(
28+
// @NotNull ViewGroup parent,
29+
// int viewType,
30+
// @NotNull MessageListViewStyle style,
31+
// @NotNull MessageListItem.MessageItem messageItem) {
32+
//
33+
// if (viewType == IMGUR_TYPE) {
34+
// return new AttachmentViewHolderImgur(parent, bubbleHelper, messageItem);
35+
// }
36+
// return super.createAttachmentViewHolder(parent, viewType, style, messageItem);
37+
// }
38+
//}

samplejava/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
android:layout_width="match_parent"
1010
android:layout_height="0dp"
1111
app:layout_constraintBottom_toBottomOf="parent"
12-
app:layout_constraintTop_toTopOf="parent"
13-
app:streamChannelPreviewLayout="@layout/stream_item_channel" />
12+
app:layout_constraintTop_toTopOf="parent" />
1413

1514
</androidx.constraintlayout.widget.ConstraintLayout>

samplekotlin/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ dependencies {
4848
implementation "androidx.appcompat:appcompat:1.2.0"
4949
implementation "androidx.constraintlayout:constraintlayout:2.0.4"
5050
implementation 'androidx.activity:activity-ktx:1.1.0'
51+
implementation "com.google.android.material:material:1.2.1"
5152
}

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ChannelActivity2 : AppCompatActivity() {
4747
binding.messageListView.setMessageViewHolderFactory(
4848
object : MessageListItemViewHolderFactory() {
4949
val IMGUR = 999
50-
override fun getViewType(item: MessageListItem): Int {
50+
override fun getItemViewType(item: MessageListItem): Int {
5151
return when (item) {
5252
is MessageListItem.MessageItem -> {
5353
item.message
@@ -56,10 +56,10 @@ class ChannelActivity2 : AppCompatActivity() {
5656
?.imageUrl
5757
?.contains("imgur")
5858
?.let { IMGUR }
59-
?: super.getViewType(item)
59+
?: super.getItemViewType(item)
6060
}
6161

62-
else -> super.getViewType(item)
62+
else -> super.getItemViewType(item)
6363
}
6464
}
6565

@@ -68,14 +68,10 @@ class ChannelActivity2 : AppCompatActivity() {
6868
viewType: Int
6969
): BaseMessageItemViewHolder<out MessageListItem> {
7070
return when (viewType) {
71-
IMGUR -> createImgurViewHolder(parentView)
71+
IMGUR -> ImgurAttachmentViewHolder(parentView)
7272
else -> super.createViewHolder(parentView, viewType)
7373
}
7474
}
75-
76-
fun createImgurViewHolder(parent: ViewGroup): BaseMessageItemViewHolder<out MessageListItem> {
77-
return ImgurAttachmentViewHolder(parent)
78-
}
7975
}
8076
)
8177

0 commit comments

Comments
 (0)