Skip to content

Commit 394f3df

Browse files
authored
Merge pull request #47 from GetStream/task/fixSettingActiveThread
Add setting thread mode in MessageListHeaderViewModel
2 parents 1bbc172 + 1f8a1e7 commit 394f3df

File tree

12 files changed

+99
-43
lines changed

12 files changed

+99
-43
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.5.5"
38+
implementation "io.getstream:stream-chat-android-ui-components:4.6.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: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel.State.NavigateUp;
1818

1919
import io.getstream.chat.android.client.models.Channel;
20+
import io.getstream.chat.android.client.models.Message;
2021
import io.getstream.chat.android.ui.message.input.viewmodel.MessageInputViewModelBinding;
2122
import io.getstream.chat.android.ui.message.list.header.MessageListHeaderView;
2223
import io.getstream.chat.android.ui.message.list.header.viewmodel.MessageListHeaderViewModel;
@@ -47,25 +48,29 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4748
throw new IllegalStateException("Specifying a channel id is required when starting ChannelActivity");
4849
}
4950

50-
// Step 1 - Create 3 separate ViewModels for the views so it's easy to customize one of the components
51+
// Step 1 - Create three separate ViewModels for the views so it's easy
52+
// to customize them individually
5153
MessageListViewModelFactory factory = new MessageListViewModelFactory(cid);
5254
ViewModelProvider provider = new ViewModelProvider(this, factory);
53-
MessageListHeaderViewModel channelHeaderViewModel = provider.get(MessageListHeaderViewModel.class);
55+
MessageListHeaderViewModel messageListHeaderViewModel = provider.get(MessageListHeaderViewModel.class);
5456
MessageListViewModel messageListViewModel = provider.get(MessageListViewModel.class);
5557
MessageInputViewModel messageInputViewModel = provider.get(MessageInputViewModel.class);
5658

5759
// TODO set custom Imgur ViewHolderFactory
5860

5961
// Step 2 - Bind the view and ViewModels, they are loosely coupled so it's easy to customize
60-
MessageListHeaderViewModelBinding.bind(channelHeaderViewModel, binding.messageListHeaderView, this);
62+
MessageListHeaderViewModelBinding.bind(messageListHeaderViewModel, binding.messageListHeaderView, this);
6163
MessageListViewModelBinding.bind(messageListViewModel, binding.messageListView, this);
6264
MessageInputViewModelBinding.bind(messageInputViewModel, binding.messageInputView, this);
6365

64-
// Step 3 - Let the message input know when we open a thread
66+
// Step 3 - Let both MessageListHeaderView and MessageInputView know when we open a thread
6567
messageListViewModel.getMode().observe(this, mode -> {
6668
if (mode instanceof Thread) {
67-
messageInputViewModel.setActiveThread(((Thread) mode).getParentMessage());
69+
Message parentMessage = ((Thread) mode).getParentMessage();
70+
messageListHeaderViewModel.setActiveThread(parentMessage);
71+
messageInputViewModel.setActiveThread(parentMessage);
6872
} else if (mode instanceof Normal) {
73+
messageListHeaderViewModel.resetThread();
6974
messageInputViewModel.resetThread();
7075
}
7176
});

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel.State.NavigateUp;
1818

1919
import io.getstream.chat.android.client.models.Channel;
20+
import io.getstream.chat.android.client.models.Message;
2021
import io.getstream.chat.android.ui.message.input.viewmodel.MessageInputViewModelBinding;
2122
import io.getstream.chat.android.ui.message.list.header.MessageListHeaderView;
2223
import io.getstream.chat.android.ui.message.list.header.viewmodel.MessageListHeaderViewModel;
@@ -47,26 +48,30 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4748
throw new IllegalStateException("Specifying a channel id is required when starting ChannelActivity2");
4849
}
4950

50-
// Step 1 - Create 3 separate ViewModels for the views so it's easy to customize one of the components
51+
// Step 1 - Create three separate ViewModels for the views so it's easy
52+
// to customize them individually
5153
MessageListViewModelFactory factory = new MessageListViewModelFactory(cid);
5254
ViewModelProvider provider = new ViewModelProvider(this, factory);
53-
MessageListHeaderViewModel channelHeaderViewModel = provider.get(MessageListHeaderViewModel.class);
55+
MessageListHeaderViewModel messageListHeaderViewModel = provider.get(MessageListHeaderViewModel.class);
5456
MessageListViewModel messageListViewModel = provider.get(MessageListViewModel.class);
5557
MessageInputViewModel messageInputViewModel = provider.get(MessageInputViewModel.class);
5658

