Skip to content

Commit bc6280a

Browse files
committed
AndroidServerEventsClient fixes
1 parent fe46c28 commit bc6280a

File tree

4 files changed

+30
-26
lines changed

4 files changed

+30
-26
lines changed

src/AndroidClient/android/src/main/java/net/servicestack/android/AndroidServerEventsClient.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ protected int readFromStream(InputStream inputStream, byte[] buffer) throws IOEx
113113
try {
114114
len = inputStream.read(buffer);
115115
} catch (SocketException e){
116+
//if we caused the Exception with call.cancel() treat as bgThread.interrupt()
116117
if (call.isCanceled()){
117118
throw new InterruptedException();
118119
}
@@ -126,11 +127,11 @@ protected int readFromStream(InputStream inputStream, byte[] buffer) throws IOEx
126127
}
127128

128129
@Override
129-
protected synchronized void interruptBackgroundThread() {
130+
protected synchronized void stopBackgroundThread() {
130131
if (bgThread != null){
131132
bgEventStream.close();
132133
try {
133-
bgThread.join(1000);
134+
bgThread.join(100);
134135
} catch (InterruptedException ignore) {}
135136
bgThread = null;
136137
bgEventStream = null;
@@ -218,11 +219,11 @@ public void complete() {
218219
});
219220
}
220221

221-
public void subscribeToChannelsAsync(final String[] channels, final AsyncSuccess success){
222+
public void subscribeToChannelsAsync(final String[] channels, final AsyncSuccessVoid success){
222223
subscribeToChannelsAsync(channels, AsyncUtils.createAsyncResult(success, null));
223224
}
224225

225-
public void subscribeToChannelsAsync(final String[] channels, final AsyncSuccess success, final AsyncError error){
226+
public void subscribeToChannelsAsync(final String[] channels, final AsyncSuccessVoid success, final AsyncError error){
226227
subscribeToChannelsAsync(channels, AsyncUtils.createAsyncResult(success, error));
227228
}
228229

@@ -248,11 +249,11 @@ public void complete() {
248249
});
249250
}
250251

251-
public void unSubscribeFromChannelsAsync(final String[] channels, final AsyncSuccess success){
252+
public void unSubscribeFromChannelsAsync(final String[] channels, final AsyncSuccessVoid success){
252253
unSubscribeFromChannelsAsync(channels, AsyncUtils.createAsyncResult(success, null));
253254
}
254255

255-
public void unSubscribeFromChannelsAsync(final String[] channels, final AsyncSuccess success, final AsyncError error){
256+
public void unSubscribeFromChannelsAsync(final String[] channels, final AsyncSuccessVoid success, final AsyncError error){
256257
unSubscribeFromChannelsAsync(channels, AsyncUtils.createAsyncResult(success, error));
257258
}
258259
}

src/AndroidClient/androidchat/androidchat.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
116116
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
117117
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jacoco" />
118+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
118119
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
119120
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/packaged" />
120121
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />

src/AndroidClient/androidchat/src/main/java/servicestack/net/androidchat/Extensions.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66

77
import net.servicestack.android.AndroidServerEventsClient;
88
import net.servicestack.android.AsyncUtils;
9+
import net.servicestack.client.AsyncSuccess;
10+
import net.servicestack.client.AsyncSuccessVoid;
911
import net.servicestack.client.Utils;
1012
import net.servicestack.client.sse.ServerEventConnect;
1113
import net.servicestack.client.sse.ServerEventsClient;
1214
import net.servicestack.func.Func;
1315

16+
import java.util.Arrays;
1417
import java.util.HashMap;
1518
import java.util.List;
1619
import java.util.Objects;
@@ -24,7 +27,7 @@
2427

2528
public class Extensions {
2629

27-
public static void updateChatHistory(AndroidServerEventsClient client, ChatCommandHandler cmdReceiver){
30+
public static void updateChatHistory(AndroidServerEventsClient client, ChatCommandHandler cmdReceiver, AsyncSuccessVoid cb){
2831

2932
client.getAndroidClient().getAsync(new GetChatHistory()
3033
.setChannels(Func.toList(client.getChannels())),
@@ -37,22 +40,19 @@ public static void updateChatHistory(AndroidServerEventsClient client, ChatComma
3740
Objects.equals(x.getChannel(), channel)));
3841
}
3942
cmdReceiver.syncAdapter();
43+
cb.success();
4044
});
4145
}
4246

43-
public static void changeChannel(AndroidServerEventsClient client, String channel, ChatCommandHandler cmdReceiver){
44-
List<String> currentChannels = Func.toList(client.getChannels());
45-
46-
if (cmdReceiver.getFullHistory().containsKey(channel) && currentChannels.contains(channel)){
47+
public static void changeChannel(AndroidServerEventsClient client, String channel, ChatCommandHandler cmdReceiver, AsyncSuccessVoid cb){
48+
if (cmdReceiver.getFullHistory().containsKey(channel) && Arrays.asList(client.getChannels()).contains(channel)){
4749
cmdReceiver.changeChannel(channel);
50+
cb.success();
4851
} else {
49-
50-
if (!currentChannels.contains(channel))
51-
currentChannels.add(channel);
52-
53-
client.subscribeToChannels(Func.toArray(currentChannels, String.class));
54-
cmdReceiver.setCurrentChannel(channel);
55-
updateChatHistory(client, cmdReceiver);
52+
client.subscribeToChannelsAsync(new String[]{ channel }, () -> {
53+
cmdReceiver.setCurrentChannel(channel);
54+
updateChatHistory(client, cmdReceiver, cb);
55+
});
5656
}
5757
}
5858

src/AndroidClient/androidchat/src/main/java/servicestack/net/androidchat/MainActivity.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public class MainActivity extends AppCompatActivity {
5252
"Announce Hello", "/cmd.announce Hello from Java Android",
5353
"Play YouTube", "/tv.watch https://youtu.be/u5CVsCnxyXg",
5454
"Background Image", "/css.background-image url(http://bit.ly/2lZxbHv)",
55-
"Background Top", "/css.background$#top #0091ea",
56-
"Background", "/css.background #fffde7",
57-
"Background Bottom", "/css.background$#bottom #cccccc",
55+
"Background Top", "/css.background$#top #86B951",
56+
"Background Color", "/css.background #eceff1",
57+
"Background Bottom", "/css.background$#bottom #91C654",
5858
"Logout", "/logout"
5959
);
6060

@@ -93,8 +93,9 @@ protected void onCreate(Bundle savedInstanceState) {
9393

9494
getClient()
9595
.setOnConnect(connectMsg -> {
96-
Extensions.updateChatHistory(getClient(), cmdReceiver);
97-
Extensions.updateUserProfile(connectMsg, mainActivity);
96+
Extensions.updateChatHistory(getClient(), cmdReceiver, () -> {
97+
Extensions.updateUserProfile(connectMsg, mainActivity);
98+
});
9899
})
99100
.setOnCommand(command -> {
100101
if (command instanceof ServerEventJoin){
@@ -156,14 +157,15 @@ public boolean onChannelClick(MenuItem menuItem)
156157
List<String> nChannels = Func.toList(getClient().getChannels());
157158
nChannels.add(nChannel);
158159
UiHelpers.resetChannelDrawer(this, navigationView, Func.toArray(nChannels, String.class));
159-
Extensions.changeChannel(getClient(), nChannel, cmdReceiver);
160-
cmdReceiver.syncAdapter();
160+
Extensions.changeChannel(getClient(), nChannel, cmdReceiver, () -> {
161+
cmdReceiver.syncAdapter();
162+
});
161163
} catch (Exception ex){
162164
errors.add(ex);
163165
}
164166
});
165167
} else {
166-
Extensions.changeChannel(getClient(), itemText, cmdReceiver);
168+
Extensions.changeChannel(getClient(), itemText, cmdReceiver, () -> {});
167169
}
168170
drawerLayout.closeDrawer(navigationView);
169171
return true;

0 commit comments

Comments
 (0)