Skip to content

Commit af5d1d6

Browse files
committed
Improve typings sample and implement missing Java Activities.
Typings sample (ChannelActivity3) was getting ChannelController on main thread and that's why IDEA was reporting an error.
1 parent 9980756 commit af5d1d6

File tree

6 files changed

+62
-55
lines changed

6 files changed

+62
-55
lines changed

samplejava/src/main/java/com/example/chattutorialjava/ChannelActivity.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.content.Context;
44
import android.content.Intent;
55
import android.os.Bundle;
6-
import android.widget.ProgressBar;
76

87
import androidx.activity.OnBackPressedCallback;
98
import androidx.annotation.Nullable;
@@ -48,7 +47,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4847

4948
// Step 0 - Get View references
5049
MessageListView messageListView = findViewById(R.id.messageListView);
51-
ProgressBar progressBar = findViewById(R.id.progressBar);
5250
ChannelHeaderView channelHeaderView = findViewById(R.id.channelHeaderView);
5351
MessageInputView messageInputView = findViewById(R.id.messageInputView);
5452

samplejava/src/main/java/com/example/chattutorialjava/ChannelActivity2.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.content.Context;
44
import android.content.Intent;
55
import android.os.Bundle;
6-
import android.widget.ProgressBar;
76

87
import androidx.activity.OnBackPressedCallback;
98
import androidx.annotation.Nullable;
@@ -48,7 +47,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4847

4948
// Step 0 - Get View references
5049
MessageListView messageListView = findViewById(R.id.messageListView);
51-
ProgressBar progressBar = findViewById(R.id.progressBar);
5250
ChannelHeaderView channelHeaderView = findViewById(R.id.channelHeaderView);
5351
MessageInputView messageInputView = findViewById(R.id.messageInputView);
5452

samplejava/src/main/java/com/example/chattutorialjava/ChannelActivity3.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.content.Context;
44
import android.content.Intent;
55
import android.os.Bundle;
6-
import android.widget.ProgressBar;
76
import android.widget.TextView;
87

98
import androidx.activity.OnBackPressedCallback;
@@ -52,7 +51,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5251