5759
// Set view holder factory for Imgur attachments
5860
binding.messageListView.setMessageViewHolderFactory(new ImgurAttachmentViewHolderFactory());
5961

6062
// Step 2 - Bind the view and ViewModels, they are loosely coupled so it's easy to customize
61-
MessageListHeaderViewModelBinding.bind(channelHeaderViewModel, binding.messageListHeaderView, this);
63+
MessageListHeaderViewModelBinding.bind(messageListHeaderViewModel, binding.messageListHeaderView, this);
6264
MessageListViewModelBinding.bind(messageListViewModel, binding.messageListView, this);
6365
MessageInputViewModelBinding.bind(messageInputViewModel, binding.messageInputView, this);
6466

65-
// Step 3 - Let the message input know when we open a thread
67+
// Step 3 - Let both MessageListHeaderView and MessageInputView know when we open a thread
6668
messageListViewModel.getMode().observe(this, mode -> {
6769
if (mode instanceof Thread) {
68-
messageInputViewModel.setActiveThread(((Thread) mode).getParentMessage());
70+
Message parentMessage = ((Thread) mode).getParentMessage();
71+
messageListHeaderViewModel.setActiveThread(parentMessage);
72+
messageInputViewModel.setActiveThread(parentMessage);
6973
} else if (mode instanceof Normal) {
74+
messageListHeaderViewModel.resetThread();
7075
messageInputViewModel.resetThread();
7176
}
7277
});

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323

2424
import io.getstream.chat.android.client.models.Channel;
25+
import io.getstream.chat.android.client.models.Message;
2526
import io.getstream.chat.android.client.models.User;
2627
import io.getstream.chat.android.livedata.ChatDomain;
2728
import io.getstream.chat.android.livedata.controller.ChannelController;
@@ -55,26 +56,30 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5556
throw new IllegalStateException("Specifying a channel id is required when starting ChannelActivity3");
5657
}
5758

58-
// Step 1 - Create 3 separate ViewModels for the views so it's easy to customize one of the components
59+
// Step 1 - Create three separate ViewModels for the views so it's easy
60+
// to customize them individually
5961
MessageListViewModelFactory factory = new MessageListViewModelFactory(cid);
6062
ViewModelProvider provider = new ViewModelProvider(this, factory);
61-
MessageListHeaderViewModel channelHeaderViewModel = provider.get(MessageListHeaderViewModel.class);
63+
MessageListHeaderViewModel messageListHeaderViewModel = provider.get(MessageListHeaderViewModel.class);
6264
MessageListViewModel messageListViewModel = provider.get(MessageListViewModel.class);
6365
MessageInputViewModel messageInputViewModel = provider.get(MessageInputViewModel.class);
6466

6567
// Set custom AttachmentViewHolderFactory
6668
binding.messageListView.setMessageViewHolderFactory(new ImgurAttachmentViewHolderFactory());
6769

6870
// Step 2 - Bind the view and ViewModels, they are loosely coupled so it's easy to customize
69-
MessageListHeaderViewModelBinding.bind(channelHeaderViewModel, binding.messageListHeaderView, this);
71+
MessageListHeaderViewModelBinding.bind(messageListHeaderViewModel, binding.messageListHeaderView, this);
7072
MessageListViewModelBinding.bind(messageListViewModel, binding.messageListView, this);
7173
MessageInputViewModelBinding.bind(messageInputViewModel, binding.messageInputView, this);
7274

73-
// Step 3 - Let the message input know when we open a thread
75+
// Step 3 - Let both MessageListHeaderView and MessageInputView know when we open a thread
7476
messageListViewModel.getMode().observe(this, mode -> {
7577
if (mode instanceof Thread) {
76-
messageInputViewModel.setActiveThread(((Thread) mode).getParentMessage());
78+
Message parentMessage = ((Thread) mode).getParentMessage();
79+
messageListHeaderViewModel.setActiveThread(parentMessage);
80+
messageInputViewModel.setActiveThread(parentMessage);
7781
} else if (mode instanceof Normal) {
82+
messageListHeaderViewModel.resetThread();
7883
messageInputViewModel.resetThread();
7984
}
8085
});

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.getstream.chat.android.client.events.TypingStartEvent;
2626
import io.getstream.chat.android.client.events.TypingStopEvent;
2727
import io.getstream.chat.android.client.models.Channel;
28+
import io.getstream.chat.android.client.models.Message;
2829
import io.getstream.chat.android.client.models.User;
2930
import io.getstream.chat.android.ui.message.input.viewmodel.MessageInputViewModelBinding;
3031
import io.getstream.chat.android.ui.message.list.header.MessageListHeaderView;
@@ -56,26 +57,30 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5657
throw new IllegalStateException("Specifying a channel id is required when starting ChannelActivity4");
5758
}
5859

