11package app .xlui .example .im .activities ;
22
3- import android .annotation .SuppressLint ;
43import android .content .Intent ;
54import android .os .Bundle ;
65import android .support .annotation .Nullable ;
2423import ua .naiksoftware .stomp .Stomp ;
2524import ua .naiksoftware .stomp .StompClient ;
2625
26+ @ SuppressWarnings ({"FieldCanBeLocal" , "ResultOfMethodCallIgnored" , "CheckResult" })
2727public class ChatActivity extends AppCompatActivity {
28- private Button broadcast ;
29- private Button groups ;
30- private Button chat ;
28+ private Button broadcastButton ;
29+ private Button groupButton ;
30+ private Button chatButton ;
3131
32- private TextView userId ;
33- private EditText chatUserId ;
34- private Button submit ;
35- private EditText chatMessage ;
36- private Button send ;
37- private TextView show ;
32+ private TextView userIdText ;
33+ private EditText chatUserIdText ;
34+ private Button submitButton ;
35+ private EditText chatMessageText ;
36+ private Button sendButton ;
37+ private TextView showText ;
3838
39- private String user_id ;
40- private String chat_user_id ;
39+ private String userId ;
40+ private String chatUserId ;
4141
4242 private void init () {
43- broadcast = findViewById (R .id .broadcast );
44- groups = findViewById (R .id .groups );
45- chat = findViewById (R .id .chat );
46- chat .setEnabled (false );
43+ broadcastButton = findViewById (R .id .broadcast );
44+ groupButton = findViewById (R .id .groups );
45+ chatButton = findViewById (R .id .chat );
46+ chatButton .setEnabled (false );
4747
48- userId = findViewById (R .id .id );
49- user_id = String .valueOf (new Random ().nextInt (100 ));
50- userId .setText (user_id );
48+ userIdText = findViewById (R .id .id );
49+ userId = String .valueOf (new Random ().nextInt (100 ));
50+ userIdText .setText (userId );
5151
52- chatUserId = findViewById (R .id .chat_user_id );
53- submit = findViewById (R .id .submit );
54- submit .setEnabled (false );
52+ chatUserIdText = findViewById (R .id .chat_user_id );
53+ submitButton = findViewById (R .id .submit );
54+ submitButton .setEnabled (false );
5555
56- chatMessage = findViewById (R .id .chat_message );
57- send = findViewById (R .id .send );
58- send .setEnabled (false );
56+ chatMessageText = findViewById (R .id .chat_message );
57+ sendButton = findViewById (R .id .send );
58+ sendButton .setEnabled (false );
5959
60- show = findViewById (R .id .show );
60+ showText = findViewById (R .id .show );
6161 }
6262
63- @ SuppressWarnings ("ResultOfMethodCallIgnored" )
64- @ SuppressLint ("CheckResult" )
6563 @ Override
6664 protected void onCreate (@ Nullable Bundle savedInstanceState ) {
6765 super .onCreate (savedInstanceState );
@@ -70,23 +68,24 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
7068 this .init ();
7169
7270 StompClient stompClient = Stomp .over (Stomp .ConnectionProvider .OKHTTP , Const .address );
71+ Toast .makeText (this , "Start connecting to server" , Toast .LENGTH_SHORT ).show ();
7372 stompClient .connect ();
74- Toast .makeText (this , "Connect start" , Toast .LENGTH_SHORT ).show ();
7573 StompUtils .lifecycle (stompClient );
7674
77- stompClient .topic (Const .chatResponse .replace (Const .placeholder , user_id )).subscribe (stompMessage -> {
75+ Log .i (Const .TAG , "Subscribe chat endpoint to receive response" );
76+ stompClient .topic (Const .chatResponse .replace (Const .placeholder , userId )).subscribe (stompMessage -> {
7877 JSONObject jsonObject = new JSONObject (stompMessage .getPayload ());
7978 Log .i (Const .TAG , "Receive: " + jsonObject .toString ());
8079 runOnUiThread (() -> {
8180 try {
82- show .append (jsonObject .getString ("response" ) + "\n " );
81+ showText .append (jsonObject .getString ("response" ) + "\n " );
8382 } catch (JSONException e ) {
8483 e .printStackTrace ();
8584 }
8685 });
8786 });
8887
89- chatUserId .addTextChangedListener (new TextWatcher () {
88+ chatUserIdText .addTextChangedListener (new TextWatcher () {
9089 @ Override
9190 public void beforeTextChanged (CharSequence s , int start , int count , int after ) {
9291
@@ -99,42 +98,43 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
9998
10099 @ Override
101100 public void afterTextChanged (Editable s ) {
102- if (!submit .isEnabled ())
103- submit .setEnabled (true );
101+ if (!submitButton .isEnabled ())
102+ submitButton .setEnabled (true );
104103 }
105104 });
106105
107- submit .setOnClickListener (v -> {
108- chat_user_id = chatUserId .getText ().toString ();
109- if (chat_user_id .length () == 0 ) {
106+ submitButton .setOnClickListener (v -> {
107+ chatUserId = chatUserIdText .getText ().toString ();
108+ if (chatUserId .length () == 0 ) {
110109 return ;
111110 }
112- submit .setEnabled (false );
113- send .setEnabled (true );
111+ submitButton .setEnabled (false );
112+ sendButton .setEnabled (true );
114113 });
115114
116- send .setOnClickListener (v -> {
115+ sendButton .setOnClickListener (v -> {
117116 JSONObject jsonObject = new JSONObject ();
118117 try {
119- jsonObject .put ("userID" , chat_user_id );
120- jsonObject .put ("fromUserID" , userId .getText ().toString ());
121- jsonObject .put ("message" , chatMessage .getText ());
118+ jsonObject .put ("userID" , chatUserId );
119+ jsonObject .put ("fromUserID" , userIdText .getText ().toString ());
120+ jsonObject .put ("message" , chatMessageText .getText ());
122121 } catch (JSONException e ) {
123122 e .printStackTrace ();
124123 }
125- if (chat_user_id == null || chat_user_id .length () == 0 ) {
126- chat_user_id = chatUserId .getText ().toString ();
124+ if (chatUserId == null || chatUserId .length () == 0 ) {
125+ chatUserId = chatUserIdText .getText ().toString ();
127126 }
128127 stompClient .send (Const .chat , jsonObject .toString ()).subscribe ();
128+ chatMessageText .setText ("" );
129129 });
130130
131- broadcast .setOnClickListener (v -> {
131+ broadcastButton .setOnClickListener (v -> {
132132 Intent intent = new Intent ();
133133 intent .setClass (ChatActivity .this , BroadcastActivity .class );
134134 startActivity (intent );
135135 this .finish ();
136136 });
137- groups .setOnClickListener (v -> {
137+ groupButton .setOnClickListener (v -> {
138138 Intent intent = new Intent ();
139139 intent .setClass (ChatActivity .this , GroupActivity .class );
140140 startActivity (intent );
0 commit comments