5352
// Step 0 - Get View references
5453
MessageListView messageListView = findViewById(R.id.messageListView);
55-
ProgressBar progressBar = findViewById(R.id.progressBar);
5654
ChannelHeaderView channelHeaderView = findViewById(R.id.channelHeaderView);
5755
MessageInputView messageInputView = findViewById(R.id.messageInputView);
5856
TextView typingHeader = findViewById(R.id.typingHeader);
@@ -111,26 +109,28 @@ public void handleOnBackPressed() {
111109
typingHeader.setText(nobodyTyping);
112110

113111
// Obtain a ChannelController
114-
ChannelController channelController =
115-
ChatDomain.instance().getUseCases()
116-
.getGetChannelController().invoke(cid)
117-
.execute().data();
118-
119-
// Observe typing users
120-
channelController.getTyping().observe(this, users -> {
121-
if (users.isEmpty()) {
122-
typingHeader.setText(nobodyTyping);
123-
} else {
124-
StringBuilder typingText = new StringBuilder("typing: ");
125-
for (int i = 0; i < users.size(); i++) {
126-
User user = users.get(i);
127-
if (i > 0) {
128-
typingText.append(", ");
112+
ChatDomain.instance().getUseCases().getGetChannelController().invoke(cid).enqueue((result) -> {
113+
ChannelController channelController = result.data();
114+
115+
runOnUiThread(() -> {
116+
// Observe typing users
117+
channelController.getTyping().observe(this, users -> {
118+
if (users.isEmpty()) {
119+
typingHeader.setText(nobodyTyping);
120+
} else {
121+
StringBuilder typingText = new StringBuilder("typing: ");
122+
for (int i = 0; i < users.size(); i++) {
123+
User user = users.get(i);
124+
if (i > 0) {
125+
typingText.append(", ");
126+
}
127+
typingText.append(user.getExtraData().get("name"));
128+
}
129+
typingHeader.setText(typingText.toString());
129130
}
130-
typingText.append(user.getExtraData().get("name"));
131-
}
132-
typingHeader.setText(typingText.toString());
133-
}
131+
});
132+
});
133+
return Unit.INSTANCE;
134134
});
135135
}
136136
}

samplejava/src/main/java/com/example/chattutorialjava/ChannelActivity4.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import android.content.Context;
44
import android.content.Intent;
55
import android.os.Bundle;
6-
import android.widget.ProgressBar;
6+
import android.text.TextUtils;
77
import android.widget.TextView;
88

99
import androidx.activity.OnBackPressedCallback;
@@ -28,7 +28,11 @@
2828
import java.util.HashSet;
2929
import java.util.Set;
3030

31+
import io.getstream.chat.android.client.ChatClient;
32+
import io.getstream.chat.android.client.events.TypingStartEvent;
33+
import io.getstream.chat.android.client.events.TypingStopEvent;
3134
import io.getstream.chat.android.client.models.Channel;
35+
import io.getstream.chat.android.client.models.User;
3236
import kotlin.Unit;
3337

3438
public class ChannelActivity4 extends AppCompatActivity {
@@ -52,7 +56,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5256

5357
// Step 0 - Get View references
5458
MessageListView messageListView = findViewById(R.id.messageListView);
55-
ProgressBar progressBar = findViewById(R.id.progressBar);
5659
ChannelHeaderView channelHeaderView = findViewById(R.id.channelHeaderView);
5760
MessageInputView messageInputView = findViewById(R.id.messageInputView);
5861
TextView typingHeader = findViewById(R.id.typingHeader);
@@ -111,24 +114,28 @@ public void handleOnBackPressed() {
111114
typingHeader.setText(nobodyTyping);
112115

113116
Set<String> currentlyTyping = new HashSet<>();
114-
// TODO update when new SDK version is out
115-
/*
116117
ChatClient
117-
.instance()
118-
.channel(cid)
119-
.subscribeFor(
120-
this,
121-
TypingStartEvent::class, TypingStopEvent::class
122-
) { event ->
123-
when (event) {
124-
is TypingStartEvent -> currentlyTyping.add(event.user.name)
125-
is TypingStopEvent -> currentlyTyping.remove(event.user.name)
126-
}
127-
128-
typingHeader.text = when {
129-
currentlyTyping.isNotEmpty() -> currentlyTyping.joinToString(prefix = "typing: ")
130-
else -> nobodyTyping
131-
}
132-
}*/
118+
.instance()
119+
.channel(cid)
120+
.subscribeFor(
121+
this,
122+
new Class[]{TypingStartEvent.class, TypingStopEvent.class}, event -> {
123+
if (event instanceof TypingStartEvent) {
124+
User user = ((TypingStartEvent) event).getUser();
125+
String name = (String) user.getExtraData().get("name");
126+
currentlyTyping.add(name);
127+
} else if (event instanceof TypingStopEvent) {
128+
User user = ((TypingStopEvent) event).getUser();
129+
String name = (String) user.getExtraData().get("name");
130+
currentlyTyping.remove(name);
131+
}
132+
133+
String typing = "nobody is typing";
134+
if (!currentlyTyping.isEmpty()) {
135+
typing = "typing: " + TextUtils.join(", ", currentlyTyping);
136+
}
137+
typingHeader.setText(typing);
138+
return Unit.INSTANCE;
139+
});
133140
}
134141
}

samplekotlin/src/main/java/com/example/chattutorial/ChannelActivity3.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,17 @@ class ChannelActivity3 : AppCompatActivity() {
8080
val nobodyTyping = "nobody is typing"
8181
binding.typingHeader.text = nobodyTyping
8282

83-
// Obtain a ChannelController
84-
val channelController =
85-
ChatDomain.instance().useCases.getChannelController(cid).execute().data()
86-
87-
// Observe typing users
88-
channelController.typing.observe(this) { users ->
89-
binding.typingHeader.text = when {
90-
users.isNotEmpty() -> users.joinToString(prefix = "typing: ") { user -> user.name }
91-
else -> nobodyTyping
83+
// Asynchronously obtain a ChannelController
84+
ChatDomain.instance().useCases.getChannelController(cid).enqueue {
85+
val channelController = it.data()
86+
// Get back to UI thread and observe typing users
87+
runOnUiThread {
88+
channelController.typing.observe(this) { users ->
89+
binding.typingHeader.text = when {
90+
users.isNotEmpty() -> users.joinToString(prefix = "typing: ") { user -> user.name }
91+
else -> nobodyTyping
92+
}
93+
}
9294
}
9395
}
9496
}

samplekotlin/src/main/java/com/example/chattutorial/ChannelActivity4.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import io.getstream.chat.android.client.ChatClient
2020
import io.getstream.chat.android.client.events.TypingStartEvent
2121
import io.getstream.chat.android.client.events.TypingStopEvent
2222
import io.getstream.chat.android.client.models.Channel
23+
import io.getstream.chat.android.client.models.name
2324

2425
class ChannelActivity4 : AppCompatActivity() {
2526

@@ -86,11 +87,12 @@ class ChannelActivity4 : AppCompatActivity() {
8687
ChatClient
8788
.instance()
8889
.channel(cid)
89-
.subscribeFor(this, TypingStartEvent::class, TypingStopEvent::class
90+
.subscribeFor(this, TypingStartEvent::class.java, TypingStopEvent::class.java
9091
) { event ->
9192
when (event) {
9293
is TypingStartEvent -> currentlyTyping.add(event.user.name)
9394
is TypingStopEvent -> currentlyTyping.remove(event.user.name)
95+
else -> Unit
9496
}
9597

9698
binding.typingHeader.text = when {

0 commit comments

Comments
 (0)