59-
// Step 1 - Create 3 separate ViewModels for the views so it's easy to customize one of the components
60+
// Step 1 - Create three separate ViewModels for the views so it's easy
61+
// to customize them individually
6062
MessageListViewModelFactory factory = new MessageListViewModelFactory(cid);
6163
ViewModelProvider provider = new ViewModelProvider(this, factory);
62-
MessageListHeaderViewModel channelHeaderViewModel = provider.get(MessageListHeaderViewModel.class);
64+
MessageListHeaderViewModel messageListHeaderViewModel = provider.get(MessageListHeaderViewModel.class);
6365
MessageListViewModel messageListViewModel = provider.get(MessageListViewModel.class);
6466
MessageInputViewModel messageInputViewModel = provider.get(MessageInputViewModel.class);
6567

6668
// Set custom AttachmentViewHolderFactory
6769
binding.messageListView.setMessageViewHolderFactory(new ImgurAttachmentViewHolderFactory());
6870

6971
// Step 2 - Bind the view and ViewModels, they are loosely coupled so it's easy to customize
70-
MessageListHeaderViewModelBinding.bind(channelHeaderViewModel, binding.messageListHeaderView, this);
72+
MessageListHeaderViewModelBinding.bind(messageListHeaderViewModel, binding.messageListHeaderView, this);
7173
MessageListViewModelBinding.bind(messageListViewModel, binding.messageListView, this);
7274
MessageInputViewModelBinding.bind(messageInputViewModel, binding.messageInputView, this);
7375

74-
// Step 3 - Let the message input know when we open a thread
76+
// Step 3 - Let both message list header and message input know when we open a thread
7577
messageListViewModel.getMode().observe(this, mode -> {
7678
if (mode instanceof Thread) {
77-
messageInputViewModel.setActiveThread(((Thread) mode).getParentMessage());
79+
Message parentMessage = ((Thread) mode).getParentMessage();
80+
messageListHeaderViewModel.setActiveThread(parentMessage);
81+
messageInputViewModel.setActiveThread(parentMessage);
7882
} else if (mode instanceof Normal) {
83+
messageListHeaderViewModel.resetThread();
7984
messageInputViewModel.resetThread();
8085
}
8186
});

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
3030
ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
3131
setContentView(binding.getRoot());
3232

33-
// Step 1 - Set up the client for API calls, the domain for offline storage and the UI components
33+
// Step 1 - Set up the client for API calls, the domain for offline storage
34+
// and the UI components
3435
ChatClient client = new ChatClient.Builder("b67pax5b2wdq", getApplicationContext()).build();
3536
new ChatDomain.Builder(client, getApplicationContext()).build();
3637
new ChatUI.Builder(getApplicationContext()).build();
@@ -62,7 +63,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
6263
ChannelListViewModel channelsViewModel =
6364
new ViewModelProvider(this, factory).get(ChannelListViewModel.class);
6465

