Skip to content

Commit f852a2c

Browse files
author
Carter Hudson
committed
Refactor java module to use ViewBinding
1 parent cd81dc4 commit f852a2c

18 files changed

+119
-106
lines changed

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

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

12+
import com.example.chattutorialjava.databinding.ActivityChannelBinding;
1213
import com.getstream.sdk.chat.viewmodel.ChannelHeaderViewModel;
1314
import com.getstream.sdk.chat.viewmodel.MessageInputViewModel;
1415
import com.getstream.sdk.chat.viewmodel.factory.ChannelViewModelFactory;
@@ -20,9 +21,7 @@
2021
import io.getstream.chat.android.client.models.Channel;
2122
import io.getstream.chat.android.ui.messages.header.ChannelHeaderViewModelBinding;
2223
import io.getstream.chat.android.ui.messages.header.MessagesHeaderView;
23-
import io.getstream.chat.android.ui.messages.view.MessageListView;
2424
import io.getstream.chat.android.ui.messages.view.MessageListViewModelBinding;
25-
import io.getstream.chat.android.ui.textinput.MessageInputView;
2625
import io.getstream.chat.android.ui.textinput.MessageInputViewModelBinding;
2726
import kotlin.Unit;
2827

@@ -39,30 +38,29 @@ public static Intent newIntent(Context context, Channel channel) {
3938
@Override
4039
protected void onCreate(@Nullable Bundle savedInstanceState) {
4140
super.onCreate(savedInstanceState);
42-
setContentView(R.layout.activity_channel);
41+
42+
// Step 0 - inflate binding
43+
ActivityChannelBinding binding = ActivityChannelBinding.inflate(getLayoutInflater());
44+
setContentView(binding.getRoot());
45+
4346
String cid = getIntent().getStringExtra(CID_KEY);
4447
if (cid == null) {
4548
throw new IllegalStateException("Specifying a channel id is required when starting ChannelActivity");
4649
}
4750

48-
// Step 0 - Get View references
49-
MessageListView messageListView = findViewById(R.id.messageListView);
50-
MessagesHeaderView channelHeaderView = findViewById(R.id.channelHeaderView);
51-
MessageInputView messageInputView = findViewById(R.id.messageInputView);
52-
5351
// Step 1 - Create 3 separate ViewModels for the views so it's easy to customize one of the components
5452
ChannelViewModelFactory factory = new ChannelViewModelFactory(cid);
5553
ViewModelProvider provider = new ViewModelProvider(this, factory);
5654
ChannelHeaderViewModel channelHeaderViewModel = provider.get(ChannelHeaderViewModel.class);
5755
MessageListViewModel messageListViewModel = provider.get(MessageListViewModel.class);
5856
MessageInputViewModel messageInputViewModel = provider.get(MessageInputViewModel.class);
5957

60-
// TODO set custom AttachmentViewHolderFactory
58+
// TODO set custom Imgur attachment View Holder Factory
6159

6260
// Step 2 - Bind the view and ViewModels, they are loosely coupled so it's easy to customize
63-
ChannelHeaderViewModelBinding.bind(channelHeaderViewModel, channelHeaderView, this);
64-
MessageListViewModelBinding.bind(messageListViewModel, messageListView, this);
65-
MessageInputViewModelBinding.bind(messageInputViewModel, messageInputView, this);
61+
ChannelHeaderViewModelBinding.bind(channelHeaderViewModel, binding.messagesHeaderView, this);
62+
MessageListViewModelBinding.bind(messageListViewModel, binding.messageListView, this);
63+
MessageInputViewModelBinding.bind(messageInputViewModel, binding.messageInputView, this);
6664

6765
// Step 3 - Let the message input know when we open a thread
6866
messageListViewModel.getMode().observe(this, mode -> {
@@ -73,7 +71,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
7371
}
7472
});
7573
// Step 4 - Let the message input know when we are editing a message
76-
messageListView.setOnMessageEditHandler(message -> {
74+
binding.messageListView.setOnMessageEditHandler(message -> {
7775
messageInputViewModel.getEditMessage().postValue(message);
7876
return Unit.INSTANCE;
7977
});
@@ -89,7 +87,9 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
8987
MessagesHeaderView.OnClickListener backHandler = () -> {
9088
messageListViewModel.onEvent(MessageListViewModel.Event.BackButtonPressed.INSTANCE);
9189
};
92-
channelHeaderView.setBackButtonClickListener(backHandler);
90+
91+
binding.messagesHeaderView.setBackButtonClickListener(backHandler);
92+
9393
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
9494
@Override
9595
public void handleOnBackPressed() {

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

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

12+
import com.example.chattutorialjava.databinding.ActivityChannel2Binding;
1213
import com.getstream.sdk.chat.viewmodel.ChannelHeaderViewModel;
1314
import com.getstream.sdk.chat.viewmodel.MessageInputViewModel;
1415
import com.getstream.sdk.chat.viewmodel.factory.ChannelViewModelFactory;
@@ -20,9 +21,7 @@
2021
import io.getstream.chat.android.client.models.Channel;
2122
import io.getstream.chat.android.ui.messages.header.ChannelHeaderViewModelBinding;
2223
import io.getstream.chat.android.ui.messages.header.MessagesHeaderView;
23-
import io.getstream.chat.android.ui.messages.view.MessageListView;
2424
import io.getstream.chat.android.ui.messages.view.MessageListViewModelBinding;
25-
import io.getstream.chat.android.ui.textinput.MessageInputView;
2625
import io.getstream.chat.android.ui.textinput.MessageInputViewModelBinding;
2726
import kotlin.Unit;
2827

@@ -39,17 +38,16 @@ public static Intent newIntent(Context context, Channel channel) {
3938
@Override
4039
protected void onCreate(@Nullable Bundle savedInstanceState) {
4140
super.onCreate(savedInstanceState);
42-
setContentView(R.layout.activity_channel_2);
41+
42+
// Step 0 - inflate binding
43+
ActivityChannel2Binding binding = ActivityChannel2Binding.inflate(getLayoutInflater());
44+
setContentView(binding.getRoot());
45+
4346
String cid = getIntent().getStringExtra(CID_KEY);
4447
if (cid == null) {
4548
throw new IllegalStateException("Specifying a channel id is required when starting ChannelActivity2");
4649
}
4750

48-
// Step 0 - Get View references
49-
MessageListView messageListView = findViewById(R.id.messageListView);
50-
MessagesHeaderView channelHeaderView = findViewById(R.id.channelHeaderView);
51-
MessageInputView messageInputView = findViewById(R.id.messageInputView);
52-
5351
// Step 1 - Create 3 separate ViewModels for the views so it's easy to customize one of the components
5452
ChannelViewModelFactory factory = new ChannelViewModelFactory(cid);
5553
ViewModelProvider provider = new ViewModelProvider(this, factory);
@@ -58,12 +56,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5856
MessageInputViewModel messageInputViewModel = provider.get(MessageInputViewModel.class);
5957

6058
// Set custom AttachmentViewHolderFactory
61-
messageListView.setMessageViewHolderFactory(new ImgurAttachmentViewHolderFactory());
59+
binding.messageListView.setMessageViewHolderFactory(new ImgurAttachmentViewHolderFactory());
6260

6361
// Step 2 - Bind the view and ViewModels, they are loosely coupled so it's easy to customize
64-
ChannelHeaderViewModelBinding.bind(channelHeaderViewModel, channelHeaderView, this);
65-
MessageListViewModelBinding.bind(messageListViewModel, messageListView, this);
66-
MessageInputViewModelBinding.bind(messageInputViewModel, messageInputView, this);
62+
ChannelHeaderViewModelBinding.bind(channelHeaderViewModel, binding.messagesHeaderView, this);
63+
MessageListViewModelBinding.bind(messageListViewModel, binding.messageListView, this);
64+
MessageInputViewModelBinding.bind(messageInputViewModel, binding.messageInputView, this);
6765

6866
// Step 3 - Let the message input know when we open a thread
6967
messageListViewModel.getMode().observe(this, mode -> {
@@ -82,7 +80,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
8280
});
8381

8482
// Step 5 - Let the message input know when we are editing a message
85-
messageListView.setOnMessageEditHandler(message -> {
83+
binding.messageListView.setOnMessageEditHandler(message -> {
8684
messageInputViewModel.getEditMessage().postValue(message);
8785
return Unit.INSTANCE;
8886
});
@@ -91,7 +89,9 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
9189
MessagesHeaderView.OnClickListener backHandler = () -> {
9290
messageListViewModel.onEvent(MessageListViewModel.Event.BackButtonPressed.INSTANCE);
9391
};
94-
channelHeaderView.setBackButtonClickListener(backHandler);
92+
93+
binding.messagesHeaderView.setBackButtonClickListener(backHandler);
94+
9595
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
9696
@Override
9797
public void handleOnBackPressed() {

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

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

14+
import com.example.chattutorialjava.databinding.ActivityChannel3Binding;
1415
import com.getstream.sdk.chat.viewmodel.ChannelHeaderViewModel;
1516
import com.getstream.sdk.chat.viewmodel.MessageInputViewModel;
1617
import com.getstream.sdk.chat.viewmodel.factory.ChannelViewModelFactory;
@@ -28,9 +29,7 @@
2829
import io.getstream.chat.android.livedata.controller.ChannelController;
2930
import io.getstream.chat.android.ui.messages.header.ChannelHeaderViewModelBinding;
3031
import io.getstream.chat.android.ui.messages.header.MessagesHeaderView;
31-
import io.getstream.chat.android.ui.messages.view.MessageListView;
3232
import io.getstream.chat.android.ui.messages.view.MessageListViewModelBinding;
33-
import io.getstream.chat.android.ui.textinput.MessageInputView;
3433
import io.getstream.chat.android.ui.textinput.MessageInputViewModelBinding;
3534
import kotlin.Unit;
3635

@@ -47,17 +46,16 @@ public static Intent newIntent(Context context, Channel channel) {
4746
@Override
4847
protected void onCreate(@Nullable Bundle savedInstanceState) {
4948
super.onCreate(savedInstanceState);
50-
setContentView(R.layout.activity_channel_3);
49+
50+
// Step 0 - inflate binding
51+
ActivityChannel3Binding binding = ActivityChannel3Binding.inflate(getLayoutInflater());
52+
setContentView(binding.getRoot());
53+
5154
String cid = getIntent().getStringExtra(CID_KEY);
5255
if (cid == null) {
5356
throw new IllegalStateException("Specifying a channel id is required when starting ChannelActivity3");
5457
}
5558

56-
// Step 0 - Get View references
57-
MessageListView messageListView = findViewById(R.id.messageListView);
58-
MessagesHeaderView channelHeaderView = findViewById(R.id.channelHeaderView);
59-
MessageInputView messageInputView = findViewById(R.id.messageInputView);
60-
6159
// Step 1 - Create 3 separate ViewModels for the views so it's easy to customize one of the components
6260
ChannelViewModelFactory factory = new ChannelViewModelFactory(cid);
6361
ViewModelProvider provider = new ViewModelProvider(this, factory);
@@ -66,12 +64,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
6664
MessageInputViewModel messageInputViewModel = provider.get(MessageInputViewModel.class);
6765

6866
// Set custom AttachmentViewHolderFactory
69-
messageListView.setMessageViewHolderFactory(new ImgurAttachmentViewHolderFactory());
67+
binding.messageListView.setMessageViewHolderFactory(new ImgurAttachmentViewHolderFactory());
7068

7169
// Step 2 - Bind the view and ViewModels, they are loosely coupled so it's easy to customize
72-
ChannelHeaderViewModelBinding.bind(channelHeaderViewModel, channelHeaderView, this);
73-
MessageListViewModelBinding.bind(messageListViewModel, messageListView, this);
74-
MessageInputViewModelBinding.bind(messageInputViewModel, messageInputView, this);
70+
ChannelHeaderViewModelBinding.bind(channelHeaderViewModel, binding.messagesHeaderView, this);
71+
MessageListViewModelBinding.bind(messageListViewModel, binding.messageListView, this);
72+
MessageInputViewModelBinding.bind(messageInputViewModel, binding.messageInputView, this);
7573

7674
// Step 3 - Let the message input know when we open a thread
7775
messageListViewModel.getMode().observe(this, mode -> {
@@ -90,7 +88,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
9088
});
9189

9290
// Step 5 - Let the message input know when we are editing a message
93-
messageListView.setOnMessageEditHandler(message -> {
91+
binding.messageListView.setOnMessageEditHandler(message -> {
9492
messageInputViewModel.getEditMessage().postValue(message);
9593
return Unit.INSTANCE;
9694
});
@@ -99,7 +97,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
9997
MessagesHeaderView.OnClickListener backHandler = () -> {
10098
messageListViewModel.onEvent(MessageListViewModel.Event.BackButtonPressed.INSTANCE);
10199
};
102-
channelHeaderView.setBackButtonClickListener(backHandler);
100+
binding.messagesHeaderView.setBackButtonClickListener(backHandler);
103101
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
104102
@Override
105103
public void handleOnBackPressed() {
@@ -113,23 +111,27 @@ public void handleOnBackPressed() {
113111
typingHeaderView.setText(nobodyTyping);
114112

115113
// Obtain a ChannelController
116-
ChatDomain.instance().getUseCases().getGetChannelController().invoke(cid).enqueue((result) -> {
117-
ChannelController channelController = result.data();
118-
119-
// Observe typing users
120-
channelController.getTyping().observe(this, typingState -> {
121-
if (typingState.getUsers().isEmpty()) {
122-
typingHeaderView.setText(nobodyTyping);
123-
} else {
124-
List<String> userNames = new LinkedList<>();
125-
for (User user : typingState.getUsers()) {
126-
userNames.add((String) user.getExtraData().get("name"));
127-
}
128-
String typing = "typing: " + TextUtils.join(", ", userNames);
129-
typingHeaderView.setText(typing);
130-
}
131-
});
132-
return Unit.INSTANCE;
133-
});
114+
ChatDomain.instance()
115+
.getUseCases()
116+
.getGetChannelController()
117+
.invoke(cid)
118+
.enqueue((result) -> {
119+
ChannelController channelController = result.data();
120+
121+
// Observe typing users
122+
channelController.getTyping().observe(this, typingState -> {
123+
if (typingState.getUsers().isEmpty()) {
124+
typingHeaderView.setText(nobodyTyping);
125+
} else {
126+
List<String> userNames = new LinkedList<>();
127+
for (User user : typingState.getUsers()) {
128+
userNames.add((String) user.getExtraData().get("name"));
129+
}
130+
String typing = "typing: " + TextUtils.join(", ", userNames);
131+
typingHeaderView.setText(typing);
132+
}
133+
});
134+
return Unit.INSTANCE;
135+
});
134136
}
135137
}

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

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

14+
import com.example.chattutorialjava.databinding.ActivityChannel4Binding;
1415
import com.getstream.sdk.chat.viewmodel.ChannelHeaderViewModel;
1516
import com.getstream.sdk.chat.viewmodel.MessageInputViewModel;
1617
import com.getstream.sdk.chat.viewmodel.factory.ChannelViewModelFactory;
@@ -29,9 +30,7 @@
2930
import io.getstream.chat.android.client.models.User;
3031
import io.getstream.chat.android.ui.messages.header.ChannelHeaderViewModelBinding;
3132
import io.getstream.chat.android.ui.messages.header.MessagesHeaderView;
32-
import io.getstream.chat.android.ui.messages.view.MessageListView;
3333
import io.getstream.chat.android.ui.messages.view.MessageListViewModelBinding;
34-
import io.getstream.chat.android.ui.textinput.MessageInputView;
3534
import io.getstream.chat.android.ui.textinput.MessageInputViewModelBinding;
3635
import kotlin.Unit;
3736

@@ -48,17 +47,16 @@ public static Intent newIntent(Context context, Channel channel) {
4847
@Override
4948
protected void onCreate(@Nullable Bundle savedInstanceState) {
5049
super.onCreate(savedInstanceState);
51-
setContentView(R.layout.activity_channel_4);
50+
51+
// Step 0 - inflate binding
52+
ActivityChannel4Binding binding = ActivityChannel4Binding.inflate(getLayoutInflater());
53+
setContentView(binding.getRoot());
54+
5255
String cid = getIntent().getStringExtra(CID_KEY);
5356
if (cid == null) {
5457
throw new IllegalStateException("Specifying a channel id is required when starting ChannelActivity4");
5558
}
5659

57-
// Step 0 - Get View references
58-
MessageListView messageListView = findViewById(R.id.messageListView);
59-
MessagesHeaderView channelHeaderView = findViewById(R.id.channelHeaderView);
60-
MessageInputView messageInputView = findViewById(R.id.messageInputView);
61-
6260
// Step 1 - Create 3 separate ViewModels for the views so it's easy to customize one of the components
6361
ChannelViewModelFactory factory = new ChannelViewModelFactory(cid);
6462
ViewModelProvider provider = new ViewModelProvider(this, factory);
@@ -67,12 +65,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
6765
MessageInputViewModel messageInputViewModel = provider.get(MessageInputViewModel.class);
6866

6967
// Set custom AttachmentViewHolderFactory
70-
messageListView.setMessageViewHolderFactory(new ImgurAttachmentViewHolderFactory());
68+
binding.messageListView.setMessageViewHolderFactory(new ImgurAttachmentViewHolderFactory());
7169

7270
// Step 2 - Bind the view and ViewModels, they are loosely coupled so it's easy to customize
73-
ChannelHeaderViewModelBinding.bind(channelHeaderViewModel, channelHeaderView, this);
74-
MessageListViewModelBinding.bind(messageListViewModel, messageListView, this);
75-
MessageInputViewModelBinding.bind(messageInputViewModel, messageInputView, this);
71+
ChannelHeaderViewModelBinding.bind(channelHeaderViewModel, binding.messagesHeaderView, this);
72+
MessageListViewModelBinding.bind(messageListViewModel, binding.messageListView, this);
73+
MessageInputViewModelBinding.bind(messageInputViewModel, binding.messageInputView, this);
7674

7775
// Step 3 - Let the message input know when we open a thread
7876
messageListViewModel.getMode().observe(this, mode -> {
@@ -91,7 +89,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
9189
});
9290

9391
// Step 5 - Let the message input know when we are editing a message
94-
messageListView.setOnMessageEditHandler(message -> {
92+
binding.messageListView.setOnMessageEditHandler(message -> {
9593
messageInputViewModel.getEditMessage().postValue(message);
9694
return Unit.INSTANCE;
9795
});
@@ -100,7 +98,9 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
10098
MessagesHeaderView.OnClickListener backHandler = () -> {
10199
messageListViewModel.onEvent(MessageListViewModel.Event.BackButtonPressed.INSTANCE);
102100
};
103-
channelHeaderView.setBackButtonClickListener(backHandler);
101+
102+
binding.messagesHeaderView.setBackButtonClickListener(backHandler);
103+
104104
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
105105
@Override
106106
public void handleOnBackPressed() {
@@ -114,8 +114,7 @@ public void handleOnBackPressed() {
114114
typingHeaderView.setText(nobodyTyping);
115115

116116
Set<String> currentlyTyping = new HashSet<>();
117-
ChatClient
118-
.instance()
117+
ChatClient.instance()
119118
.channel(cid)
120119
.subscribeFor(
121120
this,

0 commit comments

Comments
 (0)