@@ -15,6 +15,7 @@ import com.ismartcoding.plain.chat.data.ChatTargetType
1515import com.ismartcoding.plain.db.AppDatabase
1616import com.ismartcoding.plain.db.DChat
1717import com.ismartcoding.plain.events.ChannelUpdatedEvent
18+ import com.ismartcoding.plain.events.PeerOnlineStatusChangedEvent
1819import com.ismartcoding.plain.events.PeerUpdatedEvent
1920import kotlinx.coroutines.Dispatchers
2021import kotlinx.coroutines.flow.MutableStateFlow
@@ -26,7 +27,6 @@ import kotlinx.coroutines.withContext
2627data class ChatState (
2728 val target : ChatTarget = ChatTarget ("", ChatTargetType .LOCAL ),
2829 val toName : String = " " ,
29- val onlinePeerIds : Set <String > = emptySet(),
3030)
3131
3232class ChatViewModel : ISelectableViewModel <VChat >, ViewModel () {
@@ -39,10 +39,19 @@ class ChatViewModel : ISelectableViewModel<VChat>, ViewModel() {
3939 private val _chatState = MutableStateFlow (ChatState ())
4040 val chatState: StateFlow <ChatState > get() = _chatState
4141
42+ private val _onlinePeerIds = MutableStateFlow <Set <String >>(emptySet())
43+ val onlinePeerIds: StateFlow <Set <String >> get() = _onlinePeerIds
44+
4245 init {
4346 viewModelScope.launch {
4447 Channel .sharedFlow.collect { event ->
4548 when (event) {
49+ is PeerOnlineStatusChangedEvent -> {
50+ _onlinePeerIds .update { current ->
51+ if (event.online) current + event.peerId
52+ else current - event.peerId
53+ }
54+ }
4655 is PeerUpdatedEvent -> {
4756 ChatCacheManager .updatePeer(event.peer)
4857 if (_chatState .value.target.type == ChatTargetType .PEER && _chatState .value.target.toId == event.peer.id) {
0 commit comments