65-
// Step 4 - Connect the ChannelListViewModel to the ChannelListView, loose coupling makes it easy to customize
66+
// Step 4 - Connect the ChannelListViewModel to the ChannelListView, loose
67+
// coupling makes it easy to customize
6668
ChannelListViewModelBinding.bind(channelsViewModel, binding.channelListView, this);
6769
binding.channelListView.setChannelItemClickListener(channel -> {
6870
startActivity(ChannelActivity4.newIntent(this, channel));

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.5.5"
42+
implementation "io.getstream:stream-chat-android-ui-components:4.6.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: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class ChannelActivity : AppCompatActivity() {
3434
"Specifying a channel id is required when starting ChannelActivity"
3535
}
3636

37-
// Step 1 - Create 3 separate ViewModels for the views so it's easy to customize one of the components
37+
// Step 1 - Create three separate ViewModels for the views so it's easy
38+
// to customize them individually
3839
val factory = MessageListViewModelFactory(cid)
3940
val messageListHeaderViewModel: MessageListHeaderViewModel by viewModels { factory }
4041
val messageListViewModel: MessageListViewModel by viewModels { factory }
@@ -47,12 +48,18 @@ class ChannelActivity : AppCompatActivity() {
4748
messageListViewModel.bindView(binding.messageListView, this)
4849
messageInputViewModel.bindView(binding.messageInputView, this)
4950

50-
// Step 3 - Let the message input know when we open a thread
51-
// Note: the .observe support was added in kotlin 1.4, upgrade kotlin to support this syntax
51+
// Step 3 - Let both MessageListHeaderView and MessageInputView know when we open a thread
52+
// Note: the observe syntax used here requires Kotlin 1.4
5253
messageListViewModel.mode.observe(this) { mode ->
5354
when (mode) {
54-
is Thread -> messageInputViewModel.setActiveThread(mode.parentMessage)
55-
is Normal -> messageInputViewModel.resetThread()
55+
is Thread -> {
56+
messageListHeaderViewModel.setActiveThread(mode.parentMessage)
57+
messageInputViewModel.setActiveThread(mode.parentMessage)
58+
}
59+
Normal -> {
60+
messageListHeaderViewModel.resetThread()
61+
messageInputViewModel.resetThread()
62+
}
5663
}
5764
}
5865

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class ChannelActivity2 : AppCompatActivity() {
3434
"Specifying a channel id is required when starting ChannelActivity2"
3535
}
3636

37-
// Step 1 - Create 3 separate ViewModels for the views so it's easy to customize one of the components
37+
// Step 1 - Create three separate ViewModels for the views so it's easy
38+
// to customize them individually
3839
val factory = MessageListViewModelFactory(cid)
3940
val messageListHeaderViewModel: MessageListHeaderViewModel by viewModels { factory }
4041
val messageListViewModel: MessageListViewModel by viewModels { factory }
@@ -48,11 +49,18 @@ class ChannelActivity2 : AppCompatActivity() {
4849
messageListViewModel.bindView(binding.messageListView, this)
4950
messageInputViewModel.bindView(binding.messageInputView, this)
5051

51-
// Step 3 - Let the message input know when we open a thread
52+
// Step 3 - Let both MessageListHeaderView and MessageInputView know when we open a thread
53+
// Note: the observe syntax used here requires Kotlin 1.4
5254
messageListViewModel.mode.observe(this) { mode ->
5355
when (mode) {
54-
is Thread -> messageInputViewModel.setActiveThread(mode.parentMessage)
55-
is Normal -> messageInputViewModel.resetThread()
56+
is Thread -> {
57+
messageListHeaderViewModel.setActiveThread(mode.parentMessage)
58+
messageInputViewModel.setActiveThread(mode.parentMessage)
59+
}
60+
Normal -> {
61+
messageListHeaderViewModel.resetThread()
62+
messageInputViewModel.resetThread()
63+
}
5664
}
5765
}
5866

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class ChannelActivity3 : AppCompatActivity() {
3636
"Specifying a channel id is required when starting ChannelActivity3"
3737
}
3838

39-
// Step 1 - Create 3 separate ViewModels for the views so it's easy to customize one of the components
39+
// Step 1 - Create three separate ViewModels for the views so it's easy
40+
// to customize them individually
4041
val factory = MessageListViewModelFactory(cid)
4142
val messageListHeaderViewModel: MessageListHeaderViewModel by viewModels { factory }
4243
val messageListViewModel: MessageListViewModel by viewModels { factory }
@@ -50,11 +51,18 @@ class ChannelActivity3 : AppCompatActivity() {
5051
messageListViewModel.bindView(binding.messageListView, this)
5152
messageInputViewModel.bindView(binding.messageInputView, this)
5253

53-
// Step 3 - Let the message input know when we open a thread
54+
// Step 3 - Let both MessageListHeaderView and MessageInputView know when we open a thread
55+
// Note: the observe syntax used here requires Kotlin 1.4
5456
messageListViewModel.mode.observe(this) { mode ->
5557
when (mode) {
56-
is Thread -> messageInputViewModel.setActiveThread(mode.parentMessage)
57-
is Normal -> messageInputViewModel.resetThread()
58+
is Thread -> {
59+
messageListHeaderViewModel.setActiveThread(mode.parentMessage)
60+
messageInputViewModel.setActiveThread(mode.parentMessage)
61+
}
62+
Normal -> {
63+
messageListHeaderViewModel.resetThread()
64+
messageInputViewModel.resetThread()
65+
}
5866
}
5967
}
6068

0 commit comments

Comments
 (0)