|
3 | 3 | import android.content.Context; |
4 | 4 | import android.content.Intent; |
5 | 5 | import android.os.Bundle; |
6 | | -import android.widget.TextView; |
7 | 6 |
|
8 | 7 | import androidx.activity.OnBackPressedCallback; |
| 8 | +import androidx.annotation.NonNull; |
9 | 9 | import androidx.annotation.Nullable; |
10 | 10 | import androidx.appcompat.app.AppCompatActivity; |
| 11 | +import androidx.lifecycle.FlowLiveDataConversions; |
| 12 | +import androidx.lifecycle.LiveData; |
| 13 | +import androidx.lifecycle.Transformations; |
11 | 14 | import androidx.lifecycle.ViewModelProvider; |
12 | 15 |
|
13 | 16 | import com.example.chattutorial.databinding.ActivityChannel3Binding; |
|
20 | 23 | import java.util.ArrayList; |
21 | 24 | import java.util.List; |
22 | 25 |
|
| 26 | +import io.getstream.chat.android.client.ChatClient; |
23 | 27 | import io.getstream.chat.android.client.models.Channel; |
24 | 28 | import io.getstream.chat.android.client.models.Message; |
| 29 | +import io.getstream.chat.android.client.models.TypingEvent; |
| 30 | +import io.getstream.chat.android.offline.extensions.ChatClientExtensions; |
| 31 | +import io.getstream.chat.android.offline.plugin.state.channel.ChannelState; |
25 | 32 | import io.getstream.chat.android.ui.message.input.viewmodel.MessageInputViewModelBinding; |
26 | 33 | import io.getstream.chat.android.ui.message.list.adapter.viewholder.attachment.AttachmentFactoryManager; |
27 | 34 | import io.getstream.chat.android.ui.message.list.header.MessageListHeaderView; |
28 | 35 | import io.getstream.chat.android.ui.message.list.header.viewmodel.MessageListHeaderViewModel; |
29 | 36 | import io.getstream.chat.android.ui.message.list.header.viewmodel.MessageListHeaderViewModelBinding; |
30 | 37 | import io.getstream.chat.android.ui.message.list.viewmodel.MessageListViewModelBinding; |
31 | 38 | import io.getstream.chat.android.ui.message.list.viewmodel.factory.MessageListViewModelFactory; |
| 39 | +import kotlinx.coroutines.Dispatchers; |
| 40 | +import kotlinx.coroutines.flow.Flow; |
| 41 | +import kotlinx.coroutines.flow.FlowKt; |
32 | 42 |
|
33 | 43 | public class ChannelActivity3 extends AppCompatActivity { |
34 | 44 |
|
@@ -110,11 +120,41 @@ public void handleOnBackPressed() { |
110 | 120 | }); |
111 | 121 |
|
112 | 122 | // Custom typing info header bar |
113 | | - TextView typingHeaderView = findViewById(R.id.typingHeaderView); |
114 | 123 | String nobodyTyping = "nobody is typing"; |
115 | | - typingHeaderView.setText(nobodyTyping); |
| 124 | + binding.typingHeaderView.setText(nobodyTyping); |
| 125 | + |
| 126 | + // Observe typing events and update typing header depending on its state. |
| 127 | + Flow<ChannelState> channelStateFlow = ChatClientExtensions.watchChannelAsState(ChatClient.instance(), cid, 30); |
| 128 | + LiveData<TypingEvent> typingEventLiveData = Transformations.switchMap( |
| 129 | + FlowLiveDataConversions.asLiveData(channelStateFlow), |
| 130 | + channelState -> FlowLiveDataConversions.asLiveData(channelState.getTyping()) |
| 131 | + ); |
| 132 | + |
| 133 | + typingEventLiveData.observe(this, typingEvent -> { |
| 134 | + String headerText; |
| 135 | + |
| 136 | + if (typingEvent.getUsers().size() != 0) { |
| 137 | + headerText = "typing: " + joinTypingUpdatesToUserNames(typingEvent); |
| 138 | + } else { |
| 139 | + headerText = nobodyTyping; |
| 140 | + } |
| 141 | + |
| 142 | + binding.typingHeaderView.setText(headerText); |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + @NonNull |
| 147 | + private String joinTypingUpdatesToUserNames(@NonNull TypingEvent typingEvent) { |
| 148 | + StringBuilder joinedString = new StringBuilder(); |
| 149 | + |
| 150 | + for (int i = 0; i < typingEvent.getUsers().size(); i++) { |
| 151 | + if (i < typingEvent.getUsers().size() - 1) { |
| 152 | + joinedString.append(typingEvent.getUsers().get(i).getName()).append(", "); |
| 153 | + } else { |
| 154 | + joinedString.append(typingEvent.getUsers().get(i).getName()); |
| 155 | + } |
| 156 | + } |
116 | 157 |
|
117 | | - //TODO implemet an OP based solution here once the Core team introduces extensions for StateFlow |
118 | | - // based values |
| 158 | + return joinedString.toString(); |
119 | 159 | } |
120 | 160 | } |
0 commit comments