1111import androidx .appcompat .app .AppCompatActivity ;
1212import androidx .lifecycle .ViewModelProvider ;
1313
14+ import com .example .chattutorialjava .databinding .ActivityChannel3Binding ;
1415import com .getstream .sdk .chat .viewmodel .ChannelHeaderViewModel ;
1516import com .getstream .sdk .chat .viewmodel .MessageInputViewModel ;
1617import com .getstream .sdk .chat .viewmodel .factory .ChannelViewModelFactory ;
2829import io .getstream .chat .android .livedata .controller .ChannelController ;
2930import io .getstream .chat .android .ui .messages .header .ChannelHeaderViewModelBinding ;
3031import io .getstream .chat .android .ui .messages .header .MessagesHeaderView ;
31- import io .getstream .chat .android .ui .messages .view .MessageListView ;
3232import io .getstream .chat .android .ui .messages .view .MessageListViewModelBinding ;
33- import io .getstream .chat .android .ui .textinput .MessageInputView ;
3433import io .getstream .chat .android .ui .textinput .MessageInputViewModelBinding ;
3534import 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}
0 commit comments