Skip to content

Commit e185736

Browse files
committed
Merge branch 'master' of github.com:GetStream/android-chat-tutorial
2 parents 3ebab9d + fcb3e83 commit e185736

File tree

8 files changed

+71
-62
lines changed

8 files changed

+71
-62
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: 22 additions & 21 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;
@@ -25,6 +25,9 @@
2525
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel.State.NavigateUp;
2626
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModelBinding;
2727

28+
import java.util.LinkedList;
29+
import java.util.List;
30+
2831
import io.getstream.chat.android.client.models.Channel;
2932
import io.getstream.chat.android.client.models.User;
3033
import io.getstream.chat.android.livedata.ChatDomain;
@@ -52,7 +55,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5255

5356
// Step 0 - Get View references
5457
MessageListView messageListView = findViewById(R.id.messageListView);
55-
ProgressBar progressBar = findViewById(R.id.progressBar);
5658
ChannelHeaderView channelHeaderView = findViewById(R.id.channelHeaderView);
5759
MessageInputView messageInputView = findViewById(R.id.messageInputView);
5860
TextView typingHeader = findViewById(R.id.typingHeader);
@@ -111,26 +113,25 @@ public void handleOnBackPressed() {
111113
typingHeader.setText(nobodyTyping);
112114

113115
// 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(", ");
116+
ChatDomain.instance().getUseCases().getGetChannelController().invoke(cid).enqueue((result) -> {
117+
ChannelController channelController = result.data();
118+
119+
runOnUiThread(() -> {
120+
// Observe typing users
121+
channelController.getTyping().observe(this, users -> {
122+
if (users.isEmpty()) {
123+
typingHeader.setText(nobodyTyping);
124+
} else {
125+
List<String> userNames = new LinkedList<>();
126+
for (User user : users) {
127+
userNames.add((String)user.getExtraData().get("name"));
128+
}
129+
String typing = "typing: " + TextUtils.join(", ", userNames);
130+
typingHeader.setText(typing);
129131
}
130-
typingText.append(user.getExtraData().get("name"));
131-
}
132-
typingHeader.setText(typingText.toString());
133-
}
132+
});
133+
});
134+
return Unit.INSTANCE;
134135
});
135136
}
136137
}

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
}

samplejava/src/main/res/layout/list_item_attach_imgur.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
android:layout_width="150dp"
2020
android:layout_height="50dp"
2121
android:background="@drawable/imgur_logo"
22-
app:layout_constraintBottom_toBottomOf="@+id/iv_media_thumb"
23-
app:layout_constraintEnd_toEndOf="@+id/iv_media_thumb"
24-
app:layout_constraintStart_toStartOf="@+id/iv_media_thumb"
25-
app:layout_constraintTop_toTopOf="@+id/iv_media_thumb" />
22+
app:layout_constraintBottom_toBottomOf="parent"
23+
app:layout_constraintEnd_toEndOf="parent"
24+
app:layout_constraintStart_toStartOf="parent"
25+
app:layout_constraintTop_toTopOf="parent" />
2626
</androidx.constraintlayout.widget.ConstraintLayout>

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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel.Mode.Threa
1717
import com.getstream.sdk.chat.viewmodel.messages.MessageListViewModel.State.NavigateUp
1818
import com.getstream.sdk.chat.viewmodel.messages.bindView
1919
import io.getstream.chat.android.client.ChatClient
20+
import io.getstream.chat.android.client.controllers.subscribeFor
2021
import io.getstream.chat.android.client.events.TypingStartEvent
2122
import io.getstream.chat.android.client.events.TypingStopEvent
2223
import io.getstream.chat.android.client.models.Channel
24+
import io.getstream.chat.android.client.models.name
2325

2426
class ChannelActivity4 : AppCompatActivity() {
2527

@@ -88,6 +90,7 @@ class ChannelActivity4 : AppCompatActivity() {
8890
.channel(cid)
8991
.subscribeFor(this, TypingStartEvent::class.java, TypingStopEvent::class.java
9092
) { event ->
93+
@Suppress("NON_EXHAUSTIVE_WHEN_ON_SEALED_CLASS")
9194
when (event) {
9295
is TypingStartEvent -> currentlyTyping.add(event.user.extraData["name"] as String)
9396
is TypingStopEvent -> currentlyTyping.remove(event.user.extraData["name"] as String)

samplekotlin/src/main/res/layout/list_item_attach_imgur.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
android:layout_width="150dp"
2020
android:layout_height="50dp"
2121
android:background="@drawable/imgur_logo"
22-
app:layout_constraintBottom_toBottomOf="@+id/iv_media_thumb"
23-
app:layout_constraintEnd_toEndOf="@+id/iv_media_thumb"
24-
app:layout_constraintStart_toStartOf="@+id/iv_media_thumb"
25-
app:layout_constraintTop_toTopOf="@+id/iv_media_thumb" />
22+
app:layout_constraintBottom_toBottomOf="parent"
23+
app:layout_constraintEnd_toEndOf="parent"
24+
app:layout_constraintStart_toStartOf="parent"
25+
app:layout_constraintTop_toTopOf="parent" />
2626
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)