1
1
package io .syslogic .socketio .fragment ;
2
2
3
- import android .content .Context ;
4
3
import android .os .Bundle ;
5
4
import android .os .Handler ;
6
5
import android .os .Looper ;
20
19
import org .json .JSONException ;
21
20
import org .json .JSONObject ;
22
21
23
- import java .util .ArrayList ;
24
22
import java .util .Objects ;
25
23
26
24
import io .socket .client .Socket ;
27
25
import io .socket .emitter .Emitter ;
28
26
29
27
import io .syslogic .socketio .Constants ;
30
28
import io .syslogic .socketio .R ;
31
- import io .syslogic .socketio .activity . MainActivity ;
29
+ import io .syslogic .socketio .MainActivity ;
32
30
import io .syslogic .socketio .databinding .FragmentChatBinding ;
33
31
import io .syslogic .socketio .model .ChatMessage ;
34
32
import io .syslogic .socketio .model .ChatRoom ;
35
- import io .syslogic .socketio .recyclerview .MessageAdapter ;
33
+ import io .syslogic .socketio .adapter .MessageAdapter ;
36
34
37
35
/**
38
36
* Chat {@link BaseFragment}
41
39
public class ChatFragment extends BaseFragment implements FragmentResultListener {
42
40
private static final String LOG_TAG = ChatFragment .class .getSimpleName ();
43
41
private FragmentChatBinding mDataBinding = null ;
44
- private final ArrayList <ChatMessage > mItems = new ArrayList <>();
45
- private MessageAdapter mAdapter ;
46
42
private static final int TYPING_TIMER_DURATION = 600 ;
47
43
private final Handler mTypingHandler = new Handler (Looper .getMainLooper ());
48
44
private boolean mTyping = false ;
@@ -84,12 +80,6 @@ public void onDestroy() {
84
80
super .onDestroy ();
85
81
}
86
82
87
- @ Override
88
- public void onAttach (@ NonNull Context context ) {
89
- super .onAttach (context );
90
- this .mAdapter = new MessageAdapter (this .mItems );
91
- }
92
-
93
83
@ Override
94
84
public View onCreateView (@ NonNull LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
95
85
this .setDataBinding (inflater , container );
@@ -98,7 +88,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
98
88
}
99
89
100
90
this .mDataBinding .recyclerviewMessages .setLayoutManager (new LinearLayoutManager (requireContext ()));
101
- this .mDataBinding .recyclerviewMessages .setAdapter (this . mAdapter );
91
+ this .mDataBinding .recyclerviewMessages .setAdapter (new MessageAdapter () );
102
92
103
93
this .mDataBinding .buttonSend .setOnClickListener (view -> {
104
94
attemptSend ();
@@ -161,11 +151,14 @@ private void addUserCount(int numUsers) {
161
151
}
162
152
163
153
private void addItem (ChatMessage item ) {
164
- this .mItems .add (item );
165
- mAdapter .notifyItemInserted (this .mItems .size () - 1 );
154
+ getRecyclerViewAdapter ().addItem (item );
166
155
scrollToBottom ();
167
156
}
168
157
158
+ private MessageAdapter getRecyclerViewAdapter () {
159
+ return ((MessageAdapter ) this .mDataBinding .recyclerviewMessages .getAdapter ());
160
+ }
161
+
169
162
private void addLog (String message ) {
170
163
addItem (new ChatMessage .Builder (ChatMessage .TYPE_LOG ).setMessage (message ).build ());
171
164
}
@@ -182,11 +175,11 @@ private void addTyping(String username) {
182
175
}
183
176
184
177
private void removeTyping (String username ) {
185
- for (int i = this .mItems .size () - 1 ; i >= 0 ; i --) {
186
- ChatMessage message = this .mItems .get (i );
178
+ MessageAdapter adapter = getRecyclerViewAdapter ();
179
+ for (int i = adapter .getItemCount () - 1 ; i >= 0 ; i --) {
180
+ ChatMessage message = adapter .getItemAt (i );
187
181
if (message .getType () == ChatMessage .TYPE_ACTION && message .getUsername ().equals (username )) {
188
- this .mItems .remove (i );
189
- mAdapter .notifyItemRemoved (i );
182
+ adapter .removeItem (i );
190
183
}
191
184
}
192
185
}
@@ -219,7 +212,8 @@ public void leaveRoom(@NonNull MainActivity activity) {
219
212
}
220
213
221
214
private void scrollToBottom () {
222
- this .mDataBinding .recyclerviewMessages .scrollToPosition (mAdapter .getItemCount () - 1 );
215
+ this .mDataBinding .recyclerviewMessages
216
+ .scrollToPosition (getRecyclerViewAdapter ().getItemCount () - 1 );
223
217
}
224
218
225
219
private final Emitter .Listener onChatMessage = args -> requireActivity ().runOnUiThread (() -> {
0 commit comments