GetStream / stream-chat-swiftui

SwiftUI Chat SDK ➜ Stream Chat 💬
https://getstream.io/chat/sdk/swiftui/
Other
354 stars 88 forks source link

Overall unread messages counter not updating in 1:1 chats #662

Open oleg-hf opened 5 days ago

oleg-hf commented 5 days ago

What did you do?

Added subscription for unreadCountPublisher in the ChatManager object.

    func subscribeToUnreadChatMessages() {
        Observable<Int>.create { [weak self] observer in
            let subscription = self?.chatClient.currentUserController()
                .unreadCountPublisher
                .sink(
                    receiveCompletion: { _ in },
                    receiveValue: { unreadCount in
                        observer.onNext(unreadCount.messages)
                    }
                )
            return Disposables.create {
                subscription?.cancel()
            }
        }
        .catchErrorJustReturn(0) // Emit 0 in case of error
        .bind(to: unreadChatMessages)
        .disposed(by: disposeBag)
    }

What did you expect to happen?

The unreadChatMessages value should increase when a new message is sent in any channel, whether it's a group or a 1:1 chat.

What happened instead?

The unreadChatMessages value increases only for channels with three or more members. When a new message is sent in 1:1 channels, the unreadChatMessages value does not increase. Although the unread count red badge in the channel list increases, it disappears after closing and reopening the chat. The unread count red badge value is not related to the unreadChatMessages value bound to unreadCountPublisher.

GetStream Environment

GetStream Chat version: 4.63.0

GetStream Chat frameworks: StreamChat, StreamChatSwiftUI 4.63.0

iOS version: 17.5, 16.4

Swift version: 5

Xcode version: Version 16.1 (16B40)

Device: iPhone 13, 14

Additional context

It would be helpful to know if there are other publishers that emit values for 1:1 chats and why the unread count badge disappears when reopening the channel list. I want to emphasize that the badge in the list and the unreadCountPublisher's value for messages in group chats work as expected.

Both group chats and 1:1 chats are created using the same method:

func makeChannelController() throws {
        guard let instanceId = self.instanceId else { return }
        let selectedUserIds = Set(selectedUsers.map(\.id))

        channelController = try chatClient.channelController(
            createDirectMessageChannelWith: selectedUserIds,
            type: .team,
            team: instanceId,
            extraData: [:]
        )

        guard let channelController else { return }

        channelController.synchronize { [weak self] error in
            guard let self else { return }
            if error == nil {
                self.selectedUsers = []
                self.makeChannelInfo()
                self.state = .channel
            } else {
                self.state = .error
            }
        }
    }
nuno-vieira commented 5 days ago

Hi @oleg-hf,

Can you please share a video of the issue happening? Otherwise, it is difficult to understand what the actual issue is.

Thank you, Nuno

oleg-hf commented 4 days ago

Capturing the issue with unreadCountPublisher in a video is challenging because it requires multiple Xcode simulators and debugging in the background. Here's a verbal explanation:

Scenario #1 (Issue):

  1. User A and User B have a 1:1 chat. User A sends a message to User B.
  2. User B is not viewing the chat, so the message is unread.
  3. The currentUserController.unreadCountPublisher on user B's device is not emitting a new value and the unreadCount.messages for User B does not increase (verified with Xcode breakpoints).

Additional Issue for Scenario #1:

  1. User B receives a push notification and sees a red badge for 1 unread message in ChatChannelListView.
  2. After closing and reopening the chat, the badge disappears from ChatChannelListView as if the message was read, but it wasn't.

Scenario #2 (Working as Expected):

  1. User B is in a group chat with Users C and D. User D sends a new message.
  2. The currentUserController.unreadCountPublisher on User B's device updates correctly, increasing the unread count for messages.
  3. The badge on the ChatChannelListView remains visible even after closing and reopening the chat.
nuno-vieira commented 4 days ago

Hi @oleg-hf,

Thank you for the detailed description. Now it is more clear. I just did not understood this part:

After closing and reopening the chat, the badge disappears from ChatChannelListView as if the message was read, but it wasn't.

Which user, A or B? And if the user opened the chat, then it means he read the message, right? Isn't this expected?

We will investigate this a little more deeper as soon as we can. We will let you know once we have more details.

Best, Nuno

oleg-hf commented 4 days ago

Which user, A or B? And if the user opened the chat, then it means he read the message, right? Isn't this expected?

By "opening the chat" I meant opening the screen based on ChatChannelListView. User B hasn't opened the 1:1 chat yet. When user B reopens the ChatChannelListView, the unread badge disappears even if they haven't opened the 1:1 chat with user A.

nuno-vieira commented 12 hours ago

Hi @oleg-hf,

I see, we will get back to you once we investigate this one in more detail.

Thank you for the report.