Skip to content

Commit 875f73c

Browse files
author
xlui
committed
Update comment and ui language
1 parent 9f36c56 commit 875f73c

13 files changed

Lines changed: 185 additions & 180 deletions

File tree

AndroidClient/app/src/main/java/app/xlui/example/im/activities/BroadcastActivity.java

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package app.xlui.example.im.activities;
22

3-
import android.annotation.SuppressLint;
43
import android.content.Intent;
54
import android.os.Bundle;
65
import android.support.v7.app.AppCompatActivity;
@@ -24,27 +23,26 @@
2423
import ua.naiksoftware.stomp.dto.StompHeader;
2524
import ua.naiksoftware.stomp.dto.StompMessage;
2625

26+
@SuppressWarnings({"FieldCanBeLocal", "ResultOfMethodCallIgnored", "CheckResult"})
2727
public class BroadcastActivity 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 EditText name;
33-
private Button send;
34-
private TextView result;
32+
private EditText nameText;
33+
private Button sendButton;
34+
private TextView resultText;
3535

3636
private void init() {
37-
broadcast = findViewById(R.id.broadcast);
38-
broadcast.setEnabled(false);
39-
groups = findViewById(R.id.groups);
40-
chat = findViewById(R.id.chat);
41-
name = findViewById(R.id.name);
42-
send = findViewById(R.id.send);
43-
result = findViewById(R.id.show);
37+
broadcastButton = findViewById(R.id.broadcast);
38+
broadcastButton.setEnabled(false);
39+
groupButton = findViewById(R.id.groups);
40+
chatButton = findViewById(R.id.chat);
41+
nameText = findViewById(R.id.name);
42+
sendButton = findViewById(R.id.send);
43+
resultText = findViewById(R.id.show);
4444
}
4545

46-
@SuppressLint("CheckResult")
47-
@SuppressWarnings("ResultOfMethodCallIgnored")
4846
@Override
4947
protected void onCreate(Bundle savedInstanceState) {
5048
super.onCreate(savedInstanceState);
@@ -53,29 +51,29 @@ protected void onCreate(Bundle savedInstanceState) {
5351
this.init();
5452

5553
StompClient stompClient = Stomp.over(Stomp.ConnectionProvider.OKHTTP, Const.address);
56-
// 连接服务器
57-
stompClient.connect();
58-
Toast.makeText(this, "Connect start", Toast.LENGTH_SHORT).show();
5954
StompUtils.lifecycle(stompClient);
55+
Toast.makeText(this, "Start connecting to server", Toast.LENGTH_SHORT).show();
56+
// Connect to WebSocket server
57+
stompClient.connect();
6058

6159
// 订阅消息
62-
Log.i(Const.TAG, "Subscribe broadcast");
60+
Log.i(Const.TAG, "Subscribe broadcast endpoint to receive response");
6361
stompClient.topic(Const.broadcastResponse).subscribe(stompMessage -> {
6462
JSONObject jsonObject = new JSONObject(stompMessage.getPayload());
6563
Log.i(Const.TAG, "Receive: " + stompMessage.getPayload());
6664
runOnUiThread(() -> {
6765
try {
68-
result.append(jsonObject.getString("response") + "\n");
66+
resultText.append(jsonObject.getString("response") + "\n");
6967
} catch (JSONException e) {
7068
e.printStackTrace();
7169
}
7270
});
7371
});
7472

75-
send.setOnClickListener(v -> {
73+
sendButton.setOnClickListener(v -> {
7674
JSONObject jsonObject = new JSONObject();
7775
try {
78-
jsonObject.put("name", name.getText());
76+
jsonObject.put("name", nameText.getText());
7977
} catch (JSONException e) {
8078
e.printStackTrace();
8179
}
@@ -84,23 +82,24 @@ protected void onCreate(Bundle savedInstanceState) {
8482
// Stomp command
8583
StompCommand.SEND,
8684
// Stomp Headers, Send Headers with STOMP
87-
// the first header is necessary, and the other can be customized by ourselves
85+
// the first header is required, and the other can be customized by ourselves
8886
Arrays.asList(
8987
new StompHeader(StompHeader.DESTINATION, Const.broadcast),
9088
new StompHeader("authorization", "this is a token generated by your code!")
9189
),
9290
// Stomp payload
9391
jsonObject.toString())
9492
).subscribe();
93+
nameText.setText("");
9594
});
9695

97-
groups.setOnClickListener(v -> {
96+
groupButton.setOnClickListener(v -> {
9897
Intent intent = new Intent();
9998
intent.setClass(BroadcastActivity.this, GroupActivity.class);
10099
startActivity(intent);
101100
this.finish();
102101
});
103-
chat.setOnClickListener(v -> {
102+
chatButton.setOnClickListener(v -> {
104103
Intent intent = new Intent();
105104
intent.setClass(BroadcastActivity.this, ChatActivity.class);
106105
startActivity(intent);

AndroidClient/app/src/main/java/app/xlui/example/im/activities/ChatActivity.java

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package app.xlui.example.im.activities;
22

3-
import android.annotation.SuppressLint;
43
import android.content.Intent;
54
import android.os.Bundle;
65
import android.support.annotation.Nullable;
@@ -24,44 +23,43 @@
2423
import ua.naiksoftware.stomp.Stomp;
2524
import ua.naiksoftware.stomp.StompClient;
2625

26+
@SuppressWarnings({"FieldCanBeLocal", "ResultOfMethodCallIgnored", "CheckResult"})
2727
public 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

Comments
 (0)